blob: dac8cd5031e7131a6e866df7be4019b7a9b15278 [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 Sang7f427042013-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 Sang7f427042013-07-11 12:56:15 +010042#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060043#include <linux/of_device.h>
Wolfram Sang7f427042013-07-11 12:56:15 +010044#include <linux/of_irq.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
Pantelis Antoniou4edb7632014-10-28 22:36:02 +020052#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Brownell9c1600e2007-05-01 23:26:31 +020054#include "i2c-core.h"
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jean Delvare6629dcf2010-05-04 11:09:28 +020057/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020058 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000059 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010060static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static DEFINE_IDR(i2c_adapter_idr);
62
Jean Delvare4f8cf822009-09-18 22:45:46 +020063static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020064static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010065
66/* ------------------------------------------------------------------------- */
67
Jean Delvared2653e92008-04-29 23:11:39 +020068static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
69 const struct i2c_client *client)
70{
71 while (id->name[0]) {
72 if (strcmp(client->name, id->name) == 0)
73 return id;
74 id++;
75 }
76 return NULL;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int i2c_device_match(struct device *dev, struct device_driver *drv)
80{
Jean Delvare51298d12009-09-18 22:45:45 +020081 struct i2c_client *client = i2c_verify_client(dev);
82 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020083
Jean Delvare51298d12009-09-18 22:45:45 +020084 if (!client)
85 return 0;
86
Grant Likely959e85f2010-06-08 07:48:19 -060087 /* Attempt an OF style match */
88 if (of_driver_match_device(dev, drv))
89 return 1;
90
Mika Westerberg907ddf82012-11-23 12:23:40 +010091 /* Then ACPI style match */
92 if (acpi_driver_match_device(dev, drv))
93 return 1;
94
Jean Delvare51298d12009-09-18 22:45:45 +020095 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020096 /* match on an id table if there is one */
97 if (driver->id_table)
98 return i2c_match_id(driver->id_table, client) != NULL;
99
Jean Delvareeb8a7902008-05-18 20:49:41 +0200100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
104/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200105static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200106{
107 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200108
Jean Delvareeb8a7902008-05-18 20:49:41 +0200109 if (add_uevent_var(env, "MODALIAS=%s%s",
110 I2C_MODULE_PREFIX, client->name))
111 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200112 dev_dbg(dev, "uevent\n");
113 return 0;
114}
115
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530116/* i2c bus recovery routines */
117static int get_scl_gpio_value(struct i2c_adapter *adap)
118{
119 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
120}
121
122static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
123{
124 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
125}
126
127static int get_sda_gpio_value(struct i2c_adapter *adap)
128{
129 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
130}
131
132static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
133{
134 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
135 struct device *dev = &adap->dev;
136 int ret = 0;
137
138 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
139 GPIOF_OUT_INIT_HIGH, "i2c-scl");
140 if (ret) {
141 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
142 return ret;
143 }
144
145 if (bri->get_sda) {
146 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
147 /* work without SDA polling */
148 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
149 bri->sda_gpio);
150 bri->get_sda = NULL;
151 }
152 }
153
154 return ret;
155}
156
157static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
158{
159 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
160
161 if (bri->get_sda)
162 gpio_free(bri->sda_gpio);
163
164 gpio_free(bri->scl_gpio);
165}
166
167/*
168 * We are generating clock pulses. ndelay() determines durating of clk pulses.
169 * We will generate clock with rate 100 KHz and so duration of both clock levels
170 * is: delay in ns = (10^6 / 100) / 2
171 */
172#define RECOVERY_NDELAY 5000
173#define RECOVERY_CLK_CNT 9
174
175static int i2c_generic_recovery(struct i2c_adapter *adap)
176{
177 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
178 int i = 0, val = 1, ret = 0;
179
180 if (bri->prepare_recovery)
181 bri->prepare_recovery(bri);
182
183 /*
184 * By this time SCL is high, as we need to give 9 falling-rising edges
185 */
186 while (i++ < RECOVERY_CLK_CNT * 2) {
187 if (val) {
188 /* Break if SDA is high */
189 if (bri->get_sda && bri->get_sda(adap))
190 break;
191 /* SCL shouldn't be low here */
192 if (!bri->get_scl(adap)) {
193 dev_err(&adap->dev,
194 "SCL is stuck low, exit recovery\n");
195 ret = -EBUSY;
196 break;
197 }
198 }
199
200 val = !val;
201 bri->set_scl(adap, val);
202 ndelay(RECOVERY_NDELAY);
203 }
204
205 if (bri->unprepare_recovery)
206 bri->unprepare_recovery(bri);
207
208 return ret;
209}
210
211int i2c_generic_scl_recovery(struct i2c_adapter *adap)
212{
213 adap->bus_recovery_info->set_scl(adap, 1);
214 return i2c_generic_recovery(adap);
215}
216
217int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
218{
219 int ret;
220
221 ret = i2c_get_gpios_for_recovery(adap);
222 if (ret)
223 return ret;
224
225 ret = i2c_generic_recovery(adap);
226 i2c_put_gpios_for_recovery(adap);
227
228 return ret;
229}
230
231int i2c_recover_bus(struct i2c_adapter *adap)
232{
233 if (!adap->bus_recovery_info)
234 return -EOPNOTSUPP;
235
236 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
237 return adap->bus_recovery_info->recover_bus(adap);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int i2c_device_probe(struct device *dev)
241{
Jean Delvare51298d12009-09-18 22:45:45 +0200242 struct i2c_client *client = i2c_verify_client(dev);
243 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100244 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200245
Jean Delvare51298d12009-09-18 22:45:45 +0200246 if (!client)
247 return 0;
248
249 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200250 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200251 return -ENODEV;
252 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200253 if (!device_can_wakeup(&client->dev))
254 device_init_wakeup(&client->dev,
255 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200256 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200257
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);
262 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100263 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static int i2c_device_remove(struct device *dev)
267{
Jean Delvare51298d12009-09-18 22:45:45 +0200268 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200269 struct i2c_driver *driver;
270 int status;
271
Jean Delvare51298d12009-09-18 22:45:45 +0200272 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200273 return 0;
274
275 driver = to_i2c_driver(dev->driver);
276 if (driver->remove) {
277 dev_dbg(dev, "remove\n");
278 status = driver->remove(client);
279 } else {
280 dev->driver = NULL;
281 status = 0;
282 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200283 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200284 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200285 i2c_set_clientdata(client, NULL);
286 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
David Brownellf37dd802007-02-13 22:09:00 +0100290static void i2c_device_shutdown(struct device *dev)
291{
Jean Delvare51298d12009-09-18 22:45:45 +0200292 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100293 struct i2c_driver *driver;
294
Jean Delvare51298d12009-09-18 22:45:45 +0200295 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100296 return;
297 driver = to_i2c_driver(dev->driver);
298 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200299 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100300}
301
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200302#ifdef CONFIG_PM_SLEEP
303static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100304{
Jean Delvare51298d12009-09-18 22:45:45 +0200305 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100306 struct i2c_driver *driver;
307
Jean Delvare51298d12009-09-18 22:45:45 +0200308 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100309 return 0;
310 driver = to_i2c_driver(dev->driver);
311 if (!driver->suspend)
312 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200313 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100314}
315
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200316static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100317{
Jean Delvare51298d12009-09-18 22:45:45 +0200318 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100319 struct i2c_driver *driver;
320
Jean Delvare51298d12009-09-18 22:45:45 +0200321 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100322 return 0;
323 driver = to_i2c_driver(dev->driver);
324 if (!driver->resume)
325 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200326 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100327}
328
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200329static int i2c_device_pm_suspend(struct device *dev)
330{
331 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
332
Mark Brownd529de22011-01-14 22:03:49 +0100333 if (pm)
334 return pm_generic_suspend(dev);
335 else
336 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200337}
338
339static int i2c_device_pm_resume(struct device *dev)
340{
341 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200342
343 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100344 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200345 else
Mark Brownd529de22011-01-14 22:03:49 +0100346 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200347}
348
349static int i2c_device_pm_freeze(struct device *dev)
350{
351 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
352
Mark Brownd529de22011-01-14 22:03:49 +0100353 if (pm)
354 return pm_generic_freeze(dev);
355 else
356 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200357}
358
359static int i2c_device_pm_thaw(struct device *dev)
360{
361 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
362
Mark Brownd529de22011-01-14 22:03:49 +0100363 if (pm)
364 return pm_generic_thaw(dev);
365 else
366 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200367}
368
369static int i2c_device_pm_poweroff(struct device *dev)
370{
371 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
372
Mark Brownd529de22011-01-14 22:03:49 +0100373 if (pm)
374 return pm_generic_poweroff(dev);
375 else
376 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200377}
378
379static int i2c_device_pm_restore(struct device *dev)
380{
381 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200382
383 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100384 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200385 else
Mark Brownd529de22011-01-14 22:03:49 +0100386 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200387}
388#else /* !CONFIG_PM_SLEEP */
389#define i2c_device_pm_suspend NULL
390#define i2c_device_pm_resume NULL
391#define i2c_device_pm_freeze NULL
392#define i2c_device_pm_thaw NULL
393#define i2c_device_pm_poweroff NULL
394#define i2c_device_pm_restore NULL
395#endif /* !CONFIG_PM_SLEEP */
396
David Brownell9c1600e2007-05-01 23:26:31 +0200397static void i2c_client_dev_release(struct device *dev)
398{
399 kfree(to_i2c_client(dev));
400}
401
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100402static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200403show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200404{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200405 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
406 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200407}
408
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100409static ssize_t
410show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200411{
412 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200413 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200414}
415
Jean Delvare4f8cf822009-09-18 22:45:46 +0200416static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200417static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
418
419static struct attribute *i2c_dev_attrs[] = {
420 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200421 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200422 &dev_attr_modalias.attr,
423 NULL
424};
425
426static struct attribute_group i2c_dev_attr_group = {
427 .attrs = i2c_dev_attrs,
428};
429
430static const struct attribute_group *i2c_dev_attr_groups[] = {
431 &i2c_dev_attr_group,
432 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200433};
434
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100435static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100436 .suspend = i2c_device_pm_suspend,
437 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200438 .freeze = i2c_device_pm_freeze,
439 .thaw = i2c_device_pm_thaw,
440 .poweroff = i2c_device_pm_poweroff,
441 .restore = i2c_device_pm_restore,
442 SET_RUNTIME_PM_OPS(
443 pm_generic_runtime_suspend,
444 pm_generic_runtime_resume,
445 pm_generic_runtime_idle
446 )
sonic zhang54067ee2009-12-14 21:17:30 +0100447};
448
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200449struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100450 .name = "i2c",
451 .match = i2c_device_match,
452 .probe = i2c_device_probe,
453 .remove = i2c_device_remove,
454 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100455 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000456};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200457EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000458
Jean Delvare51298d12009-09-18 22:45:45 +0200459static struct device_type i2c_client_type = {
460 .groups = i2c_dev_attr_groups,
461 .uevent = i2c_device_uevent,
462 .release = i2c_client_dev_release,
463};
464
David Brownell9b766b82008-01-27 18:14:51 +0100465
466/**
467 * i2c_verify_client - return parameter as i2c_client, or NULL
468 * @dev: device, probably from some driver model iterator
469 *
470 * When traversing the driver model tree, perhaps using driver model
471 * iterators like @device_for_each_child(), you can't assume very much
472 * about the nodes you find. Use this function to avoid oopses caused
473 * by wrongly treating some non-I2C device as an i2c_client.
474 */
475struct i2c_client *i2c_verify_client(struct device *dev)
476{
Jean Delvare51298d12009-09-18 22:45:45 +0200477 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100478 ? to_i2c_client(dev)
479 : NULL;
480}
481EXPORT_SYMBOL(i2c_verify_client);
482
483
Jean Delvare3a89db52010-06-03 11:33:52 +0200484/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300485 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200486static int i2c_check_client_addr_validity(const struct i2c_client *client)
487{
488 if (client->flags & I2C_CLIENT_TEN) {
489 /* 10-bit address, all values are valid */
490 if (client->addr > 0x3ff)
491 return -EINVAL;
492 } else {
493 /* 7-bit address, reject the general call address */
494 if (client->addr == 0x00 || client->addr > 0x7f)
495 return -EINVAL;
496 }
497 return 0;
498}
499
Jean Delvare656b8762010-06-03 11:33:53 +0200500/* And this is a strict address validity check, used when probing. If a
501 * device uses a reserved address, then it shouldn't be probed. 7-bit
502 * addressing is assumed, 10-bit address devices are rare and should be
503 * explicitly enumerated. */
504static int i2c_check_addr_validity(unsigned short addr)
505{
506 /*
507 * Reserved addresses per I2C specification:
508 * 0x00 General call address / START byte
509 * 0x01 CBUS address
510 * 0x02 Reserved for different bus format
511 * 0x03 Reserved for future purposes
512 * 0x04-0x07 Hs-mode master code
513 * 0x78-0x7b 10-bit slave addressing
514 * 0x7c-0x7f Reserved for future purposes
515 */
516 if (addr < 0x08 || addr > 0x77)
517 return -EINVAL;
518 return 0;
519}
520
Jean Delvare3b5f7942010-06-03 11:33:55 +0200521static int __i2c_check_addr_busy(struct device *dev, void *addrp)
522{
523 struct i2c_client *client = i2c_verify_client(dev);
524 int addr = *(int *)addrp;
525
526 if (client && client->addr == addr)
527 return -EBUSY;
528 return 0;
529}
530
Michael Lawnick08263742010-08-11 18:21:02 +0200531/* walk up mux tree */
532static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
533{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200534 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200535 int result;
536
537 result = device_for_each_child(&adapter->dev, &addr,
538 __i2c_check_addr_busy);
539
Jean Delvare97cc4d42010-10-24 18:16:57 +0200540 if (!result && parent)
541 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200542
543 return result;
544}
545
546/* recurse down mux tree */
547static int i2c_check_mux_children(struct device *dev, void *addrp)
548{
549 int result;
550
551 if (dev->type == &i2c_adapter_type)
552 result = device_for_each_child(dev, addrp,
553 i2c_check_mux_children);
554 else
555 result = __i2c_check_addr_busy(dev, addrp);
556
557 return result;
558}
559
Jean Delvare3b5f7942010-06-03 11:33:55 +0200560static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
561{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200562 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200563 int result = 0;
564
Jean Delvare97cc4d42010-10-24 18:16:57 +0200565 if (parent)
566 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200567
568 if (!result)
569 result = device_for_each_child(&adapter->dev, &addr,
570 i2c_check_mux_children);
571
572 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200573}
574
David Brownell9c1600e2007-05-01 23:26:31 +0200575/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200576 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
577 * @adapter: Target I2C bus segment
578 */
579void i2c_lock_adapter(struct i2c_adapter *adapter)
580{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200581 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
582
583 if (parent)
584 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200585 else
586 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200587}
588EXPORT_SYMBOL_GPL(i2c_lock_adapter);
589
590/**
591 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
592 * @adapter: Target I2C bus segment
593 */
594static int i2c_trylock_adapter(struct i2c_adapter *adapter)
595{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200596 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
597
598 if (parent)
599 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200600 else
601 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200602}
603
604/**
605 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
606 * @adapter: Target I2C bus segment
607 */
608void i2c_unlock_adapter(struct i2c_adapter *adapter)
609{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200610 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
611
612 if (parent)
613 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200614 else
615 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200616}
617EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
618
619/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200620 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200621 * @adap: the adapter managing the device
622 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200623 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200624 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200625 * Create an i2c device. Binding is handled through driver model
626 * probe()/remove() methods. A driver may be bound to this device when we
627 * return from this function, or any later moment (e.g. maybe hotplugging will
628 * load the driver module). This call is not appropriate for use by mainboard
629 * initialization logic, which usually runs during an arch_initcall() long
630 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200631 *
632 * This returns the new i2c client, which may be saved for later use with
633 * i2c_unregister_device(); or NULL to indicate an error.
634 */
635struct i2c_client *
636i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
637{
638 struct i2c_client *client;
639 int status;
640
641 client = kzalloc(sizeof *client, GFP_KERNEL);
642 if (!client)
643 return NULL;
644
645 client->adapter = adap;
646
647 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200648
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200649 if (info->archdata)
650 client->dev.archdata = *info->archdata;
651
Marc Pignatee354252008-08-28 08:33:22 +0200652 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200653 client->addr = info->addr;
654 client->irq = info->irq;
655
David Brownell9c1600e2007-05-01 23:26:31 +0200656 strlcpy(client->name, info->type, sizeof(client->name));
657
Jean Delvare3a89db52010-06-03 11:33:52 +0200658 /* Check for address validity */
659 status = i2c_check_client_addr_validity(client);
660 if (status) {
661 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
662 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
663 goto out_err_silent;
664 }
665
Jean Delvaref8a227e2009-06-19 16:58:18 +0200666 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200667 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200668 if (status)
669 goto out_err;
670
671 client->dev.parent = &client->adapter->dev;
672 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200673 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700674 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100675 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200676
Jean Delvarecbb44512011-11-23 11:33:07 +0100677 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200678 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100679 client->addr | ((client->flags & I2C_CLIENT_TEN)
680 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200681 status = device_register(&client->dev);
682 if (status)
683 goto out_err;
684
Jean Delvaref8a227e2009-06-19 16:58:18 +0200685 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
686 client->name, dev_name(&client->dev));
687
David Brownell9c1600e2007-05-01 23:26:31 +0200688 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200689
690out_err:
691 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
692 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200693out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200694 kfree(client);
695 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200696}
697EXPORT_SYMBOL_GPL(i2c_new_device);
698
699
700/**
701 * i2c_unregister_device - reverse effect of i2c_new_device()
702 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200703 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200704 */
705void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200706{
David Brownella1d9e6e2007-05-01 23:26:30 +0200707 device_unregister(&client->dev);
708}
David Brownell9c1600e2007-05-01 23:26:31 +0200709EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200710
711
Jean Delvare60b129d2008-05-11 20:37:06 +0200712static const struct i2c_device_id dummy_id[] = {
713 { "dummy", 0 },
714 { },
715};
716
Jean Delvared2653e92008-04-29 23:11:39 +0200717static int dummy_probe(struct i2c_client *client,
718 const struct i2c_device_id *id)
719{
720 return 0;
721}
722
723static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100724{
725 return 0;
726}
727
728static struct i2c_driver dummy_driver = {
729 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200730 .probe = dummy_probe,
731 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200732 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100733};
734
735/**
736 * i2c_new_dummy - return a new i2c device bound to a dummy driver
737 * @adapter: the adapter managing the device
738 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100739 * Context: can sleep
740 *
741 * This returns an I2C client bound to the "dummy" driver, intended for use
742 * with devices that consume multiple addresses. Examples of such chips
743 * include various EEPROMS (like 24c04 and 24c08 models).
744 *
745 * These dummy devices have two main uses. First, most I2C and SMBus calls
746 * except i2c_transfer() need a client handle; the dummy will be that handle.
747 * And second, this prevents the specified address from being bound to a
748 * different driver.
749 *
750 * This returns the new i2c client, which should be saved for later use with
751 * i2c_unregister_device(); or NULL to indicate an error.
752 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100753struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100754{
755 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200756 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100757 };
758
David Brownelle9f13732008-01-27 18:14:52 +0100759 return i2c_new_device(adapter, &info);
760}
761EXPORT_SYMBOL_GPL(i2c_new_dummy);
762
David Brownellf37dd802007-02-13 22:09:00 +0100763/* ------------------------------------------------------------------------- */
764
David Brownell16ffadf2007-05-01 23:26:28 +0200765/* I2C bus adapters -- one roots each I2C or SMBUS segment */
766
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200767static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
David Brownellef2c83212007-05-01 23:26:28 +0200769 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 complete(&adap->dev_released);
771}
772
Jean Delvare99cd8e22009-06-19 16:58:20 +0200773/*
Jean Delvare390946b2012-09-10 10:14:02 +0200774 * This function is only needed for mutex_lock_nested, so it is never
775 * called unless locking correctness checking is enabled. Thus we
776 * make it inline to avoid a compiler warning. That's what gcc ends up
777 * doing anyway.
778 */
779static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
780{
781 unsigned int depth = 0;
782
783 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
784 depth++;
785
786 return depth;
787}
788
789/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200790 * Let users instantiate I2C devices through sysfs. This can be used when
791 * platform initialization code doesn't contain the proper data for
792 * whatever reason. Also useful for drivers that do device detection and
793 * detection fails, either because the device uses an unexpected address,
794 * or this is a compatible device with different ID register values.
795 *
796 * Parameter checking may look overzealous, but we really don't want
797 * the user to provide incorrect parameters.
798 */
799static ssize_t
800i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
801 const char *buf, size_t count)
802{
803 struct i2c_adapter *adap = to_i2c_adapter(dev);
804 struct i2c_board_info info;
805 struct i2c_client *client;
806 char *blank, end;
807 int res;
808
Jean Delvare99cd8e22009-06-19 16:58:20 +0200809 memset(&info, 0, sizeof(struct i2c_board_info));
810
811 blank = strchr(buf, ' ');
812 if (!blank) {
813 dev_err(dev, "%s: Missing parameters\n", "new_device");
814 return -EINVAL;
815 }
816 if (blank - buf > I2C_NAME_SIZE - 1) {
817 dev_err(dev, "%s: Invalid device name\n", "new_device");
818 return -EINVAL;
819 }
820 memcpy(info.type, buf, blank - buf);
821
822 /* Parse remaining parameters, reject extra parameters */
823 res = sscanf(++blank, "%hi%c", &info.addr, &end);
824 if (res < 1) {
825 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
826 return -EINVAL;
827 }
828 if (res > 1 && end != '\n') {
829 dev_err(dev, "%s: Extra parameters\n", "new_device");
830 return -EINVAL;
831 }
832
Jean Delvare99cd8e22009-06-19 16:58:20 +0200833 client = i2c_new_device(adap, &info);
834 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200835 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200836
837 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200838 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200839 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200840 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200841 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
842 info.type, info.addr);
843
844 return count;
845}
846
847/*
848 * And of course let the users delete the devices they instantiated, if
849 * they got it wrong. This interface can only be used to delete devices
850 * instantiated by i2c_sysfs_new_device above. This guarantees that we
851 * don't delete devices to which some kernel code still has references.
852 *
853 * Parameter checking may look overzealous, but we really don't want
854 * the user to delete the wrong device.
855 */
856static ssize_t
857i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
858 const char *buf, size_t count)
859{
860 struct i2c_adapter *adap = to_i2c_adapter(dev);
861 struct i2c_client *client, *next;
862 unsigned short addr;
863 char end;
864 int res;
865
866 /* Parse parameters, reject extra parameters */
867 res = sscanf(buf, "%hi%c", &addr, &end);
868 if (res < 1) {
869 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
870 return -EINVAL;
871 }
872 if (res > 1 && end != '\n') {
873 dev_err(dev, "%s: Extra parameters\n", "delete_device");
874 return -EINVAL;
875 }
876
877 /* Make sure the device was added through sysfs */
878 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200879 mutex_lock_nested(&adap->userspace_clients_lock,
880 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200881 list_for_each_entry_safe(client, next, &adap->userspace_clients,
882 detected) {
883 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200884 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
885 "delete_device", client->name, client->addr);
886
887 list_del(&client->detected);
888 i2c_unregister_device(client);
889 res = count;
890 break;
891 }
892 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200893 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200894
895 if (res < 0)
896 dev_err(dev, "%s: Can't find device in list\n",
897 "delete_device");
898 return res;
899}
900
Jean Delvare4f8cf822009-09-18 22:45:46 +0200901static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200902static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
903 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200904
905static struct attribute *i2c_adapter_attrs[] = {
906 &dev_attr_name.attr,
907 &dev_attr_new_device.attr,
908 &dev_attr_delete_device.attr,
909 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200910};
911
Jean Delvare4f8cf822009-09-18 22:45:46 +0200912static struct attribute_group i2c_adapter_attr_group = {
913 .attrs = i2c_adapter_attrs,
914};
915
916static const struct attribute_group *i2c_adapter_attr_groups[] = {
917 &i2c_adapter_attr_group,
918 NULL
919};
920
Michael Lawnick08263742010-08-11 18:21:02 +0200921struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200922 .groups = i2c_adapter_attr_groups,
923 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200924};
Michael Lawnick08263742010-08-11 18:21:02 +0200925EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Stephen Warren643dd092012-04-17 12:43:33 -0600927/**
928 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
929 * @dev: device, probably from some driver model iterator
930 *
931 * When traversing the driver model tree, perhaps using driver model
932 * iterators like @device_for_each_child(), you can't assume very much
933 * about the nodes you find. Use this function to avoid oopses caused
934 * by wrongly treating some non-I2C device as an i2c_adapter.
935 */
936struct i2c_adapter *i2c_verify_adapter(struct device *dev)
937{
938 return (dev->type == &i2c_adapter_type)
939 ? to_i2c_adapter(dev)
940 : NULL;
941}
942EXPORT_SYMBOL(i2c_verify_adapter);
943
Jean Delvare2bb50952009-09-18 22:45:46 +0200944#ifdef CONFIG_I2C_COMPAT
945static struct class_compat *i2c_adapter_compat_class;
946#endif
947
David Brownell9c1600e2007-05-01 23:26:31 +0200948static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
949{
950 struct i2c_devinfo *devinfo;
951
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200952 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200953 list_for_each_entry(devinfo, &__i2c_board_list, list) {
954 if (devinfo->busnum == adapter->nr
955 && !i2c_new_device(adapter,
956 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100957 dev_err(&adapter->dev,
958 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200959 devinfo->board_info.addr);
960 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200961 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200962}
963
Wolfram Sang7f427042013-07-11 12:56:15 +0100964/* OF support code */
965
966#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniou4edb7632014-10-28 22:36:02 +0200967static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
968 struct device_node *node)
969{
970 struct i2c_client *result;
971 struct i2c_board_info info = {};
972 struct dev_archdata dev_ad = {};
973 const __be32 *addr;
974 int len;
975
976 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
977
978 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
979 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
980 node->full_name);
981 return ERR_PTR(-EINVAL);
982 }
983
984 addr = of_get_property(node, "reg", &len);
985 if (!addr || (len < sizeof(int))) {
986 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
987 node->full_name);
988 return ERR_PTR(-EINVAL);
989 }
990
991 info.addr = be32_to_cpup(addr);
992 if (info.addr > (1 << 10) - 1) {
993 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
994 info.addr, node->full_name);
995 return ERR_PTR(-EINVAL);
996 }
997
998 info.irq = irq_of_parse_and_map(node, 0);
999 info.of_node = of_node_get(node);
1000 info.archdata = &dev_ad;
1001
1002 if (of_get_property(node, "wakeup-source", NULL))
1003 info.flags |= I2C_CLIENT_WAKE;
1004
1005 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1006
1007 result = i2c_new_device(adap, &info);
1008 if (result == NULL) {
1009 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1010 node->full_name);
1011 of_node_put(node);
1012 irq_dispose_mapping(info.irq);
1013 return ERR_PTR(-EINVAL);
1014 }
1015 return result;
1016}
1017
Wolfram Sang7f427042013-07-11 12:56:15 +01001018static void internal_of_i2c_register_devices(struct i2c_adapter *adap)
1019{
Wolfram Sang7f427042013-07-11 12:56:15 +01001020 struct device_node *node;
1021
1022 /* Only register child devices if the adapter has a node pointer set */
1023 if (!adap->dev.of_node)
1024 return;
1025
1026 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1027
Pantelis Antoniou4edb7632014-10-28 22:36:02 +02001028 for_each_available_child_of_node(adap->dev.of_node, node)
1029 of_i2c_register_device(adap, node);
Wolfram Sang7f427042013-07-11 12:56:15 +01001030}
1031
1032static int of_dev_node_match(struct device *dev, void *data)
1033{
1034 return dev->of_node == data;
1035}
1036
1037/* must call put_device() when done with returned i2c_client device */
1038struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1039{
1040 struct device *dev;
1041
1042 dev = bus_find_device(&i2c_bus_type, NULL, node,
1043 of_dev_node_match);
1044 if (!dev)
1045 return NULL;
1046
1047 return i2c_verify_client(dev);
1048}
1049EXPORT_SYMBOL(of_find_i2c_device_by_node);
1050
1051/* must call put_device() when done with returned i2c_adapter device */
1052struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1053{
1054 struct device *dev;
1055
1056 dev = bus_find_device(&i2c_bus_type, NULL, node,
1057 of_dev_node_match);
1058 if (!dev)
1059 return NULL;
1060
1061 return i2c_verify_adapter(dev);
1062}
1063EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1064#else
1065static void internal_of_i2c_register_devices(struct i2c_adapter *adap) { }
1066#endif /* CONFIG_OF */
1067
Jean Delvare69b00892009-12-06 17:06:27 +01001068static int i2c_do_add_adapter(struct i2c_driver *driver,
1069 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001070{
Jean Delvare4735c982008-07-14 22:38:36 +02001071 /* Detect supported devices on that bus, and instantiate them */
1072 i2c_detect(adap, driver);
1073
1074 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001075 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001076 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1077 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001078 dev_warn(&adap->dev, "Please use another way to instantiate "
1079 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001080 /* We ignore the return code; if it fails, too bad */
1081 driver->attach_adapter(adap);
1082 }
1083 return 0;
1084}
1085
Jean Delvare69b00892009-12-06 17:06:27 +01001086static int __process_new_adapter(struct device_driver *d, void *data)
1087{
1088 return i2c_do_add_adapter(to_i2c_driver(d), data);
1089}
1090
David Brownell6e13e642007-05-01 23:26:31 +02001091static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Jean Delvared6703282010-08-11 18:20:59 +02001093 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
David Brownell1d0b19c2008-10-14 17:30:05 +02001095 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001096 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1097 res = -EAGAIN;
1098 goto out_list;
1099 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001100
Jean Delvare2236baa2010-11-15 22:40:38 +01001101 /* Sanity checks */
1102 if (unlikely(adap->name[0] == '\0')) {
1103 pr_err("i2c-core: Attempt to register an adapter with "
1104 "no name!\n");
1105 return -EINVAL;
1106 }
1107 if (unlikely(!adap->algo)) {
1108 pr_err("i2c-core: Attempt to register adapter '%s' with "
1109 "no algo!\n", adap->name);
1110 return -EINVAL;
1111 }
1112
Mika Kuoppala194684e2009-12-06 17:06:22 +01001113 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001114 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001115 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jean Delvare8fcfef62009-03-28 21:34:43 +01001117 /* Set default timeout to 1 second if not already set */
1118 if (adap->timeout == 0)
1119 adap->timeout = HZ;
1120
Kay Sievers27d9c182009-01-07 14:29:16 +01001121 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001122 adap->dev.bus = &i2c_bus_type;
1123 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001124 res = device_register(&adap->dev);
1125 if (res)
1126 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001128 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1129
Jean Delvare2bb50952009-09-18 22:45:46 +02001130#ifdef CONFIG_I2C_COMPAT
1131 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1132 adap->dev.parent);
1133 if (res)
1134 dev_warn(&adap->dev,
1135 "Failed to create compatibility class link\n");
1136#endif
1137
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301138 /* bus recovery specific initialization */
1139 if (adap->bus_recovery_info) {
1140 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1141
1142 if (!bri->recover_bus) {
1143 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1144 adap->bus_recovery_info = NULL;
1145 goto exit_recovery;
1146 }
1147
1148 /* Generic GPIO recovery */
1149 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1150 if (!gpio_is_valid(bri->scl_gpio)) {
1151 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1152 adap->bus_recovery_info = NULL;
1153 goto exit_recovery;
1154 }
1155
1156 if (gpio_is_valid(bri->sda_gpio))
1157 bri->get_sda = get_sda_gpio_value;
1158 else
1159 bri->get_sda = NULL;
1160
1161 bri->get_scl = get_scl_gpio_value;
1162 bri->set_scl = set_scl_gpio_value;
1163 } else if (!bri->set_scl || !bri->get_scl) {
1164 /* Generic SCL recovery */
1165 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1166 adap->bus_recovery_info = NULL;
1167 }
1168 }
1169
1170exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001171 /* create pre-declared device nodes */
Wolfram Sang7f427042013-07-11 12:56:15 +01001172 internal_of_i2c_register_devices(adap);
1173
David Brownell6e13e642007-05-01 23:26:31 +02001174 if (adap->nr < __i2c_first_dynamic_bus_num)
1175 i2c_scan_static_board_info(adap);
1176
Jean Delvare4735c982008-07-14 22:38:36 +02001177 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001178 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001179 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001180 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001181
1182 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001183
Jean Delvareb119c6c2006-08-15 18:26:30 +02001184out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001185 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001186 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001187 mutex_unlock(&core_lock);
1188 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
David Brownell6e13e642007-05-01 23:26:31 +02001191/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001192 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1193 * @adap: the adapter to register (with adap->nr initialized)
1194 * Context: can sleep
1195 *
1196 * See i2c_add_numbered_adapter() for details.
1197 */
1198static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1199{
1200 int id;
1201
1202 mutex_lock(&core_lock);
1203 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1204 GFP_KERNEL);
1205 mutex_unlock(&core_lock);
1206 if (id < 0)
1207 return id == -ENOSPC ? -EBUSY : id;
1208
1209 return i2c_register_adapter(adap);
1210}
1211
1212/**
David Brownell6e13e642007-05-01 23:26:31 +02001213 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1214 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001215 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001216 *
1217 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001218 * doesn't matter or when its bus number is specified by an dt alias.
1219 * Examples of bases when the bus number doesn't matter: I2C adapters
1220 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001221 *
1222 * When this returns zero, a new bus number was allocated and stored
1223 * in adap->nr, and the specified adapter became available for clients.
1224 * Otherwise, a negative errno value is returned.
1225 */
1226int i2c_add_adapter(struct i2c_adapter *adapter)
1227{
Doug Andersonee5c2742013-03-01 08:57:31 -08001228 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001229 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001230
Doug Andersonee5c2742013-03-01 08:57:31 -08001231 if (dev->of_node) {
1232 id = of_alias_get_id(dev->of_node, "i2c");
1233 if (id >= 0) {
1234 adapter->nr = id;
1235 return __i2c_add_numbered_adapter(adapter);
1236 }
1237 }
1238
Jean Delvarecaada322008-01-27 18:14:49 +01001239 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001240 id = idr_alloc(&i2c_adapter_idr, adapter,
1241 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001242 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001243 if (id < 0)
1244 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001245
1246 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001247
David Brownell6e13e642007-05-01 23:26:31 +02001248 return i2c_register_adapter(adapter);
1249}
1250EXPORT_SYMBOL(i2c_add_adapter);
1251
1252/**
1253 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1254 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001255 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001256 *
1257 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001258 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1259 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001260 * is used to properly configure I2C devices.
1261 *
Grant Likely488bf312011-07-25 17:49:43 +02001262 * If the requested bus number is set to -1, then this function will behave
1263 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1264 *
David Brownell6e13e642007-05-01 23:26:31 +02001265 * If no devices have pre-been declared for this bus, then be sure to
1266 * register the adapter before any dynamically allocated ones. Otherwise
1267 * the required bus ID may not be available.
1268 *
1269 * When this returns zero, the specified adapter became available for
1270 * clients using the bus number provided in adap->nr. Also, the table
1271 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1272 * and the appropriate driver model device nodes are created. Otherwise, a
1273 * negative errno value is returned.
1274 */
1275int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1276{
Grant Likely488bf312011-07-25 17:49:43 +02001277 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1278 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001279
Doug Andersonee5c2742013-03-01 08:57:31 -08001280 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001281}
1282EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1283
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001284static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001285 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001286{
Jean Delvare4735c982008-07-14 22:38:36 +02001287 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001288
Jean Delvareacec2112009-03-28 21:34:40 +01001289 /* Remove the devices we created ourselves as the result of hardware
1290 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001291 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1292 if (client->adapter == adapter) {
1293 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1294 client->name, client->addr);
1295 list_del(&client->detected);
1296 i2c_unregister_device(client);
1297 }
1298 }
Jean Delvare026526f2008-01-27 18:14:49 +01001299}
1300
Jean Delvaree549c2b2009-06-19 16:58:19 +02001301static int __unregister_client(struct device *dev, void *dummy)
1302{
1303 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001304 if (client && strcmp(client->name, "dummy"))
1305 i2c_unregister_device(client);
1306 return 0;
1307}
1308
1309static int __unregister_dummy(struct device *dev, void *dummy)
1310{
1311 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001312 if (client)
1313 i2c_unregister_device(client);
1314 return 0;
1315}
1316
Jean Delvare69b00892009-12-06 17:06:27 +01001317static int __process_removed_adapter(struct device_driver *d, void *data)
1318{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001319 i2c_do_del_adapter(to_i2c_driver(d), data);
1320 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001321}
1322
David Brownelld64f73b2007-07-12 14:12:28 +02001323/**
1324 * i2c_del_adapter - unregister I2C adapter
1325 * @adap: the adapter being unregistered
1326 * Context: can sleep
1327 *
1328 * This unregisters an I2C adapter which was previously registered
1329 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1330 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001331void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001333 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001334 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001337 mutex_lock(&core_lock);
1338 found = idr_find(&i2c_adapter_idr, adap->nr);
1339 mutex_unlock(&core_lock);
1340 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001341 pr_debug("i2c-core: attempting to delete unregistered "
1342 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001343 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
Jean Delvare026526f2008-01-27 18:14:49 +01001346 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001347 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001348 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001349 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001350 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001352 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001353 mutex_lock_nested(&adap->userspace_clients_lock,
1354 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001355 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1356 detected) {
1357 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1358 client->addr);
1359 list_del(&client->detected);
1360 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001361 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001362 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001363
Jean Delvaree549c2b2009-06-19 16:58:19 +02001364 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001365 * check the returned value. This is a two-pass process, because
1366 * we can't remove the dummy devices during the first pass: they
1367 * could have been instantiated by real devices wishing to clean
1368 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001369 device_for_each_child(&adap->dev, NULL, __unregister_client);
1370 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Jean Delvare2bb50952009-09-18 22:45:46 +02001372#ifdef CONFIG_I2C_COMPAT
1373 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1374 adap->dev.parent);
1375#endif
1376
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001377 /* device name is gone after device_unregister */
1378 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* clean up the sysfs representation */
1381 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* wait for sysfs to drop all references */
1385 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
David Brownell6e13e642007-05-01 23:26:31 +02001387 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001388 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001390 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001392 /* Clear the device structure in case this adapter is ever going to be
1393 added again */
1394 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
David Brownellc0564602007-05-01 23:26:31 +02001396EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
David Brownell7b4fbc52007-05-01 23:26:30 +02001398/* ------------------------------------------------------------------------- */
1399
Jean Delvare7ae31482011-03-20 14:50:52 +01001400int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1401{
1402 int res;
1403
1404 mutex_lock(&core_lock);
1405 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1406 mutex_unlock(&core_lock);
1407
1408 return res;
1409}
1410EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1411
Jean Delvare69b00892009-12-06 17:06:27 +01001412static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001413{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001414 if (dev->type != &i2c_adapter_type)
1415 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001416 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001417}
1418
David Brownell7b4fbc52007-05-01 23:26:30 +02001419/*
1420 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001421 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
1423
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001424int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001426 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
David Brownell1d0b19c2008-10-14 17:30:05 +02001428 /* Can't register until after driver model init */
1429 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1430 return -EAGAIN;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001433 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Jean Delvare729d6dd2009-06-19 16:58:18 +02001436 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001437 * will have called probe() for all matching-but-unbound devices.
1438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 res = driver_register(&driver->driver);
1440 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001441 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001442
Mark Brownf4e8db32011-01-14 22:03:50 +01001443 /* Drivers should switch to dev_pm_ops instead. */
1444 if (driver->suspend)
1445 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1446 driver->driver.name);
1447 if (driver->resume)
1448 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1449 driver->driver.name);
1450
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001451 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Jean Delvare4735c982008-07-14 22:38:36 +02001453 INIT_LIST_HEAD(&driver->clients);
1454 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001455 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001456
Jean Delvare7eebcb72006-02-05 23:28:21 +01001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001459EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Jean Delvare69b00892009-12-06 17:06:27 +01001461static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001462{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001463 if (dev->type == &i2c_adapter_type)
1464 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1465 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001466}
1467
David Brownella1d9e6e2007-05-01 23:26:30 +02001468/**
1469 * i2c_del_driver - unregister I2C driver
1470 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001471 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001472 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001473void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jean Delvare7ae31482011-03-20 14:50:52 +01001475 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001478 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
David Brownellc0564602007-05-01 23:26:31 +02001480EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
David Brownell7b4fbc52007-05-01 23:26:30 +02001482/* ------------------------------------------------------------------------- */
1483
Jean Delvaree48d3312008-01-27 18:14:48 +01001484/**
1485 * i2c_use_client - increments the reference count of the i2c client structure
1486 * @client: the client being referenced
1487 *
1488 * Each live reference to a client should be refcounted. The driver model does
1489 * that automatically as part of driver binding, so that most drivers don't
1490 * need to do this explicitly: they hold a reference until they're unbound
1491 * from the device.
1492 *
1493 * A pointer to the client with the incremented reference counter is returned.
1494 */
1495struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
David Brownell6ea438e2008-07-14 22:38:24 +02001497 if (client && get_device(&client->dev))
1498 return client;
1499 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
David Brownellc0564602007-05-01 23:26:31 +02001501EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jean Delvaree48d3312008-01-27 18:14:48 +01001503/**
1504 * i2c_release_client - release a use of the i2c client structure
1505 * @client: the client being no longer referenced
1506 *
1507 * Must be called when a user of a client is finished with it.
1508 */
1509void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
David Brownell6ea438e2008-07-14 22:38:24 +02001511 if (client)
1512 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
David Brownellc0564602007-05-01 23:26:31 +02001514EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
David Brownell9b766b82008-01-27 18:14:51 +01001516struct i2c_cmd_arg {
1517 unsigned cmd;
1518 void *arg;
1519};
1520
1521static int i2c_cmd(struct device *dev, void *_arg)
1522{
1523 struct i2c_client *client = i2c_verify_client(dev);
1524 struct i2c_cmd_arg *arg = _arg;
1525
1526 if (client && client->driver && client->driver->command)
1527 client->driver->command(client, arg->cmd, arg->arg);
1528 return 0;
1529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1532{
David Brownell9b766b82008-01-27 18:14:51 +01001533 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
David Brownell9b766b82008-01-27 18:14:51 +01001535 cmd_arg.cmd = cmd;
1536 cmd_arg.arg = arg;
1537 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
David Brownellc0564602007-05-01 23:26:31 +02001539EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001541#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1542static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1543 void *arg)
1544{
1545 struct of_reconfig_data *rd = arg;
1546 struct i2c_adapter *adap;
1547 struct i2c_client *client;
1548
1549 switch (of_reconfig_get_state_change(action, rd)) {
1550 case OF_RECONFIG_CHANGE_ADD:
1551 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1552 if (adap == NULL)
1553 return NOTIFY_OK; /* not for us */
1554
1555 client = of_i2c_register_device(adap, rd->dn);
1556 put_device(&adap->dev);
1557
1558 if (IS_ERR(client)) {
1559 pr_err("%s: failed to create for '%s'\n",
1560 __func__, rd->dn->full_name);
1561 return notifier_from_errno(PTR_ERR(client));
1562 }
1563 break;
1564 case OF_RECONFIG_CHANGE_REMOVE:
1565 /* find our device by node */
1566 client = of_find_i2c_device_by_node(rd->dn);
1567 if (client == NULL)
1568 return NOTIFY_OK; /* no? not meant for us */
1569
1570 /* unregister takes one ref away */
1571 i2c_unregister_device(client);
1572
1573 /* and put the reference of the find */
1574 put_device(&client->dev);
1575 break;
1576 }
1577
1578 return NOTIFY_OK;
1579}
1580static struct notifier_block i2c_of_notifier = {
1581 .notifier_call = of_i2c_notify,
1582};
1583#else
1584extern struct notifier_block i2c_of_notifier;
1585#endif /* CONFIG_OF_DYNAMIC */
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587static int __init i2c_init(void)
1588{
1589 int retval;
1590
1591 retval = bus_register(&i2c_bus_type);
1592 if (retval)
1593 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001594#ifdef CONFIG_I2C_COMPAT
1595 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1596 if (!i2c_adapter_compat_class) {
1597 retval = -ENOMEM;
1598 goto bus_err;
1599 }
1600#endif
David Brownelle9f13732008-01-27 18:14:52 +01001601 retval = i2c_add_driver(&dummy_driver);
1602 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001603 goto class_err;
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001604
1605 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1606 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1607
David Brownelle9f13732008-01-27 18:14:52 +01001608 return 0;
1609
Jean Delvare2bb50952009-09-18 22:45:46 +02001610class_err:
1611#ifdef CONFIG_I2C_COMPAT
1612 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001613bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001614#endif
David Brownelle9f13732008-01-27 18:14:52 +01001615 bus_unregister(&i2c_bus_type);
1616 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static void __exit i2c_exit(void)
1620{
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001621 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1622 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001623 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001624#ifdef CONFIG_I2C_COMPAT
1625 class_compat_unregister(i2c_adapter_compat_class);
1626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 bus_unregister(&i2c_bus_type);
1628}
1629
David Brownella10f9e72008-10-14 17:30:06 +02001630/* We must initialize early, because some subsystems register i2c drivers
1631 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1632 */
1633postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634module_exit(i2c_exit);
1635
1636/* ----------------------------------------------------
1637 * the functional interface to the i2c busses.
1638 * ----------------------------------------------------
1639 */
1640
David Brownella1cdeda2008-07-14 22:38:24 +02001641/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001642 * __i2c_transfer - unlocked flavor of i2c_transfer
1643 * @adap: Handle to I2C bus
1644 * @msgs: One or more messages to execute before STOP is issued to
1645 * terminate the operation; each message begins with a START.
1646 * @num: Number of messages to be executed.
1647 *
1648 * Returns negative errno, else the number of messages executed.
1649 *
1650 * Adapter lock must be held when calling this function. No debug logging
1651 * takes place. adap->algo->master_xfer existence isn't checked.
1652 */
1653int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1654{
1655 unsigned long orig_jiffies;
1656 int ret, try;
1657
1658 /* Retry automatically on arbitration loss */
1659 orig_jiffies = jiffies;
1660 for (ret = 0, try = 0; try <= adap->retries; try++) {
1661 ret = adap->algo->master_xfer(adap, msgs, num);
1662 if (ret != -EAGAIN)
1663 break;
1664 if (time_after(jiffies, orig_jiffies + adap->timeout))
1665 break;
1666 }
1667
1668 return ret;
1669}
1670EXPORT_SYMBOL(__i2c_transfer);
1671
1672/**
David Brownella1cdeda2008-07-14 22:38:24 +02001673 * i2c_transfer - execute a single or combined I2C message
1674 * @adap: Handle to I2C bus
1675 * @msgs: One or more messages to execute before STOP is issued to
1676 * terminate the operation; each message begins with a START.
1677 * @num: Number of messages to be executed.
1678 *
1679 * Returns negative errno, else the number of messages executed.
1680 *
1681 * Note that there is no requirement that each message be sent to
1682 * the same slave address, although that is the most common model.
1683 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001684int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
David Brownella1cdeda2008-07-14 22:38:24 +02001688 /* REVISIT the fault reporting model here is weak:
1689 *
1690 * - When we get an error after receiving N bytes from a slave,
1691 * there is no way to report "N".
1692 *
1693 * - When we get a NAK after transmitting N bytes to a slave,
1694 * there is no way to report "N" ... or to let the master
1695 * continue executing the rest of this combined message, if
1696 * that's the appropriate response.
1697 *
1698 * - When for example "num" is two and we successfully complete
1699 * the first message but get an error part way through the
1700 * second, it's unclear whether that should be reported as
1701 * one (discarding status on the second message) or errno
1702 * (discarding status on the first one).
1703 */
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (adap->algo->master_xfer) {
1706#ifdef DEBUG
1707 for (ret = 0; ret < num; ret++) {
1708 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001709 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1710 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1711 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713#endif
1714
Mike Rapoportcea443a82008-01-27 18:14:50 +01001715 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001716 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001717 if (!ret)
1718 /* I2C activity is ongoing. */
1719 return -EAGAIN;
1720 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001721 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001722 }
1723
Jean Delvareb37d2a32012-06-29 07:47:19 -03001724 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001725 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 return ret;
1728 } else {
1729 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001730 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732}
David Brownellc0564602007-05-01 23:26:31 +02001733EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David Brownella1cdeda2008-07-14 22:38:24 +02001735/**
1736 * i2c_master_send - issue a single I2C message in master transmit mode
1737 * @client: Handle to slave device
1738 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001739 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001740 *
1741 * Returns negative errno, or else the number of bytes written.
1742 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001743int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001746 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct i2c_msg msg;
1748
Jean Delvare815f55f2005-05-07 22:58:46 +02001749 msg.addr = client->addr;
1750 msg.flags = client->flags & I2C_M_TEN;
1751 msg.len = count;
1752 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001753
Jean Delvare815f55f2005-05-07 22:58:46 +02001754 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001756 /*
1757 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1758 * transmitted, else error code.
1759 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001760 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
David Brownellc0564602007-05-01 23:26:31 +02001762EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
David Brownella1cdeda2008-07-14 22:38:24 +02001764/**
1765 * i2c_master_recv - issue a single I2C message in master receive mode
1766 * @client: Handle to slave device
1767 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001768 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001769 *
1770 * Returns negative errno, or else the number of bytes read.
1771 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001772int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Farid Hammane7225acf2010-05-21 18:40:58 +02001774 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct i2c_msg msg;
1776 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Jean Delvare815f55f2005-05-07 22:58:46 +02001778 msg.addr = client->addr;
1779 msg.flags = client->flags & I2C_M_TEN;
1780 msg.flags |= I2C_M_RD;
1781 msg.len = count;
1782 msg.buf = buf;
1783
1784 ret = i2c_transfer(adap, &msg, 1);
1785
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001786 /*
1787 * If everything went ok (i.e. 1 msg received), return #bytes received,
1788 * else error code.
1789 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001790 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
David Brownellc0564602007-05-01 23:26:31 +02001792EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794/* ----------------------------------------------------
1795 * the i2c address scanning function
1796 * Will not work for 10-bit addresses!
1797 * ----------------------------------------------------
1798 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001799
Jean Delvare63e4e802010-06-03 11:33:51 +02001800/*
1801 * Legacy default probe function, mostly relevant for SMBus. The default
1802 * probe method is a quick write, but it is known to corrupt the 24RF08
1803 * EEPROMs due to a state machine bug, and could also irreversibly
1804 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1805 * we use a short byte read instead. Also, some bus drivers don't implement
1806 * quick write, so we fallback to a byte read in that case too.
1807 * On x86, there is another special case for FSC hardware monitoring chips,
1808 * which want regular byte reads (address 0x73.) Fortunately, these are the
1809 * only known chips using this I2C address on PC hardware.
1810 * Returns 1 if probe succeeded, 0 if not.
1811 */
1812static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1813{
1814 int err;
1815 union i2c_smbus_data dummy;
1816
1817#ifdef CONFIG_X86
1818 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1819 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1820 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1821 I2C_SMBUS_BYTE_DATA, &dummy);
1822 else
1823#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001824 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1825 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001826 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1827 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001828 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1829 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1830 I2C_SMBUS_BYTE, &dummy);
1831 else {
1832 dev_warn(&adap->dev, "No suitable probing method supported\n");
1833 err = -EOPNOTSUPP;
1834 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001835
1836 return err >= 0;
1837}
1838
Jean Delvareccfbbd02009-12-06 17:06:25 +01001839static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001840 struct i2c_driver *driver)
1841{
1842 struct i2c_board_info info;
1843 struct i2c_adapter *adapter = temp_client->adapter;
1844 int addr = temp_client->addr;
1845 int err;
1846
1847 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001848 err = i2c_check_addr_validity(addr);
1849 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001850 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1851 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001852 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001853 }
1854
1855 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001856 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001857 return 0;
1858
Jean Delvareccfbbd02009-12-06 17:06:25 +01001859 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001860 if (!i2c_default_probe(adapter, addr))
1861 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001862
1863 /* Finally call the custom detection function */
1864 memset(&info, 0, sizeof(struct i2c_board_info));
1865 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001866 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001867 if (err) {
1868 /* -ENODEV is returned if the detection fails. We catch it
1869 here as this isn't an error. */
1870 return err == -ENODEV ? 0 : err;
1871 }
1872
1873 /* Consistency check */
1874 if (info.type[0] == '\0') {
1875 dev_err(&adapter->dev, "%s detection function provided "
1876 "no name for 0x%x\n", driver->driver.name,
1877 addr);
1878 } else {
1879 struct i2c_client *client;
1880
1881 /* Detection succeeded, instantiate the device */
1882 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1883 info.type, info.addr);
1884 client = i2c_new_device(adapter, &info);
1885 if (client)
1886 list_add_tail(&client->detected, &driver->clients);
1887 else
1888 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1889 info.type, info.addr);
1890 }
1891 return 0;
1892}
1893
1894static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1895{
Jean Delvarec3813d62009-12-14 21:17:25 +01001896 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001897 struct i2c_client *temp_client;
1898 int i, err = 0;
1899 int adap_id = i2c_adapter_id(adapter);
1900
Jean Delvarec3813d62009-12-14 21:17:25 +01001901 address_list = driver->address_list;
1902 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001903 return 0;
1904
Jean Delvare51b54ba2010-10-24 18:16:58 +02001905 /* Stop here if the classes do not match */
1906 if (!(adapter->class & driver->class))
1907 return 0;
1908
Jean Delvare4735c982008-07-14 22:38:36 +02001909 /* Set up a temporary client to help detect callback */
1910 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1911 if (!temp_client)
1912 return -ENOMEM;
1913 temp_client->adapter = adapter;
1914
Jean Delvarec3813d62009-12-14 21:17:25 +01001915 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001916 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001917 "addr 0x%02x\n", adap_id, address_list[i]);
1918 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001919 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001920 if (unlikely(err))
1921 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001922 }
1923
Jean Delvare4735c982008-07-14 22:38:36 +02001924 kfree(temp_client);
1925 return err;
1926}
1927
Jean Delvared44f19d2010-08-11 18:20:57 +02001928int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1929{
1930 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1931 I2C_SMBUS_QUICK, NULL) >= 0;
1932}
1933EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1934
Jean Delvare12b5053a2007-05-01 23:26:31 +02001935struct i2c_client *
1936i2c_new_probed_device(struct i2c_adapter *adap,
1937 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001938 unsigned short const *addr_list,
1939 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001940{
1941 int i;
1942
Jean Delvare8031d792010-08-11 18:21:00 +02001943 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001944 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001945
Jean Delvare12b5053a2007-05-01 23:26:31 +02001946 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1947 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001948 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001949 dev_warn(&adap->dev, "Invalid 7-bit address "
1950 "0x%02x\n", addr_list[i]);
1951 continue;
1952 }
1953
1954 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001955 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001956 dev_dbg(&adap->dev, "Address 0x%02x already in "
1957 "use, not probing\n", addr_list[i]);
1958 continue;
1959 }
1960
Jean Delvare63e4e802010-06-03 11:33:51 +02001961 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001962 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001963 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001964 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001965
1966 if (addr_list[i] == I2C_CLIENT_END) {
1967 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1968 return NULL;
1969 }
1970
1971 info->addr = addr_list[i];
1972 return i2c_new_device(adap, info);
1973}
1974EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1975
Jean Delvared735b342011-03-20 14:50:52 +01001976struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001979
Jean Delvarecaada322008-01-27 18:14:49 +01001980 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001981 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001982 if (adapter && !try_module_get(adapter->owner))
1983 adapter = NULL;
1984
Jean Delvarecaada322008-01-27 18:14:49 +01001985 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001986 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
David Brownellc0564602007-05-01 23:26:31 +02001988EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990void i2c_put_adapter(struct i2c_adapter *adap)
1991{
1992 module_put(adap->owner);
1993}
David Brownellc0564602007-05-01 23:26:31 +02001994EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996/* The SMBus parts */
1997
David Brownell438d6c22006-12-10 21:21:31 +01001998#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001999static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002002
Farid Hammane7225acf2010-05-21 18:40:58 +02002003 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002004 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 data = data ^ POLY;
2006 data = data << 1;
2007 }
2008 return (u8)(data >> 8);
2009}
2010
Jean Delvare421ef472005-10-26 21:28:55 +02002011/* Incremental CRC8 over count bytes in the array pointed to by p */
2012static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 int i;
2015
Farid Hammane7225acf2010-05-21 18:40:58 +02002016 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002017 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return crc;
2019}
2020
Jean Delvare421ef472005-10-26 21:28:55 +02002021/* Assume a 7-bit address, which is reasonable for SMBus */
2022static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Jean Delvare421ef472005-10-26 21:28:55 +02002024 /* The address will be sent first */
2025 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2026 pec = i2c_smbus_pec(pec, &addr, 1);
2027
2028 /* The data buffer follows */
2029 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Jean Delvare421ef472005-10-26 21:28:55 +02002032/* Used for write only transactions */
2033static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Jean Delvare421ef472005-10-26 21:28:55 +02002035 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2036 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Jean Delvare421ef472005-10-26 21:28:55 +02002039/* Return <0 on CRC error
2040 If there was a write before this read (most cases) we need to take the
2041 partial CRC from the write part into account.
2042 Note that this function does modify the message (we need to decrease the
2043 message length to hide the CRC byte from the caller). */
2044static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Jean Delvare421ef472005-10-26 21:28:55 +02002046 u8 rpec = msg->buf[--msg->len];
2047 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (rpec != cpec) {
2050 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2051 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002052 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
David Brownell438d6c22006-12-10 21:21:31 +01002054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
David Brownella1cdeda2008-07-14 22:38:24 +02002057/**
2058 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2059 * @client: Handle to slave device
2060 *
2061 * This executes the SMBus "receive byte" protocol, returning negative errno
2062 * else the byte received from the device.
2063 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002064s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002067 int status;
2068
2069 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2070 I2C_SMBUS_READ, 0,
2071 I2C_SMBUS_BYTE, &data);
2072 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
David Brownellc0564602007-05-01 23:26:31 +02002074EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
David Brownella1cdeda2008-07-14 22:38:24 +02002076/**
2077 * i2c_smbus_write_byte - SMBus "send byte" protocol
2078 * @client: Handle to slave device
2079 * @value: Byte to be sent
2080 *
2081 * This executes the SMBus "send byte" protocol, returning negative errno
2082 * else zero on success.
2083 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002084s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Farid Hammane7225acf2010-05-21 18:40:58 +02002086 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002087 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
David Brownellc0564602007-05-01 23:26:31 +02002089EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
David Brownella1cdeda2008-07-14 22:38:24 +02002091/**
2092 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2093 * @client: Handle to slave device
2094 * @command: Byte interpreted by slave
2095 *
2096 * This executes the SMBus "read byte" protocol, returning negative errno
2097 * else a data byte received from the device.
2098 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002099s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
2101 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002102 int status;
2103
2104 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2105 I2C_SMBUS_READ, command,
2106 I2C_SMBUS_BYTE_DATA, &data);
2107 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
David Brownellc0564602007-05-01 23:26:31 +02002109EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
David Brownella1cdeda2008-07-14 22:38:24 +02002111/**
2112 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2113 * @client: Handle to slave device
2114 * @command: Byte interpreted by slave
2115 * @value: Byte being written
2116 *
2117 * This executes the SMBus "write byte" protocol, returning negative errno
2118 * else zero on success.
2119 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002120s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2121 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 union i2c_smbus_data data;
2124 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002125 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2126 I2C_SMBUS_WRITE, command,
2127 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
David Brownellc0564602007-05-01 23:26:31 +02002129EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
David Brownella1cdeda2008-07-14 22:38:24 +02002131/**
2132 * i2c_smbus_read_word_data - SMBus "read word" protocol
2133 * @client: Handle to slave device
2134 * @command: Byte interpreted by slave
2135 *
2136 * This executes the SMBus "read word" protocol, returning negative errno
2137 * else a 16-bit unsigned "word" received from the device.
2138 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002139s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002142 int status;
2143
2144 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2145 I2C_SMBUS_READ, command,
2146 I2C_SMBUS_WORD_DATA, &data);
2147 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
David Brownellc0564602007-05-01 23:26:31 +02002149EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
David Brownella1cdeda2008-07-14 22:38:24 +02002151/**
2152 * i2c_smbus_write_word_data - SMBus "write word" protocol
2153 * @client: Handle to slave device
2154 * @command: Byte interpreted by slave
2155 * @value: 16-bit "word" being written
2156 *
2157 * This executes the SMBus "write word" protocol, returning negative errno
2158 * else zero on success.
2159 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002160s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2161 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162{
2163 union i2c_smbus_data data;
2164 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002165 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2166 I2C_SMBUS_WRITE, command,
2167 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
David Brownellc0564602007-05-01 23:26:31 +02002169EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
David Brownella64ec072007-10-13 23:56:31 +02002171/**
David Brownella1cdeda2008-07-14 22:38:24 +02002172 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002173 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002174 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002175 * @values: Byte array into which data will be read; big enough to hold
2176 * the data returned by the slave. SMBus allows at most 32 bytes.
2177 *
David Brownella1cdeda2008-07-14 22:38:24 +02002178 * This executes the SMBus "block read" protocol, returning negative errno
2179 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002180 *
2181 * Note that using this function requires that the client's adapter support
2182 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2183 * support this; its emulation through I2C messaging relies on a specific
2184 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2185 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002186s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002187 u8 *values)
2188{
2189 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002190 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002191
David Brownell24a5bb72008-07-14 22:38:23 +02002192 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2193 I2C_SMBUS_READ, command,
2194 I2C_SMBUS_BLOCK_DATA, &data);
2195 if (status)
2196 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002197
2198 memcpy(values, &data.block[1], data.block[0]);
2199 return data.block[0];
2200}
2201EXPORT_SYMBOL(i2c_smbus_read_block_data);
2202
David Brownella1cdeda2008-07-14 22:38:24 +02002203/**
2204 * i2c_smbus_write_block_data - SMBus "block write" protocol
2205 * @client: Handle to slave device
2206 * @command: Byte interpreted by slave
2207 * @length: Size of data block; SMBus allows at most 32 bytes
2208 * @values: Byte array which will be written.
2209 *
2210 * This executes the SMBus "block write" protocol, returning negative errno
2211 * else zero on success.
2212 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002213s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002214 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (length > I2C_SMBUS_BLOCK_MAX)
2219 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002221 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002222 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2223 I2C_SMBUS_WRITE, command,
2224 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
David Brownellc0564602007-05-01 23:26:31 +02002226EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002229s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002230 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002233 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002234
Jean Delvare4b2643d2007-07-12 14:12:29 +02002235 if (length > I2C_SMBUS_BLOCK_MAX)
2236 length = I2C_SMBUS_BLOCK_MAX;
2237 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002238 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2239 I2C_SMBUS_READ, command,
2240 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2241 if (status < 0)
2242 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002243
2244 memcpy(values, &data.block[1], data.block[0]);
2245 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
David Brownellc0564602007-05-01 23:26:31 +02002247EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Jean Delvare0cc43a12011-01-10 22:11:23 +01002249s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002250 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002251{
2252 union i2c_smbus_data data;
2253
2254 if (length > I2C_SMBUS_BLOCK_MAX)
2255 length = I2C_SMBUS_BLOCK_MAX;
2256 data.block[0] = length;
2257 memcpy(data.block + 1, values, length);
2258 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2259 I2C_SMBUS_WRITE, command,
2260 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2261}
David Brownellc0564602007-05-01 23:26:31 +02002262EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002263
David Brownell438d6c22006-12-10 21:21:31 +01002264/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002266static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2267 unsigned short flags,
2268 char read_write, u8 command, int size,
2269 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 /* So we need to generate a series of msgs. In the case of writing, we
2272 need to use only one message; when reading, we need two. We initialize
2273 most things with sane defaults, to keep the code below somewhat
2274 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002275 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2276 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002277 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002279 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002280 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002281 struct i2c_msg msg[2] = {
2282 {
2283 .addr = addr,
2284 .flags = flags,
2285 .len = 1,
2286 .buf = msgbuf0,
2287 }, {
2288 .addr = addr,
2289 .flags = flags | I2C_M_RD,
2290 .len = 0,
2291 .buf = msgbuf1,
2292 },
2293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002296 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 case I2C_SMBUS_QUICK:
2298 msg[0].len = 0;
2299 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002300 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2301 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 num = 1;
2303 break;
2304 case I2C_SMBUS_BYTE:
2305 if (read_write == I2C_SMBUS_READ) {
2306 /* Special case: only a read! */
2307 msg[0].flags = I2C_M_RD | flags;
2308 num = 1;
2309 }
2310 break;
2311 case I2C_SMBUS_BYTE_DATA:
2312 if (read_write == I2C_SMBUS_READ)
2313 msg[1].len = 1;
2314 else {
2315 msg[0].len = 2;
2316 msgbuf0[1] = data->byte;
2317 }
2318 break;
2319 case I2C_SMBUS_WORD_DATA:
2320 if (read_write == I2C_SMBUS_READ)
2321 msg[1].len = 2;
2322 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002323 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002325 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327 break;
2328 case I2C_SMBUS_PROC_CALL:
2329 num = 2; /* Special case */
2330 read_write = I2C_SMBUS_READ;
2331 msg[0].len = 3;
2332 msg[1].len = 2;
2333 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002334 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 break;
2336 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002338 msg[1].flags |= I2C_M_RECV_LEN;
2339 msg[1].len = 1; /* block length will be added by
2340 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 } else {
2342 msg[0].len = data->block[0] + 2;
2343 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002344 dev_err(&adapter->dev,
2345 "Invalid block write size %d\n",
2346 data->block[0]);
2347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002349 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 msgbuf0[i] = data->block[i-1];
2351 }
2352 break;
2353 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002354 num = 2; /* Another special case */
2355 read_write = I2C_SMBUS_READ;
2356 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002357 dev_err(&adapter->dev,
2358 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002359 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002360 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002361 }
2362 msg[0].len = data->block[0] + 2;
2363 for (i = 1; i < msg[0].len; i++)
2364 msgbuf0[i] = data->block[i-1];
2365 msg[1].flags |= I2C_M_RECV_LEN;
2366 msg[1].len = 1; /* block length will be added by
2367 the underlying bus driver */
2368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 case I2C_SMBUS_I2C_BLOCK_DATA:
2370 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002371 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 } else {
2373 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002374 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002375 dev_err(&adapter->dev,
2376 "Invalid block write size %d\n",
2377 data->block[0]);
2378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 for (i = 1; i <= data->block[0]; i++)
2381 msgbuf0[i] = data->block[i];
2382 }
2383 break;
2384 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002385 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2386 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388
Jean Delvare421ef472005-10-26 21:28:55 +02002389 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2390 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2391 if (i) {
2392 /* Compute PEC if first message is a write */
2393 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002394 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002395 i2c_smbus_add_pec(&msg[0]);
2396 else /* Write followed by read */
2397 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2398 }
2399 /* Ask for PEC if last message is a read */
2400 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002401 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002402 }
2403
David Brownell24a5bb72008-07-14 22:38:23 +02002404 status = i2c_transfer(adapter, msg, num);
2405 if (status < 0)
2406 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Jean Delvare421ef472005-10-26 21:28:55 +02002408 /* Check PEC if last message is a read */
2409 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002410 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2411 if (status < 0)
2412 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002413 }
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002416 switch (size) {
2417 case I2C_SMBUS_BYTE:
2418 data->byte = msgbuf0[0];
2419 break;
2420 case I2C_SMBUS_BYTE_DATA:
2421 data->byte = msgbuf1[0];
2422 break;
2423 case I2C_SMBUS_WORD_DATA:
2424 case I2C_SMBUS_PROC_CALL:
2425 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2426 break;
2427 case I2C_SMBUS_I2C_BLOCK_DATA:
2428 for (i = 0; i < data->block[0]; i++)
2429 data->block[i+1] = msgbuf1[i];
2430 break;
2431 case I2C_SMBUS_BLOCK_DATA:
2432 case I2C_SMBUS_BLOCK_PROC_CALL:
2433 for (i = 0; i < msgbuf1[0] + 1; i++)
2434 data->block[i] = msgbuf1[i];
2435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437 return 0;
2438}
2439
David Brownella1cdeda2008-07-14 22:38:24 +02002440/**
2441 * i2c_smbus_xfer - execute SMBus protocol operations
2442 * @adapter: Handle to I2C bus
2443 * @addr: Address of SMBus slave on that bus
2444 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2445 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2446 * @command: Byte interpreted by slave, for protocols which use such bytes
2447 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2448 * @data: Data to be read or written
2449 *
2450 * This executes an SMBus protocol operation, and returns a negative
2451 * errno code else zero on success.
2452 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002453s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002454 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002455 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002457 unsigned long orig_jiffies;
2458 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002461 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002464 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002465
2466 /* Retry automatically on arbitration loss */
2467 orig_jiffies = jiffies;
2468 for (res = 0, try = 0; try <= adapter->retries; try++) {
2469 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2470 read_write, command,
2471 protocol, data);
2472 if (res != -EAGAIN)
2473 break;
2474 if (time_after(jiffies,
2475 orig_jiffies + adapter->timeout))
2476 break;
2477 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002478 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002480 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2481 return res;
2482 /*
2483 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2484 * implement native support for the SMBus operation.
2485 */
2486 }
2487
2488 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2489 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2494MODULE_DESCRIPTION("I2C-Bus main module");
2495MODULE_LICENSE("GPL");