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