blob: e61a6c5c33725ec086993faada607766ad9d9258 [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
Jean Delvare7c81c602014-01-29 20:40:08 +010024 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020025 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010026 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053036#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/i2c.h>
39#include <linux/init.h>
40#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010041#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060043#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010044#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020045#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010046#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010047#include <linux/hardirq.h>
48#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020049#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010050#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020051#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010052#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000053#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
55
David Brownell9c1600e2007-05-01 23:26:31 +020056#include "i2c-core.h"
57
David Howellsd9a83d62014-03-06 13:35:59 +000058#define CREATE_TRACE_POINTS
59#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jean Delvare6629dcf2010-05-04 11:09:28 +020061/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020062 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000063 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010064static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_IDR(i2c_adapter_idr);
66
Jean Delvare4f8cf822009-09-18 22:45:46 +020067static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020068static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010069
David Howellsd9a83d62014-03-06 13:35:59 +000070static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
71
72void i2c_transfer_trace_reg(void)
73{
74 static_key_slow_inc(&i2c_trace_msg);
75}
76
77void i2c_transfer_trace_unreg(void)
78{
79 static_key_slow_dec(&i2c_trace_msg);
80}
81
David Brownellf37dd802007-02-13 22:09:00 +010082/* ------------------------------------------------------------------------- */
83
Jean Delvared2653e92008-04-29 23:11:39 +020084static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
85 const struct i2c_client *client)
86{
87 while (id->name[0]) {
88 if (strcmp(client->name, id->name) == 0)
89 return id;
90 id++;
91 }
92 return NULL;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int i2c_device_match(struct device *dev, struct device_driver *drv)
96{
Jean Delvare51298d12009-09-18 22:45:45 +020097 struct i2c_client *client = i2c_verify_client(dev);
98 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020099
Jean Delvare51298d12009-09-18 22:45:45 +0200100 if (!client)
101 return 0;
102
Grant Likely959e85f2010-06-08 07:48:19 -0600103 /* Attempt an OF style match */
104 if (of_driver_match_device(dev, drv))
105 return 1;
106
Mika Westerberg907ddf82012-11-23 12:23:40 +0100107 /* Then ACPI style match */
108 if (acpi_driver_match_device(dev, drv))
109 return 1;
110
Jean Delvare51298d12009-09-18 22:45:45 +0200111 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200112 /* match on an id table if there is one */
113 if (driver->id_table)
114 return i2c_match_id(driver->id_table, client) != NULL;
115
Jean Delvareeb8a7902008-05-18 20:49:41 +0200116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
David Brownell7b4fbc52007-05-01 23:26:30 +0200119
120/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200121static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200122{
123 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800124 int rc;
125
126 rc = acpi_device_uevent_modalias(dev, env);
127 if (rc != -ENODEV)
128 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200129
Jean Delvareeb8a7902008-05-18 20:49:41 +0200130 if (add_uevent_var(env, "MODALIAS=%s%s",
131 I2C_MODULE_PREFIX, client->name))
132 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200133 dev_dbg(dev, "uevent\n");
134 return 0;
135}
136
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530137/* i2c bus recovery routines */
138static int get_scl_gpio_value(struct i2c_adapter *adap)
139{
140 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
141}
142
143static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
144{
145 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
146}
147
148static int get_sda_gpio_value(struct i2c_adapter *adap)
149{
150 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
151}
152
153static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
154{
155 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
156 struct device *dev = &adap->dev;
157 int ret = 0;
158
159 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
160 GPIOF_OUT_INIT_HIGH, "i2c-scl");
161 if (ret) {
162 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
163 return ret;
164 }
165
166 if (bri->get_sda) {
167 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
168 /* work without SDA polling */
169 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
170 bri->sda_gpio);
171 bri->get_sda = NULL;
172 }
173 }
174
175 return ret;
176}
177
178static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
179{
180 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
181
182 if (bri->get_sda)
183 gpio_free(bri->sda_gpio);
184
185 gpio_free(bri->scl_gpio);
186}
187
188/*
189 * We are generating clock pulses. ndelay() determines durating of clk pulses.
190 * We will generate clock with rate 100 KHz and so duration of both clock levels
191 * is: delay in ns = (10^6 / 100) / 2
192 */
193#define RECOVERY_NDELAY 5000
194#define RECOVERY_CLK_CNT 9
195
196static int i2c_generic_recovery(struct i2c_adapter *adap)
197{
198 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
199 int i = 0, val = 1, ret = 0;
200
201 if (bri->prepare_recovery)
202 bri->prepare_recovery(bri);
203
204 /*
205 * By this time SCL is high, as we need to give 9 falling-rising edges
206 */
207 while (i++ < RECOVERY_CLK_CNT * 2) {
208 if (val) {
209 /* Break if SDA is high */
210 if (bri->get_sda && bri->get_sda(adap))
211 break;
212 /* SCL shouldn't be low here */
213 if (!bri->get_scl(adap)) {
214 dev_err(&adap->dev,
215 "SCL is stuck low, exit recovery\n");
216 ret = -EBUSY;
217 break;
218 }
219 }
220
221 val = !val;
222 bri->set_scl(adap, val);
223 ndelay(RECOVERY_NDELAY);
224 }
225
226 if (bri->unprepare_recovery)
227 bri->unprepare_recovery(bri);
228
229 return ret;
230}
231
232int i2c_generic_scl_recovery(struct i2c_adapter *adap)
233{
234 adap->bus_recovery_info->set_scl(adap, 1);
235 return i2c_generic_recovery(adap);
236}
237
238int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
239{
240 int ret;
241
242 ret = i2c_get_gpios_for_recovery(adap);
243 if (ret)
244 return ret;
245
246 ret = i2c_generic_recovery(adap);
247 i2c_put_gpios_for_recovery(adap);
248
249 return ret;
250}
251
252int i2c_recover_bus(struct i2c_adapter *adap)
253{
254 if (!adap->bus_recovery_info)
255 return -EOPNOTSUPP;
256
257 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
258 return adap->bus_recovery_info->recover_bus(adap);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static int i2c_device_probe(struct device *dev)
262{
Jean Delvare51298d12009-09-18 22:45:45 +0200263 struct i2c_client *client = i2c_verify_client(dev);
264 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100265 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200266
Jean Delvare51298d12009-09-18 22:45:45 +0200267 if (!client)
268 return 0;
269
270 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200271 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200272 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200273
Marc Pignatee354252008-08-28 08:33:22 +0200274 if (!device_can_wakeup(&client->dev))
275 device_init_wakeup(&client->dev,
276 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200277 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200278
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200279 status = of_clk_set_defaults(dev->of_node, false);
280 if (status < 0)
281 return status;
282
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200283 status = dev_pm_domain_attach(&client->dev, true);
284 if (status != -EPROBE_DEFER) {
285 status = driver->probe(client, i2c_match_id(driver->id_table,
286 client));
287 if (status)
288 dev_pm_domain_detach(&client->dev, true);
289 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100290
Hans Verkuil50c33042008-03-12 14:15:00 +0100291 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static int i2c_device_remove(struct device *dev)
295{
Jean Delvare51298d12009-09-18 22:45:45 +0200296 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200297 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100298 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200299
Jean Delvare51298d12009-09-18 22:45:45 +0200300 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200301 return 0;
302
303 driver = to_i2c_driver(dev->driver);
304 if (driver->remove) {
305 dev_dbg(dev, "remove\n");
306 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200307 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100308
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200309 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
David Brownellf37dd802007-02-13 22:09:00 +0100313static void i2c_device_shutdown(struct device *dev)
314{
Jean Delvare51298d12009-09-18 22:45:45 +0200315 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100316 struct i2c_driver *driver;
317
Jean Delvare51298d12009-09-18 22:45:45 +0200318 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100319 return;
320 driver = to_i2c_driver(dev->driver);
321 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200322 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100323}
324
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200325#ifdef CONFIG_PM_SLEEP
326static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100327{
Jean Delvare51298d12009-09-18 22:45:45 +0200328 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100329 struct i2c_driver *driver;
330
Jean Delvare51298d12009-09-18 22:45:45 +0200331 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100332 return 0;
333 driver = to_i2c_driver(dev->driver);
334 if (!driver->suspend)
335 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200336 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100337}
338
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200339static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100340{
Jean Delvare51298d12009-09-18 22:45:45 +0200341 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100342 struct i2c_driver *driver;
343
Jean Delvare51298d12009-09-18 22:45:45 +0200344 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100345 return 0;
346 driver = to_i2c_driver(dev->driver);
347 if (!driver->resume)
348 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200349 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100350}
351
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200352static int i2c_device_pm_suspend(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_suspend(dev);
358 else
359 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200360}
361
362static int i2c_device_pm_resume(struct device *dev)
363{
364 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200365
366 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100367 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200368 else
Mark Brownd529de22011-01-14 22:03:49 +0100369 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200370}
371
372static int i2c_device_pm_freeze(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_freeze(dev);
378 else
379 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200380}
381
382static int i2c_device_pm_thaw(struct device *dev)
383{
384 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
385
Mark Brownd529de22011-01-14 22:03:49 +0100386 if (pm)
387 return pm_generic_thaw(dev);
388 else
389 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200390}
391
392static int i2c_device_pm_poweroff(struct device *dev)
393{
394 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
395
Mark Brownd529de22011-01-14 22:03:49 +0100396 if (pm)
397 return pm_generic_poweroff(dev);
398 else
399 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200400}
401
402static int i2c_device_pm_restore(struct device *dev)
403{
404 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200405
406 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100407 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200408 else
Mark Brownd529de22011-01-14 22:03:49 +0100409 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200410}
411#else /* !CONFIG_PM_SLEEP */
412#define i2c_device_pm_suspend NULL
413#define i2c_device_pm_resume NULL
414#define i2c_device_pm_freeze NULL
415#define i2c_device_pm_thaw NULL
416#define i2c_device_pm_poweroff NULL
417#define i2c_device_pm_restore NULL
418#endif /* !CONFIG_PM_SLEEP */
419
David Brownell9c1600e2007-05-01 23:26:31 +0200420static void i2c_client_dev_release(struct device *dev)
421{
422 kfree(to_i2c_client(dev));
423}
424
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100425static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200426show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200427{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200428 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
429 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200430}
431
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100432static ssize_t
433show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200434{
435 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800436 int len;
437
438 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
439 if (len != -ENODEV)
440 return len;
441
Jean Delvareeb8a7902008-05-18 20:49:41 +0200442 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200443}
444
Jean Delvare4f8cf822009-09-18 22:45:46 +0200445static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200446static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
447
448static struct attribute *i2c_dev_attrs[] = {
449 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200450 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200451 &dev_attr_modalias.attr,
452 NULL
453};
454
455static struct attribute_group i2c_dev_attr_group = {
456 .attrs = i2c_dev_attrs,
457};
458
459static const struct attribute_group *i2c_dev_attr_groups[] = {
460 &i2c_dev_attr_group,
461 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200462};
463
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100464static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100465 .suspend = i2c_device_pm_suspend,
466 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200467 .freeze = i2c_device_pm_freeze,
468 .thaw = i2c_device_pm_thaw,
469 .poweroff = i2c_device_pm_poweroff,
470 .restore = i2c_device_pm_restore,
471 SET_RUNTIME_PM_OPS(
472 pm_generic_runtime_suspend,
473 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200474 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200475 )
sonic zhang54067ee2009-12-14 21:17:30 +0100476};
477
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200478struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100479 .name = "i2c",
480 .match = i2c_device_match,
481 .probe = i2c_device_probe,
482 .remove = i2c_device_remove,
483 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100484 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000485};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200486EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000487
Jean Delvare51298d12009-09-18 22:45:45 +0200488static struct device_type i2c_client_type = {
489 .groups = i2c_dev_attr_groups,
490 .uevent = i2c_device_uevent,
491 .release = i2c_client_dev_release,
492};
493
David Brownell9b766b82008-01-27 18:14:51 +0100494
495/**
496 * i2c_verify_client - return parameter as i2c_client, or NULL
497 * @dev: device, probably from some driver model iterator
498 *
499 * When traversing the driver model tree, perhaps using driver model
500 * iterators like @device_for_each_child(), you can't assume very much
501 * about the nodes you find. Use this function to avoid oopses caused
502 * by wrongly treating some non-I2C device as an i2c_client.
503 */
504struct i2c_client *i2c_verify_client(struct device *dev)
505{
Jean Delvare51298d12009-09-18 22:45:45 +0200506 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100507 ? to_i2c_client(dev)
508 : NULL;
509}
510EXPORT_SYMBOL(i2c_verify_client);
511
512
Jean Delvare3a89db52010-06-03 11:33:52 +0200513/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300514 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200515static int i2c_check_client_addr_validity(const struct i2c_client *client)
516{
517 if (client->flags & I2C_CLIENT_TEN) {
518 /* 10-bit address, all values are valid */
519 if (client->addr > 0x3ff)
520 return -EINVAL;
521 } else {
522 /* 7-bit address, reject the general call address */
523 if (client->addr == 0x00 || client->addr > 0x7f)
524 return -EINVAL;
525 }
526 return 0;
527}
528
Jean Delvare656b8762010-06-03 11:33:53 +0200529/* And this is a strict address validity check, used when probing. If a
530 * device uses a reserved address, then it shouldn't be probed. 7-bit
531 * addressing is assumed, 10-bit address devices are rare and should be
532 * explicitly enumerated. */
533static int i2c_check_addr_validity(unsigned short addr)
534{
535 /*
536 * Reserved addresses per I2C specification:
537 * 0x00 General call address / START byte
538 * 0x01 CBUS address
539 * 0x02 Reserved for different bus format
540 * 0x03 Reserved for future purposes
541 * 0x04-0x07 Hs-mode master code
542 * 0x78-0x7b 10-bit slave addressing
543 * 0x7c-0x7f Reserved for future purposes
544 */
545 if (addr < 0x08 || addr > 0x77)
546 return -EINVAL;
547 return 0;
548}
549
Jean Delvare3b5f7942010-06-03 11:33:55 +0200550static int __i2c_check_addr_busy(struct device *dev, void *addrp)
551{
552 struct i2c_client *client = i2c_verify_client(dev);
553 int addr = *(int *)addrp;
554
555 if (client && client->addr == addr)
556 return -EBUSY;
557 return 0;
558}
559
Michael Lawnick08263742010-08-11 18:21:02 +0200560/* walk up mux tree */
561static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
562{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200563 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200564 int result;
565
566 result = device_for_each_child(&adapter->dev, &addr,
567 __i2c_check_addr_busy);
568
Jean Delvare97cc4d42010-10-24 18:16:57 +0200569 if (!result && parent)
570 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200571
572 return result;
573}
574
575/* recurse down mux tree */
576static int i2c_check_mux_children(struct device *dev, void *addrp)
577{
578 int result;
579
580 if (dev->type == &i2c_adapter_type)
581 result = device_for_each_child(dev, addrp,
582 i2c_check_mux_children);
583 else
584 result = __i2c_check_addr_busy(dev, addrp);
585
586 return result;
587}
588
Jean Delvare3b5f7942010-06-03 11:33:55 +0200589static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
590{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200591 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200592 int result = 0;
593
Jean Delvare97cc4d42010-10-24 18:16:57 +0200594 if (parent)
595 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200596
597 if (!result)
598 result = device_for_each_child(&adapter->dev, &addr,
599 i2c_check_mux_children);
600
601 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200602}
603
David Brownell9c1600e2007-05-01 23:26:31 +0200604/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200605 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
606 * @adapter: Target I2C bus segment
607 */
608void i2c_lock_adapter(struct i2c_adapter *adapter)
609{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200610 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
611
612 if (parent)
613 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200614 else
615 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200616}
617EXPORT_SYMBOL_GPL(i2c_lock_adapter);
618
619/**
620 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
621 * @adapter: Target I2C bus segment
622 */
623static int i2c_trylock_adapter(struct i2c_adapter *adapter)
624{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200625 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
626
627 if (parent)
628 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200629 else
630 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200631}
632
633/**
634 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
635 * @adapter: Target I2C bus segment
636 */
637void i2c_unlock_adapter(struct i2c_adapter *adapter)
638{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200639 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
640
641 if (parent)
642 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200643 else
644 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200645}
646EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
647
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200648static void i2c_dev_set_name(struct i2c_adapter *adap,
649 struct i2c_client *client)
650{
651 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
652
653 if (adev) {
654 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
655 return;
656 }
657
658 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
659 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
660 client->addr | ((client->flags & I2C_CLIENT_TEN)
661 ? 0xa000 : 0));
662}
663
Jean Delvarefe61e072010-08-11 18:20:58 +0200664/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200665 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200666 * @adap: the adapter managing the device
667 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200668 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200669 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200670 * Create an i2c device. Binding is handled through driver model
671 * probe()/remove() methods. A driver may be bound to this device when we
672 * return from this function, or any later moment (e.g. maybe hotplugging will
673 * load the driver module). This call is not appropriate for use by mainboard
674 * initialization logic, which usually runs during an arch_initcall() long
675 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200676 *
677 * This returns the new i2c client, which may be saved for later use with
678 * i2c_unregister_device(); or NULL to indicate an error.
679 */
680struct i2c_client *
681i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
682{
683 struct i2c_client *client;
684 int status;
685
686 client = kzalloc(sizeof *client, GFP_KERNEL);
687 if (!client)
688 return NULL;
689
690 client->adapter = adap;
691
692 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200693
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200694 if (info->archdata)
695 client->dev.archdata = *info->archdata;
696
Marc Pignatee354252008-08-28 08:33:22 +0200697 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200698 client->addr = info->addr;
699 client->irq = info->irq;
700
David Brownell9c1600e2007-05-01 23:26:31 +0200701 strlcpy(client->name, info->type, sizeof(client->name));
702
Jean Delvare3a89db52010-06-03 11:33:52 +0200703 /* Check for address validity */
704 status = i2c_check_client_addr_validity(client);
705 if (status) {
706 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
707 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
708 goto out_err_silent;
709 }
710
Jean Delvaref8a227e2009-06-19 16:58:18 +0200711 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200712 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200713 if (status)
714 goto out_err;
715
716 client->dev.parent = &client->adapter->dev;
717 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200718 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700719 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100720 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200721
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200722 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200723 status = device_register(&client->dev);
724 if (status)
725 goto out_err;
726
Jean Delvaref8a227e2009-06-19 16:58:18 +0200727 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
728 client->name, dev_name(&client->dev));
729
David Brownell9c1600e2007-05-01 23:26:31 +0200730 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200731
732out_err:
733 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
734 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200735out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200736 kfree(client);
737 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200738}
739EXPORT_SYMBOL_GPL(i2c_new_device);
740
741
742/**
743 * i2c_unregister_device - reverse effect of i2c_new_device()
744 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200745 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200746 */
747void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200748{
David Brownella1d9e6e2007-05-01 23:26:30 +0200749 device_unregister(&client->dev);
750}
David Brownell9c1600e2007-05-01 23:26:31 +0200751EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200752
753
Jean Delvare60b129d2008-05-11 20:37:06 +0200754static const struct i2c_device_id dummy_id[] = {
755 { "dummy", 0 },
756 { },
757};
758
Jean Delvared2653e92008-04-29 23:11:39 +0200759static int dummy_probe(struct i2c_client *client,
760 const struct i2c_device_id *id)
761{
762 return 0;
763}
764
765static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100766{
767 return 0;
768}
769
770static struct i2c_driver dummy_driver = {
771 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200772 .probe = dummy_probe,
773 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200774 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100775};
776
777/**
778 * i2c_new_dummy - return a new i2c device bound to a dummy driver
779 * @adapter: the adapter managing the device
780 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100781 * Context: can sleep
782 *
783 * This returns an I2C client bound to the "dummy" driver, intended for use
784 * with devices that consume multiple addresses. Examples of such chips
785 * include various EEPROMS (like 24c04 and 24c08 models).
786 *
787 * These dummy devices have two main uses. First, most I2C and SMBus calls
788 * except i2c_transfer() need a client handle; the dummy will be that handle.
789 * And second, this prevents the specified address from being bound to a
790 * different driver.
791 *
792 * This returns the new i2c client, which should be saved for later use with
793 * i2c_unregister_device(); or NULL to indicate an error.
794 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100795struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100796{
797 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200798 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100799 };
800
David Brownelle9f13732008-01-27 18:14:52 +0100801 return i2c_new_device(adapter, &info);
802}
803EXPORT_SYMBOL_GPL(i2c_new_dummy);
804
David Brownellf37dd802007-02-13 22:09:00 +0100805/* ------------------------------------------------------------------------- */
806
David Brownell16ffadf2007-05-01 23:26:28 +0200807/* I2C bus adapters -- one roots each I2C or SMBUS segment */
808
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200809static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
David Brownellef2c83212007-05-01 23:26:28 +0200811 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 complete(&adap->dev_released);
813}
814
Jean Delvare99cd8e22009-06-19 16:58:20 +0200815/*
Jean Delvare390946b2012-09-10 10:14:02 +0200816 * This function is only needed for mutex_lock_nested, so it is never
817 * called unless locking correctness checking is enabled. Thus we
818 * make it inline to avoid a compiler warning. That's what gcc ends up
819 * doing anyway.
820 */
821static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
822{
823 unsigned int depth = 0;
824
825 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
826 depth++;
827
828 return depth;
829}
830
831/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200832 * Let users instantiate I2C devices through sysfs. This can be used when
833 * platform initialization code doesn't contain the proper data for
834 * whatever reason. Also useful for drivers that do device detection and
835 * detection fails, either because the device uses an unexpected address,
836 * or this is a compatible device with different ID register values.
837 *
838 * Parameter checking may look overzealous, but we really don't want
839 * the user to provide incorrect parameters.
840 */
841static ssize_t
842i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
843 const char *buf, size_t count)
844{
845 struct i2c_adapter *adap = to_i2c_adapter(dev);
846 struct i2c_board_info info;
847 struct i2c_client *client;
848 char *blank, end;
849 int res;
850
Jean Delvare99cd8e22009-06-19 16:58:20 +0200851 memset(&info, 0, sizeof(struct i2c_board_info));
852
853 blank = strchr(buf, ' ');
854 if (!blank) {
855 dev_err(dev, "%s: Missing parameters\n", "new_device");
856 return -EINVAL;
857 }
858 if (blank - buf > I2C_NAME_SIZE - 1) {
859 dev_err(dev, "%s: Invalid device name\n", "new_device");
860 return -EINVAL;
861 }
862 memcpy(info.type, buf, blank - buf);
863
864 /* Parse remaining parameters, reject extra parameters */
865 res = sscanf(++blank, "%hi%c", &info.addr, &end);
866 if (res < 1) {
867 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
868 return -EINVAL;
869 }
870 if (res > 1 && end != '\n') {
871 dev_err(dev, "%s: Extra parameters\n", "new_device");
872 return -EINVAL;
873 }
874
Jean Delvare99cd8e22009-06-19 16:58:20 +0200875 client = i2c_new_device(adap, &info);
876 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200877 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200878
879 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200880 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200881 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200882 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200883 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
884 info.type, info.addr);
885
886 return count;
887}
888
889/*
890 * And of course let the users delete the devices they instantiated, if
891 * they got it wrong. This interface can only be used to delete devices
892 * instantiated by i2c_sysfs_new_device above. This guarantees that we
893 * don't delete devices to which some kernel code still has references.
894 *
895 * Parameter checking may look overzealous, but we really don't want
896 * the user to delete the wrong device.
897 */
898static ssize_t
899i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
900 const char *buf, size_t count)
901{
902 struct i2c_adapter *adap = to_i2c_adapter(dev);
903 struct i2c_client *client, *next;
904 unsigned short addr;
905 char end;
906 int res;
907
908 /* Parse parameters, reject extra parameters */
909 res = sscanf(buf, "%hi%c", &addr, &end);
910 if (res < 1) {
911 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
912 return -EINVAL;
913 }
914 if (res > 1 && end != '\n') {
915 dev_err(dev, "%s: Extra parameters\n", "delete_device");
916 return -EINVAL;
917 }
918
919 /* Make sure the device was added through sysfs */
920 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200921 mutex_lock_nested(&adap->userspace_clients_lock,
922 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200923 list_for_each_entry_safe(client, next, &adap->userspace_clients,
924 detected) {
925 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200926 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
927 "delete_device", client->name, client->addr);
928
929 list_del(&client->detected);
930 i2c_unregister_device(client);
931 res = count;
932 break;
933 }
934 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200935 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200936
937 if (res < 0)
938 dev_err(dev, "%s: Can't find device in list\n",
939 "delete_device");
940 return res;
941}
942
Jean Delvare4f8cf822009-09-18 22:45:46 +0200943static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200944static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
945 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200946
947static struct attribute *i2c_adapter_attrs[] = {
948 &dev_attr_name.attr,
949 &dev_attr_new_device.attr,
950 &dev_attr_delete_device.attr,
951 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200952};
953
Jean Delvare4f8cf822009-09-18 22:45:46 +0200954static struct attribute_group i2c_adapter_attr_group = {
955 .attrs = i2c_adapter_attrs,
956};
957
958static const struct attribute_group *i2c_adapter_attr_groups[] = {
959 &i2c_adapter_attr_group,
960 NULL
961};
962
Michael Lawnick08263742010-08-11 18:21:02 +0200963struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200964 .groups = i2c_adapter_attr_groups,
965 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200966};
Michael Lawnick08263742010-08-11 18:21:02 +0200967EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Stephen Warren643dd092012-04-17 12:43:33 -0600969/**
970 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
971 * @dev: device, probably from some driver model iterator
972 *
973 * When traversing the driver model tree, perhaps using driver model
974 * iterators like @device_for_each_child(), you can't assume very much
975 * about the nodes you find. Use this function to avoid oopses caused
976 * by wrongly treating some non-I2C device as an i2c_adapter.
977 */
978struct i2c_adapter *i2c_verify_adapter(struct device *dev)
979{
980 return (dev->type == &i2c_adapter_type)
981 ? to_i2c_adapter(dev)
982 : NULL;
983}
984EXPORT_SYMBOL(i2c_verify_adapter);
985
Jean Delvare2bb50952009-09-18 22:45:46 +0200986#ifdef CONFIG_I2C_COMPAT
987static struct class_compat *i2c_adapter_compat_class;
988#endif
989
David Brownell9c1600e2007-05-01 23:26:31 +0200990static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
991{
992 struct i2c_devinfo *devinfo;
993
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200994 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200995 list_for_each_entry(devinfo, &__i2c_board_list, list) {
996 if (devinfo->busnum == adapter->nr
997 && !i2c_new_device(adapter,
998 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100999 dev_err(&adapter->dev,
1000 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001001 devinfo->board_info.addr);
1002 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001003 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001004}
1005
Wolfram Sang687b81d2013-07-11 12:56:15 +01001006/* OF support code */
1007
1008#if IS_ENABLED(CONFIG_OF)
1009static void of_i2c_register_devices(struct i2c_adapter *adap)
1010{
1011 void *result;
1012 struct device_node *node;
1013
1014 /* Only register child devices if the adapter has a node pointer set */
1015 if (!adap->dev.of_node)
1016 return;
1017
1018 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1019
1020 for_each_available_child_of_node(adap->dev.of_node, node) {
1021 struct i2c_board_info info = {};
1022 struct dev_archdata dev_ad = {};
1023 const __be32 *addr;
1024 int len;
1025
1026 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1027
1028 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1029 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1030 node->full_name);
1031 continue;
1032 }
1033
1034 addr = of_get_property(node, "reg", &len);
1035 if (!addr || (len < sizeof(int))) {
1036 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1037 node->full_name);
1038 continue;
1039 }
1040
1041 info.addr = be32_to_cpup(addr);
1042 if (info.addr > (1 << 10) - 1) {
1043 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1044 info.addr, node->full_name);
1045 continue;
1046 }
1047
1048 info.irq = irq_of_parse_and_map(node, 0);
1049 info.of_node = of_node_get(node);
1050 info.archdata = &dev_ad;
1051
1052 if (of_get_property(node, "wakeup-source", NULL))
1053 info.flags |= I2C_CLIENT_WAKE;
1054
1055 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1056
1057 result = i2c_new_device(adap, &info);
1058 if (result == NULL) {
1059 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1060 node->full_name);
1061 of_node_put(node);
1062 irq_dispose_mapping(info.irq);
1063 continue;
1064 }
1065 }
1066}
1067
1068static int of_dev_node_match(struct device *dev, void *data)
1069{
1070 return dev->of_node == data;
1071}
1072
1073/* must call put_device() when done with returned i2c_client device */
1074struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1075{
1076 struct device *dev;
1077
1078 dev = bus_find_device(&i2c_bus_type, NULL, node,
1079 of_dev_node_match);
1080 if (!dev)
1081 return NULL;
1082
1083 return i2c_verify_client(dev);
1084}
1085EXPORT_SYMBOL(of_find_i2c_device_by_node);
1086
1087/* must call put_device() when done with returned i2c_adapter device */
1088struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1089{
1090 struct device *dev;
1091
1092 dev = bus_find_device(&i2c_bus_type, NULL, node,
1093 of_dev_node_match);
1094 if (!dev)
1095 return NULL;
1096
1097 return i2c_verify_adapter(dev);
1098}
1099EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1100#else
1101static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1102#endif /* CONFIG_OF */
1103
Jean Delvare69b00892009-12-06 17:06:27 +01001104static int i2c_do_add_adapter(struct i2c_driver *driver,
1105 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001106{
Jean Delvare4735c982008-07-14 22:38:36 +02001107 /* Detect supported devices on that bus, and instantiate them */
1108 i2c_detect(adap, driver);
1109
1110 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001111 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001112 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1113 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001114 dev_warn(&adap->dev, "Please use another way to instantiate "
1115 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001116 /* We ignore the return code; if it fails, too bad */
1117 driver->attach_adapter(adap);
1118 }
1119 return 0;
1120}
1121
Jean Delvare69b00892009-12-06 17:06:27 +01001122static int __process_new_adapter(struct device_driver *d, void *data)
1123{
1124 return i2c_do_add_adapter(to_i2c_driver(d), data);
1125}
1126
David Brownell6e13e642007-05-01 23:26:31 +02001127static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Jean Delvared6703282010-08-11 18:20:59 +02001129 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
David Brownell1d0b19c2008-10-14 17:30:05 +02001131 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001132 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1133 res = -EAGAIN;
1134 goto out_list;
1135 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001136
Jean Delvare2236baa2010-11-15 22:40:38 +01001137 /* Sanity checks */
1138 if (unlikely(adap->name[0] == '\0')) {
1139 pr_err("i2c-core: Attempt to register an adapter with "
1140 "no name!\n");
1141 return -EINVAL;
1142 }
1143 if (unlikely(!adap->algo)) {
1144 pr_err("i2c-core: Attempt to register adapter '%s' with "
1145 "no algo!\n", adap->name);
1146 return -EINVAL;
1147 }
1148
Mika Kuoppala194684e2009-12-06 17:06:22 +01001149 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001150 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001151 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jean Delvare8fcfef62009-03-28 21:34:43 +01001153 /* Set default timeout to 1 second if not already set */
1154 if (adap->timeout == 0)
1155 adap->timeout = HZ;
1156
Kay Sievers27d9c182009-01-07 14:29:16 +01001157 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001158 adap->dev.bus = &i2c_bus_type;
1159 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001160 res = device_register(&adap->dev);
1161 if (res)
1162 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001164 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1165
Jean Delvare2bb50952009-09-18 22:45:46 +02001166#ifdef CONFIG_I2C_COMPAT
1167 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1168 adap->dev.parent);
1169 if (res)
1170 dev_warn(&adap->dev,
1171 "Failed to create compatibility class link\n");
1172#endif
1173
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301174 /* bus recovery specific initialization */
1175 if (adap->bus_recovery_info) {
1176 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1177
1178 if (!bri->recover_bus) {
1179 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1180 adap->bus_recovery_info = NULL;
1181 goto exit_recovery;
1182 }
1183
1184 /* Generic GPIO recovery */
1185 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1186 if (!gpio_is_valid(bri->scl_gpio)) {
1187 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1188 adap->bus_recovery_info = NULL;
1189 goto exit_recovery;
1190 }
1191
1192 if (gpio_is_valid(bri->sda_gpio))
1193 bri->get_sda = get_sda_gpio_value;
1194 else
1195 bri->get_sda = NULL;
1196
1197 bri->get_scl = get_scl_gpio_value;
1198 bri->set_scl = set_scl_gpio_value;
1199 } else if (!bri->set_scl || !bri->get_scl) {
1200 /* Generic SCL recovery */
1201 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1202 adap->bus_recovery_info = NULL;
1203 }
1204 }
1205
1206exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001207 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001208 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001209 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001210 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001211
David Brownell6e13e642007-05-01 23:26:31 +02001212 if (adap->nr < __i2c_first_dynamic_bus_num)
1213 i2c_scan_static_board_info(adap);
1214
Jean Delvare4735c982008-07-14 22:38:36 +02001215 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001216 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001217 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001218 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001219
1220 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001221
Jean Delvareb119c6c2006-08-15 18:26:30 +02001222out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001223 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001224 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001225 mutex_unlock(&core_lock);
1226 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
David Brownell6e13e642007-05-01 23:26:31 +02001229/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001230 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1231 * @adap: the adapter to register (with adap->nr initialized)
1232 * Context: can sleep
1233 *
1234 * See i2c_add_numbered_adapter() for details.
1235 */
1236static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1237{
1238 int id;
1239
1240 mutex_lock(&core_lock);
1241 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1242 GFP_KERNEL);
1243 mutex_unlock(&core_lock);
1244 if (id < 0)
1245 return id == -ENOSPC ? -EBUSY : id;
1246
1247 return i2c_register_adapter(adap);
1248}
1249
1250/**
David Brownell6e13e642007-05-01 23:26:31 +02001251 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1252 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001253 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001254 *
1255 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001256 * doesn't matter or when its bus number is specified by an dt alias.
1257 * Examples of bases when the bus number doesn't matter: I2C adapters
1258 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001259 *
1260 * When this returns zero, a new bus number was allocated and stored
1261 * in adap->nr, and the specified adapter became available for clients.
1262 * Otherwise, a negative errno value is returned.
1263 */
1264int i2c_add_adapter(struct i2c_adapter *adapter)
1265{
Doug Andersonee5c2742013-03-01 08:57:31 -08001266 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001267 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001268
Doug Andersonee5c2742013-03-01 08:57:31 -08001269 if (dev->of_node) {
1270 id = of_alias_get_id(dev->of_node, "i2c");
1271 if (id >= 0) {
1272 adapter->nr = id;
1273 return __i2c_add_numbered_adapter(adapter);
1274 }
1275 }
1276
Jean Delvarecaada322008-01-27 18:14:49 +01001277 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001278 id = idr_alloc(&i2c_adapter_idr, adapter,
1279 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001280 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001281 if (id < 0)
1282 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001283
1284 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001285
David Brownell6e13e642007-05-01 23:26:31 +02001286 return i2c_register_adapter(adapter);
1287}
1288EXPORT_SYMBOL(i2c_add_adapter);
1289
1290/**
1291 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1292 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001293 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001294 *
1295 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001296 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1297 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001298 * is used to properly configure I2C devices.
1299 *
Grant Likely488bf312011-07-25 17:49:43 +02001300 * If the requested bus number is set to -1, then this function will behave
1301 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1302 *
David Brownell6e13e642007-05-01 23:26:31 +02001303 * If no devices have pre-been declared for this bus, then be sure to
1304 * register the adapter before any dynamically allocated ones. Otherwise
1305 * the required bus ID may not be available.
1306 *
1307 * When this returns zero, the specified adapter became available for
1308 * clients using the bus number provided in adap->nr. Also, the table
1309 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1310 * and the appropriate driver model device nodes are created. Otherwise, a
1311 * negative errno value is returned.
1312 */
1313int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1314{
Grant Likely488bf312011-07-25 17:49:43 +02001315 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1316 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001317
Doug Andersonee5c2742013-03-01 08:57:31 -08001318 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001319}
1320EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1321
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001322static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001323 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001324{
Jean Delvare4735c982008-07-14 22:38:36 +02001325 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001326
Jean Delvareacec2112009-03-28 21:34:40 +01001327 /* Remove the devices we created ourselves as the result of hardware
1328 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001329 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1330 if (client->adapter == adapter) {
1331 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1332 client->name, client->addr);
1333 list_del(&client->detected);
1334 i2c_unregister_device(client);
1335 }
1336 }
Jean Delvare026526f2008-01-27 18:14:49 +01001337}
1338
Jean Delvaree549c2b2009-06-19 16:58:19 +02001339static int __unregister_client(struct device *dev, void *dummy)
1340{
1341 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001342 if (client && strcmp(client->name, "dummy"))
1343 i2c_unregister_device(client);
1344 return 0;
1345}
1346
1347static int __unregister_dummy(struct device *dev, void *dummy)
1348{
1349 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001350 if (client)
1351 i2c_unregister_device(client);
1352 return 0;
1353}
1354
Jean Delvare69b00892009-12-06 17:06:27 +01001355static int __process_removed_adapter(struct device_driver *d, void *data)
1356{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001357 i2c_do_del_adapter(to_i2c_driver(d), data);
1358 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001359}
1360
David Brownelld64f73b2007-07-12 14:12:28 +02001361/**
1362 * i2c_del_adapter - unregister I2C adapter
1363 * @adap: the adapter being unregistered
1364 * Context: can sleep
1365 *
1366 * This unregisters an I2C adapter which was previously registered
1367 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1368 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001369void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001371 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001372 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001375 mutex_lock(&core_lock);
1376 found = idr_find(&i2c_adapter_idr, adap->nr);
1377 mutex_unlock(&core_lock);
1378 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001379 pr_debug("i2c-core: attempting to delete unregistered "
1380 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001381 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
Lan Tianyu5d98e612014-05-20 20:59:23 +08001384 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001385 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001386 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001387 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001388 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001389 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001391 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001392 mutex_lock_nested(&adap->userspace_clients_lock,
1393 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001394 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1395 detected) {
1396 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1397 client->addr);
1398 list_del(&client->detected);
1399 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001400 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001401 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001402
Jean Delvaree549c2b2009-06-19 16:58:19 +02001403 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001404 * check the returned value. This is a two-pass process, because
1405 * we can't remove the dummy devices during the first pass: they
1406 * could have been instantiated by real devices wishing to clean
1407 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001408 device_for_each_child(&adap->dev, NULL, __unregister_client);
1409 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jean Delvare2bb50952009-09-18 22:45:46 +02001411#ifdef CONFIG_I2C_COMPAT
1412 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1413 adap->dev.parent);
1414#endif
1415
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001416 /* device name is gone after device_unregister */
1417 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 /* clean up the sysfs representation */
1420 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* wait for sysfs to drop all references */
1424 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
David Brownell6e13e642007-05-01 23:26:31 +02001426 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001427 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001429 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001431 /* Clear the device structure in case this adapter is ever going to be
1432 added again */
1433 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
David Brownellc0564602007-05-01 23:26:31 +02001435EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
David Brownell7b4fbc52007-05-01 23:26:30 +02001437/* ------------------------------------------------------------------------- */
1438
Jean Delvare7ae31482011-03-20 14:50:52 +01001439int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1440{
1441 int res;
1442
1443 mutex_lock(&core_lock);
1444 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1445 mutex_unlock(&core_lock);
1446
1447 return res;
1448}
1449EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1450
Jean Delvare69b00892009-12-06 17:06:27 +01001451static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001452{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001453 if (dev->type != &i2c_adapter_type)
1454 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001455 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001456}
1457
David Brownell7b4fbc52007-05-01 23:26:30 +02001458/*
1459 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001460 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001463int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001465 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
David Brownell1d0b19c2008-10-14 17:30:05 +02001467 /* Can't register until after driver model init */
1468 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1469 return -EAGAIN;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001472 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jean Delvare729d6dd2009-06-19 16:58:18 +02001475 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001476 * will have called probe() for all matching-but-unbound devices.
1477 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 res = driver_register(&driver->driver);
1479 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001480 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001481
Mark Brownf4e8db32011-01-14 22:03:50 +01001482 /* Drivers should switch to dev_pm_ops instead. */
1483 if (driver->suspend)
1484 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1485 driver->driver.name);
1486 if (driver->resume)
1487 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1488 driver->driver.name);
1489
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001490 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Jean Delvare4735c982008-07-14 22:38:36 +02001492 INIT_LIST_HEAD(&driver->clients);
1493 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001494 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001495
Jean Delvare7eebcb72006-02-05 23:28:21 +01001496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001498EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jean Delvare69b00892009-12-06 17:06:27 +01001500static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001501{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001502 if (dev->type == &i2c_adapter_type)
1503 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1504 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001505}
1506
David Brownella1d9e6e2007-05-01 23:26:30 +02001507/**
1508 * i2c_del_driver - unregister I2C driver
1509 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001510 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001511 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001512void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Jean Delvare7ae31482011-03-20 14:50:52 +01001514 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001517 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
David Brownellc0564602007-05-01 23:26:31 +02001519EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
David Brownell7b4fbc52007-05-01 23:26:30 +02001521/* ------------------------------------------------------------------------- */
1522
Jean Delvaree48d3312008-01-27 18:14:48 +01001523/**
1524 * i2c_use_client - increments the reference count of the i2c client structure
1525 * @client: the client being referenced
1526 *
1527 * Each live reference to a client should be refcounted. The driver model does
1528 * that automatically as part of driver binding, so that most drivers don't
1529 * need to do this explicitly: they hold a reference until they're unbound
1530 * from the device.
1531 *
1532 * A pointer to the client with the incremented reference counter is returned.
1533 */
1534struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
David Brownell6ea438e2008-07-14 22:38:24 +02001536 if (client && get_device(&client->dev))
1537 return client;
1538 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
David Brownellc0564602007-05-01 23:26:31 +02001540EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jean Delvaree48d3312008-01-27 18:14:48 +01001542/**
1543 * i2c_release_client - release a use of the i2c client structure
1544 * @client: the client being no longer referenced
1545 *
1546 * Must be called when a user of a client is finished with it.
1547 */
1548void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
David Brownell6ea438e2008-07-14 22:38:24 +02001550 if (client)
1551 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
David Brownellc0564602007-05-01 23:26:31 +02001553EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
David Brownell9b766b82008-01-27 18:14:51 +01001555struct i2c_cmd_arg {
1556 unsigned cmd;
1557 void *arg;
1558};
1559
1560static int i2c_cmd(struct device *dev, void *_arg)
1561{
1562 struct i2c_client *client = i2c_verify_client(dev);
1563 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001564 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001565
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001566 if (!client || !client->dev.driver)
1567 return 0;
1568
1569 driver = to_i2c_driver(client->dev.driver);
1570 if (driver->command)
1571 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001572 return 0;
1573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1576{
David Brownell9b766b82008-01-27 18:14:51 +01001577 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
David Brownell9b766b82008-01-27 18:14:51 +01001579 cmd_arg.cmd = cmd;
1580 cmd_arg.arg = arg;
1581 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
David Brownellc0564602007-05-01 23:26:31 +02001583EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585static int __init i2c_init(void)
1586{
1587 int retval;
1588
1589 retval = bus_register(&i2c_bus_type);
1590 if (retval)
1591 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001592#ifdef CONFIG_I2C_COMPAT
1593 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1594 if (!i2c_adapter_compat_class) {
1595 retval = -ENOMEM;
1596 goto bus_err;
1597 }
1598#endif
David Brownelle9f13732008-01-27 18:14:52 +01001599 retval = i2c_add_driver(&dummy_driver);
1600 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001601 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001602 return 0;
1603
Jean Delvare2bb50952009-09-18 22:45:46 +02001604class_err:
1605#ifdef CONFIG_I2C_COMPAT
1606 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001607bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001608#endif
David Brownelle9f13732008-01-27 18:14:52 +01001609 bus_unregister(&i2c_bus_type);
1610 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613static void __exit i2c_exit(void)
1614{
David Brownelle9f13732008-01-27 18:14:52 +01001615 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001616#ifdef CONFIG_I2C_COMPAT
1617 class_compat_unregister(i2c_adapter_compat_class);
1618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001620 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
David Brownella10f9e72008-10-14 17:30:06 +02001623/* We must initialize early, because some subsystems register i2c drivers
1624 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1625 */
1626postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627module_exit(i2c_exit);
1628
1629/* ----------------------------------------------------
1630 * the functional interface to the i2c busses.
1631 * ----------------------------------------------------
1632 */
1633
David Brownella1cdeda2008-07-14 22:38:24 +02001634/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001635 * __i2c_transfer - unlocked flavor of i2c_transfer
1636 * @adap: Handle to I2C bus
1637 * @msgs: One or more messages to execute before STOP is issued to
1638 * terminate the operation; each message begins with a START.
1639 * @num: Number of messages to be executed.
1640 *
1641 * Returns negative errno, else the number of messages executed.
1642 *
1643 * Adapter lock must be held when calling this function. No debug logging
1644 * takes place. adap->algo->master_xfer existence isn't checked.
1645 */
1646int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1647{
1648 unsigned long orig_jiffies;
1649 int ret, try;
1650
David Howellsd9a83d62014-03-06 13:35:59 +00001651 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1652 * enabled. This is an efficient way of keeping the for-loop from
1653 * being executed when not needed.
1654 */
1655 if (static_key_false(&i2c_trace_msg)) {
1656 int i;
1657 for (i = 0; i < num; i++)
1658 if (msgs[i].flags & I2C_M_RD)
1659 trace_i2c_read(adap, &msgs[i], i);
1660 else
1661 trace_i2c_write(adap, &msgs[i], i);
1662 }
1663
Jean Delvareb37d2a32012-06-29 07:47:19 -03001664 /* Retry automatically on arbitration loss */
1665 orig_jiffies = jiffies;
1666 for (ret = 0, try = 0; try <= adap->retries; try++) {
1667 ret = adap->algo->master_xfer(adap, msgs, num);
1668 if (ret != -EAGAIN)
1669 break;
1670 if (time_after(jiffies, orig_jiffies + adap->timeout))
1671 break;
1672 }
1673
David Howellsd9a83d62014-03-06 13:35:59 +00001674 if (static_key_false(&i2c_trace_msg)) {
1675 int i;
1676 for (i = 0; i < ret; i++)
1677 if (msgs[i].flags & I2C_M_RD)
1678 trace_i2c_reply(adap, &msgs[i], i);
1679 trace_i2c_result(adap, i, ret);
1680 }
1681
Jean Delvareb37d2a32012-06-29 07:47:19 -03001682 return ret;
1683}
1684EXPORT_SYMBOL(__i2c_transfer);
1685
1686/**
David Brownella1cdeda2008-07-14 22:38:24 +02001687 * i2c_transfer - execute a single or combined I2C message
1688 * @adap: Handle to I2C bus
1689 * @msgs: One or more messages to execute before STOP is issued to
1690 * terminate the operation; each message begins with a START.
1691 * @num: Number of messages to be executed.
1692 *
1693 * Returns negative errno, else the number of messages executed.
1694 *
1695 * Note that there is no requirement that each message be sent to
1696 * the same slave address, although that is the most common model.
1697 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001698int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001700 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
David Brownella1cdeda2008-07-14 22:38:24 +02001702 /* REVISIT the fault reporting model here is weak:
1703 *
1704 * - When we get an error after receiving N bytes from a slave,
1705 * there is no way to report "N".
1706 *
1707 * - When we get a NAK after transmitting N bytes to a slave,
1708 * there is no way to report "N" ... or to let the master
1709 * continue executing the rest of this combined message, if
1710 * that's the appropriate response.
1711 *
1712 * - When for example "num" is two and we successfully complete
1713 * the first message but get an error part way through the
1714 * second, it's unclear whether that should be reported as
1715 * one (discarding status on the second message) or errno
1716 * (discarding status on the first one).
1717 */
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (adap->algo->master_xfer) {
1720#ifdef DEBUG
1721 for (ret = 0; ret < num; ret++) {
1722 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001723 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1724 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1725 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727#endif
1728
Mike Rapoportcea443a2008-01-27 18:14:50 +01001729 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001730 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001731 if (!ret)
1732 /* I2C activity is ongoing. */
1733 return -EAGAIN;
1734 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001735 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001736 }
1737
Jean Delvareb37d2a32012-06-29 07:47:19 -03001738 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001739 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 return ret;
1742 } else {
1743 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001744 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746}
David Brownellc0564602007-05-01 23:26:31 +02001747EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Brownella1cdeda2008-07-14 22:38:24 +02001749/**
1750 * i2c_master_send - issue a single I2C message in master transmit mode
1751 * @client: Handle to slave device
1752 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001753 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001754 *
1755 * Returns negative errno, or else the number of bytes written.
1756 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001757int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001760 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct i2c_msg msg;
1762
Jean Delvare815f55f2005-05-07 22:58:46 +02001763 msg.addr = client->addr;
1764 msg.flags = client->flags & I2C_M_TEN;
1765 msg.len = count;
1766 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001767
Jean Delvare815f55f2005-05-07 22:58:46 +02001768 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001770 /*
1771 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1772 * transmitted, else error code.
1773 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001774 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
David Brownellc0564602007-05-01 23:26:31 +02001776EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David Brownella1cdeda2008-07-14 22:38:24 +02001778/**
1779 * i2c_master_recv - issue a single I2C message in master receive mode
1780 * @client: Handle to slave device
1781 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001782 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001783 *
1784 * Returns negative errno, or else the number of bytes read.
1785 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001786int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Farid Hammane7225acf2010-05-21 18:40:58 +02001788 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct i2c_msg msg;
1790 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jean Delvare815f55f2005-05-07 22:58:46 +02001792 msg.addr = client->addr;
1793 msg.flags = client->flags & I2C_M_TEN;
1794 msg.flags |= I2C_M_RD;
1795 msg.len = count;
1796 msg.buf = buf;
1797
1798 ret = i2c_transfer(adap, &msg, 1);
1799
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001800 /*
1801 * If everything went ok (i.e. 1 msg received), return #bytes received,
1802 * else error code.
1803 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001804 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
David Brownellc0564602007-05-01 23:26:31 +02001806EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/* ----------------------------------------------------
1809 * the i2c address scanning function
1810 * Will not work for 10-bit addresses!
1811 * ----------------------------------------------------
1812 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001813
Jean Delvare63e4e802010-06-03 11:33:51 +02001814/*
1815 * Legacy default probe function, mostly relevant for SMBus. The default
1816 * probe method is a quick write, but it is known to corrupt the 24RF08
1817 * EEPROMs due to a state machine bug, and could also irreversibly
1818 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1819 * we use a short byte read instead. Also, some bus drivers don't implement
1820 * quick write, so we fallback to a byte read in that case too.
1821 * On x86, there is another special case for FSC hardware monitoring chips,
1822 * which want regular byte reads (address 0x73.) Fortunately, these are the
1823 * only known chips using this I2C address on PC hardware.
1824 * Returns 1 if probe succeeded, 0 if not.
1825 */
1826static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1827{
1828 int err;
1829 union i2c_smbus_data dummy;
1830
1831#ifdef CONFIG_X86
1832 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1833 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1834 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1835 I2C_SMBUS_BYTE_DATA, &dummy);
1836 else
1837#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001838 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1839 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001840 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1841 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001842 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1843 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1844 I2C_SMBUS_BYTE, &dummy);
1845 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001846 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1847 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001848 err = -EOPNOTSUPP;
1849 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001850
1851 return err >= 0;
1852}
1853
Jean Delvareccfbbd02009-12-06 17:06:25 +01001854static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001855 struct i2c_driver *driver)
1856{
1857 struct i2c_board_info info;
1858 struct i2c_adapter *adapter = temp_client->adapter;
1859 int addr = temp_client->addr;
1860 int err;
1861
1862 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001863 err = i2c_check_addr_validity(addr);
1864 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001865 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1866 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001867 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001868 }
1869
1870 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001871 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001872 return 0;
1873
Jean Delvareccfbbd02009-12-06 17:06:25 +01001874 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001875 if (!i2c_default_probe(adapter, addr))
1876 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001877
1878 /* Finally call the custom detection function */
1879 memset(&info, 0, sizeof(struct i2c_board_info));
1880 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001881 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001882 if (err) {
1883 /* -ENODEV is returned if the detection fails. We catch it
1884 here as this isn't an error. */
1885 return err == -ENODEV ? 0 : err;
1886 }
1887
1888 /* Consistency check */
1889 if (info.type[0] == '\0') {
1890 dev_err(&adapter->dev, "%s detection function provided "
1891 "no name for 0x%x\n", driver->driver.name,
1892 addr);
1893 } else {
1894 struct i2c_client *client;
1895
1896 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01001897 if (adapter->class & I2C_CLASS_DEPRECATED)
1898 dev_warn(&adapter->dev,
1899 "This adapter will soon drop class based instantiation of devices. "
1900 "Please make sure client 0x%02x gets instantiated by other means. "
1901 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
1902 info.addr);
1903
Jean Delvare4735c982008-07-14 22:38:36 +02001904 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1905 info.type, info.addr);
1906 client = i2c_new_device(adapter, &info);
1907 if (client)
1908 list_add_tail(&client->detected, &driver->clients);
1909 else
1910 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1911 info.type, info.addr);
1912 }
1913 return 0;
1914}
1915
1916static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1917{
Jean Delvarec3813d62009-12-14 21:17:25 +01001918 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001919 struct i2c_client *temp_client;
1920 int i, err = 0;
1921 int adap_id = i2c_adapter_id(adapter);
1922
Jean Delvarec3813d62009-12-14 21:17:25 +01001923 address_list = driver->address_list;
1924 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001925 return 0;
1926
Wolfram Sang45552272014-07-10 13:46:21 +02001927 /* Warn that the adapter lost class based instantiation */
1928 if (adapter->class == I2C_CLASS_DEPRECATED) {
1929 dev_dbg(&adapter->dev,
1930 "This adapter dropped support for I2C classes and "
1931 "won't auto-detect %s devices anymore. If you need it, check "
1932 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
1933 driver->driver.name);
1934 return 0;
1935 }
1936
Jean Delvare51b54ba2010-10-24 18:16:58 +02001937 /* Stop here if the classes do not match */
1938 if (!(adapter->class & driver->class))
1939 return 0;
1940
Jean Delvare4735c982008-07-14 22:38:36 +02001941 /* Set up a temporary client to help detect callback */
1942 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1943 if (!temp_client)
1944 return -ENOMEM;
1945 temp_client->adapter = adapter;
1946
Jean Delvarec3813d62009-12-14 21:17:25 +01001947 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001948 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001949 "addr 0x%02x\n", adap_id, address_list[i]);
1950 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001951 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001952 if (unlikely(err))
1953 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001954 }
1955
Jean Delvare4735c982008-07-14 22:38:36 +02001956 kfree(temp_client);
1957 return err;
1958}
1959
Jean Delvared44f19d2010-08-11 18:20:57 +02001960int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1961{
1962 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1963 I2C_SMBUS_QUICK, NULL) >= 0;
1964}
1965EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1966
Jean Delvare12b5053a2007-05-01 23:26:31 +02001967struct i2c_client *
1968i2c_new_probed_device(struct i2c_adapter *adap,
1969 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001970 unsigned short const *addr_list,
1971 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001972{
1973 int i;
1974
Jean Delvare8031d792010-08-11 18:21:00 +02001975 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001976 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001977
Jean Delvare12b5053a2007-05-01 23:26:31 +02001978 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1979 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001980 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001981 dev_warn(&adap->dev, "Invalid 7-bit address "
1982 "0x%02x\n", addr_list[i]);
1983 continue;
1984 }
1985
1986 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001987 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001988 dev_dbg(&adap->dev, "Address 0x%02x already in "
1989 "use, not probing\n", addr_list[i]);
1990 continue;
1991 }
1992
Jean Delvare63e4e802010-06-03 11:33:51 +02001993 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001994 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001995 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001996 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001997
1998 if (addr_list[i] == I2C_CLIENT_END) {
1999 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2000 return NULL;
2001 }
2002
2003 info->addr = addr_list[i];
2004 return i2c_new_device(adap, info);
2005}
2006EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2007
Jean Delvared735b342011-03-20 14:50:52 +01002008struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002011
Jean Delvarecaada322008-01-27 18:14:49 +01002012 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002013 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002014 if (adapter && !try_module_get(adapter->owner))
2015 adapter = NULL;
2016
Jean Delvarecaada322008-01-27 18:14:49 +01002017 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002018 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
David Brownellc0564602007-05-01 23:26:31 +02002020EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022void i2c_put_adapter(struct i2c_adapter *adap)
2023{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002024 if (adap)
2025 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
David Brownellc0564602007-05-01 23:26:31 +02002027EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029/* The SMBus parts */
2030
David Brownell438d6c22006-12-10 21:21:31 +01002031#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002032static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002035
Farid Hammane7225acf2010-05-21 18:40:58 +02002036 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002037 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 data = data ^ POLY;
2039 data = data << 1;
2040 }
2041 return (u8)(data >> 8);
2042}
2043
Jean Delvare421ef472005-10-26 21:28:55 +02002044/* Incremental CRC8 over count bytes in the array pointed to by p */
2045static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 int i;
2048
Farid Hammane7225acf2010-05-21 18:40:58 +02002049 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002050 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return crc;
2052}
2053
Jean Delvare421ef472005-10-26 21:28:55 +02002054/* Assume a 7-bit address, which is reasonable for SMBus */
2055static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Jean Delvare421ef472005-10-26 21:28:55 +02002057 /* The address will be sent first */
2058 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2059 pec = i2c_smbus_pec(pec, &addr, 1);
2060
2061 /* The data buffer follows */
2062 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Jean Delvare421ef472005-10-26 21:28:55 +02002065/* Used for write only transactions */
2066static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Jean Delvare421ef472005-10-26 21:28:55 +02002068 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2069 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Jean Delvare421ef472005-10-26 21:28:55 +02002072/* Return <0 on CRC error
2073 If there was a write before this read (most cases) we need to take the
2074 partial CRC from the write part into account.
2075 Note that this function does modify the message (we need to decrease the
2076 message length to hide the CRC byte from the caller). */
2077static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Jean Delvare421ef472005-10-26 21:28:55 +02002079 u8 rpec = msg->buf[--msg->len];
2080 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (rpec != cpec) {
2083 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2084 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002085 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
David Brownell438d6c22006-12-10 21:21:31 +01002087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
David Brownella1cdeda2008-07-14 22:38:24 +02002090/**
2091 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2092 * @client: Handle to slave device
2093 *
2094 * This executes the SMBus "receive byte" protocol, returning negative errno
2095 * else the byte received from the device.
2096 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002097s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002100 int status;
2101
2102 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2103 I2C_SMBUS_READ, 0,
2104 I2C_SMBUS_BYTE, &data);
2105 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
David Brownellc0564602007-05-01 23:26:31 +02002107EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
David Brownella1cdeda2008-07-14 22:38:24 +02002109/**
2110 * i2c_smbus_write_byte - SMBus "send byte" protocol
2111 * @client: Handle to slave device
2112 * @value: Byte to be sent
2113 *
2114 * This executes the SMBus "send byte" protocol, returning negative errno
2115 * else zero on success.
2116 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002117s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Farid Hammane7225acf2010-05-21 18:40:58 +02002119 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002120 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
David Brownellc0564602007-05-01 23:26:31 +02002122EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
David Brownella1cdeda2008-07-14 22:38:24 +02002124/**
2125 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2126 * @client: Handle to slave device
2127 * @command: Byte interpreted by slave
2128 *
2129 * This executes the SMBus "read byte" protocol, returning negative errno
2130 * else a data byte received from the device.
2131 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002132s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002135 int status;
2136
2137 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2138 I2C_SMBUS_READ, command,
2139 I2C_SMBUS_BYTE_DATA, &data);
2140 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
David Brownellc0564602007-05-01 23:26:31 +02002142EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
David Brownella1cdeda2008-07-14 22:38:24 +02002144/**
2145 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2146 * @client: Handle to slave device
2147 * @command: Byte interpreted by slave
2148 * @value: Byte being written
2149 *
2150 * This executes the SMBus "write byte" protocol, returning negative errno
2151 * else zero on success.
2152 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002153s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2154 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 union i2c_smbus_data data;
2157 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002158 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2159 I2C_SMBUS_WRITE, command,
2160 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
David Brownellc0564602007-05-01 23:26:31 +02002162EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
David Brownella1cdeda2008-07-14 22:38:24 +02002164/**
2165 * i2c_smbus_read_word_data - SMBus "read word" protocol
2166 * @client: Handle to slave device
2167 * @command: Byte interpreted by slave
2168 *
2169 * This executes the SMBus "read word" protocol, returning negative errno
2170 * else a 16-bit unsigned "word" received from the device.
2171 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002172s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002175 int status;
2176
2177 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2178 I2C_SMBUS_READ, command,
2179 I2C_SMBUS_WORD_DATA, &data);
2180 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
David Brownellc0564602007-05-01 23:26:31 +02002182EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
David Brownella1cdeda2008-07-14 22:38:24 +02002184/**
2185 * i2c_smbus_write_word_data - SMBus "write word" protocol
2186 * @client: Handle to slave device
2187 * @command: Byte interpreted by slave
2188 * @value: 16-bit "word" being written
2189 *
2190 * This executes the SMBus "write word" protocol, returning negative errno
2191 * else zero on success.
2192 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002193s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2194 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 union i2c_smbus_data data;
2197 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002198 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2199 I2C_SMBUS_WRITE, command,
2200 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
David Brownellc0564602007-05-01 23:26:31 +02002202EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
David Brownella64ec072007-10-13 23:56:31 +02002204/**
David Brownella1cdeda2008-07-14 22:38:24 +02002205 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002206 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002207 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002208 * @values: Byte array into which data will be read; big enough to hold
2209 * the data returned by the slave. SMBus allows at most 32 bytes.
2210 *
David Brownella1cdeda2008-07-14 22:38:24 +02002211 * This executes the SMBus "block read" protocol, returning negative errno
2212 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002213 *
2214 * Note that using this function requires that the client's adapter support
2215 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2216 * support this; its emulation through I2C messaging relies on a specific
2217 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2218 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002219s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002220 u8 *values)
2221{
2222 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002223 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002224
David Brownell24a5bb72008-07-14 22:38:23 +02002225 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2226 I2C_SMBUS_READ, command,
2227 I2C_SMBUS_BLOCK_DATA, &data);
2228 if (status)
2229 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002230
2231 memcpy(values, &data.block[1], data.block[0]);
2232 return data.block[0];
2233}
2234EXPORT_SYMBOL(i2c_smbus_read_block_data);
2235
David Brownella1cdeda2008-07-14 22:38:24 +02002236/**
2237 * i2c_smbus_write_block_data - SMBus "block write" protocol
2238 * @client: Handle to slave device
2239 * @command: Byte interpreted by slave
2240 * @length: Size of data block; SMBus allows at most 32 bytes
2241 * @values: Byte array which will be written.
2242 *
2243 * This executes the SMBus "block write" protocol, returning negative errno
2244 * else zero on success.
2245 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002246s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002247 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (length > I2C_SMBUS_BLOCK_MAX)
2252 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002254 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002255 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2256 I2C_SMBUS_WRITE, command,
2257 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
David Brownellc0564602007-05-01 23:26:31 +02002259EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002262s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002263 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002266 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002267
Jean Delvare4b2643d2007-07-12 14:12:29 +02002268 if (length > I2C_SMBUS_BLOCK_MAX)
2269 length = I2C_SMBUS_BLOCK_MAX;
2270 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002271 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2272 I2C_SMBUS_READ, command,
2273 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2274 if (status < 0)
2275 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002276
2277 memcpy(values, &data.block[1], data.block[0]);
2278 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
David Brownellc0564602007-05-01 23:26:31 +02002280EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jean Delvare0cc43a12011-01-10 22:11:23 +01002282s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002283 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002284{
2285 union i2c_smbus_data data;
2286
2287 if (length > I2C_SMBUS_BLOCK_MAX)
2288 length = I2C_SMBUS_BLOCK_MAX;
2289 data.block[0] = length;
2290 memcpy(data.block + 1, values, length);
2291 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2292 I2C_SMBUS_WRITE, command,
2293 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2294}
David Brownellc0564602007-05-01 23:26:31 +02002295EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002296
David Brownell438d6c22006-12-10 21:21:31 +01002297/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002299static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2300 unsigned short flags,
2301 char read_write, u8 command, int size,
2302 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 /* So we need to generate a series of msgs. In the case of writing, we
2305 need to use only one message; when reading, we need two. We initialize
2306 most things with sane defaults, to keep the code below somewhat
2307 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002308 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2309 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002310 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002312 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002313 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002314 struct i2c_msg msg[2] = {
2315 {
2316 .addr = addr,
2317 .flags = flags,
2318 .len = 1,
2319 .buf = msgbuf0,
2320 }, {
2321 .addr = addr,
2322 .flags = flags | I2C_M_RD,
2323 .len = 0,
2324 .buf = msgbuf1,
2325 },
2326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002329 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 case I2C_SMBUS_QUICK:
2331 msg[0].len = 0;
2332 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002333 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2334 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 num = 1;
2336 break;
2337 case I2C_SMBUS_BYTE:
2338 if (read_write == I2C_SMBUS_READ) {
2339 /* Special case: only a read! */
2340 msg[0].flags = I2C_M_RD | flags;
2341 num = 1;
2342 }
2343 break;
2344 case I2C_SMBUS_BYTE_DATA:
2345 if (read_write == I2C_SMBUS_READ)
2346 msg[1].len = 1;
2347 else {
2348 msg[0].len = 2;
2349 msgbuf0[1] = data->byte;
2350 }
2351 break;
2352 case I2C_SMBUS_WORD_DATA:
2353 if (read_write == I2C_SMBUS_READ)
2354 msg[1].len = 2;
2355 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002356 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002358 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360 break;
2361 case I2C_SMBUS_PROC_CALL:
2362 num = 2; /* Special case */
2363 read_write = I2C_SMBUS_READ;
2364 msg[0].len = 3;
2365 msg[1].len = 2;
2366 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002367 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 break;
2369 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002371 msg[1].flags |= I2C_M_RECV_LEN;
2372 msg[1].len = 1; /* block length will be added by
2373 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 } else {
2375 msg[0].len = data->block[0] + 2;
2376 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002377 dev_err(&adapter->dev,
2378 "Invalid block write size %d\n",
2379 data->block[0]);
2380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002382 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 msgbuf0[i] = data->block[i-1];
2384 }
2385 break;
2386 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002387 num = 2; /* Another special case */
2388 read_write = I2C_SMBUS_READ;
2389 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002390 dev_err(&adapter->dev,
2391 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002392 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002393 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002394 }
2395 msg[0].len = data->block[0] + 2;
2396 for (i = 1; i < msg[0].len; i++)
2397 msgbuf0[i] = data->block[i-1];
2398 msg[1].flags |= I2C_M_RECV_LEN;
2399 msg[1].len = 1; /* block length will be added by
2400 the underlying bus driver */
2401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 case I2C_SMBUS_I2C_BLOCK_DATA:
2403 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002404 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 } else {
2406 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002407 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002408 dev_err(&adapter->dev,
2409 "Invalid block write size %d\n",
2410 data->block[0]);
2411 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 for (i = 1; i <= data->block[0]; i++)
2414 msgbuf0[i] = data->block[i];
2415 }
2416 break;
2417 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002418 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2419 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421
Jean Delvare421ef472005-10-26 21:28:55 +02002422 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2423 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2424 if (i) {
2425 /* Compute PEC if first message is a write */
2426 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002427 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002428 i2c_smbus_add_pec(&msg[0]);
2429 else /* Write followed by read */
2430 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2431 }
2432 /* Ask for PEC if last message is a read */
2433 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002434 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002435 }
2436
David Brownell24a5bb72008-07-14 22:38:23 +02002437 status = i2c_transfer(adapter, msg, num);
2438 if (status < 0)
2439 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Jean Delvare421ef472005-10-26 21:28:55 +02002441 /* Check PEC if last message is a read */
2442 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002443 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2444 if (status < 0)
2445 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002446 }
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002449 switch (size) {
2450 case I2C_SMBUS_BYTE:
2451 data->byte = msgbuf0[0];
2452 break;
2453 case I2C_SMBUS_BYTE_DATA:
2454 data->byte = msgbuf1[0];
2455 break;
2456 case I2C_SMBUS_WORD_DATA:
2457 case I2C_SMBUS_PROC_CALL:
2458 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2459 break;
2460 case I2C_SMBUS_I2C_BLOCK_DATA:
2461 for (i = 0; i < data->block[0]; i++)
2462 data->block[i+1] = msgbuf1[i];
2463 break;
2464 case I2C_SMBUS_BLOCK_DATA:
2465 case I2C_SMBUS_BLOCK_PROC_CALL:
2466 for (i = 0; i < msgbuf1[0] + 1; i++)
2467 data->block[i] = msgbuf1[i];
2468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470 return 0;
2471}
2472
David Brownella1cdeda2008-07-14 22:38:24 +02002473/**
2474 * i2c_smbus_xfer - execute SMBus protocol operations
2475 * @adapter: Handle to I2C bus
2476 * @addr: Address of SMBus slave on that bus
2477 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2478 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2479 * @command: Byte interpreted by slave, for protocols which use such bytes
2480 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2481 * @data: Data to be read or written
2482 *
2483 * This executes an SMBus protocol operation, and returns a negative
2484 * errno code else zero on success.
2485 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002486s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002487 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002488 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002490 unsigned long orig_jiffies;
2491 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
David Howells8a325992014-03-06 13:36:06 +00002494 /* If enabled, the following two tracepoints are conditional on
2495 * read_write and protocol.
2496 */
2497 trace_smbus_write(adapter, addr, flags, read_write,
2498 command, protocol, data);
2499 trace_smbus_read(adapter, addr, flags, read_write,
2500 command, protocol);
2501
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002502 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002505 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002506
2507 /* Retry automatically on arbitration loss */
2508 orig_jiffies = jiffies;
2509 for (res = 0, try = 0; try <= adapter->retries; try++) {
2510 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2511 read_write, command,
2512 protocol, data);
2513 if (res != -EAGAIN)
2514 break;
2515 if (time_after(jiffies,
2516 orig_jiffies + adapter->timeout))
2517 break;
2518 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002519 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002521 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002522 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002523 /*
2524 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2525 * implement native support for the SMBus operation.
2526 */
2527 }
2528
David Howells8a325992014-03-06 13:36:06 +00002529 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2530 command, protocol, data);
2531
2532trace:
2533 /* If enabled, the reply tracepoint is conditional on read_write. */
2534 trace_smbus_reply(adapter, addr, flags, read_write,
2535 command, protocol, data);
2536 trace_smbus_result(adapter, addr, flags, read_write,
2537 command, protocol, res);
2538
2539 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2544MODULE_DESCRIPTION("I2C-Bus main module");
2545MODULE_LICENSE("GPL");