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