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