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