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