blob: c12299fd8cca33901eea01b0b0a5786f5f500a01 [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}
Mark Brown0001a0c2015-04-15 19:18:39 +0100216EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530217
218int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
219{
220 int ret;
221
222 ret = i2c_get_gpios_for_recovery(adap);
223 if (ret)
224 return ret;
225
226 ret = i2c_generic_recovery(adap);
227 i2c_put_gpios_for_recovery(adap);
228
229 return ret;
230}
Mark Brown0001a0c2015-04-15 19:18:39 +0100231EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530232
233int i2c_recover_bus(struct i2c_adapter *adap)
234{
235 if (!adap->bus_recovery_info)
236 return -EOPNOTSUPP;
237
238 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
239 return adap->bus_recovery_info->recover_bus(adap);
240}
Mark Brown0001a0c2015-04-15 19:18:39 +0100241EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int i2c_device_probe(struct device *dev)
244{
Jean Delvare51298d12009-09-18 22:45:45 +0200245 struct i2c_client *client = i2c_verify_client(dev);
246 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100247 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200248
Jean Delvare51298d12009-09-18 22:45:45 +0200249 if (!client)
250 return 0;
251
252 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200253 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200254 return -ENODEV;
255 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200256 if (!device_can_wakeup(&client->dev))
257 device_init_wakeup(&client->dev,
258 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200259 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200260
Jean Delvaree0457442008-07-14 22:38:30 +0200261 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200262 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100263 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200264 i2c_set_clientdata(client, NULL);
265 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100266 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static int i2c_device_remove(struct device *dev)
270{
Jean Delvare51298d12009-09-18 22:45:45 +0200271 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200272 struct i2c_driver *driver;
273 int status;
274
Jean Delvare51298d12009-09-18 22:45:45 +0200275 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200276 return 0;
277
278 driver = to_i2c_driver(dev->driver);
279 if (driver->remove) {
280 dev_dbg(dev, "remove\n");
281 status = driver->remove(client);
282 } else {
283 dev->driver = NULL;
284 status = 0;
285 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200286 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200287 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200288 i2c_set_clientdata(client, NULL);
289 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
David Brownellf37dd802007-02-13 22:09:00 +0100293static void i2c_device_shutdown(struct device *dev)
294{
Jean Delvare51298d12009-09-18 22:45:45 +0200295 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100296 struct i2c_driver *driver;
297
Jean Delvare51298d12009-09-18 22:45:45 +0200298 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100299 return;
300 driver = to_i2c_driver(dev->driver);
301 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200302 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100303}
304
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200305#ifdef CONFIG_PM_SLEEP
306static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100307{
Jean Delvare51298d12009-09-18 22:45:45 +0200308 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100309 struct i2c_driver *driver;
310
Jean Delvare51298d12009-09-18 22:45:45 +0200311 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100312 return 0;
313 driver = to_i2c_driver(dev->driver);
314 if (!driver->suspend)
315 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200316 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100317}
318
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200319static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100320{
Jean Delvare51298d12009-09-18 22:45:45 +0200321 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100322 struct i2c_driver *driver;
323
Jean Delvare51298d12009-09-18 22:45:45 +0200324 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100325 return 0;
326 driver = to_i2c_driver(dev->driver);
327 if (!driver->resume)
328 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200329 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100330}
331
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200332static int i2c_device_pm_suspend(struct device *dev)
333{
334 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
335
Mark Brownd529de22011-01-14 22:03:49 +0100336 if (pm)
337 return pm_generic_suspend(dev);
338 else
339 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200340}
341
342static int i2c_device_pm_resume(struct device *dev)
343{
344 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200345
346 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100347 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200348 else
Mark Brownd529de22011-01-14 22:03:49 +0100349 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200350}
351
352static int i2c_device_pm_freeze(struct device *dev)
353{
354 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
355
Mark Brownd529de22011-01-14 22:03:49 +0100356 if (pm)
357 return pm_generic_freeze(dev);
358 else
359 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200360}
361
362static int i2c_device_pm_thaw(struct device *dev)
363{
364 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
365
Mark Brownd529de22011-01-14 22:03:49 +0100366 if (pm)
367 return pm_generic_thaw(dev);
368 else
369 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200370}
371
372static int i2c_device_pm_poweroff(struct device *dev)
373{
374 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
375
Mark Brownd529de22011-01-14 22:03:49 +0100376 if (pm)
377 return pm_generic_poweroff(dev);
378 else
379 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200380}
381
382static int i2c_device_pm_restore(struct device *dev)
383{
384 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200385
386 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100387 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200388 else
Mark Brownd529de22011-01-14 22:03:49 +0100389 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200390}
391#else /* !CONFIG_PM_SLEEP */
392#define i2c_device_pm_suspend NULL
393#define i2c_device_pm_resume NULL
394#define i2c_device_pm_freeze NULL
395#define i2c_device_pm_thaw NULL
396#define i2c_device_pm_poweroff NULL
397#define i2c_device_pm_restore NULL
398#endif /* !CONFIG_PM_SLEEP */
399
David Brownell9c1600e2007-05-01 23:26:31 +0200400static void i2c_client_dev_release(struct device *dev)
401{
402 kfree(to_i2c_client(dev));
403}
404
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100405static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200406show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200407{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200408 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
409 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200410}
411
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100412static ssize_t
413show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200414{
415 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200416 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200417}
418
Jean Delvare4f8cf822009-09-18 22:45:46 +0200419static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200420static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
421
422static struct attribute *i2c_dev_attrs[] = {
423 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200424 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200425 &dev_attr_modalias.attr,
426 NULL
427};
428
429static struct attribute_group i2c_dev_attr_group = {
430 .attrs = i2c_dev_attrs,
431};
432
433static const struct attribute_group *i2c_dev_attr_groups[] = {
434 &i2c_dev_attr_group,
435 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200436};
437
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100438static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100439 .suspend = i2c_device_pm_suspend,
440 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200441 .freeze = i2c_device_pm_freeze,
442 .thaw = i2c_device_pm_thaw,
443 .poweroff = i2c_device_pm_poweroff,
444 .restore = i2c_device_pm_restore,
445 SET_RUNTIME_PM_OPS(
446 pm_generic_runtime_suspend,
447 pm_generic_runtime_resume,
448 pm_generic_runtime_idle
449 )
sonic zhang54067ee2009-12-14 21:17:30 +0100450};
451
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200452struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100453 .name = "i2c",
454 .match = i2c_device_match,
455 .probe = i2c_device_probe,
456 .remove = i2c_device_remove,
457 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100458 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000459};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200460EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000461
Jean Delvare51298d12009-09-18 22:45:45 +0200462static struct device_type i2c_client_type = {
463 .groups = i2c_dev_attr_groups,
464 .uevent = i2c_device_uevent,
465 .release = i2c_client_dev_release,
466};
467
David Brownell9b766b82008-01-27 18:14:51 +0100468
469/**
470 * i2c_verify_client - return parameter as i2c_client, or NULL
471 * @dev: device, probably from some driver model iterator
472 *
473 * When traversing the driver model tree, perhaps using driver model
474 * iterators like @device_for_each_child(), you can't assume very much
475 * about the nodes you find. Use this function to avoid oopses caused
476 * by wrongly treating some non-I2C device as an i2c_client.
477 */
478struct i2c_client *i2c_verify_client(struct device *dev)
479{
Jean Delvare51298d12009-09-18 22:45:45 +0200480 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100481 ? to_i2c_client(dev)
482 : NULL;
483}
484EXPORT_SYMBOL(i2c_verify_client);
485
486
Jean Delvare3a89db52010-06-03 11:33:52 +0200487/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300488 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200489static int i2c_check_client_addr_validity(const struct i2c_client *client)
490{
491 if (client->flags & I2C_CLIENT_TEN) {
492 /* 10-bit address, all values are valid */
493 if (client->addr > 0x3ff)
494 return -EINVAL;
495 } else {
496 /* 7-bit address, reject the general call address */
497 if (client->addr == 0x00 || client->addr > 0x7f)
498 return -EINVAL;
499 }
500 return 0;
501}
502
Jean Delvare656b8762010-06-03 11:33:53 +0200503/* And this is a strict address validity check, used when probing. If a
504 * device uses a reserved address, then it shouldn't be probed. 7-bit
505 * addressing is assumed, 10-bit address devices are rare and should be
506 * explicitly enumerated. */
507static int i2c_check_addr_validity(unsigned short addr)
508{
509 /*
510 * Reserved addresses per I2C specification:
511 * 0x00 General call address / START byte
512 * 0x01 CBUS address
513 * 0x02 Reserved for different bus format
514 * 0x03 Reserved for future purposes
515 * 0x04-0x07 Hs-mode master code
516 * 0x78-0x7b 10-bit slave addressing
517 * 0x7c-0x7f Reserved for future purposes
518 */
519 if (addr < 0x08 || addr > 0x77)
520 return -EINVAL;
521 return 0;
522}
523
Jean Delvare3b5f7942010-06-03 11:33:55 +0200524static int __i2c_check_addr_busy(struct device *dev, void *addrp)
525{
526 struct i2c_client *client = i2c_verify_client(dev);
527 int addr = *(int *)addrp;
528
529 if (client && client->addr == addr)
530 return -EBUSY;
531 return 0;
532}
533
Michael Lawnick08263742010-08-11 18:21:02 +0200534/* walk up mux tree */
535static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
536{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200537 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200538 int result;
539
540 result = device_for_each_child(&adapter->dev, &addr,
541 __i2c_check_addr_busy);
542
Jean Delvare97cc4d42010-10-24 18:16:57 +0200543 if (!result && parent)
544 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200545
546 return result;
547}
548
549/* recurse down mux tree */
550static int i2c_check_mux_children(struct device *dev, void *addrp)
551{
552 int result;
553
554 if (dev->type == &i2c_adapter_type)
555 result = device_for_each_child(dev, addrp,
556 i2c_check_mux_children);
557 else
558 result = __i2c_check_addr_busy(dev, addrp);
559
560 return result;
561}
562
Jean Delvare3b5f7942010-06-03 11:33:55 +0200563static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
564{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200565 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200566 int result = 0;
567
Jean Delvare97cc4d42010-10-24 18:16:57 +0200568 if (parent)
569 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200570
571 if (!result)
572 result = device_for_each_child(&adapter->dev, &addr,
573 i2c_check_mux_children);
574
575 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200576}
577
David Brownell9c1600e2007-05-01 23:26:31 +0200578/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200579 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
580 * @adapter: Target I2C bus segment
581 */
582void i2c_lock_adapter(struct i2c_adapter *adapter)
583{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200584 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
585
586 if (parent)
587 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200588 else
589 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200590}
591EXPORT_SYMBOL_GPL(i2c_lock_adapter);
592
593/**
594 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
595 * @adapter: Target I2C bus segment
596 */
597static int i2c_trylock_adapter(struct i2c_adapter *adapter)
598{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200599 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
600
601 if (parent)
602 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200603 else
604 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200605}
606
607/**
608 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
609 * @adapter: Target I2C bus segment
610 */
611void i2c_unlock_adapter(struct i2c_adapter *adapter)
612{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200613 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
614
615 if (parent)
616 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200617 else
618 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200619}
620EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
621
622/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200623 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200624 * @adap: the adapter managing the device
625 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200626 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200627 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200628 * Create an i2c device. Binding is handled through driver model
629 * probe()/remove() methods. A driver may be bound to this device when we
630 * return from this function, or any later moment (e.g. maybe hotplugging will
631 * load the driver module). This call is not appropriate for use by mainboard
632 * initialization logic, which usually runs during an arch_initcall() long
633 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200634 *
635 * This returns the new i2c client, which may be saved for later use with
636 * i2c_unregister_device(); or NULL to indicate an error.
637 */
638struct i2c_client *
639i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
640{
641 struct i2c_client *client;
642 int status;
643
644 client = kzalloc(sizeof *client, GFP_KERNEL);
645 if (!client)
646 return NULL;
647
648 client->adapter = adap;
649
650 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200651
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200652 if (info->archdata)
653 client->dev.archdata = *info->archdata;
654
Marc Pignatee354252008-08-28 08:33:22 +0200655 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200656 client->addr = info->addr;
657 client->irq = info->irq;
658
David Brownell9c1600e2007-05-01 23:26:31 +0200659 strlcpy(client->name, info->type, sizeof(client->name));
660
Jean Delvare3a89db52010-06-03 11:33:52 +0200661 /* Check for address validity */
662 status = i2c_check_client_addr_validity(client);
663 if (status) {
664 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
665 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
666 goto out_err_silent;
667 }
668
Jean Delvaref8a227e2009-06-19 16:58:18 +0200669 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200670 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200671 if (status)
672 goto out_err;
673
674 client->dev.parent = &client->adapter->dev;
675 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200676 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700677 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100678 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200679
Jean Delvarecbb44512011-11-23 11:33:07 +0100680 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200681 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100682 client->addr | ((client->flags & I2C_CLIENT_TEN)
683 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200684 status = device_register(&client->dev);
685 if (status)
686 goto out_err;
687
Jean Delvaref8a227e2009-06-19 16:58:18 +0200688 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
689 client->name, dev_name(&client->dev));
690
David Brownell9c1600e2007-05-01 23:26:31 +0200691 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200692
693out_err:
694 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
695 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200696out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200697 kfree(client);
698 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200699}
700EXPORT_SYMBOL_GPL(i2c_new_device);
701
702
703/**
704 * i2c_unregister_device - reverse effect of i2c_new_device()
705 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200706 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200707 */
708void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200709{
Pantelis Antoniou6f491ca2015-01-15 20:33:18 +0200710#if IS_ENABLED(CONFIG_OF_DYNAMIC)
Pantelis Antoniou5722d512015-01-15 18:33:55 +0200711 if (client->dev.of_node)
712 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Pantelis Antoniou6f491ca2015-01-15 20:33:18 +0200713#endif
David Brownella1d9e6e2007-05-01 23:26:30 +0200714 device_unregister(&client->dev);
715}
David Brownell9c1600e2007-05-01 23:26:31 +0200716EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200717
718
Jean Delvare60b129d2008-05-11 20:37:06 +0200719static const struct i2c_device_id dummy_id[] = {
720 { "dummy", 0 },
721 { },
722};
723
Jean Delvared2653e92008-04-29 23:11:39 +0200724static int dummy_probe(struct i2c_client *client,
725 const struct i2c_device_id *id)
726{
727 return 0;
728}
729
730static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100731{
732 return 0;
733}
734
735static struct i2c_driver dummy_driver = {
736 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200737 .probe = dummy_probe,
738 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200739 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100740};
741
742/**
743 * i2c_new_dummy - return a new i2c device bound to a dummy driver
744 * @adapter: the adapter managing the device
745 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100746 * Context: can sleep
747 *
748 * This returns an I2C client bound to the "dummy" driver, intended for use
749 * with devices that consume multiple addresses. Examples of such chips
750 * include various EEPROMS (like 24c04 and 24c08 models).
751 *
752 * These dummy devices have two main uses. First, most I2C and SMBus calls
753 * except i2c_transfer() need a client handle; the dummy will be that handle.
754 * And second, this prevents the specified address from being bound to a
755 * different driver.
756 *
757 * This returns the new i2c client, which should be saved for later use with
758 * i2c_unregister_device(); or NULL to indicate an error.
759 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100760struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100761{
762 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200763 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100764 };
765
David Brownelle9f13732008-01-27 18:14:52 +0100766 return i2c_new_device(adapter, &info);
767}
768EXPORT_SYMBOL_GPL(i2c_new_dummy);
769
David Brownellf37dd802007-02-13 22:09:00 +0100770/* ------------------------------------------------------------------------- */
771
David Brownell16ffadf2007-05-01 23:26:28 +0200772/* I2C bus adapters -- one roots each I2C or SMBUS segment */
773
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200774static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
David Brownellef2c83212007-05-01 23:26:28 +0200776 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 complete(&adap->dev_released);
778}
779
Jean Delvare99cd8e22009-06-19 16:58:20 +0200780/*
Jean Delvare390946b2012-09-10 10:14:02 +0200781 * This function is only needed for mutex_lock_nested, so it is never
782 * called unless locking correctness checking is enabled. Thus we
783 * make it inline to avoid a compiler warning. That's what gcc ends up
784 * doing anyway.
785 */
786static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
787{
788 unsigned int depth = 0;
789
790 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
791 depth++;
792
793 return depth;
794}
795
796/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200797 * Let users instantiate I2C devices through sysfs. This can be used when
798 * platform initialization code doesn't contain the proper data for
799 * whatever reason. Also useful for drivers that do device detection and
800 * detection fails, either because the device uses an unexpected address,
801 * or this is a compatible device with different ID register values.
802 *
803 * Parameter checking may look overzealous, but we really don't want
804 * the user to provide incorrect parameters.
805 */
806static ssize_t
807i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
808 const char *buf, size_t count)
809{
810 struct i2c_adapter *adap = to_i2c_adapter(dev);
811 struct i2c_board_info info;
812 struct i2c_client *client;
813 char *blank, end;
814 int res;
815
Jean Delvare99cd8e22009-06-19 16:58:20 +0200816 memset(&info, 0, sizeof(struct i2c_board_info));
817
818 blank = strchr(buf, ' ');
819 if (!blank) {
820 dev_err(dev, "%s: Missing parameters\n", "new_device");
821 return -EINVAL;
822 }
823 if (blank - buf > I2C_NAME_SIZE - 1) {
824 dev_err(dev, "%s: Invalid device name\n", "new_device");
825 return -EINVAL;
826 }
827 memcpy(info.type, buf, blank - buf);
828
829 /* Parse remaining parameters, reject extra parameters */
830 res = sscanf(++blank, "%hi%c", &info.addr, &end);
831 if (res < 1) {
832 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
833 return -EINVAL;
834 }
835 if (res > 1 && end != '\n') {
836 dev_err(dev, "%s: Extra parameters\n", "new_device");
837 return -EINVAL;
838 }
839
Jean Delvare99cd8e22009-06-19 16:58:20 +0200840 client = i2c_new_device(adap, &info);
841 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200842 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200843
844 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200845 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200846 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200847 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200848 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
849 info.type, info.addr);
850
851 return count;
852}
853
854/*
855 * And of course let the users delete the devices they instantiated, if
856 * they got it wrong. This interface can only be used to delete devices
857 * instantiated by i2c_sysfs_new_device above. This guarantees that we
858 * don't delete devices to which some kernel code still has references.
859 *
860 * Parameter checking may look overzealous, but we really don't want
861 * the user to delete the wrong device.
862 */
863static ssize_t
864i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
865 const char *buf, size_t count)
866{
867 struct i2c_adapter *adap = to_i2c_adapter(dev);
868 struct i2c_client *client, *next;
869 unsigned short addr;
870 char end;
871 int res;
872
873 /* Parse parameters, reject extra parameters */
874 res = sscanf(buf, "%hi%c", &addr, &end);
875 if (res < 1) {
876 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
877 return -EINVAL;
878 }
879 if (res > 1 && end != '\n') {
880 dev_err(dev, "%s: Extra parameters\n", "delete_device");
881 return -EINVAL;
882 }
883
884 /* Make sure the device was added through sysfs */
885 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200886 mutex_lock_nested(&adap->userspace_clients_lock,
887 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200888 list_for_each_entry_safe(client, next, &adap->userspace_clients,
889 detected) {
890 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200891 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
892 "delete_device", client->name, client->addr);
893
894 list_del(&client->detected);
895 i2c_unregister_device(client);
896 res = count;
897 break;
898 }
899 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200900 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200901
902 if (res < 0)
903 dev_err(dev, "%s: Can't find device in list\n",
904 "delete_device");
905 return res;
906}
907
Jean Delvare4f8cf822009-09-18 22:45:46 +0200908static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200909static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
910 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200911
912static struct attribute *i2c_adapter_attrs[] = {
913 &dev_attr_name.attr,
914 &dev_attr_new_device.attr,
915 &dev_attr_delete_device.attr,
916 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200917};
918
Jean Delvare4f8cf822009-09-18 22:45:46 +0200919static struct attribute_group i2c_adapter_attr_group = {
920 .attrs = i2c_adapter_attrs,
921};
922
923static const struct attribute_group *i2c_adapter_attr_groups[] = {
924 &i2c_adapter_attr_group,
925 NULL
926};
927
Michael Lawnick08263742010-08-11 18:21:02 +0200928struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200929 .groups = i2c_adapter_attr_groups,
930 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200931};
Michael Lawnick08263742010-08-11 18:21:02 +0200932EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stephen Warren643dd092012-04-17 12:43:33 -0600934/**
935 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
936 * @dev: device, probably from some driver model iterator
937 *
938 * When traversing the driver model tree, perhaps using driver model
939 * iterators like @device_for_each_child(), you can't assume very much
940 * about the nodes you find. Use this function to avoid oopses caused
941 * by wrongly treating some non-I2C device as an i2c_adapter.
942 */
943struct i2c_adapter *i2c_verify_adapter(struct device *dev)
944{
945 return (dev->type == &i2c_adapter_type)
946 ? to_i2c_adapter(dev)
947 : NULL;
948}
949EXPORT_SYMBOL(i2c_verify_adapter);
950
Jean Delvare2bb50952009-09-18 22:45:46 +0200951#ifdef CONFIG_I2C_COMPAT
952static struct class_compat *i2c_adapter_compat_class;
953#endif
954
David Brownell9c1600e2007-05-01 23:26:31 +0200955static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
956{
957 struct i2c_devinfo *devinfo;
958
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200959 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200960 list_for_each_entry(devinfo, &__i2c_board_list, list) {
961 if (devinfo->busnum == adapter->nr
962 && !i2c_new_device(adapter,
963 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100964 dev_err(&adapter->dev,
965 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200966 devinfo->board_info.addr);
967 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200968 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200969}
970
Wolfram Sang7f427042013-07-11 12:56:15 +0100971/* OF support code */
972
973#if IS_ENABLED(CONFIG_OF)
Pantelis Antoniou4edb7632014-10-28 22:36:02 +0200974static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
975 struct device_node *node)
976{
977 struct i2c_client *result;
978 struct i2c_board_info info = {};
979 struct dev_archdata dev_ad = {};
980 const __be32 *addr;
981 int len;
982
983 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
984
985 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
986 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
987 node->full_name);
988 return ERR_PTR(-EINVAL);
989 }
990
991 addr = of_get_property(node, "reg", &len);
992 if (!addr || (len < sizeof(int))) {
993 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
994 node->full_name);
995 return ERR_PTR(-EINVAL);
996 }
997
998 info.addr = be32_to_cpup(addr);
999 if (info.addr > (1 << 10) - 1) {
1000 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1001 info.addr, node->full_name);
1002 return ERR_PTR(-EINVAL);
1003 }
1004
1005 info.irq = irq_of_parse_and_map(node, 0);
1006 info.of_node = of_node_get(node);
1007 info.archdata = &dev_ad;
1008
1009 if (of_get_property(node, "wakeup-source", NULL))
1010 info.flags |= I2C_CLIENT_WAKE;
1011
1012 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1013
1014 result = i2c_new_device(adap, &info);
1015 if (result == NULL) {
1016 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1017 node->full_name);
1018 of_node_put(node);
1019 irq_dispose_mapping(info.irq);
1020 return ERR_PTR(-EINVAL);
1021 }
1022 return result;
1023}
1024
Wolfram Sang7f427042013-07-11 12:56:15 +01001025static void internal_of_i2c_register_devices(struct i2c_adapter *adap)
1026{
Wolfram Sang7f427042013-07-11 12:56:15 +01001027 struct device_node *node;
1028
1029 /* Only register child devices if the adapter has a node pointer set */
1030 if (!adap->dev.of_node)
1031 return;
1032
1033 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1034
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001035 for_each_available_child_of_node(adap->dev.of_node, node) {
1036 if (of_node_test_and_set_flag(node, OF_POPULATED))
1037 continue;
Pantelis Antoniou4edb7632014-10-28 22:36:02 +02001038 of_i2c_register_device(adap, node);
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001039 }
Wolfram Sang7f427042013-07-11 12:56:15 +01001040}
1041
1042static int of_dev_node_match(struct device *dev, void *data)
1043{
1044 return dev->of_node == data;
1045}
1046
1047/* must call put_device() when done with returned i2c_client device */
1048struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1049{
1050 struct device *dev;
1051
1052 dev = bus_find_device(&i2c_bus_type, NULL, node,
1053 of_dev_node_match);
1054 if (!dev)
1055 return NULL;
1056
1057 return i2c_verify_client(dev);
1058}
1059EXPORT_SYMBOL(of_find_i2c_device_by_node);
1060
1061/* must call put_device() when done with returned i2c_adapter device */
1062struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1063{
1064 struct device *dev;
1065
1066 dev = bus_find_device(&i2c_bus_type, NULL, node,
1067 of_dev_node_match);
1068 if (!dev)
1069 return NULL;
1070
1071 return i2c_verify_adapter(dev);
1072}
1073EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1074#else
1075static void internal_of_i2c_register_devices(struct i2c_adapter *adap) { }
1076#endif /* CONFIG_OF */
1077
Jean Delvare69b00892009-12-06 17:06:27 +01001078static int i2c_do_add_adapter(struct i2c_driver *driver,
1079 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001080{
Jean Delvare4735c982008-07-14 22:38:36 +02001081 /* Detect supported devices on that bus, and instantiate them */
1082 i2c_detect(adap, driver);
1083
1084 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001085 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001086 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1087 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001088 dev_warn(&adap->dev, "Please use another way to instantiate "
1089 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001090 /* We ignore the return code; if it fails, too bad */
1091 driver->attach_adapter(adap);
1092 }
1093 return 0;
1094}
1095
Jean Delvare69b00892009-12-06 17:06:27 +01001096static int __process_new_adapter(struct device_driver *d, void *data)
1097{
1098 return i2c_do_add_adapter(to_i2c_driver(d), data);
1099}
1100
David Brownell6e13e642007-05-01 23:26:31 +02001101static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Jean Delvared6703282010-08-11 18:20:59 +02001103 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
David Brownell1d0b19c2008-10-14 17:30:05 +02001105 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001106 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1107 res = -EAGAIN;
1108 goto out_list;
1109 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001110
Jean Delvare2236baa2010-11-15 22:40:38 +01001111 /* Sanity checks */
1112 if (unlikely(adap->name[0] == '\0')) {
1113 pr_err("i2c-core: Attempt to register an adapter with "
1114 "no name!\n");
1115 return -EINVAL;
1116 }
1117 if (unlikely(!adap->algo)) {
1118 pr_err("i2c-core: Attempt to register adapter '%s' with "
1119 "no algo!\n", adap->name);
1120 return -EINVAL;
1121 }
1122
Mika Kuoppala194684e2009-12-06 17:06:22 +01001123 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001124 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001125 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Jean Delvare8fcfef62009-03-28 21:34:43 +01001127 /* Set default timeout to 1 second if not already set */
1128 if (adap->timeout == 0)
1129 adap->timeout = HZ;
1130
Kay Sievers27d9c182009-01-07 14:29:16 +01001131 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001132 adap->dev.bus = &i2c_bus_type;
1133 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001134 res = device_register(&adap->dev);
1135 if (res)
1136 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001138 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1139
Jean Delvare2bb50952009-09-18 22:45:46 +02001140#ifdef CONFIG_I2C_COMPAT
1141 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1142 adap->dev.parent);
1143 if (res)
1144 dev_warn(&adap->dev,
1145 "Failed to create compatibility class link\n");
1146#endif
1147
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301148 /* bus recovery specific initialization */
1149 if (adap->bus_recovery_info) {
1150 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1151
1152 if (!bri->recover_bus) {
1153 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1154 adap->bus_recovery_info = NULL;
1155 goto exit_recovery;
1156 }
1157
1158 /* Generic GPIO recovery */
1159 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1160 if (!gpio_is_valid(bri->scl_gpio)) {
1161 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1162 adap->bus_recovery_info = NULL;
1163 goto exit_recovery;
1164 }
1165
1166 if (gpio_is_valid(bri->sda_gpio))
1167 bri->get_sda = get_sda_gpio_value;
1168 else
1169 bri->get_sda = NULL;
1170
1171 bri->get_scl = get_scl_gpio_value;
1172 bri->set_scl = set_scl_gpio_value;
1173 } else if (!bri->set_scl || !bri->get_scl) {
1174 /* Generic SCL recovery */
1175 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1176 adap->bus_recovery_info = NULL;
1177 }
1178 }
1179
1180exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001181 /* create pre-declared device nodes */
Wolfram Sang7f427042013-07-11 12:56:15 +01001182 internal_of_i2c_register_devices(adap);
1183
David Brownell6e13e642007-05-01 23:26:31 +02001184 if (adap->nr < __i2c_first_dynamic_bus_num)
1185 i2c_scan_static_board_info(adap);
1186
Jean Delvare4735c982008-07-14 22:38:36 +02001187 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001188 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001189 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001190 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001191
1192 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001193
Jean Delvareb119c6c2006-08-15 18:26:30 +02001194out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001195 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001196 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001197 mutex_unlock(&core_lock);
1198 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
David Brownell6e13e642007-05-01 23:26:31 +02001201/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001202 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1203 * @adap: the adapter to register (with adap->nr initialized)
1204 * Context: can sleep
1205 *
1206 * See i2c_add_numbered_adapter() for details.
1207 */
1208static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1209{
1210 int id;
1211
1212 mutex_lock(&core_lock);
1213 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1214 GFP_KERNEL);
1215 mutex_unlock(&core_lock);
1216 if (id < 0)
1217 return id == -ENOSPC ? -EBUSY : id;
1218
1219 return i2c_register_adapter(adap);
1220}
1221
1222/**
David Brownell6e13e642007-05-01 23:26:31 +02001223 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1224 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001225 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001226 *
1227 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001228 * doesn't matter or when its bus number is specified by an dt alias.
1229 * Examples of bases when the bus number doesn't matter: I2C adapters
1230 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001231 *
1232 * When this returns zero, a new bus number was allocated and stored
1233 * in adap->nr, and the specified adapter became available for clients.
1234 * Otherwise, a negative errno value is returned.
1235 */
1236int i2c_add_adapter(struct i2c_adapter *adapter)
1237{
Doug Andersonee5c2742013-03-01 08:57:31 -08001238 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001239 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001240
Doug Andersonee5c2742013-03-01 08:57:31 -08001241 if (dev->of_node) {
1242 id = of_alias_get_id(dev->of_node, "i2c");
1243 if (id >= 0) {
1244 adapter->nr = id;
1245 return __i2c_add_numbered_adapter(adapter);
1246 }
1247 }
1248
Jean Delvarecaada322008-01-27 18:14:49 +01001249 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001250 id = idr_alloc(&i2c_adapter_idr, adapter,
1251 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001252 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001253 if (id < 0)
1254 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001255
1256 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001257
David Brownell6e13e642007-05-01 23:26:31 +02001258 return i2c_register_adapter(adapter);
1259}
1260EXPORT_SYMBOL(i2c_add_adapter);
1261
1262/**
1263 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1264 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001265 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001266 *
1267 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001268 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1269 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001270 * is used to properly configure I2C devices.
1271 *
Grant Likely488bf312011-07-25 17:49:43 +02001272 * If the requested bus number is set to -1, then this function will behave
1273 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1274 *
David Brownell6e13e642007-05-01 23:26:31 +02001275 * If no devices have pre-been declared for this bus, then be sure to
1276 * register the adapter before any dynamically allocated ones. Otherwise
1277 * the required bus ID may not be available.
1278 *
1279 * When this returns zero, the specified adapter became available for
1280 * clients using the bus number provided in adap->nr. Also, the table
1281 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1282 * and the appropriate driver model device nodes are created. Otherwise, a
1283 * negative errno value is returned.
1284 */
1285int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1286{
Grant Likely488bf312011-07-25 17:49:43 +02001287 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1288 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001289
Doug Andersonee5c2742013-03-01 08:57:31 -08001290 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001291}
1292EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1293
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001294static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001295 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001296{
Jean Delvare4735c982008-07-14 22:38:36 +02001297 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001298
Jean Delvareacec2112009-03-28 21:34:40 +01001299 /* Remove the devices we created ourselves as the result of hardware
1300 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001301 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1302 if (client->adapter == adapter) {
1303 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1304 client->name, client->addr);
1305 list_del(&client->detected);
1306 i2c_unregister_device(client);
1307 }
1308 }
Jean Delvare026526f2008-01-27 18:14:49 +01001309}
1310
Jean Delvaree549c2b2009-06-19 16:58:19 +02001311static int __unregister_client(struct device *dev, void *dummy)
1312{
1313 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001314 if (client && strcmp(client->name, "dummy"))
1315 i2c_unregister_device(client);
1316 return 0;
1317}
1318
1319static int __unregister_dummy(struct device *dev, void *dummy)
1320{
1321 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001322 if (client)
1323 i2c_unregister_device(client);
1324 return 0;
1325}
1326
Jean Delvare69b00892009-12-06 17:06:27 +01001327static int __process_removed_adapter(struct device_driver *d, void *data)
1328{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001329 i2c_do_del_adapter(to_i2c_driver(d), data);
1330 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001331}
1332
David Brownelld64f73b2007-07-12 14:12:28 +02001333/**
1334 * i2c_del_adapter - unregister I2C adapter
1335 * @adap: the adapter being unregistered
1336 * Context: can sleep
1337 *
1338 * This unregisters an I2C adapter which was previously registered
1339 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1340 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001341void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001343 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001344 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001347 mutex_lock(&core_lock);
1348 found = idr_find(&i2c_adapter_idr, adap->nr);
1349 mutex_unlock(&core_lock);
1350 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001351 pr_debug("i2c-core: attempting to delete unregistered "
1352 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001353 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Jean Delvare026526f2008-01-27 18:14:49 +01001356 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001357 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001358 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001359 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001360 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001362 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001363 mutex_lock_nested(&adap->userspace_clients_lock,
1364 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001365 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1366 detected) {
1367 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1368 client->addr);
1369 list_del(&client->detected);
1370 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001371 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001372 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001373
Jean Delvaree549c2b2009-06-19 16:58:19 +02001374 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001375 * check the returned value. This is a two-pass process, because
1376 * we can't remove the dummy devices during the first pass: they
1377 * could have been instantiated by real devices wishing to clean
1378 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001379 device_for_each_child(&adap->dev, NULL, __unregister_client);
1380 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jean Delvare2bb50952009-09-18 22:45:46 +02001382#ifdef CONFIG_I2C_COMPAT
1383 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1384 adap->dev.parent);
1385#endif
1386
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001387 /* device name is gone after device_unregister */
1388 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* clean up the sysfs representation */
1391 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* wait for sysfs to drop all references */
1395 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
David Brownell6e13e642007-05-01 23:26:31 +02001397 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001398 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001400 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001402 /* Clear the device structure in case this adapter is ever going to be
1403 added again */
1404 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
David Brownellc0564602007-05-01 23:26:31 +02001406EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
David Brownell7b4fbc52007-05-01 23:26:30 +02001408/* ------------------------------------------------------------------------- */
1409
Jean Delvare7ae31482011-03-20 14:50:52 +01001410int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1411{
1412 int res;
1413
1414 mutex_lock(&core_lock);
1415 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1416 mutex_unlock(&core_lock);
1417
1418 return res;
1419}
1420EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1421
Jean Delvare69b00892009-12-06 17:06:27 +01001422static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001423{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001424 if (dev->type != &i2c_adapter_type)
1425 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001426 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001427}
1428
David Brownell7b4fbc52007-05-01 23:26:30 +02001429/*
1430 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001431 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001434int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001436 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
David Brownell1d0b19c2008-10-14 17:30:05 +02001438 /* Can't register until after driver model init */
1439 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1440 return -EAGAIN;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001443 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Jean Delvare729d6dd2009-06-19 16:58:18 +02001446 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001447 * will have called probe() for all matching-but-unbound devices.
1448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 res = driver_register(&driver->driver);
1450 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001451 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001452
Mark Brownf4e8db32011-01-14 22:03:50 +01001453 /* Drivers should switch to dev_pm_ops instead. */
1454 if (driver->suspend)
1455 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1456 driver->driver.name);
1457 if (driver->resume)
1458 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1459 driver->driver.name);
1460
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001461 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Jean Delvare4735c982008-07-14 22:38:36 +02001463 INIT_LIST_HEAD(&driver->clients);
1464 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001465 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001466
Jean Delvare7eebcb72006-02-05 23:28:21 +01001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001469EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jean Delvare69b00892009-12-06 17:06:27 +01001471static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001472{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001473 if (dev->type == &i2c_adapter_type)
1474 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1475 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001476}
1477
David Brownella1d9e6e2007-05-01 23:26:30 +02001478/**
1479 * i2c_del_driver - unregister I2C driver
1480 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001481 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001482 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001483void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Jean Delvare7ae31482011-03-20 14:50:52 +01001485 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001488 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
David Brownellc0564602007-05-01 23:26:31 +02001490EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
David Brownell7b4fbc52007-05-01 23:26:30 +02001492/* ------------------------------------------------------------------------- */
1493
Jean Delvaree48d3312008-01-27 18:14:48 +01001494/**
1495 * i2c_use_client - increments the reference count of the i2c client structure
1496 * @client: the client being referenced
1497 *
1498 * Each live reference to a client should be refcounted. The driver model does
1499 * that automatically as part of driver binding, so that most drivers don't
1500 * need to do this explicitly: they hold a reference until they're unbound
1501 * from the device.
1502 *
1503 * A pointer to the client with the incremented reference counter is returned.
1504 */
1505struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
David Brownell6ea438e2008-07-14 22:38:24 +02001507 if (client && get_device(&client->dev))
1508 return client;
1509 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Jean Delvaree48d3312008-01-27 18:14:48 +01001513/**
1514 * i2c_release_client - release a use of the i2c client structure
1515 * @client: the client being no longer referenced
1516 *
1517 * Must be called when a user of a client is finished with it.
1518 */
1519void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
David Brownell6ea438e2008-07-14 22:38:24 +02001521 if (client)
1522 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
David Brownellc0564602007-05-01 23:26:31 +02001524EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
David Brownell9b766b82008-01-27 18:14:51 +01001526struct i2c_cmd_arg {
1527 unsigned cmd;
1528 void *arg;
1529};
1530
1531static int i2c_cmd(struct device *dev, void *_arg)
1532{
1533 struct i2c_client *client = i2c_verify_client(dev);
1534 struct i2c_cmd_arg *arg = _arg;
1535
1536 if (client && client->driver && client->driver->command)
1537 client->driver->command(client, arg->cmd, arg->arg);
1538 return 0;
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1542{
David Brownell9b766b82008-01-27 18:14:51 +01001543 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
David Brownell9b766b82008-01-27 18:14:51 +01001545 cmd_arg.cmd = cmd;
1546 cmd_arg.arg = arg;
1547 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
David Brownellc0564602007-05-01 23:26:31 +02001549EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001551#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1552static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1553 void *arg)
1554{
1555 struct of_reconfig_data *rd = arg;
1556 struct i2c_adapter *adap;
1557 struct i2c_client *client;
1558
1559 switch (of_reconfig_get_state_change(action, rd)) {
1560 case OF_RECONFIG_CHANGE_ADD:
1561 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1562 if (adap == NULL)
1563 return NOTIFY_OK; /* not for us */
1564
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001565 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1566 put_device(&adap->dev);
1567 return NOTIFY_OK;
1568 }
1569
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001570 client = of_i2c_register_device(adap, rd->dn);
1571 put_device(&adap->dev);
1572
1573 if (IS_ERR(client)) {
1574 pr_err("%s: failed to create for '%s'\n",
1575 __func__, rd->dn->full_name);
1576 return notifier_from_errno(PTR_ERR(client));
1577 }
1578 break;
1579 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou5722d512015-01-15 18:33:55 +02001580 /* already depopulated? */
1581 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1582 return NOTIFY_OK;
1583
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001584 /* find our device by node */
1585 client = of_find_i2c_device_by_node(rd->dn);
1586 if (client == NULL)
1587 return NOTIFY_OK; /* no? not meant for us */
1588
1589 /* unregister takes one ref away */
1590 i2c_unregister_device(client);
1591
1592 /* and put the reference of the find */
1593 put_device(&client->dev);
1594 break;
1595 }
1596
1597 return NOTIFY_OK;
1598}
1599static struct notifier_block i2c_of_notifier = {
1600 .notifier_call = of_i2c_notify,
1601};
1602#else
1603extern struct notifier_block i2c_of_notifier;
1604#endif /* CONFIG_OF_DYNAMIC */
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static int __init i2c_init(void)
1607{
1608 int retval;
1609
1610 retval = bus_register(&i2c_bus_type);
1611 if (retval)
1612 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001613#ifdef CONFIG_I2C_COMPAT
1614 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1615 if (!i2c_adapter_compat_class) {
1616 retval = -ENOMEM;
1617 goto bus_err;
1618 }
1619#endif
David Brownelle9f13732008-01-27 18:14:52 +01001620 retval = i2c_add_driver(&dummy_driver);
1621 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001622 goto class_err;
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001623
1624 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1625 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1626
David Brownelle9f13732008-01-27 18:14:52 +01001627 return 0;
1628
Jean Delvare2bb50952009-09-18 22:45:46 +02001629class_err:
1630#ifdef CONFIG_I2C_COMPAT
1631 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001632bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001633#endif
David Brownelle9f13732008-01-27 18:14:52 +01001634 bus_unregister(&i2c_bus_type);
1635 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638static void __exit i2c_exit(void)
1639{
Pantelis Antoniou9f2de292014-10-28 22:36:03 +02001640 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1641 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001642 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001643#ifdef CONFIG_I2C_COMPAT
1644 class_compat_unregister(i2c_adapter_compat_class);
1645#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 bus_unregister(&i2c_bus_type);
1647}
1648
David Brownella10f9e72008-10-14 17:30:06 +02001649/* We must initialize early, because some subsystems register i2c drivers
1650 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1651 */
1652postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653module_exit(i2c_exit);
1654
1655/* ----------------------------------------------------
1656 * the functional interface to the i2c busses.
1657 * ----------------------------------------------------
1658 */
1659
David Brownella1cdeda2008-07-14 22:38:24 +02001660/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001661 * __i2c_transfer - unlocked flavor of i2c_transfer
1662 * @adap: Handle to I2C bus
1663 * @msgs: One or more messages to execute before STOP is issued to
1664 * terminate the operation; each message begins with a START.
1665 * @num: Number of messages to be executed.
1666 *
1667 * Returns negative errno, else the number of messages executed.
1668 *
1669 * Adapter lock must be held when calling this function. No debug logging
1670 * takes place. adap->algo->master_xfer existence isn't checked.
1671 */
1672int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1673{
1674 unsigned long orig_jiffies;
1675 int ret, try;
1676
1677 /* Retry automatically on arbitration loss */
1678 orig_jiffies = jiffies;
1679 for (ret = 0, try = 0; try <= adap->retries; try++) {
1680 ret = adap->algo->master_xfer(adap, msgs, num);
1681 if (ret != -EAGAIN)
1682 break;
1683 if (time_after(jiffies, orig_jiffies + adap->timeout))
1684 break;
1685 }
1686
1687 return ret;
1688}
1689EXPORT_SYMBOL(__i2c_transfer);
1690
1691/**
David Brownella1cdeda2008-07-14 22:38:24 +02001692 * i2c_transfer - execute a single or combined I2C message
1693 * @adap: Handle to I2C bus
1694 * @msgs: One or more messages to execute before STOP is issued to
1695 * terminate the operation; each message begins with a START.
1696 * @num: Number of messages to be executed.
1697 *
1698 * Returns negative errno, else the number of messages executed.
1699 *
1700 * Note that there is no requirement that each message be sent to
1701 * the same slave address, although that is the most common model.
1702 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001703int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001705 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
David Brownella1cdeda2008-07-14 22:38:24 +02001707 /* REVISIT the fault reporting model here is weak:
1708 *
1709 * - When we get an error after receiving N bytes from a slave,
1710 * there is no way to report "N".
1711 *
1712 * - When we get a NAK after transmitting N bytes to a slave,
1713 * there is no way to report "N" ... or to let the master
1714 * continue executing the rest of this combined message, if
1715 * that's the appropriate response.
1716 *
1717 * - When for example "num" is two and we successfully complete
1718 * the first message but get an error part way through the
1719 * second, it's unclear whether that should be reported as
1720 * one (discarding status on the second message) or errno
1721 * (discarding status on the first one).
1722 */
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (adap->algo->master_xfer) {
1725#ifdef DEBUG
1726 for (ret = 0; ret < num; ret++) {
1727 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001728 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1729 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1730 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732#endif
1733
Mike Rapoportcea443a82008-01-27 18:14:50 +01001734 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001735 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001736 if (!ret)
1737 /* I2C activity is ongoing. */
1738 return -EAGAIN;
1739 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001740 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001741 }
1742
Jean Delvareb37d2a32012-06-29 07:47:19 -03001743 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001744 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 return ret;
1747 } else {
1748 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001749 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751}
David Brownellc0564602007-05-01 23:26:31 +02001752EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
David Brownella1cdeda2008-07-14 22:38:24 +02001754/**
1755 * i2c_master_send - issue a single I2C message in master transmit mode
1756 * @client: Handle to slave device
1757 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001758 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001759 *
1760 * Returns negative errno, or else the number of bytes written.
1761 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001762int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001765 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct i2c_msg msg;
1767
Jean Delvare815f55f2005-05-07 22:58:46 +02001768 msg.addr = client->addr;
1769 msg.flags = client->flags & I2C_M_TEN;
1770 msg.len = count;
1771 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001772
Jean Delvare815f55f2005-05-07 22:58:46 +02001773 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001775 /*
1776 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1777 * transmitted, else error code.
1778 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001779 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
David Brownellc0564602007-05-01 23:26:31 +02001781EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
David Brownella1cdeda2008-07-14 22:38:24 +02001783/**
1784 * i2c_master_recv - issue a single I2C message in master receive mode
1785 * @client: Handle to slave device
1786 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001787 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001788 *
1789 * Returns negative errno, or else the number of bytes read.
1790 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001791int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Farid Hammane7225acf2010-05-21 18:40:58 +02001793 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct i2c_msg msg;
1795 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jean Delvare815f55f2005-05-07 22:58:46 +02001797 msg.addr = client->addr;
1798 msg.flags = client->flags & I2C_M_TEN;
1799 msg.flags |= I2C_M_RD;
1800 msg.len = count;
1801 msg.buf = buf;
1802
1803 ret = i2c_transfer(adap, &msg, 1);
1804
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001805 /*
1806 * If everything went ok (i.e. 1 msg received), return #bytes received,
1807 * else error code.
1808 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001809 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
David Brownellc0564602007-05-01 23:26:31 +02001811EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/* ----------------------------------------------------
1814 * the i2c address scanning function
1815 * Will not work for 10-bit addresses!
1816 * ----------------------------------------------------
1817 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001818
Jean Delvare63e4e802010-06-03 11:33:51 +02001819/*
1820 * Legacy default probe function, mostly relevant for SMBus. The default
1821 * probe method is a quick write, but it is known to corrupt the 24RF08
1822 * EEPROMs due to a state machine bug, and could also irreversibly
1823 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1824 * we use a short byte read instead. Also, some bus drivers don't implement
1825 * quick write, so we fallback to a byte read in that case too.
1826 * On x86, there is another special case for FSC hardware monitoring chips,
1827 * which want regular byte reads (address 0x73.) Fortunately, these are the
1828 * only known chips using this I2C address on PC hardware.
1829 * Returns 1 if probe succeeded, 0 if not.
1830 */
1831static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1832{
1833 int err;
1834 union i2c_smbus_data dummy;
1835
1836#ifdef CONFIG_X86
1837 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1838 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1839 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1840 I2C_SMBUS_BYTE_DATA, &dummy);
1841 else
1842#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001843 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1844 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001845 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1846 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001847 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1848 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1849 I2C_SMBUS_BYTE, &dummy);
1850 else {
1851 dev_warn(&adap->dev, "No suitable probing method supported\n");
1852 err = -EOPNOTSUPP;
1853 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001854
1855 return err >= 0;
1856}
1857
Jean Delvareccfbbd02009-12-06 17:06:25 +01001858static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001859 struct i2c_driver *driver)
1860{
1861 struct i2c_board_info info;
1862 struct i2c_adapter *adapter = temp_client->adapter;
1863 int addr = temp_client->addr;
1864 int err;
1865
1866 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001867 err = i2c_check_addr_validity(addr);
1868 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001869 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1870 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001871 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001872 }
1873
1874 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001875 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001876 return 0;
1877
Jean Delvareccfbbd02009-12-06 17:06:25 +01001878 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001879 if (!i2c_default_probe(adapter, addr))
1880 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001881
1882 /* Finally call the custom detection function */
1883 memset(&info, 0, sizeof(struct i2c_board_info));
1884 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001885 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001886 if (err) {
1887 /* -ENODEV is returned if the detection fails. We catch it
1888 here as this isn't an error. */
1889 return err == -ENODEV ? 0 : err;
1890 }
1891
1892 /* Consistency check */
1893 if (info.type[0] == '\0') {
1894 dev_err(&adapter->dev, "%s detection function provided "
1895 "no name for 0x%x\n", driver->driver.name,
1896 addr);
1897 } else {
1898 struct i2c_client *client;
1899
1900 /* Detection succeeded, instantiate the device */
1901 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1902 info.type, info.addr);
1903 client = i2c_new_device(adapter, &info);
1904 if (client)
1905 list_add_tail(&client->detected, &driver->clients);
1906 else
1907 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1908 info.type, info.addr);
1909 }
1910 return 0;
1911}
1912
1913static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1914{
Jean Delvarec3813d62009-12-14 21:17:25 +01001915 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001916 struct i2c_client *temp_client;
1917 int i, err = 0;
1918 int adap_id = i2c_adapter_id(adapter);
1919
Jean Delvarec3813d62009-12-14 21:17:25 +01001920 address_list = driver->address_list;
1921 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001922 return 0;
1923
Jean Delvare51b54ba2010-10-24 18:16:58 +02001924 /* Stop here if the classes do not match */
1925 if (!(adapter->class & driver->class))
1926 return 0;
1927
Jean Delvare4735c982008-07-14 22:38:36 +02001928 /* Set up a temporary client to help detect callback */
1929 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1930 if (!temp_client)
1931 return -ENOMEM;
1932 temp_client->adapter = adapter;
1933
Jean Delvarec3813d62009-12-14 21:17:25 +01001934 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001935 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001936 "addr 0x%02x\n", adap_id, address_list[i]);
1937 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001938 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001939 if (unlikely(err))
1940 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001941 }
1942
Jean Delvare4735c982008-07-14 22:38:36 +02001943 kfree(temp_client);
1944 return err;
1945}
1946
Jean Delvared44f19d2010-08-11 18:20:57 +02001947int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1948{
1949 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1950 I2C_SMBUS_QUICK, NULL) >= 0;
1951}
1952EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1953
Jean Delvare12b5053a2007-05-01 23:26:31 +02001954struct i2c_client *
1955i2c_new_probed_device(struct i2c_adapter *adap,
1956 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001957 unsigned short const *addr_list,
1958 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001959{
1960 int i;
1961
Jean Delvare8031d792010-08-11 18:21:00 +02001962 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001963 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001964
Jean Delvare12b5053a2007-05-01 23:26:31 +02001965 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1966 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001967 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001968 dev_warn(&adap->dev, "Invalid 7-bit address "
1969 "0x%02x\n", addr_list[i]);
1970 continue;
1971 }
1972
1973 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001974 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001975 dev_dbg(&adap->dev, "Address 0x%02x already in "
1976 "use, not probing\n", addr_list[i]);
1977 continue;
1978 }
1979
Jean Delvare63e4e802010-06-03 11:33:51 +02001980 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001981 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001982 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001983 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001984
1985 if (addr_list[i] == I2C_CLIENT_END) {
1986 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1987 return NULL;
1988 }
1989
1990 info->addr = addr_list[i];
1991 return i2c_new_device(adap, info);
1992}
1993EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1994
Jean Delvared735b342011-03-20 14:50:52 +01001995struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001998
Jean Delvarecaada322008-01-27 18:14:49 +01001999 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002000 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002001 if (adapter && !try_module_get(adapter->owner))
2002 adapter = NULL;
2003
Jean Delvarecaada322008-01-27 18:14:49 +01002004 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002005 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
David Brownellc0564602007-05-01 23:26:31 +02002007EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009void i2c_put_adapter(struct i2c_adapter *adap)
2010{
2011 module_put(adap->owner);
2012}
David Brownellc0564602007-05-01 23:26:31 +02002013EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015/* The SMBus parts */
2016
David Brownell438d6c22006-12-10 21:21:31 +01002017#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002018static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002021
Farid Hammane7225acf2010-05-21 18:40:58 +02002022 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002023 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 data = data ^ POLY;
2025 data = data << 1;
2026 }
2027 return (u8)(data >> 8);
2028}
2029
Jean Delvare421ef472005-10-26 21:28:55 +02002030/* Incremental CRC8 over count bytes in the array pointed to by p */
2031static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 int i;
2034
Farid Hammane7225acf2010-05-21 18:40:58 +02002035 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002036 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return crc;
2038}
2039
Jean Delvare421ef472005-10-26 21:28:55 +02002040/* Assume a 7-bit address, which is reasonable for SMBus */
2041static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Jean Delvare421ef472005-10-26 21:28:55 +02002043 /* The address will be sent first */
2044 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2045 pec = i2c_smbus_pec(pec, &addr, 1);
2046
2047 /* The data buffer follows */
2048 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Jean Delvare421ef472005-10-26 21:28:55 +02002051/* Used for write only transactions */
2052static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Jean Delvare421ef472005-10-26 21:28:55 +02002054 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2055 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Jean Delvare421ef472005-10-26 21:28:55 +02002058/* Return <0 on CRC error
2059 If there was a write before this read (most cases) we need to take the
2060 partial CRC from the write part into account.
2061 Note that this function does modify the message (we need to decrease the
2062 message length to hide the CRC byte from the caller). */
2063static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Jean Delvare421ef472005-10-26 21:28:55 +02002065 u8 rpec = msg->buf[--msg->len];
2066 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (rpec != cpec) {
2069 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2070 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002071 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
David Brownell438d6c22006-12-10 21:21:31 +01002073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
David Brownella1cdeda2008-07-14 22:38:24 +02002076/**
2077 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2078 * @client: Handle to slave device
2079 *
2080 * This executes the SMBus "receive byte" protocol, returning negative errno
2081 * else the byte received from the device.
2082 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002083s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002086 int status;
2087
2088 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2089 I2C_SMBUS_READ, 0,
2090 I2C_SMBUS_BYTE, &data);
2091 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
David Brownellc0564602007-05-01 23:26:31 +02002093EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
David Brownella1cdeda2008-07-14 22:38:24 +02002095/**
2096 * i2c_smbus_write_byte - SMBus "send byte" protocol
2097 * @client: Handle to slave device
2098 * @value: Byte to be sent
2099 *
2100 * This executes the SMBus "send byte" protocol, returning negative errno
2101 * else zero on success.
2102 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002103s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Farid Hammane7225acf2010-05-21 18:40:58 +02002105 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002106 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
David Brownellc0564602007-05-01 23:26:31 +02002108EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
David Brownella1cdeda2008-07-14 22:38:24 +02002110/**
2111 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2112 * @client: Handle to slave device
2113 * @command: Byte interpreted by slave
2114 *
2115 * This executes the SMBus "read byte" protocol, returning negative errno
2116 * else a data byte received from the device.
2117 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002118s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002121 int status;
2122
2123 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2124 I2C_SMBUS_READ, command,
2125 I2C_SMBUS_BYTE_DATA, &data);
2126 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
David Brownellc0564602007-05-01 23:26:31 +02002128EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
David Brownella1cdeda2008-07-14 22:38:24 +02002130/**
2131 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2132 * @client: Handle to slave device
2133 * @command: Byte interpreted by slave
2134 * @value: Byte being written
2135 *
2136 * This executes the SMBus "write byte" protocol, returning negative errno
2137 * else zero on success.
2138 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002139s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2140 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 union i2c_smbus_data data;
2143 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002144 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2145 I2C_SMBUS_WRITE, command,
2146 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
David Brownellc0564602007-05-01 23:26:31 +02002148EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Brownella1cdeda2008-07-14 22:38:24 +02002150/**
2151 * i2c_smbus_read_word_data - SMBus "read word" protocol
2152 * @client: Handle to slave device
2153 * @command: Byte interpreted by slave
2154 *
2155 * This executes the SMBus "read word" protocol, returning negative errno
2156 * else a 16-bit unsigned "word" received from the device.
2157 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002158s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002161 int status;
2162
2163 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2164 I2C_SMBUS_READ, command,
2165 I2C_SMBUS_WORD_DATA, &data);
2166 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
David Brownellc0564602007-05-01 23:26:31 +02002168EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David Brownella1cdeda2008-07-14 22:38:24 +02002170/**
2171 * i2c_smbus_write_word_data - SMBus "write word" protocol
2172 * @client: Handle to slave device
2173 * @command: Byte interpreted by slave
2174 * @value: 16-bit "word" being written
2175 *
2176 * This executes the SMBus "write word" protocol, returning negative errno
2177 * else zero on success.
2178 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002179s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2180 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 union i2c_smbus_data data;
2183 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002184 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2185 I2C_SMBUS_WRITE, command,
2186 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
David Brownellc0564602007-05-01 23:26:31 +02002188EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
David Brownella64ec072007-10-13 23:56:31 +02002190/**
David Brownella1cdeda2008-07-14 22:38:24 +02002191 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002192 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002193 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002194 * @values: Byte array into which data will be read; big enough to hold
2195 * the data returned by the slave. SMBus allows at most 32 bytes.
2196 *
David Brownella1cdeda2008-07-14 22:38:24 +02002197 * This executes the SMBus "block read" protocol, returning negative errno
2198 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002199 *
2200 * Note that using this function requires that the client's adapter support
2201 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2202 * support this; its emulation through I2C messaging relies on a specific
2203 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2204 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002205s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002206 u8 *values)
2207{
2208 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002209 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002210
David Brownell24a5bb72008-07-14 22:38:23 +02002211 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2212 I2C_SMBUS_READ, command,
2213 I2C_SMBUS_BLOCK_DATA, &data);
2214 if (status)
2215 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002216
2217 memcpy(values, &data.block[1], data.block[0]);
2218 return data.block[0];
2219}
2220EXPORT_SYMBOL(i2c_smbus_read_block_data);
2221
David Brownella1cdeda2008-07-14 22:38:24 +02002222/**
2223 * i2c_smbus_write_block_data - SMBus "block write" protocol
2224 * @client: Handle to slave device
2225 * @command: Byte interpreted by slave
2226 * @length: Size of data block; SMBus allows at most 32 bytes
2227 * @values: Byte array which will be written.
2228 *
2229 * This executes the SMBus "block write" protocol, returning negative errno
2230 * else zero on success.
2231 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002232s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002233 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (length > I2C_SMBUS_BLOCK_MAX)
2238 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002240 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002241 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2242 I2C_SMBUS_WRITE, command,
2243 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
David Brownellc0564602007-05-01 23:26:31 +02002245EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002248s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002249 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002252 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002253
Jean Delvare4b2643d2007-07-12 14:12:29 +02002254 if (length > I2C_SMBUS_BLOCK_MAX)
2255 length = I2C_SMBUS_BLOCK_MAX;
2256 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002257 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2258 I2C_SMBUS_READ, command,
2259 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2260 if (status < 0)
2261 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002262
2263 memcpy(values, &data.block[1], data.block[0]);
2264 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
David Brownellc0564602007-05-01 23:26:31 +02002266EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Jean Delvare0cc43a12011-01-10 22:11:23 +01002268s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002269 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002270{
2271 union i2c_smbus_data data;
2272
2273 if (length > I2C_SMBUS_BLOCK_MAX)
2274 length = I2C_SMBUS_BLOCK_MAX;
2275 data.block[0] = length;
2276 memcpy(data.block + 1, values, length);
2277 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2278 I2C_SMBUS_WRITE, command,
2279 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2280}
David Brownellc0564602007-05-01 23:26:31 +02002281EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002282
David Brownell438d6c22006-12-10 21:21:31 +01002283/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002285static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2286 unsigned short flags,
2287 char read_write, u8 command, int size,
2288 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 /* So we need to generate a series of msgs. In the case of writing, we
2291 need to use only one message; when reading, we need two. We initialize
2292 most things with sane defaults, to keep the code below somewhat
2293 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002294 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2295 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002296 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002298 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002299 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002300 struct i2c_msg msg[2] = {
2301 {
2302 .addr = addr,
2303 .flags = flags,
2304 .len = 1,
2305 .buf = msgbuf0,
2306 }, {
2307 .addr = addr,
2308 .flags = flags | I2C_M_RD,
2309 .len = 0,
2310 .buf = msgbuf1,
2311 },
2312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002315 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 case I2C_SMBUS_QUICK:
2317 msg[0].len = 0;
2318 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002319 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2320 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 num = 1;
2322 break;
2323 case I2C_SMBUS_BYTE:
2324 if (read_write == I2C_SMBUS_READ) {
2325 /* Special case: only a read! */
2326 msg[0].flags = I2C_M_RD | flags;
2327 num = 1;
2328 }
2329 break;
2330 case I2C_SMBUS_BYTE_DATA:
2331 if (read_write == I2C_SMBUS_READ)
2332 msg[1].len = 1;
2333 else {
2334 msg[0].len = 2;
2335 msgbuf0[1] = data->byte;
2336 }
2337 break;
2338 case I2C_SMBUS_WORD_DATA:
2339 if (read_write == I2C_SMBUS_READ)
2340 msg[1].len = 2;
2341 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002342 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002344 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 break;
2347 case I2C_SMBUS_PROC_CALL:
2348 num = 2; /* Special case */
2349 read_write = I2C_SMBUS_READ;
2350 msg[0].len = 3;
2351 msg[1].len = 2;
2352 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002353 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 break;
2355 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002357 msg[1].flags |= I2C_M_RECV_LEN;
2358 msg[1].len = 1; /* block length will be added by
2359 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 } else {
2361 msg[0].len = data->block[0] + 2;
2362 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002363 dev_err(&adapter->dev,
2364 "Invalid block write size %d\n",
2365 data->block[0]);
2366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002368 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 msgbuf0[i] = data->block[i-1];
2370 }
2371 break;
2372 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002373 num = 2; /* Another special case */
2374 read_write = I2C_SMBUS_READ;
2375 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002376 dev_err(&adapter->dev,
2377 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002378 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002379 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002380 }
2381 msg[0].len = data->block[0] + 2;
2382 for (i = 1; i < msg[0].len; i++)
2383 msgbuf0[i] = data->block[i-1];
2384 msg[1].flags |= I2C_M_RECV_LEN;
2385 msg[1].len = 1; /* block length will be added by
2386 the underlying bus driver */
2387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 case I2C_SMBUS_I2C_BLOCK_DATA:
2389 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002390 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 } else {
2392 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002393 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002394 dev_err(&adapter->dev,
2395 "Invalid block write size %d\n",
2396 data->block[0]);
2397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 for (i = 1; i <= data->block[0]; i++)
2400 msgbuf0[i] = data->block[i];
2401 }
2402 break;
2403 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002404 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2405 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407
Jean Delvare421ef472005-10-26 21:28:55 +02002408 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2409 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2410 if (i) {
2411 /* Compute PEC if first message is a write */
2412 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002413 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002414 i2c_smbus_add_pec(&msg[0]);
2415 else /* Write followed by read */
2416 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2417 }
2418 /* Ask for PEC if last message is a read */
2419 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002420 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002421 }
2422
David Brownell24a5bb72008-07-14 22:38:23 +02002423 status = i2c_transfer(adapter, msg, num);
2424 if (status < 0)
2425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Jean Delvare421ef472005-10-26 21:28:55 +02002427 /* Check PEC if last message is a read */
2428 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002429 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2430 if (status < 0)
2431 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002432 }
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002435 switch (size) {
2436 case I2C_SMBUS_BYTE:
2437 data->byte = msgbuf0[0];
2438 break;
2439 case I2C_SMBUS_BYTE_DATA:
2440 data->byte = msgbuf1[0];
2441 break;
2442 case I2C_SMBUS_WORD_DATA:
2443 case I2C_SMBUS_PROC_CALL:
2444 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2445 break;
2446 case I2C_SMBUS_I2C_BLOCK_DATA:
2447 for (i = 0; i < data->block[0]; i++)
2448 data->block[i+1] = msgbuf1[i];
2449 break;
2450 case I2C_SMBUS_BLOCK_DATA:
2451 case I2C_SMBUS_BLOCK_PROC_CALL:
2452 for (i = 0; i < msgbuf1[0] + 1; i++)
2453 data->block[i] = msgbuf1[i];
2454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456 return 0;
2457}
2458
David Brownella1cdeda2008-07-14 22:38:24 +02002459/**
2460 * i2c_smbus_xfer - execute SMBus protocol operations
2461 * @adapter: Handle to I2C bus
2462 * @addr: Address of SMBus slave on that bus
2463 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2464 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2465 * @command: Byte interpreted by slave, for protocols which use such bytes
2466 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2467 * @data: Data to be read or written
2468 *
2469 * This executes an SMBus protocol operation, and returns a negative
2470 * errno code else zero on success.
2471 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002472s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002473 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002474 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002476 unsigned long orig_jiffies;
2477 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002480 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002483 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002484
2485 /* Retry automatically on arbitration loss */
2486 orig_jiffies = jiffies;
2487 for (res = 0, try = 0; try <= adapter->retries; try++) {
2488 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2489 read_write, command,
2490 protocol, data);
2491 if (res != -EAGAIN)
2492 break;
2493 if (time_after(jiffies,
2494 orig_jiffies + adapter->timeout))
2495 break;
2496 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002497 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002499 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2500 return res;
2501 /*
2502 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2503 * implement native support for the SMBus operation.
2504 */
2505 }
2506
2507 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2508 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2513MODULE_DESCRIPTION("I2C-Bus main module");
2514MODULE_LICENSE("GPL");