i2c: apply DT flags when probing
[deliverable/linux.git] / drivers / i2c / i2c-core.c
CommitLineData
1da177e4
LT
1/* 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
ca1f8da9 13 GNU General Public License for more details. */
1da177e4
LT
14/* ------------------------------------------------------------------------- */
15
96de0e25 16/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
1da177e4 17 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
421ef47b 18 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
7c81c60f 19 Jean Delvare <jdelvare@suse.de>
0826374b 20 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
687b81d0
WS
21 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
17f4a5c4
WS
25 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
4b1acc43 27 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
687b81d0 28 */
1da177e4 29
b4e2f6ac 30#include <dt-bindings/i2c/i2c.h>
1da177e4
LT
31#include <linux/module.h>
32#include <linux/kernel.h>
5f9296ba 33#include <linux/delay.h>
1da177e4 34#include <linux/errno.h>
5f9296ba 35#include <linux/gpio.h>
1da177e4
LT
36#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/init.h>
39#include <linux/idr.h>
b3585e4f 40#include <linux/mutex.h>
687b81d0 41#include <linux/of.h>
959e85f7 42#include <linux/of_device.h>
687b81d0 43#include <linux/of_irq.h>
86be408b 44#include <linux/clk/clk-conf.h>
b8d6f45b 45#include <linux/completion.h>
cea443a8
MR
46#include <linux/hardirq.h>
47#include <linux/irqflags.h>
f18c41da 48#include <linux/rwsem.h>
6de468ae 49#include <linux/pm_runtime.h>
f48c767c 50#include <linux/pm_domain.h>
907ddf89 51#include <linux/acpi.h>
d9a83d62 52#include <linux/jump_label.h>
1da177e4 53#include <asm/uaccess.h>
a430a345 54#include <linux/err.h>
1da177e4 55
9c1600ed
DB
56#include "i2c-core.h"
57
d9a83d62
DH
58#define CREATE_TRACE_POINTS
59#include <trace/events/i2c.h>
1da177e4 60
da899f55
WS
61#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
62#define I2C_ADDR_OFFSET_SLAVE 0x1000
63
6629dcff 64/* core_lock protects i2c_adapter_idr, and guarantees
35fc37f8 65 that device detection, deletion of detected devices, and attach_adapter
19baba4c 66 calls are serialized */
caada32a 67static DEFINE_MUTEX(core_lock);
1da177e4
LT
68static DEFINE_IDR(i2c_adapter_idr);
69
4f8cf824 70static struct device_type i2c_client_type;
4735c98f 71static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
f37dd80a 72
d9a83d62
DH
73static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
74
75void i2c_transfer_trace_reg(void)
76{
77 static_key_slow_inc(&i2c_trace_msg);
78}
79
80void i2c_transfer_trace_unreg(void)
81{
82 static_key_slow_dec(&i2c_trace_msg);
83}
84
17f4a5c4
WS
85#if defined(CONFIG_ACPI)
86struct acpi_i2c_handler_data {
87 struct acpi_connection_info info;
88 struct i2c_adapter *adapter;
89};
90
91struct gsb_buffer {
92 u8 status;
93 u8 len;
94 union {
95 u16 wdata;
96 u8 bdata;
97 u8 data[0];
98 };
99} __packed;
100
101static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
102{
103 struct i2c_board_info *info = data;
104
105 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
106 struct acpi_resource_i2c_serialbus *sb;
107
108 sb = &ares->data.i2c_serial_bus;
393cc1ce 109 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
17f4a5c4
WS
110 info->addr = sb->slave_address;
111 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
112 info->flags |= I2C_CLIENT_TEN;
113 }
dab472eb 114 } else if (!info->irq) {
17f4a5c4
WS
115 struct resource r;
116
117 if (acpi_dev_resource_interrupt(ares, 0, &r))
118 info->irq = r.start;
119 }
120
121 /* Tell the ACPI core to skip this resource */
122 return 1;
123}
124
125static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
126 void *data, void **return_value)
127{
128 struct i2c_adapter *adapter = data;
129 struct list_head resource_list;
130 struct i2c_board_info info;
131 struct acpi_device *adev;
132 int ret;
133
134 if (acpi_bus_get_device(handle, &adev))
135 return AE_OK;
136 if (acpi_bus_get_status(adev) || !adev->status.present)
137 return AE_OK;
138
139 memset(&info, 0, sizeof(info));
ce793486 140 info.fwnode = acpi_fwnode_handle(adev);
17f4a5c4
WS
141
142 INIT_LIST_HEAD(&resource_list);
143 ret = acpi_dev_get_resources(adev, &resource_list,
144 acpi_i2c_add_resource, &info);
145 acpi_dev_free_resource_list(&resource_list);
146
147 if (ret < 0 || !info.addr)
148 return AE_OK;
149
150 adev->power.flags.ignore_parent = true;
151 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
152 if (!i2c_new_device(adapter, &info)) {
153 adev->power.flags.ignore_parent = false;
154 dev_err(&adapter->dev,
155 "failed to add I2C device %s from ACPI\n",
156 dev_name(&adev->dev));
157 }
158
159 return AE_OK;
160}
161
162/**
163 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
164 * @adap: pointer to adapter
165 *
166 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
167 * namespace. When a device is found it will be added to the Linux device
168 * model and bound to the corresponding ACPI handle.
169 */
170static void acpi_i2c_register_devices(struct i2c_adapter *adap)
171{
172 acpi_handle handle;
173 acpi_status status;
174
175 if (!adap->dev.parent)
176 return;
177
178 handle = ACPI_HANDLE(adap->dev.parent);
179 if (!handle)
180 return;
181
182 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
183 acpi_i2c_add_device, NULL,
184 adap, NULL);
185 if (ACPI_FAILURE(status))
186 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
187}
188
189#else /* CONFIG_ACPI */
190static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
191#endif /* CONFIG_ACPI */
192
193#ifdef CONFIG_ACPI_I2C_OPREGION
194static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
195 u8 cmd, u8 *data, u8 data_len)
196{
197
198 struct i2c_msg msgs[2];
199 int ret;
200 u8 *buffer;
201
202 buffer = kzalloc(data_len, GFP_KERNEL);
203 if (!buffer)
204 return AE_NO_MEMORY;
205
206 msgs[0].addr = client->addr;
207 msgs[0].flags = client->flags;
208 msgs[0].len = 1;
209 msgs[0].buf = &cmd;
210
211 msgs[1].addr = client->addr;
212 msgs[1].flags = client->flags | I2C_M_RD;
213 msgs[1].len = data_len;
214 msgs[1].buf = buffer;
215
216 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
217 if (ret < 0)
218 dev_err(&client->adapter->dev, "i2c read failed\n");
219 else
220 memcpy(data, buffer, data_len);
221
222 kfree(buffer);
223 return ret;
224}
225
226static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
227 u8 cmd, u8 *data, u8 data_len)
228{
229
230 struct i2c_msg msgs[1];
231 u8 *buffer;
232 int ret = AE_OK;
233
234 buffer = kzalloc(data_len + 1, GFP_KERNEL);
235 if (!buffer)
236 return AE_NO_MEMORY;
237
238 buffer[0] = cmd;
239 memcpy(buffer + 1, data, data_len);
240
241 msgs[0].addr = client->addr;
242 msgs[0].flags = client->flags;
243 msgs[0].len = data_len + 1;
244 msgs[0].buf = buffer;
245
246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
247 if (ret < 0)
248 dev_err(&client->adapter->dev, "i2c write failed\n");
249
250 kfree(buffer);
251 return ret;
252}
253
254static acpi_status
255acpi_i2c_space_handler(u32 function, acpi_physical_address command,
256 u32 bits, u64 *value64,
257 void *handler_context, void *region_context)
258{
259 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
260 struct acpi_i2c_handler_data *data = handler_context;
261 struct acpi_connection_info *info = &data->info;
262 struct acpi_resource_i2c_serialbus *sb;
263 struct i2c_adapter *adapter = data->adapter;
7ef85f5f 264 struct i2c_client *client;
17f4a5c4
WS
265 struct acpi_resource *ares;
266 u32 accessor_type = function >> 16;
267 u8 action = function & ACPI_IO_MASK;
4470c725 268 acpi_status ret;
17f4a5c4
WS
269 int status;
270
271 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
272 if (ACPI_FAILURE(ret))
273 return ret;
274
7ef85f5f
JN
275 client = kzalloc(sizeof(*client), GFP_KERNEL);
276 if (!client) {
277 ret = AE_NO_MEMORY;
278 goto err;
279 }
280
17f4a5c4
WS
281 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
282 ret = AE_BAD_PARAMETER;
283 goto err;
284 }
285
286 sb = &ares->data.i2c_serial_bus;
287 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
288 ret = AE_BAD_PARAMETER;
289 goto err;
290 }
291
7ef85f5f
JN
292 client->adapter = adapter;
293 client->addr = sb->slave_address;
17f4a5c4
WS
294
295 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
7ef85f5f 296 client->flags |= I2C_CLIENT_TEN;
17f4a5c4
WS
297
298 switch (accessor_type) {
299 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
300 if (action == ACPI_READ) {
7ef85f5f 301 status = i2c_smbus_read_byte(client);
17f4a5c4
WS
302 if (status >= 0) {
303 gsb->bdata = status;
304 status = 0;
305 }
306 } else {
7ef85f5f 307 status = i2c_smbus_write_byte(client, gsb->bdata);
17f4a5c4
WS
308 }
309 break;
310
311 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
312 if (action == ACPI_READ) {
7ef85f5f 313 status = i2c_smbus_read_byte_data(client, command);
17f4a5c4
WS
314 if (status >= 0) {
315 gsb->bdata = status;
316 status = 0;
317 }
318 } else {
7ef85f5f 319 status = i2c_smbus_write_byte_data(client, command,
17f4a5c4
WS
320 gsb->bdata);
321 }
322 break;
323
324 case ACPI_GSB_ACCESS_ATTRIB_WORD:
325 if (action == ACPI_READ) {
7ef85f5f 326 status = i2c_smbus_read_word_data(client, command);
17f4a5c4
WS
327 if (status >= 0) {
328 gsb->wdata = status;
329 status = 0;
330 }
331 } else {
7ef85f5f 332 status = i2c_smbus_write_word_data(client, command,
17f4a5c4
WS
333 gsb->wdata);
334 }
335 break;
336
337 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
338 if (action == ACPI_READ) {
7ef85f5f 339 status = i2c_smbus_read_block_data(client, command,
17f4a5c4
WS
340 gsb->data);
341 if (status >= 0) {
342 gsb->len = status;
343 status = 0;
344 }
345 } else {
7ef85f5f 346 status = i2c_smbus_write_block_data(client, command,
17f4a5c4
WS
347 gsb->len, gsb->data);
348 }
349 break;
350
351 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
352 if (action == ACPI_READ) {
7ef85f5f 353 status = acpi_gsb_i2c_read_bytes(client, command,
17f4a5c4
WS
354 gsb->data, info->access_length);
355 if (status > 0)
356 status = 0;
357 } else {
7ef85f5f 358 status = acpi_gsb_i2c_write_bytes(client, command,
17f4a5c4
WS
359 gsb->data, info->access_length);
360 }
361 break;
362
363 default:
364 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
365 ret = AE_BAD_PARAMETER;
366 goto err;
367 }
368
369 gsb->status = status;
370
371 err:
7ef85f5f 372 kfree(client);
17f4a5c4
WS
373 ACPI_FREE(ares);
374 return ret;
375}
376
377
378static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
379{
0aef44e8 380 acpi_handle handle;
17f4a5c4
WS
381 struct acpi_i2c_handler_data *data;
382 acpi_status status;
383
0aef44e8
PH
384 if (!adapter->dev.parent)
385 return -ENODEV;
386
387 handle = ACPI_HANDLE(adapter->dev.parent);
388
17f4a5c4
WS
389 if (!handle)
390 return -ENODEV;
391
392 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
393 GFP_KERNEL);
394 if (!data)
395 return -ENOMEM;
396
397 data->adapter = adapter;
398 status = acpi_bus_attach_private_data(handle, (void *)data);
399 if (ACPI_FAILURE(status)) {
400 kfree(data);
401 return -ENOMEM;
402 }
403
404 status = acpi_install_address_space_handler(handle,
405 ACPI_ADR_SPACE_GSBUS,
406 &acpi_i2c_space_handler,
407 NULL,
408 data);
409 if (ACPI_FAILURE(status)) {
410 dev_err(&adapter->dev, "Error installing i2c space handler\n");
411 acpi_bus_detach_private_data(handle);
412 kfree(data);
413 return -ENOMEM;
414 }
415
40e7fcb1 416 acpi_walk_dep_device_list(handle);
17f4a5c4
WS
417 return 0;
418}
419
420static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
421{
0aef44e8 422 acpi_handle handle;
17f4a5c4
WS
423 struct acpi_i2c_handler_data *data;
424 acpi_status status;
425
0aef44e8
PH
426 if (!adapter->dev.parent)
427 return;
428
429 handle = ACPI_HANDLE(adapter->dev.parent);
430
17f4a5c4
WS
431 if (!handle)
432 return;
433
434 acpi_remove_address_space_handler(handle,
435 ACPI_ADR_SPACE_GSBUS,
436 &acpi_i2c_space_handler);
437
438 status = acpi_bus_get_private_data(handle, (void **)&data);
439 if (ACPI_SUCCESS(status))
440 kfree(data);
441
442 acpi_bus_detach_private_data(handle);
443}
444#else /* CONFIG_ACPI_I2C_OPREGION */
445static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
446{ }
447
448static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
449{ return 0; }
450#endif /* CONFIG_ACPI_I2C_OPREGION */
451
f37dd80a
DB
452/* ------------------------------------------------------------------------- */
453
d2653e92
JD
454static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
455 const struct i2c_client *client)
456{
457 while (id->name[0]) {
458 if (strcmp(client->name, id->name) == 0)
459 return id;
460 id++;
461 }
462 return NULL;
463}
464
1da177e4
LT
465static int i2c_device_match(struct device *dev, struct device_driver *drv)
466{
51298d12
JD
467 struct i2c_client *client = i2c_verify_client(dev);
468 struct i2c_driver *driver;
469
470 if (!client)
471 return 0;
7b4fbc50 472
959e85f7
GL
473 /* Attempt an OF style match */
474 if (of_driver_match_device(dev, drv))
475 return 1;
476
907ddf89
MW
477 /* Then ACPI style match */
478 if (acpi_driver_match_device(dev, drv))
479 return 1;
480
51298d12 481 driver = to_i2c_driver(drv);
d2653e92
JD
482 /* match on an id table if there is one */
483 if (driver->id_table)
484 return i2c_match_id(driver->id_table, client) != NULL;
485
eb8a7908 486 return 0;
1da177e4
LT
487}
488
7b4fbc50
DB
489
490/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
7eff2e7a 491static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
7b4fbc50
DB
492{
493 struct i2c_client *client = to_i2c_client(dev);
8c4ff6d0
ZR
494 int rc;
495
496 rc = acpi_device_uevent_modalias(dev, env);
497 if (rc != -ENODEV)
498 return rc;
7b4fbc50 499
eb8a7908
JD
500 if (add_uevent_var(env, "MODALIAS=%s%s",
501 I2C_MODULE_PREFIX, client->name))
502 return -ENOMEM;
7b4fbc50
DB
503 dev_dbg(dev, "uevent\n");
504 return 0;
505}
506
5f9296ba
VK
507/* i2c bus recovery routines */
508static int get_scl_gpio_value(struct i2c_adapter *adap)
509{
510 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
511}
512
513static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
514{
515 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
516}
517
518static int get_sda_gpio_value(struct i2c_adapter *adap)
519{
520 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
521}
522
523static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
524{
525 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
526 struct device *dev = &adap->dev;
527 int ret = 0;
528
529 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
530 GPIOF_OUT_INIT_HIGH, "i2c-scl");
531 if (ret) {
532 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
533 return ret;
534 }
535
536 if (bri->get_sda) {
537 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
538 /* work without SDA polling */
539 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
540 bri->sda_gpio);
541 bri->get_sda = NULL;
542 }
543 }
544
545 return ret;
546}
547
548static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
549{
550 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
551
552 if (bri->get_sda)
553 gpio_free(bri->sda_gpio);
554
555 gpio_free(bri->scl_gpio);
556}
557
558/*
559 * We are generating clock pulses. ndelay() determines durating of clk pulses.
560 * We will generate clock with rate 100 KHz and so duration of both clock levels
561 * is: delay in ns = (10^6 / 100) / 2
562 */
563#define RECOVERY_NDELAY 5000
564#define RECOVERY_CLK_CNT 9
565
566static int i2c_generic_recovery(struct i2c_adapter *adap)
567{
568 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
569 int i = 0, val = 1, ret = 0;
570
571 if (bri->prepare_recovery)
2b2190a3 572 bri->prepare_recovery(adap);
5f9296ba 573
8b062608
JL
574 bri->set_scl(adap, val);
575 ndelay(RECOVERY_NDELAY);
576
5f9296ba
VK
577 /*
578 * By this time SCL is high, as we need to give 9 falling-rising edges
579 */
580 while (i++ < RECOVERY_CLK_CNT * 2) {
581 if (val) {
582 /* Break if SDA is high */
583 if (bri->get_sda && bri->get_sda(adap))
584 break;
585 /* SCL shouldn't be low here */
586 if (!bri->get_scl(adap)) {
587 dev_err(&adap->dev,
588 "SCL is stuck low, exit recovery\n");
589 ret = -EBUSY;
590 break;
591 }
592 }
593
594 val = !val;
595 bri->set_scl(adap, val);
596 ndelay(RECOVERY_NDELAY);
597 }
598
599 if (bri->unprepare_recovery)
2b2190a3 600 bri->unprepare_recovery(adap);
5f9296ba
VK
601
602 return ret;
603}
604
605int i2c_generic_scl_recovery(struct i2c_adapter *adap)
606{
5f9296ba
VK
607 return i2c_generic_recovery(adap);
608}
c1c21f4e 609EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
5f9296ba
VK
610
611int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
612{
613 int ret;
614
615 ret = i2c_get_gpios_for_recovery(adap);
616 if (ret)
617 return ret;
618
619 ret = i2c_generic_recovery(adap);
620 i2c_put_gpios_for_recovery(adap);
621
622 return ret;
623}
c1c21f4e 624EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
5f9296ba
VK
625
626int i2c_recover_bus(struct i2c_adapter *adap)
627{
628 if (!adap->bus_recovery_info)
629 return -EOPNOTSUPP;
630
631 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
632 return adap->bus_recovery_info->recover_bus(adap);
633}
c1c21f4e 634EXPORT_SYMBOL_GPL(i2c_recover_bus);
5f9296ba 635
f37dd80a 636static int i2c_device_probe(struct device *dev)
1da177e4 637{
51298d12
JD
638 struct i2c_client *client = i2c_verify_client(dev);
639 struct i2c_driver *driver;
50c3304a 640 int status;
7b4fbc50 641
51298d12
JD
642 if (!client)
643 return 0;
644
845c8770
MW
645 if (!client->irq) {
646 int irq = -ENOENT;
647
648 if (dev->of_node)
649 irq = of_irq_get(dev->of_node, 0);
650 else if (ACPI_COMPANION(dev))
651 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
2fd36c55 652
6f34be74 653 if (irq == -EPROBE_DEFER)
2fd36c55 654 return irq;
6f34be74
GU
655 if (irq < 0)
656 irq = 0;
2fd36c55
LP
657
658 client->irq = irq;
659 }
660
51298d12 661 driver = to_i2c_driver(dev->driver);
e0457442 662 if (!driver->probe || !driver->id_table)
7b4fbc50 663 return -ENODEV;
0acc2b32 664
ee35425c
MP
665 if (!device_can_wakeup(&client->dev))
666 device_init_wakeup(&client->dev,
667 client->flags & I2C_CLIENT_WAKE);
7b4fbc50 668 dev_dbg(dev, "probe\n");
d2653e92 669
86be408b
SN
670 status = of_clk_set_defaults(dev->of_node, false);
671 if (status < 0)
672 return status;
673
e09b0d4e
UH
674 status = dev_pm_domain_attach(&client->dev, true);
675 if (status != -EPROBE_DEFER) {
676 status = driver->probe(client, i2c_match_id(driver->id_table,
677 client));
678 if (status)
679 dev_pm_domain_detach(&client->dev, true);
680 }
72fa818e 681
50c3304a 682 return status;
f37dd80a 683}
1da177e4 684
f37dd80a
DB
685static int i2c_device_remove(struct device *dev)
686{
51298d12 687 struct i2c_client *client = i2c_verify_client(dev);
a1d9e6e4 688 struct i2c_driver *driver;
72fa818e 689 int status = 0;
a1d9e6e4 690
51298d12 691 if (!client || !dev->driver)
a1d9e6e4
DB
692 return 0;
693
694 driver = to_i2c_driver(dev->driver);
695 if (driver->remove) {
696 dev_dbg(dev, "remove\n");
697 status = driver->remove(client);
a1d9e6e4 698 }
72fa818e 699
e09b0d4e 700 dev_pm_domain_detach(&client->dev, true);
a1d9e6e4 701 return status;
1da177e4
LT
702}
703
f37dd80a 704static void i2c_device_shutdown(struct device *dev)
1da177e4 705{
51298d12 706 struct i2c_client *client = i2c_verify_client(dev);
f37dd80a
DB
707 struct i2c_driver *driver;
708
51298d12 709 if (!client || !dev->driver)
f37dd80a
DB
710 return;
711 driver = to_i2c_driver(dev->driver);
712 if (driver->shutdown)
51298d12 713 driver->shutdown(client);
1da177e4
LT
714}
715
9c1600ed
DB
716static void i2c_client_dev_release(struct device *dev)
717{
718 kfree(to_i2c_client(dev));
719}
720
09b8ce0a 721static ssize_t
4f8cf824 722show_name(struct device *dev, struct device_attribute *attr, char *buf)
7b4fbc50 723{
4f8cf824
JD
724 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
725 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
7b4fbc50 726}
a5eb71b2 727static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
7b4fbc50 728
09b8ce0a
ZX
729static ssize_t
730show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
7b4fbc50
DB
731{
732 struct i2c_client *client = to_i2c_client(dev);
8c4ff6d0
ZR
733 int len;
734
735 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
736 if (len != -ENODEV)
737 return len;
738
eb8a7908 739 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
7b4fbc50 740}
51298d12
JD
741static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
742
743static struct attribute *i2c_dev_attrs[] = {
744 &dev_attr_name.attr,
7b4fbc50 745 /* modalias helps coldplug: modprobe $(cat .../modalias) */
51298d12
JD
746 &dev_attr_modalias.attr,
747 NULL
748};
a5eb71b2 749ATTRIBUTE_GROUPS(i2c_dev);
54067ee2 750
e9ca9eb9 751struct bus_type i2c_bus_type = {
f37dd80a
DB
752 .name = "i2c",
753 .match = i2c_device_match,
754 .probe = i2c_device_probe,
755 .remove = i2c_device_remove,
756 .shutdown = i2c_device_shutdown,
b864c7d5 757};
e9ca9eb9 758EXPORT_SYMBOL_GPL(i2c_bus_type);
b864c7d5 759
51298d12 760static struct device_type i2c_client_type = {
a5eb71b2 761 .groups = i2c_dev_groups,
51298d12
JD
762 .uevent = i2c_device_uevent,
763 .release = i2c_client_dev_release,
764};
765
9b766b81
DB
766
767/**
768 * i2c_verify_client - return parameter as i2c_client, or NULL
769 * @dev: device, probably from some driver model iterator
770 *
771 * When traversing the driver model tree, perhaps using driver model
772 * iterators like @device_for_each_child(), you can't assume very much
773 * about the nodes you find. Use this function to avoid oopses caused
774 * by wrongly treating some non-I2C device as an i2c_client.
775 */
776struct i2c_client *i2c_verify_client(struct device *dev)
777{
51298d12 778 return (dev->type == &i2c_client_type)
9b766b81
DB
779 ? to_i2c_client(dev)
780 : NULL;
781}
782EXPORT_SYMBOL(i2c_verify_client);
783
784
da899f55
WS
785/* Return a unique address which takes the flags of the client into account */
786static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
787{
788 unsigned short addr = client->addr;
789
790 /* For some client flags, add an arbitrary offset to avoid collisions */
791 if (client->flags & I2C_CLIENT_TEN)
792 addr |= I2C_ADDR_OFFSET_TEN_BIT;
793
794 if (client->flags & I2C_CLIENT_SLAVE)
795 addr |= I2C_ADDR_OFFSET_SLAVE;
796
797 return addr;
798}
799
3a89db5f 800/* This is a permissive address validity check, I2C address map constraints
25985edc 801 * are purposely not enforced, except for the general call address. */
c4019b70 802static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
3a89db5f 803{
c4019b70 804 if (flags & I2C_CLIENT_TEN) {
3a89db5f 805 /* 10-bit address, all values are valid */
c4019b70 806 if (addr > 0x3ff)
3a89db5f
JD
807 return -EINVAL;
808 } else {
809 /* 7-bit address, reject the general call address */
c4019b70 810 if (addr == 0x00 || addr > 0x7f)
3a89db5f
JD
811 return -EINVAL;
812 }
813 return 0;
814}
815
656b8761
JD
816/* And this is a strict address validity check, used when probing. If a
817 * device uses a reserved address, then it shouldn't be probed. 7-bit
818 * addressing is assumed, 10-bit address devices are rare and should be
819 * explicitly enumerated. */
66be6056 820static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
656b8761
JD
821{
822 /*
823 * Reserved addresses per I2C specification:
824 * 0x00 General call address / START byte
825 * 0x01 CBUS address
826 * 0x02 Reserved for different bus format
827 * 0x03 Reserved for future purposes
828 * 0x04-0x07 Hs-mode master code
829 * 0x78-0x7b 10-bit slave addressing
830 * 0x7c-0x7f Reserved for future purposes
831 */
832 if (addr < 0x08 || addr > 0x77)
833 return -EINVAL;
834 return 0;
835}
836
3b5f794b
JD
837static int __i2c_check_addr_busy(struct device *dev, void *addrp)
838{
839 struct i2c_client *client = i2c_verify_client(dev);
840 int addr = *(int *)addrp;
841
842 if (client && client->addr == addr)
843 return -EBUSY;
844 return 0;
845}
846
0826374b
ML
847/* walk up mux tree */
848static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
849{
97cc4d49 850 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0826374b
ML
851 int result;
852
853 result = device_for_each_child(&adapter->dev, &addr,
854 __i2c_check_addr_busy);
855
97cc4d49
JD
856 if (!result && parent)
857 result = i2c_check_mux_parents(parent, addr);
0826374b
ML
858
859 return result;
860}
861
862/* recurse down mux tree */
863static int i2c_check_mux_children(struct device *dev, void *addrp)
864{
865 int result;
866
867 if (dev->type == &i2c_adapter_type)
868 result = device_for_each_child(dev, addrp,
869 i2c_check_mux_children);
870 else
871 result = __i2c_check_addr_busy(dev, addrp);
872
873 return result;
874}
875
3b5f794b
JD
876static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
877{
97cc4d49 878 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
0826374b
ML
879 int result = 0;
880
97cc4d49
JD
881 if (parent)
882 result = i2c_check_mux_parents(parent, addr);
0826374b
ML
883
884 if (!result)
885 result = device_for_each_child(&adapter->dev, &addr,
886 i2c_check_mux_children);
887
888 return result;
3b5f794b
JD
889}
890
fe61e07e
JD
891/**
892 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
893 * @adapter: Target I2C bus segment
894 */
895void i2c_lock_adapter(struct i2c_adapter *adapter)
896{
97cc4d49
JD
897 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
898
899 if (parent)
900 i2c_lock_adapter(parent);
0826374b
ML
901 else
902 rt_mutex_lock(&adapter->bus_lock);
fe61e07e
JD
903}
904EXPORT_SYMBOL_GPL(i2c_lock_adapter);
905
906/**
907 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
908 * @adapter: Target I2C bus segment
909 */
910static int i2c_trylock_adapter(struct i2c_adapter *adapter)
911{
97cc4d49
JD
912 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
913
914 if (parent)
915 return i2c_trylock_adapter(parent);
0826374b
ML
916 else
917 return rt_mutex_trylock(&adapter->bus_lock);
fe61e07e
JD
918}
919
920/**
921 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
922 * @adapter: Target I2C bus segment
923 */
924void i2c_unlock_adapter(struct i2c_adapter *adapter)
925{
97cc4d49
JD
926 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
927
928 if (parent)
929 i2c_unlock_adapter(parent);
0826374b
ML
930 else
931 rt_mutex_unlock(&adapter->bus_lock);
fe61e07e
JD
932}
933EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
934
70762abb
JN
935static void i2c_dev_set_name(struct i2c_adapter *adap,
936 struct i2c_client *client)
937{
938 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
939
940 if (adev) {
941 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
942 return;
943 }
944
70762abb 945 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
da899f55 946 i2c_encode_flags_to_addr(client));
70762abb
JN
947}
948
9c1600ed 949/**
f8a227e8 950 * i2c_new_device - instantiate an i2c device
9c1600ed
DB
951 * @adap: the adapter managing the device
952 * @info: describes one I2C device; bus_num is ignored
d64f73be 953 * Context: can sleep
9c1600ed 954 *
f8a227e8
JD
955 * Create an i2c device. Binding is handled through driver model
956 * probe()/remove() methods. A driver may be bound to this device when we
957 * return from this function, or any later moment (e.g. maybe hotplugging will
958 * load the driver module). This call is not appropriate for use by mainboard
959 * initialization logic, which usually runs during an arch_initcall() long
960 * before any i2c_adapter could exist.
9c1600ed
DB
961 *
962 * This returns the new i2c client, which may be saved for later use with
963 * i2c_unregister_device(); or NULL to indicate an error.
964 */
965struct i2c_client *
966i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
967{
968 struct i2c_client *client;
969 int status;
970
971 client = kzalloc(sizeof *client, GFP_KERNEL);
972 if (!client)
973 return NULL;
974
975 client->adapter = adap;
976
977 client->dev.platform_data = info->platform_data;
3bbb835d 978
11f1f2af
AV
979 if (info->archdata)
980 client->dev.archdata = *info->archdata;
981
ee35425c 982 client->flags = info->flags;
9c1600ed
DB
983 client->addr = info->addr;
984 client->irq = info->irq;
985
9c1600ed
DB
986 strlcpy(client->name, info->type, sizeof(client->name));
987
c4019b70 988 status = i2c_check_addr_validity(client->addr, client->flags);
3a89db5f
JD
989 if (status) {
990 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
991 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
992 goto out_err_silent;
993 }
994
f8a227e8 995 /* Check for address business */
3b5f794b 996 status = i2c_check_addr_busy(adap, client->addr);
f8a227e8
JD
997 if (status)
998 goto out_err;
999
1000 client->dev.parent = &client->adapter->dev;
1001 client->dev.bus = &i2c_bus_type;
51298d12 1002 client->dev.type = &i2c_client_type;
d12d42f7 1003 client->dev.of_node = info->of_node;
ce793486 1004 client->dev.fwnode = info->fwnode;
f8a227e8 1005
70762abb 1006 i2c_dev_set_name(adap, client);
f8a227e8
JD
1007 status = device_register(&client->dev);
1008 if (status)
1009 goto out_err;
1010
f8a227e8
JD
1011 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1012 client->name, dev_name(&client->dev));
1013
9c1600ed 1014 return client;
f8a227e8
JD
1015
1016out_err:
1017 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1018 "(%d)\n", client->name, client->addr, status);
3a89db5f 1019out_err_silent:
f8a227e8
JD
1020 kfree(client);
1021 return NULL;
9c1600ed
DB
1022}
1023EXPORT_SYMBOL_GPL(i2c_new_device);
1024
1025
1026/**
1027 * i2c_unregister_device - reverse effect of i2c_new_device()
1028 * @client: value returned from i2c_new_device()
d64f73be 1029 * Context: can sleep
9c1600ed
DB
1030 */
1031void i2c_unregister_device(struct i2c_client *client)
a1d9e6e4 1032{
4f001fd3
PA
1033 if (client->dev.of_node)
1034 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
a1d9e6e4
DB
1035 device_unregister(&client->dev);
1036}
9c1600ed 1037EXPORT_SYMBOL_GPL(i2c_unregister_device);
a1d9e6e4
DB
1038
1039
60b129d7
JD
1040static const struct i2c_device_id dummy_id[] = {
1041 { "dummy", 0 },
1042 { },
1043};
1044
d2653e92
JD
1045static int dummy_probe(struct i2c_client *client,
1046 const struct i2c_device_id *id)
1047{
1048 return 0;
1049}
1050
1051static int dummy_remove(struct i2c_client *client)
e9f1373b
DB
1052{
1053 return 0;
1054}
1055
1056static struct i2c_driver dummy_driver = {
1057 .driver.name = "dummy",
d2653e92
JD
1058 .probe = dummy_probe,
1059 .remove = dummy_remove,
60b129d7 1060 .id_table = dummy_id,
e9f1373b
DB
1061};
1062
1063/**
1064 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1065 * @adapter: the adapter managing the device
1066 * @address: seven bit address to be used
e9f1373b
DB
1067 * Context: can sleep
1068 *
1069 * This returns an I2C client bound to the "dummy" driver, intended for use
1070 * with devices that consume multiple addresses. Examples of such chips
1071 * include various EEPROMS (like 24c04 and 24c08 models).
1072 *
1073 * These dummy devices have two main uses. First, most I2C and SMBus calls
1074 * except i2c_transfer() need a client handle; the dummy will be that handle.
1075 * And second, this prevents the specified address from being bound to a
1076 * different driver.
1077 *
1078 * This returns the new i2c client, which should be saved for later use with
1079 * i2c_unregister_device(); or NULL to indicate an error.
1080 */
09b8ce0a 1081struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
e9f1373b
DB
1082{
1083 struct i2c_board_info info = {
60b129d7 1084 I2C_BOARD_INFO("dummy", address),
e9f1373b
DB
1085 };
1086
e9f1373b
DB
1087 return i2c_new_device(adapter, &info);
1088}
1089EXPORT_SYMBOL_GPL(i2c_new_dummy);
1090
f37dd80a
DB
1091/* ------------------------------------------------------------------------- */
1092
16ffadfc
DB
1093/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1094
83eaaed0 1095static void i2c_adapter_dev_release(struct device *dev)
1da177e4 1096{
ef2c8321 1097 struct i2c_adapter *adap = to_i2c_adapter(dev);
1da177e4
LT
1098 complete(&adap->dev_released);
1099}
1100
390946b1
JD
1101/*
1102 * This function is only needed for mutex_lock_nested, so it is never
1103 * called unless locking correctness checking is enabled. Thus we
1104 * make it inline to avoid a compiler warning. That's what gcc ends up
1105 * doing anyway.
1106 */
1107static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1108{
1109 unsigned int depth = 0;
1110
1111 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1112 depth++;
1113
1114 return depth;
1115}
1116
99cd8e25
JD
1117/*
1118 * Let users instantiate I2C devices through sysfs. This can be used when
1119 * platform initialization code doesn't contain the proper data for
1120 * whatever reason. Also useful for drivers that do device detection and
1121 * detection fails, either because the device uses an unexpected address,
1122 * or this is a compatible device with different ID register values.
1123 *
1124 * Parameter checking may look overzealous, but we really don't want
1125 * the user to provide incorrect parameters.
1126 */
1127static ssize_t
1128i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1129 const char *buf, size_t count)
1130{
1131 struct i2c_adapter *adap = to_i2c_adapter(dev);
1132 struct i2c_board_info info;
1133 struct i2c_client *client;
1134 char *blank, end;
1135 int res;
1136
99cd8e25
JD
1137 memset(&info, 0, sizeof(struct i2c_board_info));
1138
1139 blank = strchr(buf, ' ');
1140 if (!blank) {
1141 dev_err(dev, "%s: Missing parameters\n", "new_device");
1142 return -EINVAL;
1143 }
1144 if (blank - buf > I2C_NAME_SIZE - 1) {
1145 dev_err(dev, "%s: Invalid device name\n", "new_device");
1146 return -EINVAL;
1147 }
1148 memcpy(info.type, buf, blank - buf);
1149
1150 /* Parse remaining parameters, reject extra parameters */
1151 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1152 if (res < 1) {
1153 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1154 return -EINVAL;
1155 }
1156 if (res > 1 && end != '\n') {
1157 dev_err(dev, "%s: Extra parameters\n", "new_device");
1158 return -EINVAL;
1159 }
1160
99cd8e25
JD
1161 client = i2c_new_device(adap, &info);
1162 if (!client)
3a89db5f 1163 return -EINVAL;
99cd8e25
JD
1164
1165 /* Keep track of the added device */
dafc50d1 1166 mutex_lock(&adap->userspace_clients_lock);
6629dcff 1167 list_add_tail(&client->detected, &adap->userspace_clients);
dafc50d1 1168 mutex_unlock(&adap->userspace_clients_lock);
99cd8e25
JD
1169 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1170 info.type, info.addr);
1171
1172 return count;
1173}
a5eb71b2 1174static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
99cd8e25
JD
1175
1176/*
1177 * And of course let the users delete the devices they instantiated, if
1178 * they got it wrong. This interface can only be used to delete devices
1179 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1180 * don't delete devices to which some kernel code still has references.
1181 *
1182 * Parameter checking may look overzealous, but we really don't want
1183 * the user to delete the wrong device.
1184 */
1185static ssize_t
1186i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1187 const char *buf, size_t count)
1188{
1189 struct i2c_adapter *adap = to_i2c_adapter(dev);
1190 struct i2c_client *client, *next;
1191 unsigned short addr;
1192 char end;
1193 int res;
1194
1195 /* Parse parameters, reject extra parameters */
1196 res = sscanf(buf, "%hi%c", &addr, &end);
1197 if (res < 1) {
1198 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1199 return -EINVAL;
1200 }
1201 if (res > 1 && end != '\n') {
1202 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1203 return -EINVAL;
1204 }
1205
1206 /* Make sure the device was added through sysfs */
1207 res = -ENOENT;
390946b1
JD
1208 mutex_lock_nested(&adap->userspace_clients_lock,
1209 i2c_adapter_depth(adap));
6629dcff
JD
1210 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1211 detected) {
1212 if (client->addr == addr) {
99cd8e25
JD
1213 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1214 "delete_device", client->name, client->addr);
1215
1216 list_del(&client->detected);
1217 i2c_unregister_device(client);
1218 res = count;
1219 break;
1220 }
1221 }
dafc50d1 1222 mutex_unlock(&adap->userspace_clients_lock);
99cd8e25
JD
1223
1224 if (res < 0)
1225 dev_err(dev, "%s: Can't find device in list\n",
1226 "delete_device");
1227 return res;
1228}
e9b526fe
AS
1229static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1230 i2c_sysfs_delete_device);
4f8cf824
JD
1231
1232static struct attribute *i2c_adapter_attrs[] = {
1233 &dev_attr_name.attr,
1234 &dev_attr_new_device.attr,
1235 &dev_attr_delete_device.attr,
1236 NULL
1237};
a5eb71b2 1238ATTRIBUTE_GROUPS(i2c_adapter);
b119dc3f 1239
0826374b 1240struct device_type i2c_adapter_type = {
a5eb71b2 1241 .groups = i2c_adapter_groups,
4f8cf824 1242 .release = i2c_adapter_dev_release,
1da177e4 1243};
0826374b 1244EXPORT_SYMBOL_GPL(i2c_adapter_type);
1da177e4 1245
643dd09e
SW
1246/**
1247 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1248 * @dev: device, probably from some driver model iterator
1249 *
1250 * When traversing the driver model tree, perhaps using driver model
1251 * iterators like @device_for_each_child(), you can't assume very much
1252 * about the nodes you find. Use this function to avoid oopses caused
1253 * by wrongly treating some non-I2C device as an i2c_adapter.
1254 */
1255struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1256{
1257 return (dev->type == &i2c_adapter_type)
1258 ? to_i2c_adapter(dev)
1259 : NULL;
1260}
1261EXPORT_SYMBOL(i2c_verify_adapter);
1262
2bb5095a
JD
1263#ifdef CONFIG_I2C_COMPAT
1264static struct class_compat *i2c_adapter_compat_class;
1265#endif
1266
9c1600ed
DB
1267static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1268{
1269 struct i2c_devinfo *devinfo;
1270
f18c41da 1271 down_read(&__i2c_board_lock);
9c1600ed
DB
1272 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1273 if (devinfo->busnum == adapter->nr
1274 && !i2c_new_device(adapter,
1275 &devinfo->board_info))
09b8ce0a
ZX
1276 dev_err(&adapter->dev,
1277 "Can't create device at 0x%02x\n",
9c1600ed
DB
1278 devinfo->board_info.addr);
1279 }
f18c41da 1280 up_read(&__i2c_board_lock);
9c1600ed
DB
1281}
1282
687b81d0
WS
1283/* OF support code */
1284
1285#if IS_ENABLED(CONFIG_OF)
a430a345
PA
1286static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1287 struct device_node *node)
687b81d0 1288{
a430a345
PA
1289 struct i2c_client *result;
1290 struct i2c_board_info info = {};
1291 struct dev_archdata dev_ad = {};
b4e2f6ac
WS
1292 const __be32 *addr_be;
1293 u32 addr;
a430a345 1294 int len;
687b81d0 1295
a430a345 1296 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
687b81d0 1297
a430a345
PA
1298 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1299 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1300 node->full_name);
1301 return ERR_PTR(-EINVAL);
1302 }
687b81d0 1303
b4e2f6ac
WS
1304 addr_be = of_get_property(node, "reg", &len);
1305 if (!addr_be || (len < sizeof(*addr_be))) {
a430a345
PA
1306 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1307 node->full_name);
1308 return ERR_PTR(-EINVAL);
1309 }
687b81d0 1310
b4e2f6ac
WS
1311 addr = be32_to_cpup(addr_be);
1312 if (addr & I2C_TEN_BIT_ADDRESS) {
1313 addr &= ~I2C_TEN_BIT_ADDRESS;
1314 info.flags |= I2C_CLIENT_TEN;
1315 }
1316
1317 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1318 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1319 info.flags |= I2C_CLIENT_SLAVE;
1320 }
1321
1322 if (i2c_check_addr_validity(addr, info.flags)) {
a430a345
PA
1323 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1324 info.addr, node->full_name);
1325 return ERR_PTR(-EINVAL);
1326 }
687b81d0 1327
b4e2f6ac 1328 info.addr = addr;
a430a345
PA
1329 info.of_node = of_node_get(node);
1330 info.archdata = &dev_ad;
687b81d0 1331
a430a345
PA
1332 if (of_get_property(node, "wakeup-source", NULL))
1333 info.flags |= I2C_CLIENT_WAKE;
687b81d0 1334
a430a345
PA
1335 result = i2c_new_device(adap, &info);
1336 if (result == NULL) {
1337 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1338 node->full_name);
1339 of_node_put(node);
a430a345
PA
1340 return ERR_PTR(-EINVAL);
1341 }
1342 return result;
1343}
687b81d0 1344
a430a345
PA
1345static void of_i2c_register_devices(struct i2c_adapter *adap)
1346{
1347 struct device_node *node;
687b81d0 1348
a430a345
PA
1349 /* Only register child devices if the adapter has a node pointer set */
1350 if (!adap->dev.of_node)
1351 return;
687b81d0 1352
a430a345
PA
1353 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1354
4f001fd3
PA
1355 for_each_available_child_of_node(adap->dev.of_node, node) {
1356 if (of_node_test_and_set_flag(node, OF_POPULATED))
1357 continue;
a430a345 1358 of_i2c_register_device(adap, node);
4f001fd3 1359 }
687b81d0
WS
1360}
1361
1362static int of_dev_node_match(struct device *dev, void *data)
1363{
1364 return dev->of_node == data;
1365}
1366
1367/* must call put_device() when done with returned i2c_client device */
1368struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1369{
1370 struct device *dev;
e3311469 1371 struct i2c_client *client;
687b81d0 1372
e3311469 1373 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
687b81d0
WS
1374 if (!dev)
1375 return NULL;
1376
e3311469
VZ
1377 client = i2c_verify_client(dev);
1378 if (!client)
1379 put_device(dev);
1380
1381 return client;
687b81d0
WS
1382}
1383EXPORT_SYMBOL(of_find_i2c_device_by_node);
1384
1385/* must call put_device() when done with returned i2c_adapter device */
1386struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1387{
1388 struct device *dev;
e3311469 1389 struct i2c_adapter *adapter;
687b81d0 1390
e3311469 1391 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
687b81d0
WS
1392 if (!dev)
1393 return NULL;
1394
e3311469
VZ
1395 adapter = i2c_verify_adapter(dev);
1396 if (!adapter)
1397 put_device(dev);
1398
1399 return adapter;
687b81d0
WS
1400}
1401EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
48e9743d
VZ
1402
1403/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1404struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1405{
1406 struct i2c_adapter *adapter;
1407
1408 adapter = of_find_i2c_adapter_by_node(node);
1409 if (!adapter)
1410 return NULL;
1411
1412 if (!try_module_get(adapter->owner)) {
1413 put_device(&adapter->dev);
1414 adapter = NULL;
1415 }
1416
1417 return adapter;
1418}
1419EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
687b81d0
WS
1420#else
1421static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1422#endif /* CONFIG_OF */
1423
69b0089a
JD
1424static int i2c_do_add_adapter(struct i2c_driver *driver,
1425 struct i2c_adapter *adap)
026526f5 1426{
4735c98f
JD
1427 /* Detect supported devices on that bus, and instantiate them */
1428 i2c_detect(adap, driver);
1429
1430 /* Let legacy drivers scan this bus for matching devices */
026526f5 1431 if (driver->attach_adapter) {
a920ff41
JD
1432 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1433 driver->driver.name);
fe6fc258
JD
1434 dev_warn(&adap->dev, "Please use another way to instantiate "
1435 "your i2c_client\n");
026526f5
JD
1436 /* We ignore the return code; if it fails, too bad */
1437 driver->attach_adapter(adap);
1438 }
1439 return 0;
1440}
1441
69b0089a
JD
1442static int __process_new_adapter(struct device_driver *d, void *data)
1443{
1444 return i2c_do_add_adapter(to_i2c_driver(d), data);
1445}
1446
6e13e641 1447static int i2c_register_adapter(struct i2c_adapter *adap)
1da177e4 1448{
d6703281 1449 int res = 0;
1da177e4 1450
1d0b19c9 1451 /* Can't register until after driver model init */
35fc37f8
JD
1452 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1453 res = -EAGAIN;
1454 goto out_list;
1455 }
1d0b19c9 1456
2236baa7
JD
1457 /* Sanity checks */
1458 if (unlikely(adap->name[0] == '\0')) {
1459 pr_err("i2c-core: Attempt to register an adapter with "
1460 "no name!\n");
1461 return -EINVAL;
1462 }
1463 if (unlikely(!adap->algo)) {
1464 pr_err("i2c-core: Attempt to register adapter '%s' with "
1465 "no algo!\n", adap->name);
1466 return -EINVAL;
1467 }
1468
194684e5 1469 rt_mutex_init(&adap->bus_lock);
dafc50d1 1470 mutex_init(&adap->userspace_clients_lock);
6629dcff 1471 INIT_LIST_HEAD(&adap->userspace_clients);
1da177e4 1472
8fcfef6e
JD
1473 /* Set default timeout to 1 second if not already set */
1474 if (adap->timeout == 0)
1475 adap->timeout = HZ;
1476
27d9c183 1477 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
4f8cf824
JD
1478 adap->dev.bus = &i2c_bus_type;
1479 adap->dev.type = &i2c_adapter_type;
b119c6c9
JD
1480 res = device_register(&adap->dev);
1481 if (res)
1482 goto out_list;
1da177e4 1483
b6d7b3d1
JD
1484 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1485
6ada5c1e
CK
1486 pm_runtime_no_callbacks(&adap->dev);
1487
2bb5095a
JD
1488#ifdef CONFIG_I2C_COMPAT
1489 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1490 adap->dev.parent);
1491 if (res)
1492 dev_warn(&adap->dev,
1493 "Failed to create compatibility class link\n");
1494#endif
1495
5f9296ba
VK
1496 /* bus recovery specific initialization */
1497 if (adap->bus_recovery_info) {
1498 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1499
1500 if (!bri->recover_bus) {
1501 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1502 adap->bus_recovery_info = NULL;
1503 goto exit_recovery;
1504 }
1505
1506 /* Generic GPIO recovery */
1507 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1508 if (!gpio_is_valid(bri->scl_gpio)) {
1509 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1510 adap->bus_recovery_info = NULL;
1511 goto exit_recovery;
1512 }
1513
1514 if (gpio_is_valid(bri->sda_gpio))
1515 bri->get_sda = get_sda_gpio_value;
1516 else
1517 bri->get_sda = NULL;
1518
1519 bri->get_scl = get_scl_gpio_value;
1520 bri->set_scl = set_scl_gpio_value;
1521 } else if (!bri->set_scl || !bri->get_scl) {
1522 /* Generic SCL recovery */
1523 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1524 adap->bus_recovery_info = NULL;
1525 }
1526 }
1527
1528exit_recovery:
729d6dd5 1529 /* create pre-declared device nodes */
687b81d0 1530 of_i2c_register_devices(adap);
55e71edb 1531 acpi_i2c_register_devices(adap);
5d98e61d 1532 acpi_i2c_install_space_handler(adap);
687b81d0 1533
6e13e641
DB
1534 if (adap->nr < __i2c_first_dynamic_bus_num)
1535 i2c_scan_static_board_info(adap);
1536
4735c98f 1537 /* Notify drivers */
35fc37f8 1538 mutex_lock(&core_lock);
d6703281 1539 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
caada32a 1540 mutex_unlock(&core_lock);
35fc37f8
JD
1541
1542 return 0;
b119c6c9 1543
b119c6c9 1544out_list:
35fc37f8 1545 mutex_lock(&core_lock);
b119c6c9 1546 idr_remove(&i2c_adapter_idr, adap->nr);
35fc37f8
JD
1547 mutex_unlock(&core_lock);
1548 return res;
1da177e4
LT
1549}
1550
ee5c2744
DA
1551/**
1552 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1553 * @adap: the adapter to register (with adap->nr initialized)
1554 * Context: can sleep
1555 *
1556 * See i2c_add_numbered_adapter() for details.
1557 */
1558static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1559{
1560 int id;
1561
1562 mutex_lock(&core_lock);
1563 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1564 GFP_KERNEL);
1565 mutex_unlock(&core_lock);
1566 if (id < 0)
1567 return id == -ENOSPC ? -EBUSY : id;
1568
1569 return i2c_register_adapter(adap);
1570}
1571
6e13e641
DB
1572/**
1573 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1574 * @adapter: the adapter to add
d64f73be 1575 * Context: can sleep
6e13e641
DB
1576 *
1577 * This routine is used to declare an I2C adapter when its bus number
ee5c2744
DA
1578 * doesn't matter or when its bus number is specified by an dt alias.
1579 * Examples of bases when the bus number doesn't matter: I2C adapters
1580 * dynamically added by USB links or PCI plugin cards.
6e13e641
DB
1581 *
1582 * When this returns zero, a new bus number was allocated and stored
1583 * in adap->nr, and the specified adapter became available for clients.
1584 * Otherwise, a negative errno value is returned.
1585 */
1586int i2c_add_adapter(struct i2c_adapter *adapter)
1587{
ee5c2744 1588 struct device *dev = &adapter->dev;
4ae42b0f 1589 int id;
6e13e641 1590
ee5c2744
DA
1591 if (dev->of_node) {
1592 id = of_alias_get_id(dev->of_node, "i2c");
1593 if (id >= 0) {
1594 adapter->nr = id;
1595 return __i2c_add_numbered_adapter(adapter);
1596 }
1597 }
1598
caada32a 1599 mutex_lock(&core_lock);
4ae42b0f
TH
1600 id = idr_alloc(&i2c_adapter_idr, adapter,
1601 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
caada32a 1602 mutex_unlock(&core_lock);
4ae42b0f
TH
1603 if (id < 0)
1604 return id;
6e13e641
DB
1605
1606 adapter->nr = id;
4ae42b0f 1607
6e13e641
DB
1608 return i2c_register_adapter(adapter);
1609}
1610EXPORT_SYMBOL(i2c_add_adapter);
1611
1612/**
1613 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1614 * @adap: the adapter to register (with adap->nr initialized)
d64f73be 1615 * Context: can sleep
6e13e641
DB
1616 *
1617 * This routine is used to declare an I2C adapter when its bus number
8c07e46f
RD
1618 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1619 * or otherwise built in to the system's mainboard, and where i2c_board_info
6e13e641
DB
1620 * is used to properly configure I2C devices.
1621 *
488bf314
GL
1622 * If the requested bus number is set to -1, then this function will behave
1623 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1624 *
6e13e641
DB
1625 * If no devices have pre-been declared for this bus, then be sure to
1626 * register the adapter before any dynamically allocated ones. Otherwise
1627 * the required bus ID may not be available.
1628 *
1629 * When this returns zero, the specified adapter became available for
1630 * clients using the bus number provided in adap->nr. Also, the table
1631 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1632 * and the appropriate driver model device nodes are created. Otherwise, a
1633 * negative errno value is returned.
1634 */
1635int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1636{
488bf314
GL
1637 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1638 return i2c_add_adapter(adap);
6e13e641 1639
ee5c2744 1640 return __i2c_add_numbered_adapter(adap);
6e13e641
DB
1641}
1642EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1643
19baba4c 1644static void i2c_do_del_adapter(struct i2c_driver *driver,
69b0089a 1645 struct i2c_adapter *adapter)
026526f5 1646{
4735c98f 1647 struct i2c_client *client, *_n;
026526f5 1648
acec211c
JD
1649 /* Remove the devices we created ourselves as the result of hardware
1650 * probing (using a driver's detect method) */
4735c98f
JD
1651 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1652 if (client->adapter == adapter) {
1653 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1654 client->name, client->addr);
1655 list_del(&client->detected);
1656 i2c_unregister_device(client);
1657 }
1658 }
026526f5
JD
1659}
1660
e549c2b5 1661static int __unregister_client(struct device *dev, void *dummy)
5219bf88
JD
1662{
1663 struct i2c_client *client = i2c_verify_client(dev);
1664 if (client && strcmp(client->name, "dummy"))
1665 i2c_unregister_device(client);
1666 return 0;
1667}
1668
1669static int __unregister_dummy(struct device *dev, void *dummy)
e549c2b5
JD
1670{
1671 struct i2c_client *client = i2c_verify_client(dev);
1672 if (client)
1673 i2c_unregister_device(client);
1674 return 0;
1675}
1676
69b0089a
JD
1677static int __process_removed_adapter(struct device_driver *d, void *data)
1678{
19baba4c
LPC
1679 i2c_do_del_adapter(to_i2c_driver(d), data);
1680 return 0;
69b0089a
JD
1681}
1682
d64f73be
DB
1683/**
1684 * i2c_del_adapter - unregister I2C adapter
1685 * @adap: the adapter being unregistered
1686 * Context: can sleep
1687 *
1688 * This unregisters an I2C adapter which was previously registered
1689 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1690 */
71546300 1691void i2c_del_adapter(struct i2c_adapter *adap)
1da177e4 1692{
35fc37f8 1693 struct i2c_adapter *found;
bbd2d9c9 1694 struct i2c_client *client, *next;
1da177e4
LT
1695
1696 /* First make sure that this adapter was ever added */
35fc37f8
JD
1697 mutex_lock(&core_lock);
1698 found = idr_find(&i2c_adapter_idr, adap->nr);
1699 mutex_unlock(&core_lock);
1700 if (found != adap) {
b6d7b3d1
JD
1701 pr_debug("i2c-core: attempting to delete unregistered "
1702 "adapter [%s]\n", adap->name);
71546300 1703 return;
1da177e4
LT
1704 }
1705
5d98e61d 1706 acpi_i2c_remove_space_handler(adap);
026526f5 1707 /* Tell drivers about this removal */
35fc37f8 1708 mutex_lock(&core_lock);
19baba4c 1709 bus_for_each_drv(&i2c_bus_type, NULL, adap,
69b0089a 1710 __process_removed_adapter);
35fc37f8 1711 mutex_unlock(&core_lock);
1da177e4 1712
bbd2d9c9 1713 /* Remove devices instantiated from sysfs */
390946b1
JD
1714 mutex_lock_nested(&adap->userspace_clients_lock,
1715 i2c_adapter_depth(adap));
6629dcff
JD
1716 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1717 detected) {
1718 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1719 client->addr);
1720 list_del(&client->detected);
1721 i2c_unregister_device(client);
bbd2d9c9 1722 }
dafc50d1 1723 mutex_unlock(&adap->userspace_clients_lock);
bbd2d9c9 1724
e549c2b5 1725 /* Detach any active clients. This can't fail, thus we do not
5219bf88
JD
1726 * check the returned value. This is a two-pass process, because
1727 * we can't remove the dummy devices during the first pass: they
1728 * could have been instantiated by real devices wishing to clean
1729 * them up properly, so we give them a chance to do that first. */
19baba4c
LPC
1730 device_for_each_child(&adap->dev, NULL, __unregister_client);
1731 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1da177e4 1732
2bb5095a
JD
1733#ifdef CONFIG_I2C_COMPAT
1734 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1735 adap->dev.parent);
1736#endif
1737
c5567521
TLSC
1738 /* device name is gone after device_unregister */
1739 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1740
26680ee2
WS
1741 /* wait until all references to the device are gone
1742 *
1743 * FIXME: This is old code and should ideally be replaced by an
1744 * alternative which results in decoupling the lifetime of the struct
1745 * device from the i2c_adapter, like spi or netdev do. Any solution
95cc1e3d 1746 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
26680ee2 1747 */
1da177e4 1748 init_completion(&adap->dev_released);
1da177e4 1749 device_unregister(&adap->dev);
1da177e4 1750 wait_for_completion(&adap->dev_released);
1da177e4 1751
6e13e641 1752 /* free bus id */
35fc37f8 1753 mutex_lock(&core_lock);
1da177e4 1754 idr_remove(&i2c_adapter_idr, adap->nr);
35fc37f8 1755 mutex_unlock(&core_lock);
1da177e4 1756
bd4bc3db
JD
1757 /* Clear the device structure in case this adapter is ever going to be
1758 added again */
1759 memset(&adap->dev, 0, sizeof(adap->dev));
1da177e4 1760}
c0564606 1761EXPORT_SYMBOL(i2c_del_adapter);
1da177e4 1762
7b4fbc50
DB
1763/* ------------------------------------------------------------------------- */
1764
7ae31482
JD
1765int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1766{
1767 int res;
1768
1769 mutex_lock(&core_lock);
1770 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1771 mutex_unlock(&core_lock);
1772
1773 return res;
1774}
1775EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1776
69b0089a 1777static int __process_new_driver(struct device *dev, void *data)
7f101a97 1778{
4f8cf824
JD
1779 if (dev->type != &i2c_adapter_type)
1780 return 0;
69b0089a 1781 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
7f101a97
DY
1782}
1783
7b4fbc50
DB
1784/*
1785 * An i2c_driver is used with one or more i2c_client (device) nodes to access
729d6dd5 1786 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1da177e4
LT
1787 */
1788
de59cf9e 1789int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1da177e4 1790{
7eebcb7c 1791 int res;
1da177e4 1792
1d0b19c9
DB
1793 /* Can't register until after driver model init */
1794 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1795 return -EAGAIN;
1796
1da177e4 1797 /* add the driver to the list of i2c drivers in the driver core */
de59cf9e 1798 driver->driver.owner = owner;
1da177e4 1799 driver->driver.bus = &i2c_bus_type;
1da177e4 1800
729d6dd5 1801 /* When registration returns, the driver core
6e13e641
DB
1802 * will have called probe() for all matching-but-unbound devices.
1803 */
1da177e4
LT
1804 res = driver_register(&driver->driver);
1805 if (res)
7eebcb7c 1806 return res;
438d6c2c 1807
35d8b2e6 1808 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1da177e4 1809
4735c98f
JD
1810 INIT_LIST_HEAD(&driver->clients);
1811 /* Walk the adapters that are already present */
7ae31482 1812 i2c_for_each_dev(driver, __process_new_driver);
35fc37f8 1813
7f101a97
DY
1814 return 0;
1815}
1816EXPORT_SYMBOL(i2c_register_driver);
1817
69b0089a 1818static int __process_removed_driver(struct device *dev, void *data)
7f101a97 1819{
19baba4c
LPC
1820 if (dev->type == &i2c_adapter_type)
1821 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1822 return 0;
1da177e4
LT
1823}
1824
a1d9e6e4
DB
1825/**
1826 * i2c_del_driver - unregister I2C driver
1827 * @driver: the driver being unregistered
d64f73be 1828 * Context: can sleep
a1d9e6e4 1829 */
b3e82096 1830void i2c_del_driver(struct i2c_driver *driver)
1da177e4 1831{
7ae31482 1832 i2c_for_each_dev(driver, __process_removed_driver);
1da177e4
LT
1833
1834 driver_unregister(&driver->driver);
35d8b2e6 1835 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1da177e4 1836}
c0564606 1837EXPORT_SYMBOL(i2c_del_driver);
1da177e4 1838
7b4fbc50
DB
1839/* ------------------------------------------------------------------------- */
1840
e48d3319
JD
1841/**
1842 * i2c_use_client - increments the reference count of the i2c client structure
1843 * @client: the client being referenced
1844 *
1845 * Each live reference to a client should be refcounted. The driver model does
1846 * that automatically as part of driver binding, so that most drivers don't
1847 * need to do this explicitly: they hold a reference until they're unbound
1848 * from the device.
1849 *
1850 * A pointer to the client with the incremented reference counter is returned.
1851 */
1852struct i2c_client *i2c_use_client(struct i2c_client *client)
1da177e4 1853{
6ea438ec
DB
1854 if (client && get_device(&client->dev))
1855 return client;
1856 return NULL;
1da177e4 1857}
c0564606 1858EXPORT_SYMBOL(i2c_use_client);
1da177e4 1859
e48d3319
JD
1860/**
1861 * i2c_release_client - release a use of the i2c client structure
1862 * @client: the client being no longer referenced
1863 *
1864 * Must be called when a user of a client is finished with it.
1865 */
1866void i2c_release_client(struct i2c_client *client)
1da177e4 1867{
6ea438ec
DB
1868 if (client)
1869 put_device(&client->dev);
1da177e4 1870}
c0564606 1871EXPORT_SYMBOL(i2c_release_client);
1da177e4 1872
9b766b81
DB
1873struct i2c_cmd_arg {
1874 unsigned cmd;
1875 void *arg;
1876};
1877
1878static int i2c_cmd(struct device *dev, void *_arg)
1879{
1880 struct i2c_client *client = i2c_verify_client(dev);
1881 struct i2c_cmd_arg *arg = _arg;
0acc2b32
LPC
1882 struct i2c_driver *driver;
1883
1884 if (!client || !client->dev.driver)
1885 return 0;
9b766b81 1886
0acc2b32
LPC
1887 driver = to_i2c_driver(client->dev.driver);
1888 if (driver->command)
1889 driver->command(client, arg->cmd, arg->arg);
9b766b81
DB
1890 return 0;
1891}
1892
1da177e4
LT
1893void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1894{
9b766b81 1895 struct i2c_cmd_arg cmd_arg;
1da177e4 1896
9b766b81
DB
1897 cmd_arg.cmd = cmd;
1898 cmd_arg.arg = arg;
1899 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1da177e4 1900}
c0564606 1901EXPORT_SYMBOL(i2c_clients_command);
1da177e4 1902
ea7513bb
PA
1903#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1904static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1905 void *arg)
1906{
1907 struct of_reconfig_data *rd = arg;
1908 struct i2c_adapter *adap;
1909 struct i2c_client *client;
1910
1911 switch (of_reconfig_get_state_change(action, rd)) {
1912 case OF_RECONFIG_CHANGE_ADD:
1913 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1914 if (adap == NULL)
1915 return NOTIFY_OK; /* not for us */
1916
4f001fd3
PA
1917 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1918 put_device(&adap->dev);
1919 return NOTIFY_OK;
1920 }
1921
ea7513bb
PA
1922 client = of_i2c_register_device(adap, rd->dn);
1923 put_device(&adap->dev);
1924
1925 if (IS_ERR(client)) {
1926 pr_err("%s: failed to create for '%s'\n",
1927 __func__, rd->dn->full_name);
1928 return notifier_from_errno(PTR_ERR(client));
1929 }
1930 break;
1931 case OF_RECONFIG_CHANGE_REMOVE:
4f001fd3
PA
1932 /* already depopulated? */
1933 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1934 return NOTIFY_OK;
1935
ea7513bb
PA
1936 /* find our device by node */
1937 client = of_find_i2c_device_by_node(rd->dn);
1938 if (client == NULL)
1939 return NOTIFY_OK; /* no? not meant for us */
1940
1941 /* unregister takes one ref away */
1942 i2c_unregister_device(client);
1943
1944 /* and put the reference of the find */
1945 put_device(&client->dev);
1946 break;
1947 }
1948
1949 return NOTIFY_OK;
1950}
1951static struct notifier_block i2c_of_notifier = {
1952 .notifier_call = of_i2c_notify,
1953};
1954#else
1955extern struct notifier_block i2c_of_notifier;
1956#endif /* CONFIG_OF_DYNAMIC */
1957
1da177e4
LT
1958static int __init i2c_init(void)
1959{
1960 int retval;
1961
03bde7c3
WS
1962 retval = of_alias_get_highest_id("i2c");
1963
1964 down_write(&__i2c_board_lock);
1965 if (retval >= __i2c_first_dynamic_bus_num)
1966 __i2c_first_dynamic_bus_num = retval + 1;
1967 up_write(&__i2c_board_lock);
1968
1da177e4 1969 retval = bus_register(&i2c_bus_type);
1da177e4
LT
1970 if (retval)
1971 return retval;
2bb5095a
JD
1972#ifdef CONFIG_I2C_COMPAT
1973 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1974 if (!i2c_adapter_compat_class) {
1975 retval = -ENOMEM;
1976 goto bus_err;
1977 }
1978#endif
e9f1373b
DB
1979 retval = i2c_add_driver(&dummy_driver);
1980 if (retval)
2bb5095a 1981 goto class_err;
ea7513bb
PA
1982
1983 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1984 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1985
e9f1373b
DB
1986 return 0;
1987
2bb5095a
JD
1988class_err:
1989#ifdef CONFIG_I2C_COMPAT
1990 class_compat_unregister(i2c_adapter_compat_class);
e9f1373b 1991bus_err:
2bb5095a 1992#endif
e9f1373b
DB
1993 bus_unregister(&i2c_bus_type);
1994 return retval;
1da177e4
LT
1995}
1996
1997static void __exit i2c_exit(void)
1998{
ea7513bb
PA
1999 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2000 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
e9f1373b 2001 i2c_del_driver(&dummy_driver);
2bb5095a
JD
2002#ifdef CONFIG_I2C_COMPAT
2003 class_compat_unregister(i2c_adapter_compat_class);
2004#endif
1da177e4 2005 bus_unregister(&i2c_bus_type);
d9a83d62 2006 tracepoint_synchronize_unregister();
1da177e4
LT
2007}
2008
a10f9e7c
DB
2009/* We must initialize early, because some subsystems register i2c drivers
2010 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2011 */
2012postcore_initcall(i2c_init);
1da177e4
LT
2013module_exit(i2c_exit);
2014
2015/* ----------------------------------------------------
2016 * the functional interface to the i2c busses.
2017 * ----------------------------------------------------
2018 */
2019
b7f62584
WS
2020/* Check if val is exceeding the quirk IFF quirk is non 0 */
2021#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2022
2023static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2024{
2025 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2026 err_msg, msg->addr, msg->len,
2027 msg->flags & I2C_M_RD ? "read" : "write");
2028 return -EOPNOTSUPP;
2029}
2030
2031static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2032{
2033 const struct i2c_adapter_quirks *q = adap->quirks;
2034 int max_num = q->max_num_msgs, i;
2035 bool do_len_check = true;
2036
2037 if (q->flags & I2C_AQ_COMB) {
2038 max_num = 2;
2039
2040 /* special checks for combined messages */
2041 if (num == 2) {
2042 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2043 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2044
2045 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2046 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2047
2048 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2049 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2050
2051 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2052 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2053
2054 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2055 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2056
2057 do_len_check = false;
2058 }
2059 }
2060
2061 if (i2c_quirk_exceeded(num, max_num))
2062 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2063
2064 for (i = 0; i < num; i++) {
2065 u16 len = msgs[i].len;
2066
2067 if (msgs[i].flags & I2C_M_RD) {
2068 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2069 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2070 } else {
2071 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2072 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2073 }
2074 }
2075
2076 return 0;
2077}
2078
b37d2a3a
JD
2079/**
2080 * __i2c_transfer - unlocked flavor of i2c_transfer
2081 * @adap: Handle to I2C bus
2082 * @msgs: One or more messages to execute before STOP is issued to
2083 * terminate the operation; each message begins with a START.
2084 * @num: Number of messages to be executed.
2085 *
2086 * Returns negative errno, else the number of messages executed.
2087 *
2088 * Adapter lock must be held when calling this function. No debug logging
2089 * takes place. adap->algo->master_xfer existence isn't checked.
2090 */
2091int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2092{
2093 unsigned long orig_jiffies;
2094 int ret, try;
2095
b7f62584
WS
2096 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2097 return -EOPNOTSUPP;
2098
d9a83d62
DH
2099 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2100 * enabled. This is an efficient way of keeping the for-loop from
2101 * being executed when not needed.
2102 */
2103 if (static_key_false(&i2c_trace_msg)) {
2104 int i;
2105 for (i = 0; i < num; i++)
2106 if (msgs[i].flags & I2C_M_RD)
2107 trace_i2c_read(adap, &msgs[i], i);
2108 else
2109 trace_i2c_write(adap, &msgs[i], i);
2110 }
2111
b37d2a3a
JD
2112 /* Retry automatically on arbitration loss */
2113 orig_jiffies = jiffies;
2114 for (ret = 0, try = 0; try <= adap->retries; try++) {
2115 ret = adap->algo->master_xfer(adap, msgs, num);
2116 if (ret != -EAGAIN)
2117 break;
2118 if (time_after(jiffies, orig_jiffies + adap->timeout))
2119 break;
2120 }
2121
d9a83d62
DH
2122 if (static_key_false(&i2c_trace_msg)) {
2123 int i;
2124 for (i = 0; i < ret; i++)
2125 if (msgs[i].flags & I2C_M_RD)
2126 trace_i2c_reply(adap, &msgs[i], i);
2127 trace_i2c_result(adap, i, ret);
2128 }
2129
b37d2a3a
JD
2130 return ret;
2131}
2132EXPORT_SYMBOL(__i2c_transfer);
2133
a1cdedac
DB
2134/**
2135 * i2c_transfer - execute a single or combined I2C message
2136 * @adap: Handle to I2C bus
2137 * @msgs: One or more messages to execute before STOP is issued to
2138 * terminate the operation; each message begins with a START.
2139 * @num: Number of messages to be executed.
2140 *
2141 * Returns negative errno, else the number of messages executed.
2142 *
2143 * Note that there is no requirement that each message be sent to
2144 * the same slave address, although that is the most common model.
2145 */
09b8ce0a 2146int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1da177e4 2147{
b37d2a3a 2148 int ret;
1da177e4 2149
a1cdedac
DB
2150 /* REVISIT the fault reporting model here is weak:
2151 *
2152 * - When we get an error after receiving N bytes from a slave,
2153 * there is no way to report "N".
2154 *
2155 * - When we get a NAK after transmitting N bytes to a slave,
2156 * there is no way to report "N" ... or to let the master
2157 * continue executing the rest of this combined message, if
2158 * that's the appropriate response.
2159 *
2160 * - When for example "num" is two and we successfully complete
2161 * the first message but get an error part way through the
2162 * second, it's unclear whether that should be reported as
2163 * one (discarding status on the second message) or errno
2164 * (discarding status on the first one).
2165 */
2166
1da177e4
LT
2167 if (adap->algo->master_xfer) {
2168#ifdef DEBUG
2169 for (ret = 0; ret < num; ret++) {
2170 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
209d27c3
JD
2171 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2172 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2173 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1da177e4
LT
2174 }
2175#endif
2176
cea443a8 2177 if (in_atomic() || irqs_disabled()) {
fe61e07e 2178 ret = i2c_trylock_adapter(adap);
cea443a8
MR
2179 if (!ret)
2180 /* I2C activity is ongoing. */
2181 return -EAGAIN;
2182 } else {
fe61e07e 2183 i2c_lock_adapter(adap);
cea443a8
MR
2184 }
2185
b37d2a3a 2186 ret = __i2c_transfer(adap, msgs, num);
fe61e07e 2187 i2c_unlock_adapter(adap);
1da177e4
LT
2188
2189 return ret;
2190 } else {
2191 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
24a5bb7b 2192 return -EOPNOTSUPP;
1da177e4
LT
2193 }
2194}
c0564606 2195EXPORT_SYMBOL(i2c_transfer);
1da177e4 2196
a1cdedac
DB
2197/**
2198 * i2c_master_send - issue a single I2C message in master transmit mode
2199 * @client: Handle to slave device
2200 * @buf: Data that will be written to the slave
0c43ea54 2201 * @count: How many bytes to write, must be less than 64k since msg.len is u16
a1cdedac
DB
2202 *
2203 * Returns negative errno, or else the number of bytes written.
2204 */
0cc43a18 2205int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
1da177e4
LT
2206{
2207 int ret;
7225acf4 2208 struct i2c_adapter *adap = client->adapter;
1da177e4
LT
2209 struct i2c_msg msg;
2210
815f55f2
JD
2211 msg.addr = client->addr;
2212 msg.flags = client->flags & I2C_M_TEN;
2213 msg.len = count;
2214 msg.buf = (char *)buf;
438d6c2c 2215
815f55f2 2216 ret = i2c_transfer(adap, &msg, 1);
1da177e4 2217
834aa6f3
WS
2218 /*
2219 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2220 * transmitted, else error code.
2221 */
815f55f2 2222 return (ret == 1) ? count : ret;
1da177e4 2223}
c0564606 2224EXPORT_SYMBOL(i2c_master_send);
1da177e4 2225
a1cdedac
DB
2226/**
2227 * i2c_master_recv - issue a single I2C message in master receive mode
2228 * @client: Handle to slave device
2229 * @buf: Where to store data read from slave
0c43ea54 2230 * @count: How many bytes to read, must be less than 64k since msg.len is u16
a1cdedac
DB
2231 *
2232 * Returns negative errno, or else the number of bytes read.
2233 */
0cc43a18 2234int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
1da177e4 2235{
7225acf4 2236 struct i2c_adapter *adap = client->adapter;
1da177e4
LT
2237 struct i2c_msg msg;
2238 int ret;
815f55f2
JD
2239
2240 msg.addr = client->addr;
2241 msg.flags = client->flags & I2C_M_TEN;
2242 msg.flags |= I2C_M_RD;
2243 msg.len = count;
2244 msg.buf = buf;
2245
2246 ret = i2c_transfer(adap, &msg, 1);
2247
834aa6f3
WS
2248 /*
2249 * If everything went ok (i.e. 1 msg received), return #bytes received,
2250 * else error code.
2251 */
815f55f2 2252 return (ret == 1) ? count : ret;
1da177e4 2253}
c0564606 2254EXPORT_SYMBOL(i2c_master_recv);
1da177e4 2255
1da177e4
LT
2256/* ----------------------------------------------------
2257 * the i2c address scanning function
2258 * Will not work for 10-bit addresses!
2259 * ----------------------------------------------------
2260 */
1da177e4 2261
63e4e802
JD
2262/*
2263 * Legacy default probe function, mostly relevant for SMBus. The default
2264 * probe method is a quick write, but it is known to corrupt the 24RF08
2265 * EEPROMs due to a state machine bug, and could also irreversibly
2266 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2267 * we use a short byte read instead. Also, some bus drivers don't implement
2268 * quick write, so we fallback to a byte read in that case too.
2269 * On x86, there is another special case for FSC hardware monitoring chips,
2270 * which want regular byte reads (address 0x73.) Fortunately, these are the
2271 * only known chips using this I2C address on PC hardware.
2272 * Returns 1 if probe succeeded, 0 if not.
2273 */
2274static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2275{
2276 int err;
2277 union i2c_smbus_data dummy;
2278
2279#ifdef CONFIG_X86
2280 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2281 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2282 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2283 I2C_SMBUS_BYTE_DATA, &dummy);
2284 else
2285#endif
8031d79b
JD
2286 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2287 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
63e4e802
JD
2288 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2289 I2C_SMBUS_QUICK, NULL);
8031d79b
JD
2290 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2291 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2292 I2C_SMBUS_BYTE, &dummy);
2293 else {
d63a9e85
AL
2294 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2295 addr);
8031d79b
JD
2296 err = -EOPNOTSUPP;
2297 }
63e4e802
JD
2298
2299 return err >= 0;
2300}
2301
ccfbbd08 2302static int i2c_detect_address(struct i2c_client *temp_client,
4735c98f
JD
2303 struct i2c_driver *driver)
2304{
2305 struct i2c_board_info info;
2306 struct i2c_adapter *adapter = temp_client->adapter;
2307 int addr = temp_client->addr;
2308 int err;
2309
2310 /* Make sure the address is valid */
66be6056 2311 err = i2c_check_7bit_addr_validity_strict(addr);
656b8761 2312 if (err) {
4735c98f
JD
2313 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2314 addr);
656b8761 2315 return err;
4735c98f
JD
2316 }
2317
2318 /* Skip if already in use */
3b5f794b 2319 if (i2c_check_addr_busy(adapter, addr))
4735c98f
JD
2320 return 0;
2321
ccfbbd08 2322 /* Make sure there is something at this address */
63e4e802
JD
2323 if (!i2c_default_probe(adapter, addr))
2324 return 0;
4735c98f
JD
2325
2326 /* Finally call the custom detection function */
2327 memset(&info, 0, sizeof(struct i2c_board_info));
2328 info.addr = addr;
310ec792 2329 err = driver->detect(temp_client, &info);
4735c98f
JD
2330 if (err) {
2331 /* -ENODEV is returned if the detection fails. We catch it
2332 here as this isn't an error. */
2333 return err == -ENODEV ? 0 : err;
2334 }
2335
2336 /* Consistency check */
2337 if (info.type[0] == '\0') {
2338 dev_err(&adapter->dev, "%s detection function provided "
2339 "no name for 0x%x\n", driver->driver.name,
2340 addr);
2341 } else {
2342 struct i2c_client *client;
2343
2344 /* Detection succeeded, instantiate the device */
0c176170
WS
2345 if (adapter->class & I2C_CLASS_DEPRECATED)
2346 dev_warn(&adapter->dev,
2347 "This adapter will soon drop class based instantiation of devices. "
2348 "Please make sure client 0x%02x gets instantiated by other means. "
2349 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2350 info.addr);
2351
4735c98f
JD
2352 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2353 info.type, info.addr);
2354 client = i2c_new_device(adapter, &info);
2355 if (client)
2356 list_add_tail(&client->detected, &driver->clients);
2357 else
2358 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2359 info.type, info.addr);
2360 }
2361 return 0;
2362}
2363
2364static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2365{
c3813d6a 2366 const unsigned short *address_list;
4735c98f
JD
2367 struct i2c_client *temp_client;
2368 int i, err = 0;
2369 int adap_id = i2c_adapter_id(adapter);
2370
c3813d6a
JD
2371 address_list = driver->address_list;
2372 if (!driver->detect || !address_list)
4735c98f
JD
2373 return 0;
2374
45552272
WS
2375 /* Warn that the adapter lost class based instantiation */
2376 if (adapter->class == I2C_CLASS_DEPRECATED) {
2377 dev_dbg(&adapter->dev,
2378 "This adapter dropped support for I2C classes and "
2379 "won't auto-detect %s devices anymore. If you need it, check "
2380 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2381 driver->driver.name);
2382 return 0;
2383 }
2384
51b54ba9
JD
2385 /* Stop here if the classes do not match */
2386 if (!(adapter->class & driver->class))
2387 return 0;
2388
4735c98f
JD
2389 /* Set up a temporary client to help detect callback */
2390 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2391 if (!temp_client)
2392 return -ENOMEM;
2393 temp_client->adapter = adapter;
2394
c3813d6a 2395 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
4735c98f 2396 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
c3813d6a
JD
2397 "addr 0x%02x\n", adap_id, address_list[i]);
2398 temp_client->addr = address_list[i];
ccfbbd08 2399 err = i2c_detect_address(temp_client, driver);
51b54ba9
JD
2400 if (unlikely(err))
2401 break;
4735c98f
JD
2402 }
2403
4735c98f
JD
2404 kfree(temp_client);
2405 return err;
2406}
2407
d44f19d5
JD
2408int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2409{
2410 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2411 I2C_SMBUS_QUICK, NULL) >= 0;
2412}
2413EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2414
12b5053a
JD
2415struct i2c_client *
2416i2c_new_probed_device(struct i2c_adapter *adap,
2417 struct i2c_board_info *info,
9a94241a
JD
2418 unsigned short const *addr_list,
2419 int (*probe)(struct i2c_adapter *, unsigned short addr))
12b5053a
JD
2420{
2421 int i;
2422
8031d79b 2423 if (!probe)
9a94241a 2424 probe = i2c_default_probe;
12b5053a 2425
12b5053a
JD
2426 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2427 /* Check address validity */
66be6056 2428 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
12b5053a
JD
2429 dev_warn(&adap->dev, "Invalid 7-bit address "
2430 "0x%02x\n", addr_list[i]);
2431 continue;
2432 }
2433
2434 /* Check address availability */
3b5f794b 2435 if (i2c_check_addr_busy(adap, addr_list[i])) {
12b5053a
JD
2436 dev_dbg(&adap->dev, "Address 0x%02x already in "
2437 "use, not probing\n", addr_list[i]);
2438 continue;
2439 }
2440
63e4e802 2441 /* Test address responsiveness */
9a94241a 2442 if (probe(adap, addr_list[i]))
63e4e802 2443 break;
12b5053a 2444 }
12b5053a
JD
2445
2446 if (addr_list[i] == I2C_CLIENT_END) {
2447 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2448 return NULL;
2449 }
2450
2451 info->addr = addr_list[i];
2452 return i2c_new_device(adap, info);
2453}
2454EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2455
d735b34d 2456struct i2c_adapter *i2c_get_adapter(int nr)
1da177e4 2457{
1da177e4 2458 struct i2c_adapter *adapter;
438d6c2c 2459
caada32a 2460 mutex_lock(&core_lock);
d735b34d 2461 adapter = idr_find(&i2c_adapter_idr, nr);
611e12ea
VZ
2462 if (!adapter)
2463 goto exit;
2464
2465 if (try_module_get(adapter->owner))
2466 get_device(&adapter->dev);
2467 else
a0920e10
MH
2468 adapter = NULL;
2469
611e12ea 2470 exit:
caada32a 2471 mutex_unlock(&core_lock);
a0920e10 2472 return adapter;
1da177e4 2473}
c0564606 2474EXPORT_SYMBOL(i2c_get_adapter);
1da177e4
LT
2475
2476void i2c_put_adapter(struct i2c_adapter *adap)
2477{
611e12ea
VZ
2478 if (!adap)
2479 return;
2480
2481 put_device(&adap->dev);
2482 module_put(adap->owner);
1da177e4 2483}
c0564606 2484EXPORT_SYMBOL(i2c_put_adapter);
1da177e4
LT
2485
2486/* The SMBus parts */
2487
438d6c2c 2488#define POLY (0x1070U << 3)
09b8ce0a 2489static u8 crc8(u16 data)
1da177e4
LT
2490{
2491 int i;
438d6c2c 2492
7225acf4 2493 for (i = 0; i < 8; i++) {
438d6c2c 2494 if (data & 0x8000)
1da177e4
LT
2495 data = data ^ POLY;
2496 data = data << 1;
2497 }
2498 return (u8)(data >> 8);
2499}
2500
421ef47b
JD
2501/* Incremental CRC8 over count bytes in the array pointed to by p */
2502static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1da177e4
LT
2503{
2504 int i;
2505
7225acf4 2506 for (i = 0; i < count; i++)
421ef47b 2507 crc = crc8((crc ^ p[i]) << 8);
1da177e4
LT
2508 return crc;
2509}
2510
421ef47b
JD
2511/* Assume a 7-bit address, which is reasonable for SMBus */
2512static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1da177e4 2513{
421ef47b
JD
2514 /* The address will be sent first */
2515 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2516 pec = i2c_smbus_pec(pec, &addr, 1);
2517
2518 /* The data buffer follows */
2519 return i2c_smbus_pec(pec, msg->buf, msg->len);
1da177e4
LT
2520}
2521
421ef47b
JD
2522/* Used for write only transactions */
2523static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1da177e4 2524{
421ef47b
JD
2525 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2526 msg->len++;
1da177e4
LT
2527}
2528
421ef47b
JD
2529/* Return <0 on CRC error
2530 If there was a write before this read (most cases) we need to take the
2531 partial CRC from the write part into account.
2532 Note that this function does modify the message (we need to decrease the
2533 message length to hide the CRC byte from the caller). */
2534static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1da177e4 2535{
421ef47b
JD
2536 u8 rpec = msg->buf[--msg->len];
2537 cpec = i2c_smbus_msg_pec(cpec, msg);
1da177e4 2538
1da177e4
LT
2539 if (rpec != cpec) {
2540 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2541 rpec, cpec);
24a5bb7b 2542 return -EBADMSG;
1da177e4 2543 }
438d6c2c 2544 return 0;
1da177e4
LT
2545}
2546
a1cdedac
DB
2547/**
2548 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2549 * @client: Handle to slave device
2550 *
2551 * This executes the SMBus "receive byte" protocol, returning negative errno
2552 * else the byte received from the device.
2553 */
0cc43a18 2554s32 i2c_smbus_read_byte(const struct i2c_client *client)
1da177e4
LT
2555{
2556 union i2c_smbus_data data;
24a5bb7b
DB
2557 int status;
2558
2559 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2560 I2C_SMBUS_READ, 0,
2561 I2C_SMBUS_BYTE, &data);
2562 return (status < 0) ? status : data.byte;
1da177e4 2563}
c0564606 2564EXPORT_SYMBOL(i2c_smbus_read_byte);
1da177e4 2565
a1cdedac
DB
2566/**
2567 * i2c_smbus_write_byte - SMBus "send byte" protocol
2568 * @client: Handle to slave device
2569 * @value: Byte to be sent
2570 *
2571 * This executes the SMBus "send byte" protocol, returning negative errno
2572 * else zero on success.
2573 */
0cc43a18 2574s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
1da177e4 2575{
7225acf4 2576 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
421ef47b 2577 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1da177e4 2578}
c0564606 2579EXPORT_SYMBOL(i2c_smbus_write_byte);
1da177e4 2580
a1cdedac
DB
2581/**
2582 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2583 * @client: Handle to slave device
2584 * @command: Byte interpreted by slave
2585 *
2586 * This executes the SMBus "read byte" protocol, returning negative errno
2587 * else a data byte received from the device.
2588 */
0cc43a18 2589s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
1da177e4
LT
2590{
2591 union i2c_smbus_data data;
24a5bb7b
DB
2592 int status;
2593
2594 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2595 I2C_SMBUS_READ, command,
2596 I2C_SMBUS_BYTE_DATA, &data);
2597 return (status < 0) ? status : data.byte;
1da177e4 2598}
c0564606 2599EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1da177e4 2600
a1cdedac
DB
2601/**
2602 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2603 * @client: Handle to slave device
2604 * @command: Byte interpreted by slave
2605 * @value: Byte being written
2606 *
2607 * This executes the SMBus "write byte" protocol, returning negative errno
2608 * else zero on success.
2609 */
0cc43a18
JD
2610s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2611 u8 value)
1da177e4
LT
2612{
2613 union i2c_smbus_data data;
2614 data.byte = value;
7225acf4
FH
2615 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2616 I2C_SMBUS_WRITE, command,
2617 I2C_SMBUS_BYTE_DATA, &data);
1da177e4 2618}
c0564606 2619EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1da177e4 2620
a1cdedac
DB
2621/**
2622 * i2c_smbus_read_word_data - SMBus "read word" protocol
2623 * @client: Handle to slave device
2624 * @command: Byte interpreted by slave
2625 *
2626 * This executes the SMBus "read word" protocol, returning negative errno
2627 * else a 16-bit unsigned "word" received from the device.
2628 */
0cc43a18 2629s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
1da177e4
LT
2630{
2631 union i2c_smbus_data data;
24a5bb7b
DB
2632 int status;
2633
2634 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2635 I2C_SMBUS_READ, command,
2636 I2C_SMBUS_WORD_DATA, &data);
2637 return (status < 0) ? status : data.word;
1da177e4 2638}
c0564606 2639EXPORT_SYMBOL(i2c_smbus_read_word_data);
1da177e4 2640
a1cdedac
DB
2641/**
2642 * i2c_smbus_write_word_data - SMBus "write word" protocol
2643 * @client: Handle to slave device
2644 * @command: Byte interpreted by slave
2645 * @value: 16-bit "word" being written
2646 *
2647 * This executes the SMBus "write word" protocol, returning negative errno
2648 * else zero on success.
2649 */
0cc43a18
JD
2650s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2651 u16 value)
1da177e4
LT
2652{
2653 union i2c_smbus_data data;
2654 data.word = value;
7225acf4
FH
2655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2656 I2C_SMBUS_WRITE, command,
2657 I2C_SMBUS_WORD_DATA, &data);
1da177e4 2658}
c0564606 2659EXPORT_SYMBOL(i2c_smbus_write_word_data);
1da177e4 2660
a64ec07d 2661/**
a1cdedac 2662 * i2c_smbus_read_block_data - SMBus "block read" protocol
a64ec07d 2663 * @client: Handle to slave device
a1cdedac 2664 * @command: Byte interpreted by slave
a64ec07d
DB
2665 * @values: Byte array into which data will be read; big enough to hold
2666 * the data returned by the slave. SMBus allows at most 32 bytes.
2667 *
a1cdedac
DB
2668 * This executes the SMBus "block read" protocol, returning negative errno
2669 * else the number of data bytes in the slave's response.
a64ec07d
DB
2670 *
2671 * Note that using this function requires that the client's adapter support
2672 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2673 * support this; its emulation through I2C messaging relies on a specific
2674 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2675 */
0cc43a18 2676s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
b86a1bc8
JD
2677 u8 *values)
2678{
2679 union i2c_smbus_data data;
24a5bb7b 2680 int status;
b86a1bc8 2681
24a5bb7b
DB
2682 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2683 I2C_SMBUS_READ, command,
2684 I2C_SMBUS_BLOCK_DATA, &data);
2685 if (status)
2686 return status;
b86a1bc8
JD
2687
2688 memcpy(values, &data.block[1], data.block[0]);
2689 return data.block[0];
2690}
2691EXPORT_SYMBOL(i2c_smbus_read_block_data);
2692
a1cdedac
DB
2693/**
2694 * i2c_smbus_write_block_data - SMBus "block write" protocol
2695 * @client: Handle to slave device
2696 * @command: Byte interpreted by slave
2697 * @length: Size of data block; SMBus allows at most 32 bytes
2698 * @values: Byte array which will be written.
2699 *
2700 * This executes the SMBus "block write" protocol, returning negative errno
2701 * else zero on success.
2702 */
0cc43a18 2703s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
46f5ed75 2704 u8 length, const u8 *values)
1da177e4
LT
2705{
2706 union i2c_smbus_data data;
7656032b 2707
1da177e4
LT
2708 if (length > I2C_SMBUS_BLOCK_MAX)
2709 length = I2C_SMBUS_BLOCK_MAX;
1da177e4 2710 data.block[0] = length;
7656032b 2711 memcpy(&data.block[1], values, length);
7225acf4
FH
2712 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2713 I2C_SMBUS_WRITE, command,
2714 I2C_SMBUS_BLOCK_DATA, &data);
1da177e4 2715}
c0564606 2716EXPORT_SYMBOL(i2c_smbus_write_block_data);
1da177e4
LT
2717
2718/* Returns the number of read bytes */
0cc43a18 2719s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
4b2643d7 2720 u8 length, u8 *values)
1da177e4
LT
2721{
2722 union i2c_smbus_data data;
24a5bb7b 2723 int status;
7656032b 2724
4b2643d7
JD
2725 if (length > I2C_SMBUS_BLOCK_MAX)
2726 length = I2C_SMBUS_BLOCK_MAX;
2727 data.block[0] = length;
24a5bb7b
DB
2728 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2729 I2C_SMBUS_READ, command,
2730 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2731 if (status < 0)
2732 return status;
7656032b
JD
2733
2734 memcpy(values, &data.block[1], data.block[0]);
2735 return data.block[0];
1da177e4 2736}
c0564606 2737EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1da177e4 2738
0cc43a18 2739s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
46f5ed75 2740 u8 length, const u8 *values)
21bbd691
JD
2741{
2742 union i2c_smbus_data data;
2743
2744 if (length > I2C_SMBUS_BLOCK_MAX)
2745 length = I2C_SMBUS_BLOCK_MAX;
2746 data.block[0] = length;
2747 memcpy(data.block + 1, values, length);
2748 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2749 I2C_SMBUS_WRITE, command,
2750 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2751}
c0564606 2752EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
21bbd691 2753
438d6c2c 2754/* Simulate a SMBus command using the i2c protocol
1da177e4 2755 No checking of parameters is done! */
7225acf4
FH
2756static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2757 unsigned short flags,
2758 char read_write, u8 command, int size,
2759 union i2c_smbus_data *data)
1da177e4
LT
2760{
2761 /* So we need to generate a series of msgs. In the case of writing, we
2762 need to use only one message; when reading, we need two. We initialize
2763 most things with sane defaults, to keep the code below somewhat
2764 simpler. */
5c50d188
HI
2765 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2766 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
7225acf4 2767 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1da177e4 2768 int i;
421ef47b 2769 u8 partial_pec = 0;
24a5bb7b 2770 int status;
230da094
S
2771 struct i2c_msg msg[2] = {
2772 {
2773 .addr = addr,
2774 .flags = flags,
2775 .len = 1,
2776 .buf = msgbuf0,
2777 }, {
2778 .addr = addr,
2779 .flags = flags | I2C_M_RD,
2780 .len = 0,
2781 .buf = msgbuf1,
2782 },
2783 };
1da177e4
LT
2784
2785 msgbuf0[0] = command;
7225acf4 2786 switch (size) {
1da177e4
LT
2787 case I2C_SMBUS_QUICK:
2788 msg[0].len = 0;
2789 /* Special case: The read/write field is used as data */
f29d2e02
RK
2790 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2791 I2C_M_RD : 0);
1da177e4
LT
2792 num = 1;
2793 break;
2794 case I2C_SMBUS_BYTE:
2795 if (read_write == I2C_SMBUS_READ) {
2796 /* Special case: only a read! */
2797 msg[0].flags = I2C_M_RD | flags;
2798 num = 1;
2799 }
2800 break;
2801 case I2C_SMBUS_BYTE_DATA:
2802 if (read_write == I2C_SMBUS_READ)
2803 msg[1].len = 1;
2804 else {
2805 msg[0].len = 2;
2806 msgbuf0[1] = data->byte;
2807 }
2808 break;
2809 case I2C_SMBUS_WORD_DATA:
2810 if (read_write == I2C_SMBUS_READ)
2811 msg[1].len = 2;
2812 else {
7225acf4 2813 msg[0].len = 3;
1da177e4 2814 msgbuf0[1] = data->word & 0xff;
7eff82c8 2815 msgbuf0[2] = data->word >> 8;
1da177e4
LT
2816 }
2817 break;
2818 case I2C_SMBUS_PROC_CALL:
2819 num = 2; /* Special case */
2820 read_write = I2C_SMBUS_READ;
2821 msg[0].len = 3;
2822 msg[1].len = 2;
2823 msgbuf0[1] = data->word & 0xff;
7eff82c8 2824 msgbuf0[2] = data->word >> 8;
1da177e4
LT
2825 break;
2826 case I2C_SMBUS_BLOCK_DATA:
1da177e4 2827 if (read_write == I2C_SMBUS_READ) {
209d27c3
JD
2828 msg[1].flags |= I2C_M_RECV_LEN;
2829 msg[1].len = 1; /* block length will be added by
2830 the underlying bus driver */
1da177e4
LT
2831 } else {
2832 msg[0].len = data->block[0] + 2;
2833 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
24a5bb7b
DB
2834 dev_err(&adapter->dev,
2835 "Invalid block write size %d\n",
2836 data->block[0]);
2837 return -EINVAL;
1da177e4 2838 }
5c50d188 2839 for (i = 1; i < msg[0].len; i++)
1da177e4
LT
2840 msgbuf0[i] = data->block[i-1];
2841 }
2842 break;
2843 case I2C_SMBUS_BLOCK_PROC_CALL:
209d27c3
JD
2844 num = 2; /* Another special case */
2845 read_write = I2C_SMBUS_READ;
2846 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
24a5bb7b
DB
2847 dev_err(&adapter->dev,
2848 "Invalid block write size %d\n",
209d27c3 2849 data->block[0]);
24a5bb7b 2850 return -EINVAL;
209d27c3
JD
2851 }
2852 msg[0].len = data->block[0] + 2;
2853 for (i = 1; i < msg[0].len; i++)
2854 msgbuf0[i] = data->block[i-1];
2855 msg[1].flags |= I2C_M_RECV_LEN;
2856 msg[1].len = 1; /* block length will be added by
2857 the underlying bus driver */
2858 break;
1da177e4
LT
2859 case I2C_SMBUS_I2C_BLOCK_DATA:
2860 if (read_write == I2C_SMBUS_READ) {
4b2643d7 2861 msg[1].len = data->block[0];
1da177e4
LT
2862 } else {
2863 msg[0].len = data->block[0] + 1;
30dac746 2864 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
24a5bb7b
DB
2865 dev_err(&adapter->dev,
2866 "Invalid block write size %d\n",
2867 data->block[0]);
2868 return -EINVAL;
1da177e4
LT
2869 }
2870 for (i = 1; i <= data->block[0]; i++)
2871 msgbuf0[i] = data->block[i];
2872 }
2873 break;
2874 default:
24a5bb7b
DB
2875 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2876 return -EOPNOTSUPP;
1da177e4
LT
2877 }
2878
421ef47b
JD
2879 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2880 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2881 if (i) {
2882 /* Compute PEC if first message is a write */
2883 if (!(msg[0].flags & I2C_M_RD)) {
438d6c2c 2884 if (num == 1) /* Write only */
421ef47b
JD
2885 i2c_smbus_add_pec(&msg[0]);
2886 else /* Write followed by read */
2887 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2888 }
2889 /* Ask for PEC if last message is a read */
2890 if (msg[num-1].flags & I2C_M_RD)
438d6c2c 2891 msg[num-1].len++;
421ef47b
JD
2892 }
2893
24a5bb7b
DB
2894 status = i2c_transfer(adapter, msg, num);
2895 if (status < 0)
2896 return status;
1da177e4 2897
421ef47b
JD
2898 /* Check PEC if last message is a read */
2899 if (i && (msg[num-1].flags & I2C_M_RD)) {
24a5bb7b
DB
2900 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2901 if (status < 0)
2902 return status;
421ef47b
JD
2903 }
2904
1da177e4 2905 if (read_write == I2C_SMBUS_READ)
7225acf4
FH
2906 switch (size) {
2907 case I2C_SMBUS_BYTE:
2908 data->byte = msgbuf0[0];
2909 break;
2910 case I2C_SMBUS_BYTE_DATA:
2911 data->byte = msgbuf1[0];
2912 break;
2913 case I2C_SMBUS_WORD_DATA:
2914 case I2C_SMBUS_PROC_CALL:
2915 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2916 break;
2917 case I2C_SMBUS_I2C_BLOCK_DATA:
2918 for (i = 0; i < data->block[0]; i++)
2919 data->block[i+1] = msgbuf1[i];
2920 break;
2921 case I2C_SMBUS_BLOCK_DATA:
2922 case I2C_SMBUS_BLOCK_PROC_CALL:
2923 for (i = 0; i < msgbuf1[0] + 1; i++)
2924 data->block[i] = msgbuf1[i];
2925 break;
1da177e4
LT
2926 }
2927 return 0;
2928}
2929
a1cdedac
DB
2930/**
2931 * i2c_smbus_xfer - execute SMBus protocol operations
2932 * @adapter: Handle to I2C bus
2933 * @addr: Address of SMBus slave on that bus
2934 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2935 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2936 * @command: Byte interpreted by slave, for protocols which use such bytes
2937 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2938 * @data: Data to be read or written
2939 *
2940 * This executes an SMBus protocol operation, and returns a negative
2941 * errno code else zero on success.
2942 */
09b8ce0a 2943s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
a1cdedac 2944 char read_write, u8 command, int protocol,
09b8ce0a 2945 union i2c_smbus_data *data)
1da177e4 2946{
66b650f0
CW
2947 unsigned long orig_jiffies;
2948 int try;
1da177e4 2949 s32 res;
1da177e4 2950
8a325997
DH
2951 /* If enabled, the following two tracepoints are conditional on
2952 * read_write and protocol.
2953 */
2954 trace_smbus_write(adapter, addr, flags, read_write,
2955 command, protocol, data);
2956 trace_smbus_read(adapter, addr, flags, read_write,
2957 command, protocol);
2958
d47726c5 2959 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
1da177e4
LT
2960
2961 if (adapter->algo->smbus_xfer) {
fe61e07e 2962 i2c_lock_adapter(adapter);
66b650f0
CW
2963
2964 /* Retry automatically on arbitration loss */
2965 orig_jiffies = jiffies;
2966 for (res = 0, try = 0; try <= adapter->retries; try++) {
2967 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2968 read_write, command,
2969 protocol, data);
2970 if (res != -EAGAIN)
2971 break;
2972 if (time_after(jiffies,
2973 orig_jiffies + adapter->timeout))
2974 break;
2975 }
fe61e07e 2976 i2c_unlock_adapter(adapter);
1da177e4 2977
72fc2c7f 2978 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
8a325997 2979 goto trace;
72fc2c7f
LP
2980 /*
2981 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2982 * implement native support for the SMBus operation.
2983 */
2984 }
2985
8a325997
DH
2986 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2987 command, protocol, data);
2988
2989trace:
2990 /* If enabled, the reply tracepoint is conditional on read_write. */
2991 trace_smbus_reply(adapter, addr, flags, read_write,
2992 command, protocol, data);
2993 trace_smbus_result(adapter, addr, flags, read_write,
2994 command, protocol, res);
2995
2996 return res;
1da177e4 2997}
1da177e4 2998EXPORT_SYMBOL(i2c_smbus_xfer);
1da177e4 2999
d5fd120e 3000#if IS_ENABLED(CONFIG_I2C_SLAVE)
4b1acc43
WS
3001int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3002{
3003 int ret;
3004
0c7cab96
WS
3005 if (!client || !slave_cb) {
3006 WARN(1, "insufficent data\n");
4b1acc43 3007 return -EINVAL;
0c7cab96 3008 }
4b1acc43
WS
3009
3010 if (!(client->flags & I2C_CLIENT_TEN)) {
3011 /* Enforce stricter address checking */
66be6056 3012 ret = i2c_check_7bit_addr_validity_strict(client->addr);
0c7cab96
WS
3013 if (ret) {
3014 dev_err(&client->dev, "%s: invalid address\n", __func__);
4b1acc43 3015 return ret;
0c7cab96 3016 }
4b1acc43
WS
3017 }
3018
0c7cab96
WS
3019 if (!client->adapter->algo->reg_slave) {
3020 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
4b1acc43 3021 return -EOPNOTSUPP;
0c7cab96 3022 }
4b1acc43
WS
3023
3024 client->slave_cb = slave_cb;
3025
3026 i2c_lock_adapter(client->adapter);
3027 ret = client->adapter->algo->reg_slave(client);
3028 i2c_unlock_adapter(client->adapter);
3029
0c7cab96 3030 if (ret) {
4b1acc43 3031 client->slave_cb = NULL;
0c7cab96
WS
3032 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3033 }
4b1acc43
WS
3034
3035 return ret;
3036}
3037EXPORT_SYMBOL_GPL(i2c_slave_register);
3038
3039int i2c_slave_unregister(struct i2c_client *client)
3040{
3041 int ret;
3042
0c7cab96
WS
3043 if (!client->adapter->algo->unreg_slave) {
3044 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
4b1acc43 3045 return -EOPNOTSUPP;
0c7cab96 3046 }
4b1acc43
WS
3047
3048 i2c_lock_adapter(client->adapter);
3049 ret = client->adapter->algo->unreg_slave(client);
3050 i2c_unlock_adapter(client->adapter);
3051
3052 if (ret == 0)
3053 client->slave_cb = NULL;
0c7cab96
WS
3054 else
3055 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
4b1acc43
WS
3056
3057 return ret;
3058}
3059EXPORT_SYMBOL_GPL(i2c_slave_unregister);
d5fd120e 3060#endif
4b1acc43 3061
1da177e4
LT
3062MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3063MODULE_DESCRIPTION("I2C-Bus main module");
3064MODULE_LICENSE("GPL");
This page took 1.028205 seconds and 5 git commands to generate.