blob: 03a8ae64d7625bacf1bd0ede1c6fb374bf15c43e [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;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100677 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
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));
1106 info.acpi_node.handle = handle;
1107 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
1142 handle = ACPI_HANDLE(adap->dev.parent);
1143 if (!handle)
1144 return;
1145
1146 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1147 acpi_i2c_add_device, NULL,
1148 adap, NULL);
1149 if (ACPI_FAILURE(status))
1150 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1151}
1152#else
1153static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1154#endif /* CONFIG_ACPI */
1155
Jean Delvare69b00892009-12-06 17:06:27 +01001156static int i2c_do_add_adapter(struct i2c_driver *driver,
1157 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001158{
Jean Delvare4735c982008-07-14 22:38:36 +02001159 /* Detect supported devices on that bus, and instantiate them */
1160 i2c_detect(adap, driver);
1161
1162 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001163 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001164 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1165 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001166 dev_warn(&adap->dev, "Please use another way to instantiate "
1167 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001168 /* We ignore the return code; if it fails, too bad */
1169 driver->attach_adapter(adap);
1170 }
1171 return 0;
1172}
1173
Jean Delvare69b00892009-12-06 17:06:27 +01001174static int __process_new_adapter(struct device_driver *d, void *data)
1175{
1176 return i2c_do_add_adapter(to_i2c_driver(d), data);
1177}
1178
David Brownell6e13e642007-05-01 23:26:31 +02001179static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Jean Delvared6703282010-08-11 18:20:59 +02001181 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
David Brownell1d0b19c2008-10-14 17:30:05 +02001183 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001184 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1185 res = -EAGAIN;
1186 goto out_list;
1187 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001188
Jean Delvare2236baa2010-11-15 22:40:38 +01001189 /* Sanity checks */
1190 if (unlikely(adap->name[0] == '\0')) {
1191 pr_err("i2c-core: Attempt to register an adapter with "
1192 "no name!\n");
1193 return -EINVAL;
1194 }
1195 if (unlikely(!adap->algo)) {
1196 pr_err("i2c-core: Attempt to register adapter '%s' with "
1197 "no algo!\n", adap->name);
1198 return -EINVAL;
1199 }
1200
Mika Kuoppala194684e2009-12-06 17:06:22 +01001201 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001202 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001203 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jean Delvare8fcfef62009-03-28 21:34:43 +01001205 /* Set default timeout to 1 second if not already set */
1206 if (adap->timeout == 0)
1207 adap->timeout = HZ;
1208
Kay Sievers27d9c182009-01-07 14:29:16 +01001209 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001210 adap->dev.bus = &i2c_bus_type;
1211 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001212 res = device_register(&adap->dev);
1213 if (res)
1214 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001216 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1217
Jean Delvare2bb50952009-09-18 22:45:46 +02001218#ifdef CONFIG_I2C_COMPAT
1219 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1220 adap->dev.parent);
1221 if (res)
1222 dev_warn(&adap->dev,
1223 "Failed to create compatibility class link\n");
1224#endif
1225
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301226 /* bus recovery specific initialization */
1227 if (adap->bus_recovery_info) {
1228 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1229
1230 if (!bri->recover_bus) {
1231 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1232 adap->bus_recovery_info = NULL;
1233 goto exit_recovery;
1234 }
1235
1236 /* Generic GPIO recovery */
1237 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1238 if (!gpio_is_valid(bri->scl_gpio)) {
1239 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1240 adap->bus_recovery_info = NULL;
1241 goto exit_recovery;
1242 }
1243
1244 if (gpio_is_valid(bri->sda_gpio))
1245 bri->get_sda = get_sda_gpio_value;
1246 else
1247 bri->get_sda = NULL;
1248
1249 bri->get_scl = get_scl_gpio_value;
1250 bri->set_scl = set_scl_gpio_value;
1251 } else if (!bri->set_scl || !bri->get_scl) {
1252 /* Generic SCL recovery */
1253 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1254 adap->bus_recovery_info = NULL;
1255 }
1256 }
1257
1258exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001259 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001260 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001261 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001262
David Brownell6e13e642007-05-01 23:26:31 +02001263 if (adap->nr < __i2c_first_dynamic_bus_num)
1264 i2c_scan_static_board_info(adap);
1265
Jean Delvare4735c982008-07-14 22:38:36 +02001266 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001267 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001268 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001269 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001270
1271 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001272
Jean Delvareb119c6c2006-08-15 18:26:30 +02001273out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001274 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001275 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001276 mutex_unlock(&core_lock);
1277 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
David Brownell6e13e642007-05-01 23:26:31 +02001280/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001281 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1282 * @adap: the adapter to register (with adap->nr initialized)
1283 * Context: can sleep
1284 *
1285 * See i2c_add_numbered_adapter() for details.
1286 */
1287static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1288{
1289 int id;
1290
1291 mutex_lock(&core_lock);
1292 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1293 GFP_KERNEL);
1294 mutex_unlock(&core_lock);
1295 if (id < 0)
1296 return id == -ENOSPC ? -EBUSY : id;
1297
1298 return i2c_register_adapter(adap);
1299}
1300
1301/**
David Brownell6e13e642007-05-01 23:26:31 +02001302 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1303 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001304 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001305 *
1306 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001307 * doesn't matter or when its bus number is specified by an dt alias.
1308 * Examples of bases when the bus number doesn't matter: I2C adapters
1309 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001310 *
1311 * When this returns zero, a new bus number was allocated and stored
1312 * in adap->nr, and the specified adapter became available for clients.
1313 * Otherwise, a negative errno value is returned.
1314 */
1315int i2c_add_adapter(struct i2c_adapter *adapter)
1316{
Doug Andersonee5c2742013-03-01 08:57:31 -08001317 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001318 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001319
Doug Andersonee5c2742013-03-01 08:57:31 -08001320 if (dev->of_node) {
1321 id = of_alias_get_id(dev->of_node, "i2c");
1322 if (id >= 0) {
1323 adapter->nr = id;
1324 return __i2c_add_numbered_adapter(adapter);
1325 }
1326 }
1327
Jean Delvarecaada322008-01-27 18:14:49 +01001328 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001329 id = idr_alloc(&i2c_adapter_idr, adapter,
1330 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001331 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001332 if (id < 0)
1333 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001334
1335 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001336
David Brownell6e13e642007-05-01 23:26:31 +02001337 return i2c_register_adapter(adapter);
1338}
1339EXPORT_SYMBOL(i2c_add_adapter);
1340
1341/**
1342 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1343 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001344 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001345 *
1346 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001347 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1348 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001349 * is used to properly configure I2C devices.
1350 *
Grant Likely488bf312011-07-25 17:49:43 +02001351 * If the requested bus number is set to -1, then this function will behave
1352 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1353 *
David Brownell6e13e642007-05-01 23:26:31 +02001354 * If no devices have pre-been declared for this bus, then be sure to
1355 * register the adapter before any dynamically allocated ones. Otherwise
1356 * the required bus ID may not be available.
1357 *
1358 * When this returns zero, the specified adapter became available for
1359 * clients using the bus number provided in adap->nr. Also, the table
1360 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1361 * and the appropriate driver model device nodes are created. Otherwise, a
1362 * negative errno value is returned.
1363 */
1364int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1365{
Grant Likely488bf312011-07-25 17:49:43 +02001366 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1367 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001368
Doug Andersonee5c2742013-03-01 08:57:31 -08001369 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001370}
1371EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1372
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001373static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001374 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001375{
Jean Delvare4735c982008-07-14 22:38:36 +02001376 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001377
Jean Delvareacec2112009-03-28 21:34:40 +01001378 /* Remove the devices we created ourselves as the result of hardware
1379 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001380 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1381 if (client->adapter == adapter) {
1382 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1383 client->name, client->addr);
1384 list_del(&client->detected);
1385 i2c_unregister_device(client);
1386 }
1387 }
Jean Delvare026526f2008-01-27 18:14:49 +01001388}
1389
Jean Delvaree549c2b2009-06-19 16:58:19 +02001390static int __unregister_client(struct device *dev, void *dummy)
1391{
1392 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001393 if (client && strcmp(client->name, "dummy"))
1394 i2c_unregister_device(client);
1395 return 0;
1396}
1397
1398static int __unregister_dummy(struct device *dev, void *dummy)
1399{
1400 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001401 if (client)
1402 i2c_unregister_device(client);
1403 return 0;
1404}
1405
Jean Delvare69b00892009-12-06 17:06:27 +01001406static int __process_removed_adapter(struct device_driver *d, void *data)
1407{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001408 i2c_do_del_adapter(to_i2c_driver(d), data);
1409 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001410}
1411
David Brownelld64f73b2007-07-12 14:12:28 +02001412/**
1413 * i2c_del_adapter - unregister I2C adapter
1414 * @adap: the adapter being unregistered
1415 * Context: can sleep
1416 *
1417 * This unregisters an I2C adapter which was previously registered
1418 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1419 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001420void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001422 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001423 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001426 mutex_lock(&core_lock);
1427 found = idr_find(&i2c_adapter_idr, adap->nr);
1428 mutex_unlock(&core_lock);
1429 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001430 pr_debug("i2c-core: attempting to delete unregistered "
1431 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001432 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
Jean Delvare026526f2008-01-27 18:14:49 +01001435 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001436 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001437 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001438 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001439 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001441 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001442 mutex_lock_nested(&adap->userspace_clients_lock,
1443 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001444 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1445 detected) {
1446 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1447 client->addr);
1448 list_del(&client->detected);
1449 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001450 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001451 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001452
Jean Delvaree549c2b2009-06-19 16:58:19 +02001453 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001454 * check the returned value. This is a two-pass process, because
1455 * we can't remove the dummy devices during the first pass: they
1456 * could have been instantiated by real devices wishing to clean
1457 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001458 device_for_each_child(&adap->dev, NULL, __unregister_client);
1459 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Jean Delvare2bb50952009-09-18 22:45:46 +02001461#ifdef CONFIG_I2C_COMPAT
1462 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1463 adap->dev.parent);
1464#endif
1465
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001466 /* device name is gone after device_unregister */
1467 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* clean up the sysfs representation */
1470 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* wait for sysfs to drop all references */
1474 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
David Brownell6e13e642007-05-01 23:26:31 +02001476 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001477 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001479 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001481 /* Clear the device structure in case this adapter is ever going to be
1482 added again */
1483 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
David Brownellc0564602007-05-01 23:26:31 +02001485EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
David Brownell7b4fbc52007-05-01 23:26:30 +02001487/* ------------------------------------------------------------------------- */
1488
Jean Delvare7ae31482011-03-20 14:50:52 +01001489int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1490{
1491 int res;
1492
1493 mutex_lock(&core_lock);
1494 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1495 mutex_unlock(&core_lock);
1496
1497 return res;
1498}
1499EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1500
Jean Delvare69b00892009-12-06 17:06:27 +01001501static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001502{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001503 if (dev->type != &i2c_adapter_type)
1504 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001505 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001506}
1507
David Brownell7b4fbc52007-05-01 23:26:30 +02001508/*
1509 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001510 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001513int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001515 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
David Brownell1d0b19c2008-10-14 17:30:05 +02001517 /* Can't register until after driver model init */
1518 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1519 return -EAGAIN;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001522 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Jean Delvare729d6dd2009-06-19 16:58:18 +02001525 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001526 * will have called probe() for all matching-but-unbound devices.
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 res = driver_register(&driver->driver);
1529 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001530 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001531
Mark Brownf4e8db32011-01-14 22:03:50 +01001532 /* Drivers should switch to dev_pm_ops instead. */
1533 if (driver->suspend)
1534 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1535 driver->driver.name);
1536 if (driver->resume)
1537 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1538 driver->driver.name);
1539
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001540 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jean Delvare4735c982008-07-14 22:38:36 +02001542 INIT_LIST_HEAD(&driver->clients);
1543 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001544 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001545
Jean Delvare7eebcb72006-02-05 23:28:21 +01001546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001548EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Jean Delvare69b00892009-12-06 17:06:27 +01001550static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001551{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001552 if (dev->type == &i2c_adapter_type)
1553 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1554 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001555}
1556
David Brownella1d9e6e2007-05-01 23:26:30 +02001557/**
1558 * i2c_del_driver - unregister I2C driver
1559 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001560 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001561 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001562void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Jean Delvare7ae31482011-03-20 14:50:52 +01001564 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001567 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
David Brownellc0564602007-05-01 23:26:31 +02001569EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
David Brownell7b4fbc52007-05-01 23:26:30 +02001571/* ------------------------------------------------------------------------- */
1572
Jean Delvaree48d3312008-01-27 18:14:48 +01001573/**
1574 * i2c_use_client - increments the reference count of the i2c client structure
1575 * @client: the client being referenced
1576 *
1577 * Each live reference to a client should be refcounted. The driver model does
1578 * that automatically as part of driver binding, so that most drivers don't
1579 * need to do this explicitly: they hold a reference until they're unbound
1580 * from the device.
1581 *
1582 * A pointer to the client with the incremented reference counter is returned.
1583 */
1584struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
David Brownell6ea438e2008-07-14 22:38:24 +02001586 if (client && get_device(&client->dev))
1587 return client;
1588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
David Brownellc0564602007-05-01 23:26:31 +02001590EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Jean Delvaree48d3312008-01-27 18:14:48 +01001592/**
1593 * i2c_release_client - release a use of the i2c client structure
1594 * @client: the client being no longer referenced
1595 *
1596 * Must be called when a user of a client is finished with it.
1597 */
1598void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
David Brownell6ea438e2008-07-14 22:38:24 +02001600 if (client)
1601 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
David Brownellc0564602007-05-01 23:26:31 +02001603EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
David Brownell9b766b82008-01-27 18:14:51 +01001605struct i2c_cmd_arg {
1606 unsigned cmd;
1607 void *arg;
1608};
1609
1610static int i2c_cmd(struct device *dev, void *_arg)
1611{
1612 struct i2c_client *client = i2c_verify_client(dev);
1613 struct i2c_cmd_arg *arg = _arg;
1614
1615 if (client && client->driver && client->driver->command)
1616 client->driver->command(client, arg->cmd, arg->arg);
1617 return 0;
1618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1621{
David Brownell9b766b82008-01-27 18:14:51 +01001622 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David Brownell9b766b82008-01-27 18:14:51 +01001624 cmd_arg.cmd = cmd;
1625 cmd_arg.arg = arg;
1626 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
David Brownellc0564602007-05-01 23:26:31 +02001628EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630static int __init i2c_init(void)
1631{
1632 int retval;
1633
1634 retval = bus_register(&i2c_bus_type);
1635 if (retval)
1636 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001637#ifdef CONFIG_I2C_COMPAT
1638 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1639 if (!i2c_adapter_compat_class) {
1640 retval = -ENOMEM;
1641 goto bus_err;
1642 }
1643#endif
David Brownelle9f13732008-01-27 18:14:52 +01001644 retval = i2c_add_driver(&dummy_driver);
1645 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001646 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001647 return 0;
1648
Jean Delvare2bb50952009-09-18 22:45:46 +02001649class_err:
1650#ifdef CONFIG_I2C_COMPAT
1651 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001652bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001653#endif
David Brownelle9f13732008-01-27 18:14:52 +01001654 bus_unregister(&i2c_bus_type);
1655 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658static void __exit i2c_exit(void)
1659{
David Brownelle9f13732008-01-27 18:14:52 +01001660 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001661#ifdef CONFIG_I2C_COMPAT
1662 class_compat_unregister(i2c_adapter_compat_class);
1663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 bus_unregister(&i2c_bus_type);
1665}
1666
David Brownella10f9e72008-10-14 17:30:06 +02001667/* We must initialize early, because some subsystems register i2c drivers
1668 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1669 */
1670postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671module_exit(i2c_exit);
1672
1673/* ----------------------------------------------------
1674 * the functional interface to the i2c busses.
1675 * ----------------------------------------------------
1676 */
1677
David Brownella1cdeda2008-07-14 22:38:24 +02001678/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001679 * __i2c_transfer - unlocked flavor of i2c_transfer
1680 * @adap: Handle to I2C bus
1681 * @msgs: One or more messages to execute before STOP is issued to
1682 * terminate the operation; each message begins with a START.
1683 * @num: Number of messages to be executed.
1684 *
1685 * Returns negative errno, else the number of messages executed.
1686 *
1687 * Adapter lock must be held when calling this function. No debug logging
1688 * takes place. adap->algo->master_xfer existence isn't checked.
1689 */
1690int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1691{
1692 unsigned long orig_jiffies;
1693 int ret, try;
1694
1695 /* Retry automatically on arbitration loss */
1696 orig_jiffies = jiffies;
1697 for (ret = 0, try = 0; try <= adap->retries; try++) {
1698 ret = adap->algo->master_xfer(adap, msgs, num);
1699 if (ret != -EAGAIN)
1700 break;
1701 if (time_after(jiffies, orig_jiffies + adap->timeout))
1702 break;
1703 }
1704
1705 return ret;
1706}
1707EXPORT_SYMBOL(__i2c_transfer);
1708
1709/**
David Brownella1cdeda2008-07-14 22:38:24 +02001710 * i2c_transfer - execute a single or combined I2C message
1711 * @adap: Handle to I2C bus
1712 * @msgs: One or more messages to execute before STOP is issued to
1713 * terminate the operation; each message begins with a START.
1714 * @num: Number of messages to be executed.
1715 *
1716 * Returns negative errno, else the number of messages executed.
1717 *
1718 * Note that there is no requirement that each message be sent to
1719 * the same slave address, although that is the most common model.
1720 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001721int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001723 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
David Brownella1cdeda2008-07-14 22:38:24 +02001725 /* REVISIT the fault reporting model here is weak:
1726 *
1727 * - When we get an error after receiving N bytes from a slave,
1728 * there is no way to report "N".
1729 *
1730 * - When we get a NAK after transmitting N bytes to a slave,
1731 * there is no way to report "N" ... or to let the master
1732 * continue executing the rest of this combined message, if
1733 * that's the appropriate response.
1734 *
1735 * - When for example "num" is two and we successfully complete
1736 * the first message but get an error part way through the
1737 * second, it's unclear whether that should be reported as
1738 * one (discarding status on the second message) or errno
1739 * (discarding status on the first one).
1740 */
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (adap->algo->master_xfer) {
1743#ifdef DEBUG
1744 for (ret = 0; ret < num; ret++) {
1745 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001746 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1747 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1748 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750#endif
1751
Mike Rapoportcea443a2008-01-27 18:14:50 +01001752 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001753 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001754 if (!ret)
1755 /* I2C activity is ongoing. */
1756 return -EAGAIN;
1757 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001758 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001759 }
1760
Jean Delvareb37d2a32012-06-29 07:47:19 -03001761 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001762 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 return ret;
1765 } else {
1766 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001767 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769}
David Brownellc0564602007-05-01 23:26:31 +02001770EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
David Brownella1cdeda2008-07-14 22:38:24 +02001772/**
1773 * i2c_master_send - issue a single I2C message in master transmit mode
1774 * @client: Handle to slave device
1775 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001776 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001777 *
1778 * Returns negative errno, or else the number of bytes written.
1779 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001780int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001783 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct i2c_msg msg;
1785
Jean Delvare815f55f2005-05-07 22:58:46 +02001786 msg.addr = client->addr;
1787 msg.flags = client->flags & I2C_M_TEN;
1788 msg.len = count;
1789 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001790
Jean Delvare815f55f2005-05-07 22:58:46 +02001791 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001793 /*
1794 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1795 * transmitted, else error code.
1796 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001797 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
David Brownellc0564602007-05-01 23:26:31 +02001799EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
David Brownella1cdeda2008-07-14 22:38:24 +02001801/**
1802 * i2c_master_recv - issue a single I2C message in master receive mode
1803 * @client: Handle to slave device
1804 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001805 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001806 *
1807 * Returns negative errno, or else the number of bytes read.
1808 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001809int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Farid Hammane7225acf2010-05-21 18:40:58 +02001811 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct i2c_msg msg;
1813 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Jean Delvare815f55f2005-05-07 22:58:46 +02001815 msg.addr = client->addr;
1816 msg.flags = client->flags & I2C_M_TEN;
1817 msg.flags |= I2C_M_RD;
1818 msg.len = count;
1819 msg.buf = buf;
1820
1821 ret = i2c_transfer(adap, &msg, 1);
1822
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001823 /*
1824 * If everything went ok (i.e. 1 msg received), return #bytes received,
1825 * else error code.
1826 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001827 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
David Brownellc0564602007-05-01 23:26:31 +02001829EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831/* ----------------------------------------------------
1832 * the i2c address scanning function
1833 * Will not work for 10-bit addresses!
1834 * ----------------------------------------------------
1835 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001836
Jean Delvare63e4e802010-06-03 11:33:51 +02001837/*
1838 * Legacy default probe function, mostly relevant for SMBus. The default
1839 * probe method is a quick write, but it is known to corrupt the 24RF08
1840 * EEPROMs due to a state machine bug, and could also irreversibly
1841 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1842 * we use a short byte read instead. Also, some bus drivers don't implement
1843 * quick write, so we fallback to a byte read in that case too.
1844 * On x86, there is another special case for FSC hardware monitoring chips,
1845 * which want regular byte reads (address 0x73.) Fortunately, these are the
1846 * only known chips using this I2C address on PC hardware.
1847 * Returns 1 if probe succeeded, 0 if not.
1848 */
1849static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1850{
1851 int err;
1852 union i2c_smbus_data dummy;
1853
1854#ifdef CONFIG_X86
1855 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1856 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1857 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1858 I2C_SMBUS_BYTE_DATA, &dummy);
1859 else
1860#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001861 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1862 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001863 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1864 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001865 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1866 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1867 I2C_SMBUS_BYTE, &dummy);
1868 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001869 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1870 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001871 err = -EOPNOTSUPP;
1872 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001873
1874 return err >= 0;
1875}
1876
Jean Delvareccfbbd02009-12-06 17:06:25 +01001877static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001878 struct i2c_driver *driver)
1879{
1880 struct i2c_board_info info;
1881 struct i2c_adapter *adapter = temp_client->adapter;
1882 int addr = temp_client->addr;
1883 int err;
1884
1885 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001886 err = i2c_check_addr_validity(addr);
1887 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001888 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1889 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001890 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001891 }
1892
1893 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001894 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001895 return 0;
1896
Jean Delvareccfbbd02009-12-06 17:06:25 +01001897 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001898 if (!i2c_default_probe(adapter, addr))
1899 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001900
1901 /* Finally call the custom detection function */
1902 memset(&info, 0, sizeof(struct i2c_board_info));
1903 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001904 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001905 if (err) {
1906 /* -ENODEV is returned if the detection fails. We catch it
1907 here as this isn't an error. */
1908 return err == -ENODEV ? 0 : err;
1909 }
1910
1911 /* Consistency check */
1912 if (info.type[0] == '\0') {
1913 dev_err(&adapter->dev, "%s detection function provided "
1914 "no name for 0x%x\n", driver->driver.name,
1915 addr);
1916 } else {
1917 struct i2c_client *client;
1918
1919 /* Detection succeeded, instantiate the device */
1920 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1921 info.type, info.addr);
1922 client = i2c_new_device(adapter, &info);
1923 if (client)
1924 list_add_tail(&client->detected, &driver->clients);
1925 else
1926 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1927 info.type, info.addr);
1928 }
1929 return 0;
1930}
1931
1932static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1933{
Jean Delvarec3813d62009-12-14 21:17:25 +01001934 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001935 struct i2c_client *temp_client;
1936 int i, err = 0;
1937 int adap_id = i2c_adapter_id(adapter);
1938
Jean Delvarec3813d62009-12-14 21:17:25 +01001939 address_list = driver->address_list;
1940 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001941 return 0;
1942
Jean Delvare51b54ba2010-10-24 18:16:58 +02001943 /* Stop here if the classes do not match */
1944 if (!(adapter->class & driver->class))
1945 return 0;
1946
Jean Delvare4735c982008-07-14 22:38:36 +02001947 /* Set up a temporary client to help detect callback */
1948 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1949 if (!temp_client)
1950 return -ENOMEM;
1951 temp_client->adapter = adapter;
1952
Jean Delvarec3813d62009-12-14 21:17:25 +01001953 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001954 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001955 "addr 0x%02x\n", adap_id, address_list[i]);
1956 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001957 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001958 if (unlikely(err))
1959 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001960 }
1961
Jean Delvare4735c982008-07-14 22:38:36 +02001962 kfree(temp_client);
1963 return err;
1964}
1965
Jean Delvared44f19d2010-08-11 18:20:57 +02001966int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1967{
1968 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1969 I2C_SMBUS_QUICK, NULL) >= 0;
1970}
1971EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1972
Jean Delvare12b5053a2007-05-01 23:26:31 +02001973struct i2c_client *
1974i2c_new_probed_device(struct i2c_adapter *adap,
1975 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001976 unsigned short const *addr_list,
1977 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001978{
1979 int i;
1980
Jean Delvare8031d792010-08-11 18:21:00 +02001981 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001982 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001983
Jean Delvare12b5053a2007-05-01 23:26:31 +02001984 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1985 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001986 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001987 dev_warn(&adap->dev, "Invalid 7-bit address "
1988 "0x%02x\n", addr_list[i]);
1989 continue;
1990 }
1991
1992 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001993 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001994 dev_dbg(&adap->dev, "Address 0x%02x already in "
1995 "use, not probing\n", addr_list[i]);
1996 continue;
1997 }
1998
Jean Delvare63e4e802010-06-03 11:33:51 +02001999 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002000 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002001 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002002 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002003
2004 if (addr_list[i] == I2C_CLIENT_END) {
2005 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2006 return NULL;
2007 }
2008
2009 info->addr = addr_list[i];
2010 return i2c_new_device(adap, info);
2011}
2012EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2013
Jean Delvared735b342011-03-20 14:50:52 +01002014struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002017
Jean Delvarecaada322008-01-27 18:14:49 +01002018 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002019 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002020 if (adapter && !try_module_get(adapter->owner))
2021 adapter = NULL;
2022
Jean Delvarecaada322008-01-27 18:14:49 +01002023 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002024 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
David Brownellc0564602007-05-01 23:26:31 +02002026EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028void i2c_put_adapter(struct i2c_adapter *adap)
2029{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002030 if (adap)
2031 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
David Brownellc0564602007-05-01 23:26:31 +02002033EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035/* The SMBus parts */
2036
David Brownell438d6c22006-12-10 21:21:31 +01002037#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002038static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002041
Farid Hammane7225acf2010-05-21 18:40:58 +02002042 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002043 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 data = data ^ POLY;
2045 data = data << 1;
2046 }
2047 return (u8)(data >> 8);
2048}
2049
Jean Delvare421ef472005-10-26 21:28:55 +02002050/* Incremental CRC8 over count bytes in the array pointed to by p */
2051static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 int i;
2054
Farid Hammane7225acf2010-05-21 18:40:58 +02002055 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002056 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return crc;
2058}
2059
Jean Delvare421ef472005-10-26 21:28:55 +02002060/* Assume a 7-bit address, which is reasonable for SMBus */
2061static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Jean Delvare421ef472005-10-26 21:28:55 +02002063 /* The address will be sent first */
2064 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2065 pec = i2c_smbus_pec(pec, &addr, 1);
2066
2067 /* The data buffer follows */
2068 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Jean Delvare421ef472005-10-26 21:28:55 +02002071/* Used for write only transactions */
2072static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Jean Delvare421ef472005-10-26 21:28:55 +02002074 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2075 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Jean Delvare421ef472005-10-26 21:28:55 +02002078/* Return <0 on CRC error
2079 If there was a write before this read (most cases) we need to take the
2080 partial CRC from the write part into account.
2081 Note that this function does modify the message (we need to decrease the
2082 message length to hide the CRC byte from the caller). */
2083static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Jean Delvare421ef472005-10-26 21:28:55 +02002085 u8 rpec = msg->buf[--msg->len];
2086 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (rpec != cpec) {
2089 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2090 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002091 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
David Brownell438d6c22006-12-10 21:21:31 +01002093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
David Brownella1cdeda2008-07-14 22:38:24 +02002096/**
2097 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2098 * @client: Handle to slave device
2099 *
2100 * This executes the SMBus "receive byte" protocol, returning negative errno
2101 * else the byte received from the device.
2102 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002103s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002106 int status;
2107
2108 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2109 I2C_SMBUS_READ, 0,
2110 I2C_SMBUS_BYTE, &data);
2111 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
David Brownellc0564602007-05-01 23:26:31 +02002113EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
David Brownella1cdeda2008-07-14 22:38:24 +02002115/**
2116 * i2c_smbus_write_byte - SMBus "send byte" protocol
2117 * @client: Handle to slave device
2118 * @value: Byte to be sent
2119 *
2120 * This executes the SMBus "send byte" protocol, returning negative errno
2121 * else zero on success.
2122 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002123s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Farid Hammane7225acf2010-05-21 18:40:58 +02002125 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002126 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
David Brownellc0564602007-05-01 23:26:31 +02002128EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
David Brownella1cdeda2008-07-14 22:38:24 +02002130/**
2131 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2132 * @client: Handle to slave device
2133 * @command: Byte interpreted by slave
2134 *
2135 * This executes the SMBus "read byte" protocol, returning negative errno
2136 * else a data byte received from the device.
2137 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002138s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002141 int status;
2142
2143 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2144 I2C_SMBUS_READ, command,
2145 I2C_SMBUS_BYTE_DATA, &data);
2146 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
David Brownellc0564602007-05-01 23:26:31 +02002148EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Brownella1cdeda2008-07-14 22:38:24 +02002150/**
2151 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2152 * @client: Handle to slave device
2153 * @command: Byte interpreted by slave
2154 * @value: Byte being written
2155 *
2156 * This executes the SMBus "write byte" protocol, returning negative errno
2157 * else zero on success.
2158 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002159s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2160 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 union i2c_smbus_data data;
2163 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002164 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2165 I2C_SMBUS_WRITE, command,
2166 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
David Brownellc0564602007-05-01 23:26:31 +02002168EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David Brownella1cdeda2008-07-14 22:38:24 +02002170/**
2171 * i2c_smbus_read_word_data - SMBus "read word" protocol
2172 * @client: Handle to slave device
2173 * @command: Byte interpreted by slave
2174 *
2175 * This executes the SMBus "read word" protocol, returning negative errno
2176 * else a 16-bit unsigned "word" received from the device.
2177 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002178s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002181 int status;
2182
2183 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2184 I2C_SMBUS_READ, command,
2185 I2C_SMBUS_WORD_DATA, &data);
2186 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
David Brownellc0564602007-05-01 23:26:31 +02002188EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
David Brownella1cdeda2008-07-14 22:38:24 +02002190/**
2191 * i2c_smbus_write_word_data - SMBus "write word" protocol
2192 * @client: Handle to slave device
2193 * @command: Byte interpreted by slave
2194 * @value: 16-bit "word" being written
2195 *
2196 * This executes the SMBus "write word" protocol, returning negative errno
2197 * else zero on success.
2198 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002199s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2200 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 union i2c_smbus_data data;
2203 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002204 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2205 I2C_SMBUS_WRITE, command,
2206 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
David Brownellc0564602007-05-01 23:26:31 +02002208EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
David Brownella64ec072007-10-13 23:56:31 +02002210/**
David Brownella1cdeda2008-07-14 22:38:24 +02002211 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002212 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002213 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002214 * @values: Byte array into which data will be read; big enough to hold
2215 * the data returned by the slave. SMBus allows at most 32 bytes.
2216 *
David Brownella1cdeda2008-07-14 22:38:24 +02002217 * This executes the SMBus "block read" protocol, returning negative errno
2218 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002219 *
2220 * Note that using this function requires that the client's adapter support
2221 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2222 * support this; its emulation through I2C messaging relies on a specific
2223 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2224 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002225s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002226 u8 *values)
2227{
2228 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002229 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002230
David Brownell24a5bb72008-07-14 22:38:23 +02002231 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2232 I2C_SMBUS_READ, command,
2233 I2C_SMBUS_BLOCK_DATA, &data);
2234 if (status)
2235 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002236
2237 memcpy(values, &data.block[1], data.block[0]);
2238 return data.block[0];
2239}
2240EXPORT_SYMBOL(i2c_smbus_read_block_data);
2241
David Brownella1cdeda2008-07-14 22:38:24 +02002242/**
2243 * i2c_smbus_write_block_data - SMBus "block write" protocol
2244 * @client: Handle to slave device
2245 * @command: Byte interpreted by slave
2246 * @length: Size of data block; SMBus allows at most 32 bytes
2247 * @values: Byte array which will be written.
2248 *
2249 * This executes the SMBus "block write" protocol, returning negative errno
2250 * else zero on success.
2251 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002252s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002253 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (length > I2C_SMBUS_BLOCK_MAX)
2258 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002260 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002261 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2262 I2C_SMBUS_WRITE, command,
2263 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
David Brownellc0564602007-05-01 23:26:31 +02002265EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002268s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002269 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002272 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002273
Jean Delvare4b2643d2007-07-12 14:12:29 +02002274 if (length > I2C_SMBUS_BLOCK_MAX)
2275 length = I2C_SMBUS_BLOCK_MAX;
2276 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002277 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2278 I2C_SMBUS_READ, command,
2279 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2280 if (status < 0)
2281 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002282
2283 memcpy(values, &data.block[1], data.block[0]);
2284 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
David Brownellc0564602007-05-01 23:26:31 +02002286EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Jean Delvare0cc43a12011-01-10 22:11:23 +01002288s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002289 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002290{
2291 union i2c_smbus_data data;
2292
2293 if (length > I2C_SMBUS_BLOCK_MAX)
2294 length = I2C_SMBUS_BLOCK_MAX;
2295 data.block[0] = length;
2296 memcpy(data.block + 1, values, length);
2297 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2298 I2C_SMBUS_WRITE, command,
2299 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2300}
David Brownellc0564602007-05-01 23:26:31 +02002301EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002302
David Brownell438d6c22006-12-10 21:21:31 +01002303/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002305static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2306 unsigned short flags,
2307 char read_write, u8 command, int size,
2308 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 /* So we need to generate a series of msgs. In the case of writing, we
2311 need to use only one message; when reading, we need two. We initialize
2312 most things with sane defaults, to keep the code below somewhat
2313 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002314 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2315 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002316 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002318 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002319 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002320 struct i2c_msg msg[2] = {
2321 {
2322 .addr = addr,
2323 .flags = flags,
2324 .len = 1,
2325 .buf = msgbuf0,
2326 }, {
2327 .addr = addr,
2328 .flags = flags | I2C_M_RD,
2329 .len = 0,
2330 .buf = msgbuf1,
2331 },
2332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002335 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 case I2C_SMBUS_QUICK:
2337 msg[0].len = 0;
2338 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002339 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2340 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 num = 1;
2342 break;
2343 case I2C_SMBUS_BYTE:
2344 if (read_write == I2C_SMBUS_READ) {
2345 /* Special case: only a read! */
2346 msg[0].flags = I2C_M_RD | flags;
2347 num = 1;
2348 }
2349 break;
2350 case I2C_SMBUS_BYTE_DATA:
2351 if (read_write == I2C_SMBUS_READ)
2352 msg[1].len = 1;
2353 else {
2354 msg[0].len = 2;
2355 msgbuf0[1] = data->byte;
2356 }
2357 break;
2358 case I2C_SMBUS_WORD_DATA:
2359 if (read_write == I2C_SMBUS_READ)
2360 msg[1].len = 2;
2361 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002362 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002364 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 break;
2367 case I2C_SMBUS_PROC_CALL:
2368 num = 2; /* Special case */
2369 read_write = I2C_SMBUS_READ;
2370 msg[0].len = 3;
2371 msg[1].len = 2;
2372 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002373 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 break;
2375 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002377 msg[1].flags |= I2C_M_RECV_LEN;
2378 msg[1].len = 1; /* block length will be added by
2379 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 } else {
2381 msg[0].len = data->block[0] + 2;
2382 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002383 dev_err(&adapter->dev,
2384 "Invalid block write size %d\n",
2385 data->block[0]);
2386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002388 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 msgbuf0[i] = data->block[i-1];
2390 }
2391 break;
2392 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002393 num = 2; /* Another special case */
2394 read_write = I2C_SMBUS_READ;
2395 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002396 dev_err(&adapter->dev,
2397 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002398 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002399 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002400 }
2401 msg[0].len = data->block[0] + 2;
2402 for (i = 1; i < msg[0].len; i++)
2403 msgbuf0[i] = data->block[i-1];
2404 msg[1].flags |= I2C_M_RECV_LEN;
2405 msg[1].len = 1; /* block length will be added by
2406 the underlying bus driver */
2407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 case I2C_SMBUS_I2C_BLOCK_DATA:
2409 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002410 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 } else {
2412 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002413 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002414 dev_err(&adapter->dev,
2415 "Invalid block write size %d\n",
2416 data->block[0]);
2417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419 for (i = 1; i <= data->block[0]; i++)
2420 msgbuf0[i] = data->block[i];
2421 }
2422 break;
2423 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002424 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2425 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427
Jean Delvare421ef472005-10-26 21:28:55 +02002428 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2429 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2430 if (i) {
2431 /* Compute PEC if first message is a write */
2432 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002433 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002434 i2c_smbus_add_pec(&msg[0]);
2435 else /* Write followed by read */
2436 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2437 }
2438 /* Ask for PEC if last message is a read */
2439 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002440 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002441 }
2442
David Brownell24a5bb72008-07-14 22:38:23 +02002443 status = i2c_transfer(adapter, msg, num);
2444 if (status < 0)
2445 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Jean Delvare421ef472005-10-26 21:28:55 +02002447 /* Check PEC if last message is a read */
2448 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002449 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2450 if (status < 0)
2451 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002452 }
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002455 switch (size) {
2456 case I2C_SMBUS_BYTE:
2457 data->byte = msgbuf0[0];
2458 break;
2459 case I2C_SMBUS_BYTE_DATA:
2460 data->byte = msgbuf1[0];
2461 break;
2462 case I2C_SMBUS_WORD_DATA:
2463 case I2C_SMBUS_PROC_CALL:
2464 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2465 break;
2466 case I2C_SMBUS_I2C_BLOCK_DATA:
2467 for (i = 0; i < data->block[0]; i++)
2468 data->block[i+1] = msgbuf1[i];
2469 break;
2470 case I2C_SMBUS_BLOCK_DATA:
2471 case I2C_SMBUS_BLOCK_PROC_CALL:
2472 for (i = 0; i < msgbuf1[0] + 1; i++)
2473 data->block[i] = msgbuf1[i];
2474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 return 0;
2477}
2478
David Brownella1cdeda2008-07-14 22:38:24 +02002479/**
2480 * i2c_smbus_xfer - execute SMBus protocol operations
2481 * @adapter: Handle to I2C bus
2482 * @addr: Address of SMBus slave on that bus
2483 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2484 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2485 * @command: Byte interpreted by slave, for protocols which use such bytes
2486 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2487 * @data: Data to be read or written
2488 *
2489 * This executes an SMBus protocol operation, and returns a negative
2490 * errno code else zero on success.
2491 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002492s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002493 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002494 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002496 unsigned long orig_jiffies;
2497 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002500 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002503 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002504
2505 /* Retry automatically on arbitration loss */
2506 orig_jiffies = jiffies;
2507 for (res = 0, try = 0; try <= adapter->retries; try++) {
2508 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2509 read_write, command,
2510 protocol, data);
2511 if (res != -EAGAIN)
2512 break;
2513 if (time_after(jiffies,
2514 orig_jiffies + adapter->timeout))
2515 break;
2516 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002517 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002519 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2520 return res;
2521 /*
2522 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2523 * implement native support for the SMBus operation.
2524 */
2525 }
2526
2527 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2528 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2533MODULE_DESCRIPTION("I2C-Bus main module");
2534MODULE_LICENSE("GPL");