blob: 92588d3bae45a24131c43ee4399bf135e22b70a9 [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{
Pantelis Antoniou5722d512015-01-15 18:33:55 +0200707 if (client->dev.of_node)
708 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +0200709 device_unregister(&client->dev);
710}
David Brownell9c1600e2007-05-01 23:26:31 +0200711EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200712
713
Jean Delvare60b129d2008-05-11 20:37:06 +0200714static const struct i2c_device_id dummy_id[] = {
715 { "dummy", 0 },
716 { },
717};
718
Jean Delvared2653e92008-04-29 23:11:39 +0200719static int dummy_probe(struct i2c_client *client,
720 const struct i2c_device_id *id)
721{
722 return 0;
723}
724
725static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100726{
727 return 0;
728}
729
730static struct i2c_driver dummy_driver = {
731 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200732 .probe = dummy_probe,
733 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200734 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100735};
736
737/**
738 * i2c_new_dummy - return a new i2c device bound to a dummy driver
739 * @adapter: the adapter managing the device
740 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100741 * Context: can sleep
742 *
743 * This returns an I2C client bound to the "dummy" driver, intended for use
744 * with devices that consume multiple addresses. Examples of such chips
745 * include various EEPROMS (like 24c04 and 24c08 models).
746 *
747 * These dummy devices have two main uses. First, most I2C and SMBus calls
748 * except i2c_transfer() need a client handle; the dummy will be that handle.
749 * And second, this prevents the specified address from being bound to a
750 * different driver.
751 *
752 * This returns the new i2c client, which should be saved for later use with
753 * i2c_unregister_device(); or NULL to indicate an error.
754 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100755struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100756{
757 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200758 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100759 };
760
David Brownelle9f13732008-01-27 18:14:52 +0100761 return i2c_new_device(adapter, &info);
762}
763EXPORT_SYMBOL_GPL(i2c_new_dummy);
764
David Brownellf37dd802007-02-13 22:09:00 +0100765/* ------------------------------------------------------------------------- */
766
David Brownell16ffadf2007-05-01 23:26:28 +0200767/* I2C bus adapters -- one roots each I2C or SMBUS segment */
768
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200769static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
David Brownellef2c83212007-05-01 23:26:28 +0200771 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 complete(&adap->dev_released);
773}
774
Jean Delvare99cd8e22009-06-19 16:58:20 +0200775/*
Jean Delvare390946b2012-09-10 10:14:02 +0200776 * This function is only needed for mutex_lock_nested, so it is never
777 * called unless locking correctness checking is enabled. Thus we
778 * make it inline to avoid a compiler warning. That's what gcc ends up
779 * doing anyway.
780 */
781static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
782{
783 unsigned int depth = 0;
784
785 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
786 depth++;
787
788 return depth;
789}
790
791/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200792 * Let users instantiate I2C devices through sysfs. This can be used when
793 * platform initialization code doesn't contain the proper data for
794 * whatever reason. Also useful for drivers that do device detection and
795 * detection fails, either because the device uses an unexpected address,
796 * or this is a compatible device with different ID register values.
797 *
798 * Parameter checking may look overzealous, but we really don't want
799 * the user to provide incorrect parameters.
800 */
801static ssize_t
802i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
803 const char *buf, size_t count)
804{
805 struct i2c_adapter *adap = to_i2c_adapter(dev);
806 struct i2c_board_info info;
807 struct i2c_client *client;
808 char *blank, end;
809 int res;
810
Jean Delvare99cd8e22009-06-19 16:58:20 +0200811 memset(&info, 0, sizeof(struct i2c_board_info));
812
813 blank = strchr(buf, ' ');
814 if (!blank) {
815 dev_err(dev, "%s: Missing parameters\n", "new_device");
816 return -EINVAL;
817 }
818 if (blank - buf > I2C_NAME_SIZE - 1) {
819 dev_err(dev, "%s: Invalid device name\n", "new_device");
820 return -EINVAL;
821 }
822 memcpy(info.type, buf, blank - buf);
823
824 /* Parse remaining parameters, reject extra parameters */
825 res = sscanf(++blank, "%hi%c", &info.addr, &end);
826 if (res < 1) {
827 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
828 return -EINVAL;
829 }
830 if (res > 1 && end != '\n') {
831 dev_err(dev, "%s: Extra parameters\n", "new_device");
832 return -EINVAL;
833 }
834
Jean Delvare99cd8e22009-06-19 16:58:20 +0200835 client = i2c_new_device(adap, &info);
836 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200837 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200838
839 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200840 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200841 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200842 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200843 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
844 info.type, info.addr);
845
846 return count;
847}
848
849/*
850 * And of course let the users delete the devices they instantiated, if
851 * they got it wrong. This interface can only be used to delete devices
852 * instantiated by i2c_sysfs_new_device above. This guarantees that we
853 * don't delete devices to which some kernel code still has references.
854 *
855 * Parameter checking may look overzealous, but we really don't want
856 * the user to delete the wrong device.
857 */
858static ssize_t
859i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
860 const char *buf, size_t count)
861{
862 struct i2c_adapter *adap = to_i2c_adapter(dev);
863 struct i2c_client *client, *next;
864 unsigned short addr;
865 char end;
866 int res;
867
868 /* Parse parameters, reject extra parameters */
869 res = sscanf(buf, "%hi%c", &addr, &end);
870 if (res < 1) {
871 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
872 return -EINVAL;
873 }
874 if (res > 1 && end != '\n') {
875 dev_err(dev, "%s: Extra parameters\n", "delete_device");
876 return -EINVAL;
877 }
878
879 /* Make sure the device was added through sysfs */
880 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200881 mutex_lock_nested(&adap->userspace_clients_lock,
882 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200883 list_for_each_entry_safe(client, next, &adap->userspace_clients,
884 detected) {
885 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200886 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
887 "delete_device", client->name, client->addr);
888
889 list_del(&client->detected);
890 i2c_unregister_device(client);
891 res = count;
892 break;
893 }
894 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200895 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200896
897 if (res < 0)
898 dev_err(dev, "%s: Can't find device in list\n",
899 "delete_device");
900 return res;
901}
902
Jean Delvare4f8cf822009-09-18 22:45:46 +0200903static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200904static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
905 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200906
907static struct attribute *i2c_adapter_attrs[] = {
908 &dev_attr_name.attr,
909 &dev_attr_new_device.attr,
910 &dev_attr_delete_device.attr,
911 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200912};
913
Jean Delvare4f8cf822009-09-18 22:45:46 +0200914static struct attribute_group i2c_adapter_attr_group = {
915 .attrs = i2c_adapter_attrs,
916};
917
918static const struct attribute_group *i2c_adapter_attr_groups[] = {
919 &i2c_adapter_attr_group,
920 NULL
921};
922
Michael Lawnick08263742010-08-11 18:21:02 +0200923struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200924 .groups = i2c_adapter_attr_groups,
925 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200926};
Michael Lawnick08263742010-08-11 18:21:02 +0200927EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Stephen Warren643dd092012-04-17 12:43:33 -0600929/**
930 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
931 * @dev: device, probably from some driver model iterator
932 *
933 * When traversing the driver model tree, perhaps using driver model
934 * iterators like @device_for_each_child(), you can't assume very much
935 * about the nodes you find. Use this function to avoid oopses caused
936 * by wrongly treating some non-I2C device as an i2c_adapter.
937 */
938struct i2c_adapter *i2c_verify_adapter(struct device *dev)
939{
940 return (dev->type == &i2c_adapter_type)
941 ? to_i2c_adapter(dev)
942 : NULL;
943}
944EXPORT_SYMBOL(i2c_verify_adapter);
945
Jean Delvare2bb50952009-09-18 22:45:46 +0200946#ifdef CONFIG_I2C_COMPAT
947static struct class_compat *i2c_adapter_compat_class;
948#endif
949
David Brownell9c1600e2007-05-01 23:26:31 +0200950static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
951{
952 struct i2c_devinfo *devinfo;
953
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200954 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200955 list_for_each_entry(devinfo, &__i2c_board_list, list) {
956 if (devinfo->busnum == adapter->nr
957 && !i2c_new_device(adapter,
958 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100959 dev_err(&adapter->dev,
960 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200961 devinfo->board_info.addr);
962 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200963 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200964}
965
Wolfram Sang7f427042013-07-11 12:56:15 +0100966/* OF support code */
967
968#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniou4edb7632014-10-28 22:36:02 +0200969static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
970 struct device_node *node)
971{
972 struct i2c_client *result;
973 struct i2c_board_info info = {};
974 struct dev_archdata dev_ad = {};
975 const __be32 *addr;
976 int len;
977
978 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
979
980 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
981 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
982 node->full_name);
983 return ERR_PTR(-EINVAL);
984 }
985
986 addr = of_get_property(node, "reg", &len);
987 if (!addr || (len < sizeof(int))) {
988 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
989 node->full_name);
990 return ERR_PTR(-EINVAL);
991 }
992
993 info.addr = be32_to_cpup(addr);
994 if (info.addr > (1 << 10) - 1) {
995 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
996 info.addr, node->full_name);
997 return ERR_PTR(-EINVAL);
998 }
999
1000 info.irq = irq_of_parse_and_map(node, 0);
1001 info.of_node = of_node_get(node);
1002 info.archdata = &dev_ad;
1003
1004 if (of_get_property(node, "wakeup-source", NULL))
1005 info.flags |= I2C_CLIENT_WAKE;
1006
1007 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1008
1009 result = i2c_new_device(adap, &info);
1010 if (result == NULL) {
1011 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1012 node->full_name);
1013 of_node_put(node);
1014 irq_dispose_mapping(info.irq);
1015 return ERR_PTR(-EINVAL);
1016 }
1017 return result;
1018}
1019
Wolfram Sang7f427042013-07-11 12:56:15 +01001020static void internal_of_i2c_register_devices(struct i2c_adapter *adap)
1021{
Wolfram Sang7f427042013-07-11 12:56:15 +01001022 struct device_node *node;
1023
1024 /* Only register child devices if the adapter has a node pointer set */
1025 if (!adap->dev.of_node)
1026 return;
1027
1028 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1029
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001030 for_each_available_child_of_node(adap->dev.of_node, node) {
1031 if (of_node_test_and_set_flag(node, OF_POPULATED))
1032 continue;
Pantelis Antoniou4edb7632014-10-28 22:36:02 +02001033 of_i2c_register_device(adap, node);
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001034 }
Wolfram Sang7f427042013-07-11 12:56:15 +01001035}
1036
1037static int of_dev_node_match(struct device *dev, void *data)
1038{
1039 return dev->of_node == data;
1040}
1041
1042/* must call put_device() when done with returned i2c_client device */
1043struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1044{
1045 struct device *dev;
1046
1047 dev = bus_find_device(&i2c_bus_type, NULL, node,
1048 of_dev_node_match);
1049 if (!dev)
1050 return NULL;
1051
1052 return i2c_verify_client(dev);
1053}
1054EXPORT_SYMBOL(of_find_i2c_device_by_node);
1055
1056/* must call put_device() when done with returned i2c_adapter device */
1057struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1058{
1059 struct device *dev;
1060
1061 dev = bus_find_device(&i2c_bus_type, NULL, node,
1062 of_dev_node_match);
1063 if (!dev)
1064 return NULL;
1065
1066 return i2c_verify_adapter(dev);
1067}
1068EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1069#else
1070static void internal_of_i2c_register_devices(struct i2c_adapter *adap) { }
1071#endif /* CONFIG_OF */
1072
Jean Delvare69b00892009-12-06 17:06:27 +01001073static int i2c_do_add_adapter(struct i2c_driver *driver,
1074 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001075{
Jean Delvare4735c982008-07-14 22:38:36 +02001076 /* Detect supported devices on that bus, and instantiate them */
1077 i2c_detect(adap, driver);
1078
1079 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001080 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001081 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1082 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001083 dev_warn(&adap->dev, "Please use another way to instantiate "
1084 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001085 /* We ignore the return code; if it fails, too bad */
1086 driver->attach_adapter(adap);
1087 }
1088 return 0;
1089}
1090
Jean Delvare69b00892009-12-06 17:06:27 +01001091static int __process_new_adapter(struct device_driver *d, void *data)
1092{
1093 return i2c_do_add_adapter(to_i2c_driver(d), data);
1094}
1095
David Brownell6e13e642007-05-01 23:26:31 +02001096static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Jean Delvared6703282010-08-11 18:20:59 +02001098 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
David Brownell1d0b19c2008-10-14 17:30:05 +02001100 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001101 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1102 res = -EAGAIN;
1103 goto out_list;
1104 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001105
Jean Delvare2236baa2010-11-15 22:40:38 +01001106 /* Sanity checks */
1107 if (unlikely(adap->name[0] == '\0')) {
1108 pr_err("i2c-core: Attempt to register an adapter with "
1109 "no name!\n");
1110 return -EINVAL;
1111 }
1112 if (unlikely(!adap->algo)) {
1113 pr_err("i2c-core: Attempt to register adapter '%s' with "
1114 "no algo!\n", adap->name);
1115 return -EINVAL;
1116 }
1117
Mika Kuoppala194684e2009-12-06 17:06:22 +01001118 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001119 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001120 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Jean Delvare8fcfef62009-03-28 21:34:43 +01001122 /* Set default timeout to 1 second if not already set */
1123 if (adap->timeout == 0)
1124 adap->timeout = HZ;
1125
Kay Sievers27d9c182009-01-07 14:29:16 +01001126 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001127 adap->dev.bus = &i2c_bus_type;
1128 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001129 res = device_register(&adap->dev);
1130 if (res)
1131 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001133 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1134
Jean Delvare2bb50952009-09-18 22:45:46 +02001135#ifdef CONFIG_I2C_COMPAT
1136 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1137 adap->dev.parent);
1138 if (res)
1139 dev_warn(&adap->dev,
1140 "Failed to create compatibility class link\n");
1141#endif
1142
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301143 /* bus recovery specific initialization */
1144 if (adap->bus_recovery_info) {
1145 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1146
1147 if (!bri->recover_bus) {
1148 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1149 adap->bus_recovery_info = NULL;
1150 goto exit_recovery;
1151 }
1152
1153 /* Generic GPIO recovery */
1154 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1155 if (!gpio_is_valid(bri->scl_gpio)) {
1156 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1157 adap->bus_recovery_info = NULL;
1158 goto exit_recovery;
1159 }
1160
1161 if (gpio_is_valid(bri->sda_gpio))
1162 bri->get_sda = get_sda_gpio_value;
1163 else
1164 bri->get_sda = NULL;
1165
1166 bri->get_scl = get_scl_gpio_value;
1167 bri->set_scl = set_scl_gpio_value;
1168 } else if (!bri->set_scl || !bri->get_scl) {
1169 /* Generic SCL recovery */
1170 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1171 adap->bus_recovery_info = NULL;
1172 }
1173 }
1174
1175exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001176 /* create pre-declared device nodes */
Wolfram Sang7f427042013-07-11 12:56:15 +01001177 internal_of_i2c_register_devices(adap);
1178
David Brownell6e13e642007-05-01 23:26:31 +02001179 if (adap->nr < __i2c_first_dynamic_bus_num)
1180 i2c_scan_static_board_info(adap);
1181
Jean Delvare4735c982008-07-14 22:38:36 +02001182 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001183 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001184 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001185 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001186
1187 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001188
Jean Delvareb119c6c2006-08-15 18:26:30 +02001189out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001190 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001191 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001192 mutex_unlock(&core_lock);
1193 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
David Brownell6e13e642007-05-01 23:26:31 +02001196/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001197 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1198 * @adap: the adapter to register (with adap->nr initialized)
1199 * Context: can sleep
1200 *
1201 * See i2c_add_numbered_adapter() for details.
1202 */
1203static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1204{
1205 int id;
1206
1207 mutex_lock(&core_lock);
1208 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1209 GFP_KERNEL);
1210 mutex_unlock(&core_lock);
1211 if (id < 0)
1212 return id == -ENOSPC ? -EBUSY : id;
1213
1214 return i2c_register_adapter(adap);
1215}
1216
1217/**
David Brownell6e13e642007-05-01 23:26:31 +02001218 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1219 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001220 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001221 *
1222 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001223 * doesn't matter or when its bus number is specified by an dt alias.
1224 * Examples of bases when the bus number doesn't matter: I2C adapters
1225 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001226 *
1227 * When this returns zero, a new bus number was allocated and stored
1228 * in adap->nr, and the specified adapter became available for clients.
1229 * Otherwise, a negative errno value is returned.
1230 */
1231int i2c_add_adapter(struct i2c_adapter *adapter)
1232{
Doug Andersonee5c2742013-03-01 08:57:31 -08001233 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001234 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001235
Doug Andersonee5c2742013-03-01 08:57:31 -08001236 if (dev->of_node) {
1237 id = of_alias_get_id(dev->of_node, "i2c");
1238 if (id >= 0) {
1239 adapter->nr = id;
1240 return __i2c_add_numbered_adapter(adapter);
1241 }
1242 }
1243
Jean Delvarecaada322008-01-27 18:14:49 +01001244 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001245 id = idr_alloc(&i2c_adapter_idr, adapter,
1246 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001247 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001248 if (id < 0)
1249 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001250
1251 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001252
David Brownell6e13e642007-05-01 23:26:31 +02001253 return i2c_register_adapter(adapter);
1254}
1255EXPORT_SYMBOL(i2c_add_adapter);
1256
1257/**
1258 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1259 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001260 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001261 *
1262 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001263 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1264 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001265 * is used to properly configure I2C devices.
1266 *
Grant Likely488bf312011-07-25 17:49:43 +02001267 * If the requested bus number is set to -1, then this function will behave
1268 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1269 *
David Brownell6e13e642007-05-01 23:26:31 +02001270 * If no devices have pre-been declared for this bus, then be sure to
1271 * register the adapter before any dynamically allocated ones. Otherwise
1272 * the required bus ID may not be available.
1273 *
1274 * When this returns zero, the specified adapter became available for
1275 * clients using the bus number provided in adap->nr. Also, the table
1276 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1277 * and the appropriate driver model device nodes are created. Otherwise, a
1278 * negative errno value is returned.
1279 */
1280int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1281{
Grant Likely488bf312011-07-25 17:49:43 +02001282 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1283 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001284
Doug Andersonee5c2742013-03-01 08:57:31 -08001285 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001286}
1287EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1288
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001289static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001290 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001291{
Jean Delvare4735c982008-07-14 22:38:36 +02001292 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001293
Jean Delvareacec2112009-03-28 21:34:40 +01001294 /* Remove the devices we created ourselves as the result of hardware
1295 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001296 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1297 if (client->adapter == adapter) {
1298 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1299 client->name, client->addr);
1300 list_del(&client->detected);
1301 i2c_unregister_device(client);
1302 }
1303 }
Jean Delvare026526f2008-01-27 18:14:49 +01001304}
1305
Jean Delvaree549c2b2009-06-19 16:58:19 +02001306static int __unregister_client(struct device *dev, void *dummy)
1307{
1308 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001309 if (client && strcmp(client->name, "dummy"))
1310 i2c_unregister_device(client);
1311 return 0;
1312}
1313
1314static int __unregister_dummy(struct device *dev, void *dummy)
1315{
1316 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001317 if (client)
1318 i2c_unregister_device(client);
1319 return 0;
1320}
1321
Jean Delvare69b00892009-12-06 17:06:27 +01001322static int __process_removed_adapter(struct device_driver *d, void *data)
1323{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001324 i2c_do_del_adapter(to_i2c_driver(d), data);
1325 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001326}
1327
David Brownelld64f73b2007-07-12 14:12:28 +02001328/**
1329 * i2c_del_adapter - unregister I2C adapter
1330 * @adap: the adapter being unregistered
1331 * Context: can sleep
1332 *
1333 * This unregisters an I2C adapter which was previously registered
1334 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1335 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001336void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001338 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001339 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001342 mutex_lock(&core_lock);
1343 found = idr_find(&i2c_adapter_idr, adap->nr);
1344 mutex_unlock(&core_lock);
1345 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001346 pr_debug("i2c-core: attempting to delete unregistered "
1347 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Jean Delvare026526f2008-01-27 18:14:49 +01001351 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001352 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001353 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001354 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001355 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001357 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001358 mutex_lock_nested(&adap->userspace_clients_lock,
1359 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001360 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1361 detected) {
1362 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1363 client->addr);
1364 list_del(&client->detected);
1365 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001366 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001367 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001368
Jean Delvaree549c2b2009-06-19 16:58:19 +02001369 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001370 * check the returned value. This is a two-pass process, because
1371 * we can't remove the dummy devices during the first pass: they
1372 * could have been instantiated by real devices wishing to clean
1373 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001374 device_for_each_child(&adap->dev, NULL, __unregister_client);
1375 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jean Delvare2bb50952009-09-18 22:45:46 +02001377#ifdef CONFIG_I2C_COMPAT
1378 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1379 adap->dev.parent);
1380#endif
1381
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001382 /* device name is gone after device_unregister */
1383 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* clean up the sysfs representation */
1386 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* wait for sysfs to drop all references */
1390 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
David Brownell6e13e642007-05-01 23:26:31 +02001392 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001393 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001395 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001397 /* Clear the device structure in case this adapter is ever going to be
1398 added again */
1399 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
David Brownellc0564602007-05-01 23:26:31 +02001401EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
David Brownell7b4fbc52007-05-01 23:26:30 +02001403/* ------------------------------------------------------------------------- */
1404
Jean Delvare7ae31482011-03-20 14:50:52 +01001405int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1406{
1407 int res;
1408
1409 mutex_lock(&core_lock);
1410 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1411 mutex_unlock(&core_lock);
1412
1413 return res;
1414}
1415EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1416
Jean Delvare69b00892009-12-06 17:06:27 +01001417static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001418{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001419 if (dev->type != &i2c_adapter_type)
1420 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001421 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001422}
1423
David Brownell7b4fbc52007-05-01 23:26:30 +02001424/*
1425 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001426 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001429int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001431 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
David Brownell1d0b19c2008-10-14 17:30:05 +02001433 /* Can't register until after driver model init */
1434 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1435 return -EAGAIN;
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001438 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jean Delvare729d6dd2009-06-19 16:58:18 +02001441 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001442 * will have called probe() for all matching-but-unbound devices.
1443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 res = driver_register(&driver->driver);
1445 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001446 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001447
Mark Brownf4e8db32011-01-14 22:03:50 +01001448 /* Drivers should switch to dev_pm_ops instead. */
1449 if (driver->suspend)
1450 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1451 driver->driver.name);
1452 if (driver->resume)
1453 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1454 driver->driver.name);
1455
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001456 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Jean Delvare4735c982008-07-14 22:38:36 +02001458 INIT_LIST_HEAD(&driver->clients);
1459 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001460 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001461
Jean Delvare7eebcb72006-02-05 23:28:21 +01001462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001464EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jean Delvare69b00892009-12-06 17:06:27 +01001466static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001467{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001468 if (dev->type == &i2c_adapter_type)
1469 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1470 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001471}
1472
David Brownella1d9e6e2007-05-01 23:26:30 +02001473/**
1474 * i2c_del_driver - unregister I2C driver
1475 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001476 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001477 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001478void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Jean Delvare7ae31482011-03-20 14:50:52 +01001480 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001483 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
David Brownellc0564602007-05-01 23:26:31 +02001485EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
David Brownell7b4fbc52007-05-01 23:26:30 +02001487/* ------------------------------------------------------------------------- */
1488
Jean Delvaree48d3312008-01-27 18:14:48 +01001489/**
1490 * i2c_use_client - increments the reference count of the i2c client structure
1491 * @client: the client being referenced
1492 *
1493 * Each live reference to a client should be refcounted. The driver model does
1494 * that automatically as part of driver binding, so that most drivers don't
1495 * need to do this explicitly: they hold a reference until they're unbound
1496 * from the device.
1497 *
1498 * A pointer to the client with the incremented reference counter is returned.
1499 */
1500struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
David Brownell6ea438e2008-07-14 22:38:24 +02001502 if (client && get_device(&client->dev))
1503 return client;
1504 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
David Brownellc0564602007-05-01 23:26:31 +02001506EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Jean Delvaree48d3312008-01-27 18:14:48 +01001508/**
1509 * i2c_release_client - release a use of the i2c client structure
1510 * @client: the client being no longer referenced
1511 *
1512 * Must be called when a user of a client is finished with it.
1513 */
1514void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
David Brownell6ea438e2008-07-14 22:38:24 +02001516 if (client)
1517 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
David Brownellc0564602007-05-01 23:26:31 +02001519EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
David Brownell9b766b82008-01-27 18:14:51 +01001521struct i2c_cmd_arg {
1522 unsigned cmd;
1523 void *arg;
1524};
1525
1526static int i2c_cmd(struct device *dev, void *_arg)
1527{
1528 struct i2c_client *client = i2c_verify_client(dev);
1529 struct i2c_cmd_arg *arg = _arg;
1530
1531 if (client && client->driver && client->driver->command)
1532 client->driver->command(client, arg->cmd, arg->arg);
1533 return 0;
1534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1537{
David Brownell9b766b82008-01-27 18:14:51 +01001538 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
David Brownell9b766b82008-01-27 18:14:51 +01001540 cmd_arg.cmd = cmd;
1541 cmd_arg.arg = arg;
1542 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
David Brownellc0564602007-05-01 23:26:31 +02001544EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001546#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1547static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1548 void *arg)
1549{
1550 struct of_reconfig_data *rd = arg;
1551 struct i2c_adapter *adap;
1552 struct i2c_client *client;
1553
1554 switch (of_reconfig_get_state_change(action, rd)) {
1555 case OF_RECONFIG_CHANGE_ADD:
1556 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1557 if (adap == NULL)
1558 return NOTIFY_OK; /* not for us */
1559
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001560 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1561 put_device(&adap->dev);
1562 return NOTIFY_OK;
1563 }
1564
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001565 client = of_i2c_register_device(adap, rd->dn);
1566 put_device(&adap->dev);
1567
1568 if (IS_ERR(client)) {
1569 pr_err("%s: failed to create for '%s'\n",
1570 __func__, rd->dn->full_name);
1571 return notifier_from_errno(PTR_ERR(client));
1572 }
1573 break;
1574 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001575 /* already depopulated? */
1576 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1577 return NOTIFY_OK;
1578
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001579 /* find our device by node */
1580 client = of_find_i2c_device_by_node(rd->dn);
1581 if (client == NULL)
1582 return NOTIFY_OK; /* no? not meant for us */
1583
1584 /* unregister takes one ref away */
1585 i2c_unregister_device(client);
1586
1587 /* and put the reference of the find */
1588 put_device(&client->dev);
1589 break;
1590 }
1591
1592 return NOTIFY_OK;
1593}
1594static struct notifier_block i2c_of_notifier = {
1595 .notifier_call = of_i2c_notify,
1596};
1597#else
1598extern struct notifier_block i2c_of_notifier;
1599#endif /* CONFIG_OF_DYNAMIC */
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static int __init i2c_init(void)
1602{
1603 int retval;
1604
1605 retval = bus_register(&i2c_bus_type);
1606 if (retval)
1607 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001608#ifdef CONFIG_I2C_COMPAT
1609 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1610 if (!i2c_adapter_compat_class) {
1611 retval = -ENOMEM;
1612 goto bus_err;
1613 }
1614#endif
David Brownelle9f13732008-01-27 18:14:52 +01001615 retval = i2c_add_driver(&dummy_driver);
1616 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001617 goto class_err;
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001618
1619 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1620 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1621
David Brownelle9f13732008-01-27 18:14:52 +01001622 return 0;
1623
Jean Delvare2bb50952009-09-18 22:45:46 +02001624class_err:
1625#ifdef CONFIG_I2C_COMPAT
1626 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001627bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001628#endif
David Brownelle9f13732008-01-27 18:14:52 +01001629 bus_unregister(&i2c_bus_type);
1630 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633static void __exit i2c_exit(void)
1634{
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001635 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1636 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001637 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001638#ifdef CONFIG_I2C_COMPAT
1639 class_compat_unregister(i2c_adapter_compat_class);
1640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 bus_unregister(&i2c_bus_type);
1642}
1643
David Brownella10f9e72008-10-14 17:30:06 +02001644/* We must initialize early, because some subsystems register i2c drivers
1645 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1646 */
1647postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648module_exit(i2c_exit);
1649
1650/* ----------------------------------------------------
1651 * the functional interface to the i2c busses.
1652 * ----------------------------------------------------
1653 */
1654
David Brownella1cdeda2008-07-14 22:38:24 +02001655/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001656 * __i2c_transfer - unlocked flavor of i2c_transfer
1657 * @adap: Handle to I2C bus
1658 * @msgs: One or more messages to execute before STOP is issued to
1659 * terminate the operation; each message begins with a START.
1660 * @num: Number of messages to be executed.
1661 *
1662 * Returns negative errno, else the number of messages executed.
1663 *
1664 * Adapter lock must be held when calling this function. No debug logging
1665 * takes place. adap->algo->master_xfer existence isn't checked.
1666 */
1667int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1668{
1669 unsigned long orig_jiffies;
1670 int ret, try;
1671
1672 /* Retry automatically on arbitration loss */
1673 orig_jiffies = jiffies;
1674 for (ret = 0, try = 0; try <= adap->retries; try++) {
1675 ret = adap->algo->master_xfer(adap, msgs, num);
1676 if (ret != -EAGAIN)
1677 break;
1678 if (time_after(jiffies, orig_jiffies + adap->timeout))
1679 break;
1680 }
1681
1682 return ret;
1683}
1684EXPORT_SYMBOL(__i2c_transfer);
1685
1686/**
David Brownella1cdeda2008-07-14 22:38:24 +02001687 * i2c_transfer - execute a single or combined I2C message
1688 * @adap: Handle to I2C bus
1689 * @msgs: One or more messages to execute before STOP is issued to
1690 * terminate the operation; each message begins with a START.
1691 * @num: Number of messages to be executed.
1692 *
1693 * Returns negative errno, else the number of messages executed.
1694 *
1695 * Note that there is no requirement that each message be sent to
1696 * the same slave address, although that is the most common model.
1697 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001698int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001700 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
David Brownella1cdeda2008-07-14 22:38:24 +02001702 /* REVISIT the fault reporting model here is weak:
1703 *
1704 * - When we get an error after receiving N bytes from a slave,
1705 * there is no way to report "N".
1706 *
1707 * - When we get a NAK after transmitting N bytes to a slave,
1708 * there is no way to report "N" ... or to let the master
1709 * continue executing the rest of this combined message, if
1710 * that's the appropriate response.
1711 *
1712 * - When for example "num" is two and we successfully complete
1713 * the first message but get an error part way through the
1714 * second, it's unclear whether that should be reported as
1715 * one (discarding status on the second message) or errno
1716 * (discarding status on the first one).
1717 */
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (adap->algo->master_xfer) {
1720#ifdef DEBUG
1721 for (ret = 0; ret < num; ret++) {
1722 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001723 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1724 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1725 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727#endif
1728
Mike Rapoportcea443a82008-01-27 18:14:50 +01001729 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001730 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001731 if (!ret)
1732 /* I2C activity is ongoing. */
1733 return -EAGAIN;
1734 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001735 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001736 }
1737
Jean Delvareb37d2a32012-06-29 07:47:19 -03001738 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001739 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 return ret;
1742 } else {
1743 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001744 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746}
David Brownellc0564602007-05-01 23:26:31 +02001747EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Brownella1cdeda2008-07-14 22:38:24 +02001749/**
1750 * i2c_master_send - issue a single I2C message in master transmit mode
1751 * @client: Handle to slave device
1752 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001753 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001754 *
1755 * Returns negative errno, or else the number of bytes written.
1756 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001757int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001760 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct i2c_msg msg;
1762
Jean Delvare815f55f2005-05-07 22:58:46 +02001763 msg.addr = client->addr;
1764 msg.flags = client->flags & I2C_M_TEN;
1765 msg.len = count;
1766 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001767
Jean Delvare815f55f2005-05-07 22:58:46 +02001768 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001770 /*
1771 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1772 * transmitted, else error code.
1773 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001774 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
David Brownellc0564602007-05-01 23:26:31 +02001776EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David Brownella1cdeda2008-07-14 22:38:24 +02001778/**
1779 * i2c_master_recv - issue a single I2C message in master receive mode
1780 * @client: Handle to slave device
1781 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001782 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001783 *
1784 * Returns negative errno, or else the number of bytes read.
1785 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001786int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Farid Hammane7225acf2010-05-21 18:40:58 +02001788 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct i2c_msg msg;
1790 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jean Delvare815f55f2005-05-07 22:58:46 +02001792 msg.addr = client->addr;
1793 msg.flags = client->flags & I2C_M_TEN;
1794 msg.flags |= I2C_M_RD;
1795 msg.len = count;
1796 msg.buf = buf;
1797
1798 ret = i2c_transfer(adap, &msg, 1);
1799
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001800 /*
1801 * If everything went ok (i.e. 1 msg received), return #bytes received,
1802 * else error code.
1803 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001804 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
David Brownellc0564602007-05-01 23:26:31 +02001806EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/* ----------------------------------------------------
1809 * the i2c address scanning function
1810 * Will not work for 10-bit addresses!
1811 * ----------------------------------------------------
1812 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001813
Jean Delvare63e4e802010-06-03 11:33:51 +02001814/*
1815 * Legacy default probe function, mostly relevant for SMBus. The default
1816 * probe method is a quick write, but it is known to corrupt the 24RF08
1817 * EEPROMs due to a state machine bug, and could also irreversibly
1818 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1819 * we use a short byte read instead. Also, some bus drivers don't implement
1820 * quick write, so we fallback to a byte read in that case too.
1821 * On x86, there is another special case for FSC hardware monitoring chips,
1822 * which want regular byte reads (address 0x73.) Fortunately, these are the
1823 * only known chips using this I2C address on PC hardware.
1824 * Returns 1 if probe succeeded, 0 if not.
1825 */
1826static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1827{
1828 int err;
1829 union i2c_smbus_data dummy;
1830
1831#ifdef CONFIG_X86
1832 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1833 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1834 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1835 I2C_SMBUS_BYTE_DATA, &dummy);
1836 else
1837#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001838 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1839 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001840 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1841 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001842 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1843 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1844 I2C_SMBUS_BYTE, &dummy);
1845 else {
1846 dev_warn(&adap->dev, "No suitable probing method supported\n");
1847 err = -EOPNOTSUPP;
1848 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001849
1850 return err >= 0;
1851}
1852
Jean Delvareccfbbd02009-12-06 17:06:25 +01001853static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001854 struct i2c_driver *driver)
1855{
1856 struct i2c_board_info info;
1857 struct i2c_adapter *adapter = temp_client->adapter;
1858 int addr = temp_client->addr;
1859 int err;
1860
1861 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001862 err = i2c_check_addr_validity(addr);
1863 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001864 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1865 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001866 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001867 }
1868
1869 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001870 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001871 return 0;
1872
Jean Delvareccfbbd02009-12-06 17:06:25 +01001873 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001874 if (!i2c_default_probe(adapter, addr))
1875 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001876
1877 /* Finally call the custom detection function */
1878 memset(&info, 0, sizeof(struct i2c_board_info));
1879 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001880 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001881 if (err) {
1882 /* -ENODEV is returned if the detection fails. We catch it
1883 here as this isn't an error. */
1884 return err == -ENODEV ? 0 : err;
1885 }
1886
1887 /* Consistency check */
1888 if (info.type[0] == '\0') {
1889 dev_err(&adapter->dev, "%s detection function provided "
1890 "no name for 0x%x\n", driver->driver.name,
1891 addr);
1892 } else {
1893 struct i2c_client *client;
1894
1895 /* Detection succeeded, instantiate the device */
1896 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1897 info.type, info.addr);
1898 client = i2c_new_device(adapter, &info);
1899 if (client)
1900 list_add_tail(&client->detected, &driver->clients);
1901 else
1902 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1903 info.type, info.addr);
1904 }
1905 return 0;
1906}
1907
1908static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1909{
Jean Delvarec3813d62009-12-14 21:17:25 +01001910 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001911 struct i2c_client *temp_client;
1912 int i, err = 0;
1913 int adap_id = i2c_adapter_id(adapter);
1914
Jean Delvarec3813d62009-12-14 21:17:25 +01001915 address_list = driver->address_list;
1916 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001917 return 0;
1918
Jean Delvare51b54ba2010-10-24 18:16:58 +02001919 /* Stop here if the classes do not match */
1920 if (!(adapter->class & driver->class))
1921 return 0;
1922
Jean Delvare4735c982008-07-14 22:38:36 +02001923 /* Set up a temporary client to help detect callback */
1924 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1925 if (!temp_client)
1926 return -ENOMEM;
1927 temp_client->adapter = adapter;
1928
Jean Delvarec3813d62009-12-14 21:17:25 +01001929 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001930 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001931 "addr 0x%02x\n", adap_id, address_list[i]);
1932 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001933 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001934 if (unlikely(err))
1935 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001936 }
1937
Jean Delvare4735c982008-07-14 22:38:36 +02001938 kfree(temp_client);
1939 return err;
1940}
1941
Jean Delvared44f19d2010-08-11 18:20:57 +02001942int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1943{
1944 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1945 I2C_SMBUS_QUICK, NULL) >= 0;
1946}
1947EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1948
Jean Delvare12b5053a2007-05-01 23:26:31 +02001949struct i2c_client *
1950i2c_new_probed_device(struct i2c_adapter *adap,
1951 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001952 unsigned short const *addr_list,
1953 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001954{
1955 int i;
1956
Jean Delvare8031d792010-08-11 18:21:00 +02001957 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001958 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001959
Jean Delvare12b5053a2007-05-01 23:26:31 +02001960 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1961 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001962 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001963 dev_warn(&adap->dev, "Invalid 7-bit address "
1964 "0x%02x\n", addr_list[i]);
1965 continue;
1966 }
1967
1968 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001969 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001970 dev_dbg(&adap->dev, "Address 0x%02x already in "
1971 "use, not probing\n", addr_list[i]);
1972 continue;
1973 }
1974
Jean Delvare63e4e802010-06-03 11:33:51 +02001975 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001976 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001977 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001978 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001979
1980 if (addr_list[i] == I2C_CLIENT_END) {
1981 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1982 return NULL;
1983 }
1984
1985 info->addr = addr_list[i];
1986 return i2c_new_device(adap, info);
1987}
1988EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1989
Jean Delvared735b342011-03-20 14:50:52 +01001990struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001993
Jean Delvarecaada322008-01-27 18:14:49 +01001994 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001995 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001996 if (adapter && !try_module_get(adapter->owner))
1997 adapter = NULL;
1998
Jean Delvarecaada322008-01-27 18:14:49 +01001999 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002000 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
David Brownellc0564602007-05-01 23:26:31 +02002002EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004void i2c_put_adapter(struct i2c_adapter *adap)
2005{
2006 module_put(adap->owner);
2007}
David Brownellc0564602007-05-01 23:26:31 +02002008EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010/* The SMBus parts */
2011
David Brownell438d6c22006-12-10 21:21:31 +01002012#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002013static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002016
Farid Hammane7225acf2010-05-21 18:40:58 +02002017 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002018 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 data = data ^ POLY;
2020 data = data << 1;
2021 }
2022 return (u8)(data >> 8);
2023}
2024
Jean Delvare421ef472005-10-26 21:28:55 +02002025/* Incremental CRC8 over count bytes in the array pointed to by p */
2026static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 int i;
2029
Farid Hammane7225acf2010-05-21 18:40:58 +02002030 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002031 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return crc;
2033}
2034
Jean Delvare421ef472005-10-26 21:28:55 +02002035/* Assume a 7-bit address, which is reasonable for SMBus */
2036static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Jean Delvare421ef472005-10-26 21:28:55 +02002038 /* The address will be sent first */
2039 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2040 pec = i2c_smbus_pec(pec, &addr, 1);
2041
2042 /* The data buffer follows */
2043 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jean Delvare421ef472005-10-26 21:28:55 +02002046/* Used for write only transactions */
2047static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Jean Delvare421ef472005-10-26 21:28:55 +02002049 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2050 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Jean Delvare421ef472005-10-26 21:28:55 +02002053/* Return <0 on CRC error
2054 If there was a write before this read (most cases) we need to take the
2055 partial CRC from the write part into account.
2056 Note that this function does modify the message (we need to decrease the
2057 message length to hide the CRC byte from the caller). */
2058static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Jean Delvare421ef472005-10-26 21:28:55 +02002060 u8 rpec = msg->buf[--msg->len];
2061 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (rpec != cpec) {
2064 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2065 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002066 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
David Brownell438d6c22006-12-10 21:21:31 +01002068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
David Brownella1cdeda2008-07-14 22:38:24 +02002071/**
2072 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2073 * @client: Handle to slave device
2074 *
2075 * This executes the SMBus "receive byte" protocol, returning negative errno
2076 * else the byte received from the device.
2077 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002078s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002081 int status;
2082
2083 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2084 I2C_SMBUS_READ, 0,
2085 I2C_SMBUS_BYTE, &data);
2086 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
David Brownellc0564602007-05-01 23:26:31 +02002088EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
David Brownella1cdeda2008-07-14 22:38:24 +02002090/**
2091 * i2c_smbus_write_byte - SMBus "send byte" protocol
2092 * @client: Handle to slave device
2093 * @value: Byte to be sent
2094 *
2095 * This executes the SMBus "send byte" protocol, returning negative errno
2096 * else zero on success.
2097 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002098s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Farid Hammane7225acf2010-05-21 18:40:58 +02002100 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002101 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
David Brownellc0564602007-05-01 23:26:31 +02002103EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
David Brownella1cdeda2008-07-14 22:38:24 +02002105/**
2106 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2107 * @client: Handle to slave device
2108 * @command: Byte interpreted by slave
2109 *
2110 * This executes the SMBus "read byte" protocol, returning negative errno
2111 * else a data byte received from the device.
2112 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002113s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002116 int status;
2117
2118 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2119 I2C_SMBUS_READ, command,
2120 I2C_SMBUS_BYTE_DATA, &data);
2121 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
David Brownellc0564602007-05-01 23:26:31 +02002123EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
David Brownella1cdeda2008-07-14 22:38:24 +02002125/**
2126 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2127 * @client: Handle to slave device
2128 * @command: Byte interpreted by slave
2129 * @value: Byte being written
2130 *
2131 * This executes the SMBus "write byte" protocol, returning negative errno
2132 * else zero on success.
2133 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002134s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2135 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 union i2c_smbus_data data;
2138 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002139 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2140 I2C_SMBUS_WRITE, command,
2141 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
David Brownellc0564602007-05-01 23:26:31 +02002143EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
David Brownella1cdeda2008-07-14 22:38:24 +02002145/**
2146 * i2c_smbus_read_word_data - SMBus "read word" protocol
2147 * @client: Handle to slave device
2148 * @command: Byte interpreted by slave
2149 *
2150 * This executes the SMBus "read word" protocol, returning negative errno
2151 * else a 16-bit unsigned "word" received from the device.
2152 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002153s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002156 int status;
2157
2158 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2159 I2C_SMBUS_READ, command,
2160 I2C_SMBUS_WORD_DATA, &data);
2161 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
David Brownellc0564602007-05-01 23:26:31 +02002163EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
David Brownella1cdeda2008-07-14 22:38:24 +02002165/**
2166 * i2c_smbus_write_word_data - SMBus "write word" protocol
2167 * @client: Handle to slave device
2168 * @command: Byte interpreted by slave
2169 * @value: 16-bit "word" being written
2170 *
2171 * This executes the SMBus "write word" protocol, returning negative errno
2172 * else zero on success.
2173 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002174s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2175 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 union i2c_smbus_data data;
2178 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002179 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2180 I2C_SMBUS_WRITE, command,
2181 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
David Brownellc0564602007-05-01 23:26:31 +02002183EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
David Brownella64ec072007-10-13 23:56:31 +02002185/**
David Brownella1cdeda2008-07-14 22:38:24 +02002186 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002187 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002188 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002189 * @values: Byte array into which data will be read; big enough to hold
2190 * the data returned by the slave. SMBus allows at most 32 bytes.
2191 *
David Brownella1cdeda2008-07-14 22:38:24 +02002192 * This executes the SMBus "block read" protocol, returning negative errno
2193 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002194 *
2195 * Note that using this function requires that the client's adapter support
2196 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2197 * support this; its emulation through I2C messaging relies on a specific
2198 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2199 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002200s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002201 u8 *values)
2202{
2203 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002204 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002205
David Brownell24a5bb72008-07-14 22:38:23 +02002206 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2207 I2C_SMBUS_READ, command,
2208 I2C_SMBUS_BLOCK_DATA, &data);
2209 if (status)
2210 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002211
2212 memcpy(values, &data.block[1], data.block[0]);
2213 return data.block[0];
2214}
2215EXPORT_SYMBOL(i2c_smbus_read_block_data);
2216
David Brownella1cdeda2008-07-14 22:38:24 +02002217/**
2218 * i2c_smbus_write_block_data - SMBus "block write" protocol
2219 * @client: Handle to slave device
2220 * @command: Byte interpreted by slave
2221 * @length: Size of data block; SMBus allows at most 32 bytes
2222 * @values: Byte array which will be written.
2223 *
2224 * This executes the SMBus "block write" protocol, returning negative errno
2225 * else zero on success.
2226 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002227s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002228 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (length > I2C_SMBUS_BLOCK_MAX)
2233 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002235 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002236 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2237 I2C_SMBUS_WRITE, command,
2238 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
David Brownellc0564602007-05-01 23:26:31 +02002240EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002243s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002244 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002247 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002248
Jean Delvare4b2643d2007-07-12 14:12:29 +02002249 if (length > I2C_SMBUS_BLOCK_MAX)
2250 length = I2C_SMBUS_BLOCK_MAX;
2251 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002252 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2253 I2C_SMBUS_READ, command,
2254 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2255 if (status < 0)
2256 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002257
2258 memcpy(values, &data.block[1], data.block[0]);
2259 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
David Brownellc0564602007-05-01 23:26:31 +02002261EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jean Delvare0cc43a12011-01-10 22:11:23 +01002263s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002264 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002265{
2266 union i2c_smbus_data data;
2267
2268 if (length > I2C_SMBUS_BLOCK_MAX)
2269 length = I2C_SMBUS_BLOCK_MAX;
2270 data.block[0] = length;
2271 memcpy(data.block + 1, values, length);
2272 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2273 I2C_SMBUS_WRITE, command,
2274 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2275}
David Brownellc0564602007-05-01 23:26:31 +02002276EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002277
David Brownell438d6c22006-12-10 21:21:31 +01002278/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002280static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2281 unsigned short flags,
2282 char read_write, u8 command, int size,
2283 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 /* So we need to generate a series of msgs. In the case of writing, we
2286 need to use only one message; when reading, we need two. We initialize
2287 most things with sane defaults, to keep the code below somewhat
2288 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002289 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2290 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002291 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002293 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002294 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002295 struct i2c_msg msg[2] = {
2296 {
2297 .addr = addr,
2298 .flags = flags,
2299 .len = 1,
2300 .buf = msgbuf0,
2301 }, {
2302 .addr = addr,
2303 .flags = flags | I2C_M_RD,
2304 .len = 0,
2305 .buf = msgbuf1,
2306 },
2307 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002310 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 case I2C_SMBUS_QUICK:
2312 msg[0].len = 0;
2313 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002314 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2315 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 num = 1;
2317 break;
2318 case I2C_SMBUS_BYTE:
2319 if (read_write == I2C_SMBUS_READ) {
2320 /* Special case: only a read! */
2321 msg[0].flags = I2C_M_RD | flags;
2322 num = 1;
2323 }
2324 break;
2325 case I2C_SMBUS_BYTE_DATA:
2326 if (read_write == I2C_SMBUS_READ)
2327 msg[1].len = 1;
2328 else {
2329 msg[0].len = 2;
2330 msgbuf0[1] = data->byte;
2331 }
2332 break;
2333 case I2C_SMBUS_WORD_DATA:
2334 if (read_write == I2C_SMBUS_READ)
2335 msg[1].len = 2;
2336 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002337 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002339 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 break;
2342 case I2C_SMBUS_PROC_CALL:
2343 num = 2; /* Special case */
2344 read_write = I2C_SMBUS_READ;
2345 msg[0].len = 3;
2346 msg[1].len = 2;
2347 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002348 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002352 msg[1].flags |= I2C_M_RECV_LEN;
2353 msg[1].len = 1; /* block length will be added by
2354 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 } else {
2356 msg[0].len = data->block[0] + 2;
2357 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002358 dev_err(&adapter->dev,
2359 "Invalid block write size %d\n",
2360 data->block[0]);
2361 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002363 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 msgbuf0[i] = data->block[i-1];
2365 }
2366 break;
2367 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002368 num = 2; /* Another special case */
2369 read_write = I2C_SMBUS_READ;
2370 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002371 dev_err(&adapter->dev,
2372 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002373 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002374 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002375 }
2376 msg[0].len = data->block[0] + 2;
2377 for (i = 1; i < msg[0].len; i++)
2378 msgbuf0[i] = data->block[i-1];
2379 msg[1].flags |= I2C_M_RECV_LEN;
2380 msg[1].len = 1; /* block length will be added by
2381 the underlying bus driver */
2382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 case I2C_SMBUS_I2C_BLOCK_DATA:
2384 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002385 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 } else {
2387 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002388 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002389 dev_err(&adapter->dev,
2390 "Invalid block write size %d\n",
2391 data->block[0]);
2392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 for (i = 1; i <= data->block[0]; i++)
2395 msgbuf0[i] = data->block[i];
2396 }
2397 break;
2398 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002399 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2400 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
Jean Delvare421ef472005-10-26 21:28:55 +02002403 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2404 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2405 if (i) {
2406 /* Compute PEC if first message is a write */
2407 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002408 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002409 i2c_smbus_add_pec(&msg[0]);
2410 else /* Write followed by read */
2411 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2412 }
2413 /* Ask for PEC if last message is a read */
2414 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002415 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002416 }
2417
David Brownell24a5bb72008-07-14 22:38:23 +02002418 status = i2c_transfer(adapter, msg, num);
2419 if (status < 0)
2420 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Jean Delvare421ef472005-10-26 21:28:55 +02002422 /* Check PEC if last message is a read */
2423 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002424 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2425 if (status < 0)
2426 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002427 }
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002430 switch (size) {
2431 case I2C_SMBUS_BYTE:
2432 data->byte = msgbuf0[0];
2433 break;
2434 case I2C_SMBUS_BYTE_DATA:
2435 data->byte = msgbuf1[0];
2436 break;
2437 case I2C_SMBUS_WORD_DATA:
2438 case I2C_SMBUS_PROC_CALL:
2439 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2440 break;
2441 case I2C_SMBUS_I2C_BLOCK_DATA:
2442 for (i = 0; i < data->block[0]; i++)
2443 data->block[i+1] = msgbuf1[i];
2444 break;
2445 case I2C_SMBUS_BLOCK_DATA:
2446 case I2C_SMBUS_BLOCK_PROC_CALL:
2447 for (i = 0; i < msgbuf1[0] + 1; i++)
2448 data->block[i] = msgbuf1[i];
2449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 return 0;
2452}
2453
David Brownella1cdeda2008-07-14 22:38:24 +02002454/**
2455 * i2c_smbus_xfer - execute SMBus protocol operations
2456 * @adapter: Handle to I2C bus
2457 * @addr: Address of SMBus slave on that bus
2458 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2459 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2460 * @command: Byte interpreted by slave, for protocols which use such bytes
2461 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2462 * @data: Data to be read or written
2463 *
2464 * This executes an SMBus protocol operation, and returns a negative
2465 * errno code else zero on success.
2466 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002467s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002468 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002469 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002471 unsigned long orig_jiffies;
2472 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002475 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002478 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002479
2480 /* Retry automatically on arbitration loss */
2481 orig_jiffies = jiffies;
2482 for (res = 0, try = 0; try <= adapter->retries; try++) {
2483 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2484 read_write, command,
2485 protocol, data);
2486 if (res != -EAGAIN)
2487 break;
2488 if (time_after(jiffies,
2489 orig_jiffies + adapter->timeout))
2490 break;
2491 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002492 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002494 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2495 return res;
2496 /*
2497 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2498 * implement native support for the SMBus operation.
2499 */
2500 }
2501
2502 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2503 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2508MODULE_DESCRIPTION("I2C-Bus main module");
2509MODULE_LICENSE("GPL");