blob: f74af33f5ddc4db19f528ae5818bbaa9fe9cbb53 [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 Rapoportcea443a2008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jean Delvare6629dcf2010-05-04 11:09:28 +020056/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020057 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000058 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010059static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_IDR(i2c_adapter_idr);
61
Jean Delvare4f8cf822009-09-18 22:45:46 +020062static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020063static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010064
65/* ------------------------------------------------------------------------- */
66
Jean Delvared2653e92008-04-29 23:11:39 +020067static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
68 const struct i2c_client *client)
69{
70 while (id->name[0]) {
71 if (strcmp(client->name, id->name) == 0)
72 return id;
73 id++;
74 }
75 return NULL;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int i2c_device_match(struct device *dev, struct device_driver *drv)
79{
Jean Delvare51298d12009-09-18 22:45:45 +020080 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020082
Jean Delvare51298d12009-09-18 22:45:45 +020083 if (!client)
84 return 0;
85
Grant Likely959e85f2010-06-08 07:48:19 -060086 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
88 return 1;
89
Mika Westerberg907ddf82012-11-23 12:23:40 +010090 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
93
Jean Delvare51298d12009-09-18 22:45:45 +020094 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020095 /* match on an id table if there is one */
96 if (driver->id_table)
97 return i2c_match_id(driver->id_table, client) != NULL;
98
Jean Delvareeb8a7902008-05-18 20:49:41 +020099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
103/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105{
106 struct i2c_client *client = to_i2c_client(dev);
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
621/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200622 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200623 * @adap: the adapter managing the device
624 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200625 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200626 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200627 * Create an i2c device. Binding is handled through driver model
628 * probe()/remove() methods. A driver may be bound to this device when we
629 * return from this function, or any later moment (e.g. maybe hotplugging will
630 * load the driver module). This call is not appropriate for use by mainboard
631 * initialization logic, which usually runs during an arch_initcall() long
632 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200633 *
634 * This returns the new i2c client, which may be saved for later use with
635 * i2c_unregister_device(); or NULL to indicate an error.
636 */
637struct i2c_client *
638i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
639{
640 struct i2c_client *client;
641 int status;
642
643 client = kzalloc(sizeof *client, GFP_KERNEL);
644 if (!client)
645 return NULL;
646
647 client->adapter = adap;
648
649 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200650
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200651 if (info->archdata)
652 client->dev.archdata = *info->archdata;
653
Marc Pignatee354252008-08-28 08:33:22 +0200654 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200655 client->addr = info->addr;
656 client->irq = info->irq;
657
David Brownell9c1600e2007-05-01 23:26:31 +0200658 strlcpy(client->name, info->type, sizeof(client->name));
659
Jean Delvare3a89db52010-06-03 11:33:52 +0200660 /* Check for address validity */
661 status = i2c_check_client_addr_validity(client);
662 if (status) {
663 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
664 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
665 goto out_err_silent;
666 }
667
Jean Delvaref8a227e2009-06-19 16:58:18 +0200668 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200669 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200670 if (status)
671 goto out_err;
672
673 client->dev.parent = &client->adapter->dev;
674 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200675 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700676 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100677 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200678
Jean Delvarecbb44512011-11-23 11:33:07 +0100679 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200680 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100681 client->addr | ((client->flags & I2C_CLIENT_TEN)
682 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200683 status = device_register(&client->dev);
684 if (status)
685 goto out_err;
686
Jean Delvaref8a227e2009-06-19 16:58:18 +0200687 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
688 client->name, dev_name(&client->dev));
689
David Brownell9c1600e2007-05-01 23:26:31 +0200690 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200691
692out_err:
693 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
694 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200695out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200696 kfree(client);
697 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200698}
699EXPORT_SYMBOL_GPL(i2c_new_device);
700
701
702/**
703 * i2c_unregister_device - reverse effect of i2c_new_device()
704 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200705 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200706 */
707void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200708{
David Brownella1d9e6e2007-05-01 23:26:30 +0200709 device_unregister(&client->dev);
710}
David Brownell9c1600e2007-05-01 23:26:31 +0200711EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200712
713
Jean Delvare60b129d2008-05-11 20:37:06 +0200714static const struct i2c_device_id dummy_id[] = {
715 { "dummy", 0 },
716 { },
717};
718
Jean Delvared2653e92008-04-29 23:11:39 +0200719static int dummy_probe(struct i2c_client *client,
720 const struct i2c_device_id *id)
721{
722 return 0;
723}
724
725static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100726{
727 return 0;
728}
729
730static struct i2c_driver dummy_driver = {
731 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200732 .probe = dummy_probe,
733 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200734 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100735};
736
737/**
738 * i2c_new_dummy - return a new i2c device bound to a dummy driver
739 * @adapter: the adapter managing the device
740 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100741 * Context: can sleep
742 *
743 * This returns an I2C client bound to the "dummy" driver, intended for use
744 * with devices that consume multiple addresses. Examples of such chips
745 * include various EEPROMS (like 24c04 and 24c08 models).
746 *
747 * These dummy devices have two main uses. First, most I2C and SMBus calls
748 * except i2c_transfer() need a client handle; the dummy will be that handle.
749 * And second, this prevents the specified address from being bound to a
750 * different driver.
751 *
752 * This returns the new i2c client, which should be saved for later use with
753 * i2c_unregister_device(); or NULL to indicate an error.
754 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100755struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100756{
757 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200758 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100759 };
760
David Brownelle9f13732008-01-27 18:14:52 +0100761 return i2c_new_device(adapter, &info);
762}
763EXPORT_SYMBOL_GPL(i2c_new_dummy);
764
David Brownellf37dd802007-02-13 22:09:00 +0100765/* ------------------------------------------------------------------------- */
766
David Brownell16ffadf2007-05-01 23:26:28 +0200767/* I2C bus adapters -- one roots each I2C or SMBUS segment */
768
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200769static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
David Brownellef2c83212007-05-01 23:26:28 +0200771 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 complete(&adap->dev_released);
773}
774
Jean Delvare99cd8e22009-06-19 16:58:20 +0200775/*
Jean Delvare390946b2012-09-10 10:14:02 +0200776 * This function is only needed for mutex_lock_nested, so it is never
777 * called unless locking correctness checking is enabled. Thus we
778 * make it inline to avoid a compiler warning. That's what gcc ends up
779 * doing anyway.
780 */
781static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
782{
783 unsigned int depth = 0;
784
785 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
786 depth++;
787
788 return depth;
789}
790
791/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200792 * Let users instantiate I2C devices through sysfs. This can be used when
793 * platform initialization code doesn't contain the proper data for
794 * whatever reason. Also useful for drivers that do device detection and
795 * detection fails, either because the device uses an unexpected address,
796 * or this is a compatible device with different ID register values.
797 *
798 * Parameter checking may look overzealous, but we really don't want
799 * the user to provide incorrect parameters.
800 */
801static ssize_t
802i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
804{
805 struct i2c_adapter *adap = to_i2c_adapter(dev);
806 struct i2c_board_info info;
807 struct i2c_client *client;
808 char *blank, end;
809 int res;
810
Jean Delvare99cd8e22009-06-19 16:58:20 +0200811 memset(&info, 0, sizeof(struct i2c_board_info));
812
813 blank = strchr(buf, ' ');
814 if (!blank) {
815 dev_err(dev, "%s: Missing parameters\n", "new_device");
816 return -EINVAL;
817 }
818 if (blank - buf > I2C_NAME_SIZE - 1) {
819 dev_err(dev, "%s: Invalid device name\n", "new_device");
820 return -EINVAL;
821 }
822 memcpy(info.type, buf, blank - buf);
823
824 /* Parse remaining parameters, reject extra parameters */
825 res = sscanf(++blank, "%hi%c", &info.addr, &end);
826 if (res < 1) {
827 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
828 return -EINVAL;
829 }
830 if (res > 1 && end != '\n') {
831 dev_err(dev, "%s: Extra parameters\n", "new_device");
832 return -EINVAL;
833 }
834
Jean Delvare99cd8e22009-06-19 16:58:20 +0200835 client = i2c_new_device(adap, &info);
836 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200837 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200838
839 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200840 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200841 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200842 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200843 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
844 info.type, info.addr);
845
846 return count;
847}
848
849/*
850 * And of course let the users delete the devices they instantiated, if
851 * they got it wrong. This interface can only be used to delete devices
852 * instantiated by i2c_sysfs_new_device above. This guarantees that we
853 * don't delete devices to which some kernel code still has references.
854 *
855 * Parameter checking may look overzealous, but we really don't want
856 * the user to delete the wrong device.
857 */
858static ssize_t
859i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
860 const char *buf, size_t count)
861{
862 struct i2c_adapter *adap = to_i2c_adapter(dev);
863 struct i2c_client *client, *next;
864 unsigned short addr;
865 char end;
866 int res;
867
868 /* Parse parameters, reject extra parameters */
869 res = sscanf(buf, "%hi%c", &addr, &end);
870 if (res < 1) {
871 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
872 return -EINVAL;
873 }
874 if (res > 1 && end != '\n') {
875 dev_err(dev, "%s: Extra parameters\n", "delete_device");
876 return -EINVAL;
877 }
878
879 /* Make sure the device was added through sysfs */
880 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200881 mutex_lock_nested(&adap->userspace_clients_lock,
882 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200883 list_for_each_entry_safe(client, next, &adap->userspace_clients,
884 detected) {
885 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200886 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
887 "delete_device", client->name, client->addr);
888
889 list_del(&client->detected);
890 i2c_unregister_device(client);
891 res = count;
892 break;
893 }
894 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200895 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200896
897 if (res < 0)
898 dev_err(dev, "%s: Can't find device in list\n",
899 "delete_device");
900 return res;
901}
902
Jean Delvare4f8cf822009-09-18 22:45:46 +0200903static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200904static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
905 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200906
907static struct attribute *i2c_adapter_attrs[] = {
908 &dev_attr_name.attr,
909 &dev_attr_new_device.attr,
910 &dev_attr_delete_device.attr,
911 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200912};
913
Jean Delvare4f8cf822009-09-18 22:45:46 +0200914static struct attribute_group i2c_adapter_attr_group = {
915 .attrs = i2c_adapter_attrs,
916};
917
918static const struct attribute_group *i2c_adapter_attr_groups[] = {
919 &i2c_adapter_attr_group,
920 NULL
921};
922
Michael Lawnick08263742010-08-11 18:21:02 +0200923struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200924 .groups = i2c_adapter_attr_groups,
925 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200926};
Michael Lawnick08263742010-08-11 18:21:02 +0200927EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Stephen Warren643dd092012-04-17 12:43:33 -0600929/**
930 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
931 * @dev: device, probably from some driver model iterator
932 *
933 * When traversing the driver model tree, perhaps using driver model
934 * iterators like @device_for_each_child(), you can't assume very much
935 * about the nodes you find. Use this function to avoid oopses caused
936 * by wrongly treating some non-I2C device as an i2c_adapter.
937 */
938struct i2c_adapter *i2c_verify_adapter(struct device *dev)
939{
940 return (dev->type == &i2c_adapter_type)
941 ? to_i2c_adapter(dev)
942 : NULL;
943}
944EXPORT_SYMBOL(i2c_verify_adapter);
945
Jean Delvare2bb50952009-09-18 22:45:46 +0200946#ifdef CONFIG_I2C_COMPAT
947static struct class_compat *i2c_adapter_compat_class;
948#endif
949
David Brownell9c1600e2007-05-01 23:26:31 +0200950static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
951{
952 struct i2c_devinfo *devinfo;
953
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200954 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200955 list_for_each_entry(devinfo, &__i2c_board_list, list) {
956 if (devinfo->busnum == adapter->nr
957 && !i2c_new_device(adapter,
958 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100959 dev_err(&adapter->dev,
960 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200961 devinfo->board_info.addr);
962 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200963 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200964}
965
Wolfram Sang687b81d2013-07-11 12:56:15 +0100966/* OF support code */
967
968#if IS_ENABLED(CONFIG_OF)
969static void of_i2c_register_devices(struct i2c_adapter *adap)
970{
971 void *result;
972 struct device_node *node;
973
974 /* Only register child devices if the adapter has a node pointer set */
975 if (!adap->dev.of_node)
976 return;
977
978 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
979
980 for_each_available_child_of_node(adap->dev.of_node, node) {
981 struct i2c_board_info info = {};
982 struct dev_archdata dev_ad = {};
983 const __be32 *addr;
984 int len;
985
986 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
987
988 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
989 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
990 node->full_name);
991 continue;
992 }
993
994 addr = of_get_property(node, "reg", &len);
995 if (!addr || (len < sizeof(int))) {
996 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
997 node->full_name);
998 continue;
999 }
1000
1001 info.addr = be32_to_cpup(addr);
1002 if (info.addr > (1 << 10) - 1) {
1003 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1004 info.addr, node->full_name);
1005 continue;
1006 }
1007
1008 info.irq = irq_of_parse_and_map(node, 0);
1009 info.of_node = of_node_get(node);
1010 info.archdata = &dev_ad;
1011
1012 if (of_get_property(node, "wakeup-source", NULL))
1013 info.flags |= I2C_CLIENT_WAKE;
1014
1015 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1016
1017 result = i2c_new_device(adap, &info);
1018 if (result == NULL) {
1019 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1020 node->full_name);
1021 of_node_put(node);
1022 irq_dispose_mapping(info.irq);
1023 continue;
1024 }
1025 }
1026}
1027
1028static int of_dev_node_match(struct device *dev, void *data)
1029{
1030 return dev->of_node == data;
1031}
1032
1033/* must call put_device() when done with returned i2c_client device */
1034struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1035{
1036 struct device *dev;
1037
1038 dev = bus_find_device(&i2c_bus_type, NULL, node,
1039 of_dev_node_match);
1040 if (!dev)
1041 return NULL;
1042
1043 return i2c_verify_client(dev);
1044}
1045EXPORT_SYMBOL(of_find_i2c_device_by_node);
1046
1047/* must call put_device() when done with returned i2c_adapter device */
1048struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1049{
1050 struct device *dev;
1051
1052 dev = bus_find_device(&i2c_bus_type, NULL, node,
1053 of_dev_node_match);
1054 if (!dev)
1055 return NULL;
1056
1057 return i2c_verify_adapter(dev);
1058}
1059EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1060#else
1061static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1062#endif /* CONFIG_OF */
1063
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001064/* ACPI support code */
1065
1066#if IS_ENABLED(CONFIG_ACPI)
1067static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1068{
1069 struct i2c_board_info *info = data;
1070
1071 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1072 struct acpi_resource_i2c_serialbus *sb;
1073
1074 sb = &ares->data.i2c_serial_bus;
1075 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1076 info->addr = sb->slave_address;
1077 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1078 info->flags |= I2C_CLIENT_TEN;
1079 }
1080 } else if (info->irq < 0) {
1081 struct resource r;
1082
1083 if (acpi_dev_resource_interrupt(ares, 0, &r))
1084 info->irq = r.start;
1085 }
1086
1087 /* Tell the ACPI core to skip this resource */
1088 return 1;
1089}
1090
1091static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1092 void *data, void **return_value)
1093{
1094 struct i2c_adapter *adapter = data;
1095 struct list_head resource_list;
1096 struct i2c_board_info info;
1097 struct acpi_device *adev;
1098 int ret;
1099
1100 if (acpi_bus_get_device(handle, &adev))
1101 return AE_OK;
1102 if (acpi_bus_get_status(adev) || !adev->status.present)
1103 return AE_OK;
1104
1105 memset(&info, 0, sizeof(info));
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001106 info.acpi_node.companion = adev;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001107 info.irq = -1;
1108
1109 INIT_LIST_HEAD(&resource_list);
1110 ret = acpi_dev_get_resources(adev, &resource_list,
1111 acpi_i2c_add_resource, &info);
1112 acpi_dev_free_resource_list(&resource_list);
1113
1114 if (ret < 0 || !info.addr)
1115 return AE_OK;
1116
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001117 adev->power.flags.ignore_parent = true;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001118 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1119 if (!i2c_new_device(adapter, &info)) {
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001120 adev->power.flags.ignore_parent = false;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001121 dev_err(&adapter->dev,
1122 "failed to add I2C device %s from ACPI\n",
1123 dev_name(&adev->dev));
1124 }
1125
1126 return AE_OK;
1127}
1128
1129/**
1130 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1131 * @adap: pointer to adapter
1132 *
1133 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1134 * namespace. When a device is found it will be added to the Linux device
1135 * model and bound to the corresponding ACPI handle.
1136 */
1137static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1138{
1139 acpi_handle handle;
1140 acpi_status status;
1141
Jean Delvare47b6e4772013-10-10 08:04:06 +02001142 if (!adap->dev.parent)
1143 return;
1144
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001145 handle = ACPI_HANDLE(adap->dev.parent);
1146 if (!handle)
1147 return;
1148
1149 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1150 acpi_i2c_add_device, NULL,
1151 adap, NULL);
1152 if (ACPI_FAILURE(status))
1153 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1154}
1155#else
1156static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1157#endif /* CONFIG_ACPI */
1158
Jean Delvare69b00892009-12-06 17:06:27 +01001159static int i2c_do_add_adapter(struct i2c_driver *driver,
1160 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001161{
Jean Delvare4735c982008-07-14 22:38:36 +02001162 /* Detect supported devices on that bus, and instantiate them */
1163 i2c_detect(adap, driver);
1164
1165 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001166 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001167 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1168 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001169 dev_warn(&adap->dev, "Please use another way to instantiate "
1170 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001171 /* We ignore the return code; if it fails, too bad */
1172 driver->attach_adapter(adap);
1173 }
1174 return 0;
1175}
1176
Jean Delvare69b00892009-12-06 17:06:27 +01001177static int __process_new_adapter(struct device_driver *d, void *data)
1178{
1179 return i2c_do_add_adapter(to_i2c_driver(d), data);
1180}
1181
David Brownell6e13e642007-05-01 23:26:31 +02001182static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jean Delvared6703282010-08-11 18:20:59 +02001184 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
David Brownell1d0b19c2008-10-14 17:30:05 +02001186 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001187 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1188 res = -EAGAIN;
1189 goto out_list;
1190 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001191
Jean Delvare2236baa2010-11-15 22:40:38 +01001192 /* Sanity checks */
1193 if (unlikely(adap->name[0] == '\0')) {
1194 pr_err("i2c-core: Attempt to register an adapter with "
1195 "no name!\n");
1196 return -EINVAL;
1197 }
1198 if (unlikely(!adap->algo)) {
1199 pr_err("i2c-core: Attempt to register adapter '%s' with "
1200 "no algo!\n", adap->name);
1201 return -EINVAL;
1202 }
1203
Mika Kuoppala194684e2009-12-06 17:06:22 +01001204 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001205 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001206 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jean Delvare8fcfef62009-03-28 21:34:43 +01001208 /* Set default timeout to 1 second if not already set */
1209 if (adap->timeout == 0)
1210 adap->timeout = HZ;
1211
Kay Sievers27d9c182009-01-07 14:29:16 +01001212 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001213 adap->dev.bus = &i2c_bus_type;
1214 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001215 res = device_register(&adap->dev);
1216 if (res)
1217 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001219 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1220
Jean Delvare2bb50952009-09-18 22:45:46 +02001221#ifdef CONFIG_I2C_COMPAT
1222 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1223 adap->dev.parent);
1224 if (res)
1225 dev_warn(&adap->dev,
1226 "Failed to create compatibility class link\n");
1227#endif
1228
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301229 /* bus recovery specific initialization */
1230 if (adap->bus_recovery_info) {
1231 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1232
1233 if (!bri->recover_bus) {
1234 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1235 adap->bus_recovery_info = NULL;
1236 goto exit_recovery;
1237 }
1238
1239 /* Generic GPIO recovery */
1240 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1241 if (!gpio_is_valid(bri->scl_gpio)) {
1242 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1243 adap->bus_recovery_info = NULL;
1244 goto exit_recovery;
1245 }
1246
1247 if (gpio_is_valid(bri->sda_gpio))
1248 bri->get_sda = get_sda_gpio_value;
1249 else
1250 bri->get_sda = NULL;
1251
1252 bri->get_scl = get_scl_gpio_value;
1253 bri->set_scl = set_scl_gpio_value;
1254 } else if (!bri->set_scl || !bri->get_scl) {
1255 /* Generic SCL recovery */
1256 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1257 adap->bus_recovery_info = NULL;
1258 }
1259 }
1260
1261exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001262 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001263 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001264 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001265
David Brownell6e13e642007-05-01 23:26:31 +02001266 if (adap->nr < __i2c_first_dynamic_bus_num)
1267 i2c_scan_static_board_info(adap);
1268
Jean Delvare4735c982008-07-14 22:38:36 +02001269 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001270 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001271 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001272 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001273
1274 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001275
Jean Delvareb119c6c2006-08-15 18:26:30 +02001276out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001277 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001278 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001279 mutex_unlock(&core_lock);
1280 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
David Brownell6e13e642007-05-01 23:26:31 +02001283/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001284 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1285 * @adap: the adapter to register (with adap->nr initialized)
1286 * Context: can sleep
1287 *
1288 * See i2c_add_numbered_adapter() for details.
1289 */
1290static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1291{
1292 int id;
1293
1294 mutex_lock(&core_lock);
1295 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1296 GFP_KERNEL);
1297 mutex_unlock(&core_lock);
1298 if (id < 0)
1299 return id == -ENOSPC ? -EBUSY : id;
1300
1301 return i2c_register_adapter(adap);
1302}
1303
1304/**
David Brownell6e13e642007-05-01 23:26:31 +02001305 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1306 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001307 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001308 *
1309 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001310 * doesn't matter or when its bus number is specified by an dt alias.
1311 * Examples of bases when the bus number doesn't matter: I2C adapters
1312 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001313 *
1314 * When this returns zero, a new bus number was allocated and stored
1315 * in adap->nr, and the specified adapter became available for clients.
1316 * Otherwise, a negative errno value is returned.
1317 */
1318int i2c_add_adapter(struct i2c_adapter *adapter)
1319{
Doug Andersonee5c2742013-03-01 08:57:31 -08001320 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001321 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001322
Doug Andersonee5c2742013-03-01 08:57:31 -08001323 if (dev->of_node) {
1324 id = of_alias_get_id(dev->of_node, "i2c");
1325 if (id >= 0) {
1326 adapter->nr = id;
1327 return __i2c_add_numbered_adapter(adapter);
1328 }
1329 }
1330
Jean Delvarecaada322008-01-27 18:14:49 +01001331 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001332 id = idr_alloc(&i2c_adapter_idr, adapter,
1333 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001334 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001335 if (id < 0)
1336 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001337
1338 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001339
David Brownell6e13e642007-05-01 23:26:31 +02001340 return i2c_register_adapter(adapter);
1341}
1342EXPORT_SYMBOL(i2c_add_adapter);
1343
1344/**
1345 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1346 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001347 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001348 *
1349 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001350 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1351 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001352 * is used to properly configure I2C devices.
1353 *
Grant Likely488bf312011-07-25 17:49:43 +02001354 * If the requested bus number is set to -1, then this function will behave
1355 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1356 *
David Brownell6e13e642007-05-01 23:26:31 +02001357 * If no devices have pre-been declared for this bus, then be sure to
1358 * register the adapter before any dynamically allocated ones. Otherwise
1359 * the required bus ID may not be available.
1360 *
1361 * When this returns zero, the specified adapter became available for
1362 * clients using the bus number provided in adap->nr. Also, the table
1363 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1364 * and the appropriate driver model device nodes are created. Otherwise, a
1365 * negative errno value is returned.
1366 */
1367int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1368{
Grant Likely488bf312011-07-25 17:49:43 +02001369 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1370 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001371
Doug Andersonee5c2742013-03-01 08:57:31 -08001372 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001373}
1374EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1375
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001376static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001377 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001378{
Jean Delvare4735c982008-07-14 22:38:36 +02001379 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001380
Jean Delvareacec2112009-03-28 21:34:40 +01001381 /* Remove the devices we created ourselves as the result of hardware
1382 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001383 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1384 if (client->adapter == adapter) {
1385 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1386 client->name, client->addr);
1387 list_del(&client->detected);
1388 i2c_unregister_device(client);
1389 }
1390 }
Jean Delvare026526f2008-01-27 18:14:49 +01001391}
1392
Jean Delvaree549c2b2009-06-19 16:58:19 +02001393static int __unregister_client(struct device *dev, void *dummy)
1394{
1395 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001396 if (client && strcmp(client->name, "dummy"))
1397 i2c_unregister_device(client);
1398 return 0;
1399}
1400
1401static int __unregister_dummy(struct device *dev, void *dummy)
1402{
1403 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001404 if (client)
1405 i2c_unregister_device(client);
1406 return 0;
1407}
1408
Jean Delvare69b00892009-12-06 17:06:27 +01001409static int __process_removed_adapter(struct device_driver *d, void *data)
1410{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001411 i2c_do_del_adapter(to_i2c_driver(d), data);
1412 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001413}
1414
David Brownelld64f73b2007-07-12 14:12:28 +02001415/**
1416 * i2c_del_adapter - unregister I2C adapter
1417 * @adap: the adapter being unregistered
1418 * Context: can sleep
1419 *
1420 * This unregisters an I2C adapter which was previously registered
1421 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1422 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001423void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001425 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001426 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001429 mutex_lock(&core_lock);
1430 found = idr_find(&i2c_adapter_idr, adap->nr);
1431 mutex_unlock(&core_lock);
1432 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001433 pr_debug("i2c-core: attempting to delete unregistered "
1434 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
Jean Delvare026526f2008-01-27 18:14:49 +01001438 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001439 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001440 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001441 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001442 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001444 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001445 mutex_lock_nested(&adap->userspace_clients_lock,
1446 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001447 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1448 detected) {
1449 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1450 client->addr);
1451 list_del(&client->detected);
1452 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001453 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001454 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001455
Jean Delvaree549c2b2009-06-19 16:58:19 +02001456 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001457 * check the returned value. This is a two-pass process, because
1458 * we can't remove the dummy devices during the first pass: they
1459 * could have been instantiated by real devices wishing to clean
1460 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001461 device_for_each_child(&adap->dev, NULL, __unregister_client);
1462 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Jean Delvare2bb50952009-09-18 22:45:46 +02001464#ifdef CONFIG_I2C_COMPAT
1465 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1466 adap->dev.parent);
1467#endif
1468
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001469 /* device name is gone after device_unregister */
1470 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* clean up the sysfs representation */
1473 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* wait for sysfs to drop all references */
1477 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
David Brownell6e13e642007-05-01 23:26:31 +02001479 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001480 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001482 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001484 /* Clear the device structure in case this adapter is ever going to be
1485 added again */
1486 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
David Brownellc0564602007-05-01 23:26:31 +02001488EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
David Brownell7b4fbc52007-05-01 23:26:30 +02001490/* ------------------------------------------------------------------------- */
1491
Jean Delvare7ae31482011-03-20 14:50:52 +01001492int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1493{
1494 int res;
1495
1496 mutex_lock(&core_lock);
1497 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1498 mutex_unlock(&core_lock);
1499
1500 return res;
1501}
1502EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1503
Jean Delvare69b00892009-12-06 17:06:27 +01001504static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001505{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001506 if (dev->type != &i2c_adapter_type)
1507 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001508 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001509}
1510
David Brownell7b4fbc52007-05-01 23:26:30 +02001511/*
1512 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001513 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
1515
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001516int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001518 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
David Brownell1d0b19c2008-10-14 17:30:05 +02001520 /* Can't register until after driver model init */
1521 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1522 return -EAGAIN;
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001525 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jean Delvare729d6dd2009-06-19 16:58:18 +02001528 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001529 * will have called probe() for all matching-but-unbound devices.
1530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 res = driver_register(&driver->driver);
1532 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001533 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001534
Mark Brownf4e8db32011-01-14 22:03:50 +01001535 /* Drivers should switch to dev_pm_ops instead. */
1536 if (driver->suspend)
1537 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1538 driver->driver.name);
1539 if (driver->resume)
1540 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1541 driver->driver.name);
1542
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001543 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Jean Delvare4735c982008-07-14 22:38:36 +02001545 INIT_LIST_HEAD(&driver->clients);
1546 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001547 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001548
Jean Delvare7eebcb72006-02-05 23:28:21 +01001549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001551EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Jean Delvare69b00892009-12-06 17:06:27 +01001553static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001554{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001555 if (dev->type == &i2c_adapter_type)
1556 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1557 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001558}
1559
David Brownella1d9e6e2007-05-01 23:26:30 +02001560/**
1561 * i2c_del_driver - unregister I2C driver
1562 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001563 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001564 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001565void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Jean Delvare7ae31482011-03-20 14:50:52 +01001567 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001570 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
David Brownellc0564602007-05-01 23:26:31 +02001572EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
David Brownell7b4fbc52007-05-01 23:26:30 +02001574/* ------------------------------------------------------------------------- */
1575
Jean Delvaree48d3312008-01-27 18:14:48 +01001576/**
1577 * i2c_use_client - increments the reference count of the i2c client structure
1578 * @client: the client being referenced
1579 *
1580 * Each live reference to a client should be refcounted. The driver model does
1581 * that automatically as part of driver binding, so that most drivers don't
1582 * need to do this explicitly: they hold a reference until they're unbound
1583 * from the device.
1584 *
1585 * A pointer to the client with the incremented reference counter is returned.
1586 */
1587struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
David Brownell6ea438e2008-07-14 22:38:24 +02001589 if (client && get_device(&client->dev))
1590 return client;
1591 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
David Brownellc0564602007-05-01 23:26:31 +02001593EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jean Delvaree48d3312008-01-27 18:14:48 +01001595/**
1596 * i2c_release_client - release a use of the i2c client structure
1597 * @client: the client being no longer referenced
1598 *
1599 * Must be called when a user of a client is finished with it.
1600 */
1601void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
David Brownell6ea438e2008-07-14 22:38:24 +02001603 if (client)
1604 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
David Brownellc0564602007-05-01 23:26:31 +02001606EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David Brownell9b766b82008-01-27 18:14:51 +01001608struct i2c_cmd_arg {
1609 unsigned cmd;
1610 void *arg;
1611};
1612
1613static int i2c_cmd(struct device *dev, void *_arg)
1614{
1615 struct i2c_client *client = i2c_verify_client(dev);
1616 struct i2c_cmd_arg *arg = _arg;
1617
1618 if (client && client->driver && client->driver->command)
1619 client->driver->command(client, arg->cmd, arg->arg);
1620 return 0;
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1624{
David Brownell9b766b82008-01-27 18:14:51 +01001625 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
David Brownell9b766b82008-01-27 18:14:51 +01001627 cmd_arg.cmd = cmd;
1628 cmd_arg.arg = arg;
1629 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
David Brownellc0564602007-05-01 23:26:31 +02001631EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633static int __init i2c_init(void)
1634{
1635 int retval;
1636
1637 retval = bus_register(&i2c_bus_type);
1638 if (retval)
1639 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001640#ifdef CONFIG_I2C_COMPAT
1641 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1642 if (!i2c_adapter_compat_class) {
1643 retval = -ENOMEM;
1644 goto bus_err;
1645 }
1646#endif
David Brownelle9f13732008-01-27 18:14:52 +01001647 retval = i2c_add_driver(&dummy_driver);
1648 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001649 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001650 return 0;
1651
Jean Delvare2bb50952009-09-18 22:45:46 +02001652class_err:
1653#ifdef CONFIG_I2C_COMPAT
1654 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001655bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001656#endif
David Brownelle9f13732008-01-27 18:14:52 +01001657 bus_unregister(&i2c_bus_type);
1658 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661static void __exit i2c_exit(void)
1662{
David Brownelle9f13732008-01-27 18:14:52 +01001663 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001664#ifdef CONFIG_I2C_COMPAT
1665 class_compat_unregister(i2c_adapter_compat_class);
1666#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 bus_unregister(&i2c_bus_type);
1668}
1669
David Brownella10f9e72008-10-14 17:30:06 +02001670/* We must initialize early, because some subsystems register i2c drivers
1671 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1672 */
1673postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674module_exit(i2c_exit);
1675
1676/* ----------------------------------------------------
1677 * the functional interface to the i2c busses.
1678 * ----------------------------------------------------
1679 */
1680
David Brownella1cdeda2008-07-14 22:38:24 +02001681/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001682 * __i2c_transfer - unlocked flavor of i2c_transfer
1683 * @adap: Handle to I2C bus
1684 * @msgs: One or more messages to execute before STOP is issued to
1685 * terminate the operation; each message begins with a START.
1686 * @num: Number of messages to be executed.
1687 *
1688 * Returns negative errno, else the number of messages executed.
1689 *
1690 * Adapter lock must be held when calling this function. No debug logging
1691 * takes place. adap->algo->master_xfer existence isn't checked.
1692 */
1693int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1694{
1695 unsigned long orig_jiffies;
1696 int ret, try;
1697
1698 /* Retry automatically on arbitration loss */
1699 orig_jiffies = jiffies;
1700 for (ret = 0, try = 0; try <= adap->retries; try++) {
1701 ret = adap->algo->master_xfer(adap, msgs, num);
1702 if (ret != -EAGAIN)
1703 break;
1704 if (time_after(jiffies, orig_jiffies + adap->timeout))
1705 break;
1706 }
1707
1708 return ret;
1709}
1710EXPORT_SYMBOL(__i2c_transfer);
1711
1712/**
David Brownella1cdeda2008-07-14 22:38:24 +02001713 * i2c_transfer - execute a single or combined I2C message
1714 * @adap: Handle to I2C bus
1715 * @msgs: One or more messages to execute before STOP is issued to
1716 * terminate the operation; each message begins with a START.
1717 * @num: Number of messages to be executed.
1718 *
1719 * Returns negative errno, else the number of messages executed.
1720 *
1721 * Note that there is no requirement that each message be sent to
1722 * the same slave address, although that is the most common model.
1723 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001724int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001726 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
David Brownella1cdeda2008-07-14 22:38:24 +02001728 /* REVISIT the fault reporting model here is weak:
1729 *
1730 * - When we get an error after receiving N bytes from a slave,
1731 * there is no way to report "N".
1732 *
1733 * - When we get a NAK after transmitting N bytes to a slave,
1734 * there is no way to report "N" ... or to let the master
1735 * continue executing the rest of this combined message, if
1736 * that's the appropriate response.
1737 *
1738 * - When for example "num" is two and we successfully complete
1739 * the first message but get an error part way through the
1740 * second, it's unclear whether that should be reported as
1741 * one (discarding status on the second message) or errno
1742 * (discarding status on the first one).
1743 */
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (adap->algo->master_xfer) {
1746#ifdef DEBUG
1747 for (ret = 0; ret < num; ret++) {
1748 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001749 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1750 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1751 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753#endif
1754
Mike Rapoportcea443a2008-01-27 18:14:50 +01001755 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001756 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001757 if (!ret)
1758 /* I2C activity is ongoing. */
1759 return -EAGAIN;
1760 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001761 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001762 }
1763
Jean Delvareb37d2a32012-06-29 07:47:19 -03001764 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001765 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 return ret;
1768 } else {
1769 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001770 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772}
David Brownellc0564602007-05-01 23:26:31 +02001773EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
David Brownella1cdeda2008-07-14 22:38:24 +02001775/**
1776 * i2c_master_send - issue a single I2C message in master transmit mode
1777 * @client: Handle to slave device
1778 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001779 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001780 *
1781 * Returns negative errno, or else the number of bytes written.
1782 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001783int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001786 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct i2c_msg msg;
1788
Jean Delvare815f55f2005-05-07 22:58:46 +02001789 msg.addr = client->addr;
1790 msg.flags = client->flags & I2C_M_TEN;
1791 msg.len = count;
1792 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001793
Jean Delvare815f55f2005-05-07 22:58:46 +02001794 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001796 /*
1797 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1798 * transmitted, else error code.
1799 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001800 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
David Brownellc0564602007-05-01 23:26:31 +02001802EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
David Brownella1cdeda2008-07-14 22:38:24 +02001804/**
1805 * i2c_master_recv - issue a single I2C message in master receive mode
1806 * @client: Handle to slave device
1807 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001808 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001809 *
1810 * Returns negative errno, or else the number of bytes read.
1811 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001812int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Farid Hammane7225acf2010-05-21 18:40:58 +02001814 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct i2c_msg msg;
1816 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jean Delvare815f55f2005-05-07 22:58:46 +02001818 msg.addr = client->addr;
1819 msg.flags = client->flags & I2C_M_TEN;
1820 msg.flags |= I2C_M_RD;
1821 msg.len = count;
1822 msg.buf = buf;
1823
1824 ret = i2c_transfer(adap, &msg, 1);
1825
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001826 /*
1827 * If everything went ok (i.e. 1 msg received), return #bytes received,
1828 * else error code.
1829 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001830 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
David Brownellc0564602007-05-01 23:26:31 +02001832EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/* ----------------------------------------------------
1835 * the i2c address scanning function
1836 * Will not work for 10-bit addresses!
1837 * ----------------------------------------------------
1838 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001839
Jean Delvare63e4e802010-06-03 11:33:51 +02001840/*
1841 * Legacy default probe function, mostly relevant for SMBus. The default
1842 * probe method is a quick write, but it is known to corrupt the 24RF08
1843 * EEPROMs due to a state machine bug, and could also irreversibly
1844 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1845 * we use a short byte read instead. Also, some bus drivers don't implement
1846 * quick write, so we fallback to a byte read in that case too.
1847 * On x86, there is another special case for FSC hardware monitoring chips,
1848 * which want regular byte reads (address 0x73.) Fortunately, these are the
1849 * only known chips using this I2C address on PC hardware.
1850 * Returns 1 if probe succeeded, 0 if not.
1851 */
1852static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1853{
1854 int err;
1855 union i2c_smbus_data dummy;
1856
1857#ifdef CONFIG_X86
1858 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1859 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1860 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1861 I2C_SMBUS_BYTE_DATA, &dummy);
1862 else
1863#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001864 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1865 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001866 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1867 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001868 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1869 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1870 I2C_SMBUS_BYTE, &dummy);
1871 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001872 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1873 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001874 err = -EOPNOTSUPP;
1875 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001876
1877 return err >= 0;
1878}
1879
Jean Delvareccfbbd02009-12-06 17:06:25 +01001880static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001881 struct i2c_driver *driver)
1882{
1883 struct i2c_board_info info;
1884 struct i2c_adapter *adapter = temp_client->adapter;
1885 int addr = temp_client->addr;
1886 int err;
1887
1888 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001889 err = i2c_check_addr_validity(addr);
1890 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001891 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1892 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001893 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001894 }
1895
1896 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001897 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001898 return 0;
1899
Jean Delvareccfbbd02009-12-06 17:06:25 +01001900 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001901 if (!i2c_default_probe(adapter, addr))
1902 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001903
1904 /* Finally call the custom detection function */
1905 memset(&info, 0, sizeof(struct i2c_board_info));
1906 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001907 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001908 if (err) {
1909 /* -ENODEV is returned if the detection fails. We catch it
1910 here as this isn't an error. */
1911 return err == -ENODEV ? 0 : err;
1912 }
1913
1914 /* Consistency check */
1915 if (info.type[0] == '\0') {
1916 dev_err(&adapter->dev, "%s detection function provided "
1917 "no name for 0x%x\n", driver->driver.name,
1918 addr);
1919 } else {
1920 struct i2c_client *client;
1921
1922 /* Detection succeeded, instantiate the device */
1923 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1924 info.type, info.addr);
1925 client = i2c_new_device(adapter, &info);
1926 if (client)
1927 list_add_tail(&client->detected, &driver->clients);
1928 else
1929 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1930 info.type, info.addr);
1931 }
1932 return 0;
1933}
1934
1935static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1936{
Jean Delvarec3813d62009-12-14 21:17:25 +01001937 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001938 struct i2c_client *temp_client;
1939 int i, err = 0;
1940 int adap_id = i2c_adapter_id(adapter);
1941
Jean Delvarec3813d62009-12-14 21:17:25 +01001942 address_list = driver->address_list;
1943 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001944 return 0;
1945
Jean Delvare51b54ba2010-10-24 18:16:58 +02001946 /* Stop here if the classes do not match */
1947 if (!(adapter->class & driver->class))
1948 return 0;
1949
Jean Delvare4735c982008-07-14 22:38:36 +02001950 /* Set up a temporary client to help detect callback */
1951 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1952 if (!temp_client)
1953 return -ENOMEM;
1954 temp_client->adapter = adapter;
1955
Jean Delvarec3813d62009-12-14 21:17:25 +01001956 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001957 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001958 "addr 0x%02x\n", adap_id, address_list[i]);
1959 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001960 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001961 if (unlikely(err))
1962 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001963 }
1964
Jean Delvare4735c982008-07-14 22:38:36 +02001965 kfree(temp_client);
1966 return err;
1967}
1968
Jean Delvared44f19d2010-08-11 18:20:57 +02001969int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1970{
1971 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1972 I2C_SMBUS_QUICK, NULL) >= 0;
1973}
1974EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1975
Jean Delvare12b5053a2007-05-01 23:26:31 +02001976struct i2c_client *
1977i2c_new_probed_device(struct i2c_adapter *adap,
1978 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001979 unsigned short const *addr_list,
1980 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001981{
1982 int i;
1983
Jean Delvare8031d792010-08-11 18:21:00 +02001984 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001985 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001986
Jean Delvare12b5053a2007-05-01 23:26:31 +02001987 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1988 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001989 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001990 dev_warn(&adap->dev, "Invalid 7-bit address "
1991 "0x%02x\n", addr_list[i]);
1992 continue;
1993 }
1994
1995 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001996 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001997 dev_dbg(&adap->dev, "Address 0x%02x already in "
1998 "use, not probing\n", addr_list[i]);
1999 continue;
2000 }
2001
Jean Delvare63e4e802010-06-03 11:33:51 +02002002 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002003 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002004 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002005 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002006
2007 if (addr_list[i] == I2C_CLIENT_END) {
2008 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2009 return NULL;
2010 }
2011
2012 info->addr = addr_list[i];
2013 return i2c_new_device(adap, info);
2014}
2015EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2016
Jean Delvared735b342011-03-20 14:50:52 +01002017struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002020
Jean Delvarecaada322008-01-27 18:14:49 +01002021 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002022 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002023 if (adapter && !try_module_get(adapter->owner))
2024 adapter = NULL;
2025
Jean Delvarecaada322008-01-27 18:14:49 +01002026 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002027 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
David Brownellc0564602007-05-01 23:26:31 +02002029EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031void i2c_put_adapter(struct i2c_adapter *adap)
2032{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002033 if (adap)
2034 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
David Brownellc0564602007-05-01 23:26:31 +02002036EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038/* The SMBus parts */
2039
David Brownell438d6c22006-12-10 21:21:31 +01002040#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002041static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002044
Farid Hammane7225acf2010-05-21 18:40:58 +02002045 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002046 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 data = data ^ POLY;
2048 data = data << 1;
2049 }
2050 return (u8)(data >> 8);
2051}
2052
Jean Delvare421ef472005-10-26 21:28:55 +02002053/* Incremental CRC8 over count bytes in the array pointed to by p */
2054static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int i;
2057
Farid Hammane7225acf2010-05-21 18:40:58 +02002058 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002059 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return crc;
2061}
2062
Jean Delvare421ef472005-10-26 21:28:55 +02002063/* Assume a 7-bit address, which is reasonable for SMBus */
2064static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Jean Delvare421ef472005-10-26 21:28:55 +02002066 /* The address will be sent first */
2067 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2068 pec = i2c_smbus_pec(pec, &addr, 1);
2069
2070 /* The data buffer follows */
2071 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Jean Delvare421ef472005-10-26 21:28:55 +02002074/* Used for write only transactions */
2075static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Jean Delvare421ef472005-10-26 21:28:55 +02002077 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2078 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Jean Delvare421ef472005-10-26 21:28:55 +02002081/* Return <0 on CRC error
2082 If there was a write before this read (most cases) we need to take the
2083 partial CRC from the write part into account.
2084 Note that this function does modify the message (we need to decrease the
2085 message length to hide the CRC byte from the caller). */
2086static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Jean Delvare421ef472005-10-26 21:28:55 +02002088 u8 rpec = msg->buf[--msg->len];
2089 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (rpec != cpec) {
2092 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2093 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002094 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
David Brownell438d6c22006-12-10 21:21:31 +01002096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
David Brownella1cdeda2008-07-14 22:38:24 +02002099/**
2100 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2101 * @client: Handle to slave device
2102 *
2103 * This executes the SMBus "receive byte" protocol, returning negative errno
2104 * else the byte received from the device.
2105 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002106s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002109 int status;
2110
2111 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2112 I2C_SMBUS_READ, 0,
2113 I2C_SMBUS_BYTE, &data);
2114 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
David Brownellc0564602007-05-01 23:26:31 +02002116EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David Brownella1cdeda2008-07-14 22:38:24 +02002118/**
2119 * i2c_smbus_write_byte - SMBus "send byte" protocol
2120 * @client: Handle to slave device
2121 * @value: Byte to be sent
2122 *
2123 * This executes the SMBus "send byte" protocol, returning negative errno
2124 * else zero on success.
2125 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002126s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Farid Hammane7225acf2010-05-21 18:40:58 +02002128 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002129 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
David Brownellc0564602007-05-01 23:26:31 +02002131EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
David Brownella1cdeda2008-07-14 22:38:24 +02002133/**
2134 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2135 * @client: Handle to slave device
2136 * @command: Byte interpreted by slave
2137 *
2138 * This executes the SMBus "read byte" protocol, returning negative errno
2139 * else a data byte received from the device.
2140 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002141s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002144 int status;
2145
2146 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2147 I2C_SMBUS_READ, command,
2148 I2C_SMBUS_BYTE_DATA, &data);
2149 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
David Brownellc0564602007-05-01 23:26:31 +02002151EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
David Brownella1cdeda2008-07-14 22:38:24 +02002153/**
2154 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2155 * @client: Handle to slave device
2156 * @command: Byte interpreted by slave
2157 * @value: Byte being written
2158 *
2159 * This executes the SMBus "write byte" protocol, returning negative errno
2160 * else zero on success.
2161 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002162s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2163 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 union i2c_smbus_data data;
2166 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002167 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2168 I2C_SMBUS_WRITE, command,
2169 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
David Brownellc0564602007-05-01 23:26:31 +02002171EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
David Brownella1cdeda2008-07-14 22:38:24 +02002173/**
2174 * i2c_smbus_read_word_data - SMBus "read word" protocol
2175 * @client: Handle to slave device
2176 * @command: Byte interpreted by slave
2177 *
2178 * This executes the SMBus "read word" protocol, returning negative errno
2179 * else a 16-bit unsigned "word" received from the device.
2180 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002181s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002184 int status;
2185
2186 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2187 I2C_SMBUS_READ, command,
2188 I2C_SMBUS_WORD_DATA, &data);
2189 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
David Brownellc0564602007-05-01 23:26:31 +02002191EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
David Brownella1cdeda2008-07-14 22:38:24 +02002193/**
2194 * i2c_smbus_write_word_data - SMBus "write word" protocol
2195 * @client: Handle to slave device
2196 * @command: Byte interpreted by slave
2197 * @value: 16-bit "word" being written
2198 *
2199 * This executes the SMBus "write word" protocol, returning negative errno
2200 * else zero on success.
2201 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002202s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2203 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 union i2c_smbus_data data;
2206 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002207 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2208 I2C_SMBUS_WRITE, command,
2209 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
David Brownellc0564602007-05-01 23:26:31 +02002211EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
David Brownella64ec072007-10-13 23:56:31 +02002213/**
David Brownella1cdeda2008-07-14 22:38:24 +02002214 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002215 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002216 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002217 * @values: Byte array into which data will be read; big enough to hold
2218 * the data returned by the slave. SMBus allows at most 32 bytes.
2219 *
David Brownella1cdeda2008-07-14 22:38:24 +02002220 * This executes the SMBus "block read" protocol, returning negative errno
2221 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002222 *
2223 * Note that using this function requires that the client's adapter support
2224 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2225 * support this; its emulation through I2C messaging relies on a specific
2226 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2227 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002228s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002229 u8 *values)
2230{
2231 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002232 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002233
David Brownell24a5bb72008-07-14 22:38:23 +02002234 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2235 I2C_SMBUS_READ, command,
2236 I2C_SMBUS_BLOCK_DATA, &data);
2237 if (status)
2238 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002239
2240 memcpy(values, &data.block[1], data.block[0]);
2241 return data.block[0];
2242}
2243EXPORT_SYMBOL(i2c_smbus_read_block_data);
2244
David Brownella1cdeda2008-07-14 22:38:24 +02002245/**
2246 * i2c_smbus_write_block_data - SMBus "block write" protocol
2247 * @client: Handle to slave device
2248 * @command: Byte interpreted by slave
2249 * @length: Size of data block; SMBus allows at most 32 bytes
2250 * @values: Byte array which will be written.
2251 *
2252 * This executes the SMBus "block write" protocol, returning negative errno
2253 * else zero on success.
2254 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002255s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002256 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (length > I2C_SMBUS_BLOCK_MAX)
2261 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002263 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002264 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2265 I2C_SMBUS_WRITE, command,
2266 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
David Brownellc0564602007-05-01 23:26:31 +02002268EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002271s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002272 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002275 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002276
Jean Delvare4b2643d2007-07-12 14:12:29 +02002277 if (length > I2C_SMBUS_BLOCK_MAX)
2278 length = I2C_SMBUS_BLOCK_MAX;
2279 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002280 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2281 I2C_SMBUS_READ, command,
2282 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2283 if (status < 0)
2284 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002285
2286 memcpy(values, &data.block[1], data.block[0]);
2287 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
David Brownellc0564602007-05-01 23:26:31 +02002289EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Jean Delvare0cc43a12011-01-10 22:11:23 +01002291s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002292 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002293{
2294 union i2c_smbus_data data;
2295
2296 if (length > I2C_SMBUS_BLOCK_MAX)
2297 length = I2C_SMBUS_BLOCK_MAX;
2298 data.block[0] = length;
2299 memcpy(data.block + 1, values, length);
2300 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2301 I2C_SMBUS_WRITE, command,
2302 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2303}
David Brownellc0564602007-05-01 23:26:31 +02002304EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002305
David Brownell438d6c22006-12-10 21:21:31 +01002306/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002308static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2309 unsigned short flags,
2310 char read_write, u8 command, int size,
2311 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
2313 /* So we need to generate a series of msgs. In the case of writing, we
2314 need to use only one message; when reading, we need two. We initialize
2315 most things with sane defaults, to keep the code below somewhat
2316 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002317 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2318 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002319 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002321 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002322 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002323 struct i2c_msg msg[2] = {
2324 {
2325 .addr = addr,
2326 .flags = flags,
2327 .len = 1,
2328 .buf = msgbuf0,
2329 }, {
2330 .addr = addr,
2331 .flags = flags | I2C_M_RD,
2332 .len = 0,
2333 .buf = msgbuf1,
2334 },
2335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002338 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 case I2C_SMBUS_QUICK:
2340 msg[0].len = 0;
2341 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002342 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2343 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 num = 1;
2345 break;
2346 case I2C_SMBUS_BYTE:
2347 if (read_write == I2C_SMBUS_READ) {
2348 /* Special case: only a read! */
2349 msg[0].flags = I2C_M_RD | flags;
2350 num = 1;
2351 }
2352 break;
2353 case I2C_SMBUS_BYTE_DATA:
2354 if (read_write == I2C_SMBUS_READ)
2355 msg[1].len = 1;
2356 else {
2357 msg[0].len = 2;
2358 msgbuf0[1] = data->byte;
2359 }
2360 break;
2361 case I2C_SMBUS_WORD_DATA:
2362 if (read_write == I2C_SMBUS_READ)
2363 msg[1].len = 2;
2364 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002365 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002367 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 break;
2370 case I2C_SMBUS_PROC_CALL:
2371 num = 2; /* Special case */
2372 read_write = I2C_SMBUS_READ;
2373 msg[0].len = 3;
2374 msg[1].len = 2;
2375 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002376 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 break;
2378 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002380 msg[1].flags |= I2C_M_RECV_LEN;
2381 msg[1].len = 1; /* block length will be added by
2382 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 } else {
2384 msg[0].len = data->block[0] + 2;
2385 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002386 dev_err(&adapter->dev,
2387 "Invalid block write size %d\n",
2388 data->block[0]);
2389 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002391 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 msgbuf0[i] = data->block[i-1];
2393 }
2394 break;
2395 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002396 num = 2; /* Another special case */
2397 read_write = I2C_SMBUS_READ;
2398 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002399 dev_err(&adapter->dev,
2400 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002401 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002402 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002403 }
2404 msg[0].len = data->block[0] + 2;
2405 for (i = 1; i < msg[0].len; i++)
2406 msgbuf0[i] = data->block[i-1];
2407 msg[1].flags |= I2C_M_RECV_LEN;
2408 msg[1].len = 1; /* block length will be added by
2409 the underlying bus driver */
2410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 case I2C_SMBUS_I2C_BLOCK_DATA:
2412 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002413 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 } else {
2415 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002416 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002417 dev_err(&adapter->dev,
2418 "Invalid block write size %d\n",
2419 data->block[0]);
2420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 for (i = 1; i <= data->block[0]; i++)
2423 msgbuf0[i] = data->block[i];
2424 }
2425 break;
2426 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002427 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2428 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
Jean Delvare421ef472005-10-26 21:28:55 +02002431 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2432 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2433 if (i) {
2434 /* Compute PEC if first message is a write */
2435 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002436 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002437 i2c_smbus_add_pec(&msg[0]);
2438 else /* Write followed by read */
2439 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2440 }
2441 /* Ask for PEC if last message is a read */
2442 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002443 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002444 }
2445
David Brownell24a5bb72008-07-14 22:38:23 +02002446 status = i2c_transfer(adapter, msg, num);
2447 if (status < 0)
2448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Jean Delvare421ef472005-10-26 21:28:55 +02002450 /* Check PEC if last message is a read */
2451 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002452 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2453 if (status < 0)
2454 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002455 }
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002458 switch (size) {
2459 case I2C_SMBUS_BYTE:
2460 data->byte = msgbuf0[0];
2461 break;
2462 case I2C_SMBUS_BYTE_DATA:
2463 data->byte = msgbuf1[0];
2464 break;
2465 case I2C_SMBUS_WORD_DATA:
2466 case I2C_SMBUS_PROC_CALL:
2467 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2468 break;
2469 case I2C_SMBUS_I2C_BLOCK_DATA:
2470 for (i = 0; i < data->block[0]; i++)
2471 data->block[i+1] = msgbuf1[i];
2472 break;
2473 case I2C_SMBUS_BLOCK_DATA:
2474 case I2C_SMBUS_BLOCK_PROC_CALL:
2475 for (i = 0; i < msgbuf1[0] + 1; i++)
2476 data->block[i] = msgbuf1[i];
2477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479 return 0;
2480}
2481
David Brownella1cdeda2008-07-14 22:38:24 +02002482/**
2483 * i2c_smbus_xfer - execute SMBus protocol operations
2484 * @adapter: Handle to I2C bus
2485 * @addr: Address of SMBus slave on that bus
2486 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2487 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2488 * @command: Byte interpreted by slave, for protocols which use such bytes
2489 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2490 * @data: Data to be read or written
2491 *
2492 * This executes an SMBus protocol operation, and returns a negative
2493 * errno code else zero on success.
2494 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002495s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002496 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002497 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002499 unsigned long orig_jiffies;
2500 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002503 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002506 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002507
2508 /* Retry automatically on arbitration loss */
2509 orig_jiffies = jiffies;
2510 for (res = 0, try = 0; try <= adapter->retries; try++) {
2511 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2512 read_write, command,
2513 protocol, data);
2514 if (res != -EAGAIN)
2515 break;
2516 if (time_after(jiffies,
2517 orig_jiffies + adapter->timeout))
2518 break;
2519 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002520 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002522 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2523 return res;
2524 /*
2525 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2526 * implement native support for the SMBus operation.
2527 */
2528 }
2529
2530 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2531 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2536MODULE_DESCRIPTION("I2C-Bus main module");
2537MODULE_LICENSE("GPL");