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