blob: cf830915713b387a0b9f4037b6d6fbeffbe2f889 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 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>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053031#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053033#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/i2c.h>
36#include <linux/init.h>
37#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010038#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010039#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060040#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010041#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020042#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010043#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010044#include <linux/hardirq.h>
45#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020046#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010047#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020048#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010049#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000050#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
David Howellsd9a83d62014-03-06 13:35:59 +000055#define CREATE_TRACE_POINTS
56#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jean Delvare6629dcf2010-05-04 11:09:28 +020058/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020059 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000060 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010061static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_IDR(i2c_adapter_idr);
63
Jean Delvare4f8cf822009-09-18 22:45:46 +020064static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020065static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010066
David Howellsd9a83d62014-03-06 13:35:59 +000067static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
68
69void i2c_transfer_trace_reg(void)
70{
71 static_key_slow_inc(&i2c_trace_msg);
72}
73
74void i2c_transfer_trace_unreg(void)
75{
76 static_key_slow_dec(&i2c_trace_msg);
77}
78
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020079#if defined(CONFIG_ACPI)
80struct acpi_i2c_handler_data {
81 struct acpi_connection_info info;
82 struct i2c_adapter *adapter;
83};
84
85struct gsb_buffer {
86 u8 status;
87 u8 len;
88 union {
89 u16 wdata;
90 u8 bdata;
91 u8 data[0];
92 };
93} __packed;
94
95static 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
119static 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 */
165static 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 */
185static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
186#endif /* CONFIG_ACPI */
187
188#ifdef CONFIG_ACPI_I2C_OPREGION
189static 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
221static 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
249static acpi_status
250acpi_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
368static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
369{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200370 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200371 struct acpi_i2c_handler_data *data;
372 acpi_status status;
373
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200374 if (!adapter->dev.parent)
375 return -ENODEV;
376
377 handle = ACPI_HANDLE(adapter->dev.parent);
378
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200379 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
409static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
410{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200411 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200412 struct acpi_i2c_handler_data *data;
413 acpi_status status;
414
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200415 if (!adapter->dev.parent)
416 return;
417
418 handle = ACPI_HANDLE(adapter->dev.parent);
419
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200420 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 */
434static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
435{ }
436
437static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
438{ return 0; }
439#endif /* CONFIG_ACPI_I2C_OPREGION */
440
David Brownellf37dd802007-02-13 22:09:00 +0100441/* ------------------------------------------------------------------------- */
442
Jean Delvared2653e92008-04-29 23:11:39 +0200443static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int i2c_device_match(struct device *dev, struct device_driver *drv)
455{
Jean Delvare51298d12009-09-18 22:45:45 +0200456 struct i2c_client *client = i2c_verify_client(dev);
457 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200458
Jean Delvare51298d12009-09-18 22:45:45 +0200459 if (!client)
460 return 0;
461
Grant Likely959e85f2010-06-08 07:48:19 -0600462 /* Attempt an OF style match */
463 if (of_driver_match_device(dev, drv))
464 return 1;
465
Mika Westerberg907ddf82012-11-23 12:23:40 +0100466 /* Then ACPI style match */
467 if (acpi_driver_match_device(dev, drv))
468 return 1;
469
Jean Delvare51298d12009-09-18 22:45:45 +0200470 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200471 /* 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
Jean Delvareeb8a7902008-05-18 20:49:41 +0200475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
David Brownell7b4fbc52007-05-01 23:26:30 +0200478
479/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200480static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200481{
482 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800483 int rc;
484
485 rc = acpi_device_uevent_modalias(dev, env);
486 if (rc != -ENODEV)
487 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200488
Jean Delvareeb8a7902008-05-18 20:49:41 +0200489 if (add_uevent_var(env, "MODALIAS=%s%s",
490 I2C_MODULE_PREFIX, client->name))
491 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200492 dev_dbg(dev, "uevent\n");
493 return 0;
494}
495
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530496/* i2c bus recovery routines */
497static int get_scl_gpio_value(struct i2c_adapter *adap)
498{
499 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
500}
501
502static 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
507static int get_sda_gpio_value(struct i2c_adapter *adap)
508{
509 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
510}
511
512static 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
537static 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
555static 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
591int 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
597int 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
611int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int i2c_device_probe(struct device *dev)
621{
Jean Delvare51298d12009-09-18 22:45:45 +0200622 struct i2c_client *client = i2c_verify_client(dev);
623 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100624 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200625
Jean Delvare51298d12009-09-18 22:45:45 +0200626 if (!client)
627 return 0;
628
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200629 if (!client->irq && dev->of_node) {
630 int irq = of_irq_get(dev->of_node, 0);
631
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100632 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200633 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100634 if (irq < 0)
635 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200636
637 client->irq = irq;
638 }
639
Jean Delvare51298d12009-09-18 22:45:45 +0200640 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200641 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200642 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200643
Marc Pignatee354252008-08-28 08:33:22 +0200644 if (!device_can_wakeup(&client->dev))
645 device_init_wakeup(&client->dev,
646 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200647 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200648
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200649 status = of_clk_set_defaults(dev->of_node, false);
650 if (status < 0)
651 return status;
652
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200653 status = dev_pm_domain_attach(&client->dev, true);
654 if (status != -EPROBE_DEFER) {
655 status = driver->probe(client, i2c_match_id(driver->id_table,
656 client));
657 if (status)
658 dev_pm_domain_detach(&client->dev, true);
659 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100660
Hans Verkuil50c33042008-03-12 14:15:00 +0100661 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static int i2c_device_remove(struct device *dev)
665{
Jean Delvare51298d12009-09-18 22:45:45 +0200666 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200667 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100668 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200669
Jean Delvare51298d12009-09-18 22:45:45 +0200670 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200671 return 0;
672
673 driver = to_i2c_driver(dev->driver);
674 if (driver->remove) {
675 dev_dbg(dev, "remove\n");
676 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200677 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100678
Laurent Pincharte4df3a02014-10-30 15:59:37 +0200679 if (dev->of_node)
680 irq_dispose_mapping(client->irq);
681
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200682 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
David Brownellf37dd802007-02-13 22:09:00 +0100686static void i2c_device_shutdown(struct device *dev)
687{
Jean Delvare51298d12009-09-18 22:45:45 +0200688 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100689 struct i2c_driver *driver;
690
Jean Delvare51298d12009-09-18 22:45:45 +0200691 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100692 return;
693 driver = to_i2c_driver(dev->driver);
694 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200695 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100696}
697
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200698#ifdef CONFIG_PM_SLEEP
699static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100700{
Jean Delvare51298d12009-09-18 22:45:45 +0200701 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100702 struct i2c_driver *driver;
703
Jean Delvare51298d12009-09-18 22:45:45 +0200704 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100705 return 0;
706 driver = to_i2c_driver(dev->driver);
707 if (!driver->suspend)
708 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200709 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100710}
711
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200712static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100713{
Jean Delvare51298d12009-09-18 22:45:45 +0200714 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100715 struct i2c_driver *driver;
716
Jean Delvare51298d12009-09-18 22:45:45 +0200717 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100718 return 0;
719 driver = to_i2c_driver(dev->driver);
720 if (!driver->resume)
721 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200722 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100723}
724
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200725static int i2c_device_pm_suspend(struct device *dev)
726{
727 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
728
Mark Brownd529de22011-01-14 22:03:49 +0100729 if (pm)
730 return pm_generic_suspend(dev);
731 else
732 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200733}
734
735static int i2c_device_pm_resume(struct device *dev)
736{
737 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200738
739 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100740 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200741 else
Mark Brownd529de22011-01-14 22:03:49 +0100742 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200743}
744
745static int i2c_device_pm_freeze(struct device *dev)
746{
747 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
748
Mark Brownd529de22011-01-14 22:03:49 +0100749 if (pm)
750 return pm_generic_freeze(dev);
751 else
752 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200753}
754
755static int i2c_device_pm_thaw(struct device *dev)
756{
757 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
758
Mark Brownd529de22011-01-14 22:03:49 +0100759 if (pm)
760 return pm_generic_thaw(dev);
761 else
762 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200763}
764
765static int i2c_device_pm_poweroff(struct device *dev)
766{
767 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
768
Mark Brownd529de22011-01-14 22:03:49 +0100769 if (pm)
770 return pm_generic_poweroff(dev);
771 else
772 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200773}
774
775static int i2c_device_pm_restore(struct device *dev)
776{
777 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200778
779 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100780 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200781 else
Mark Brownd529de22011-01-14 22:03:49 +0100782 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200783}
784#else /* !CONFIG_PM_SLEEP */
785#define i2c_device_pm_suspend NULL
786#define i2c_device_pm_resume NULL
787#define i2c_device_pm_freeze NULL
788#define i2c_device_pm_thaw NULL
789#define i2c_device_pm_poweroff NULL
790#define i2c_device_pm_restore NULL
791#endif /* !CONFIG_PM_SLEEP */
792
David Brownell9c1600e2007-05-01 23:26:31 +0200793static void i2c_client_dev_release(struct device *dev)
794{
795 kfree(to_i2c_client(dev));
796}
797
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100798static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200799show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200800{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200801 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
802 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200803}
804
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100805static ssize_t
806show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200807{
808 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800809 int len;
810
811 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
812 if (len != -ENODEV)
813 return len;
814
Jean Delvareeb8a7902008-05-18 20:49:41 +0200815 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200816}
817
Jean Delvare4f8cf822009-09-18 22:45:46 +0200818static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200819static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
820
821static struct attribute *i2c_dev_attrs[] = {
822 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200823 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200824 &dev_attr_modalias.attr,
825 NULL
826};
827
828static struct attribute_group i2c_dev_attr_group = {
829 .attrs = i2c_dev_attrs,
830};
831
832static const struct attribute_group *i2c_dev_attr_groups[] = {
833 &i2c_dev_attr_group,
834 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200835};
836
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100837static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100838 .suspend = i2c_device_pm_suspend,
839 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200840 .freeze = i2c_device_pm_freeze,
841 .thaw = i2c_device_pm_thaw,
842 .poweroff = i2c_device_pm_poweroff,
843 .restore = i2c_device_pm_restore,
844 SET_RUNTIME_PM_OPS(
845 pm_generic_runtime_suspend,
846 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200847 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200848 )
sonic zhang54067ee2009-12-14 21:17:30 +0100849};
850
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200851struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100852 .name = "i2c",
853 .match = i2c_device_match,
854 .probe = i2c_device_probe,
855 .remove = i2c_device_remove,
856 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100857 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000858};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200859EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000860
Jean Delvare51298d12009-09-18 22:45:45 +0200861static struct device_type i2c_client_type = {
862 .groups = i2c_dev_attr_groups,
863 .uevent = i2c_device_uevent,
864 .release = i2c_client_dev_release,
865};
866
David Brownell9b766b82008-01-27 18:14:51 +0100867
868/**
869 * i2c_verify_client - return parameter as i2c_client, or NULL
870 * @dev: device, probably from some driver model iterator
871 *
872 * When traversing the driver model tree, perhaps using driver model
873 * iterators like @device_for_each_child(), you can't assume very much
874 * about the nodes you find. Use this function to avoid oopses caused
875 * by wrongly treating some non-I2C device as an i2c_client.
876 */
877struct i2c_client *i2c_verify_client(struct device *dev)
878{
Jean Delvare51298d12009-09-18 22:45:45 +0200879 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100880 ? to_i2c_client(dev)
881 : NULL;
882}
883EXPORT_SYMBOL(i2c_verify_client);
884
885
Jean Delvare3a89db52010-06-03 11:33:52 +0200886/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300887 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200888static int i2c_check_client_addr_validity(const struct i2c_client *client)
889{
890 if (client->flags & I2C_CLIENT_TEN) {
891 /* 10-bit address, all values are valid */
892 if (client->addr > 0x3ff)
893 return -EINVAL;
894 } else {
895 /* 7-bit address, reject the general call address */
896 if (client->addr == 0x00 || client->addr > 0x7f)
897 return -EINVAL;
898 }
899 return 0;
900}
901
Jean Delvare656b8762010-06-03 11:33:53 +0200902/* And this is a strict address validity check, used when probing. If a
903 * device uses a reserved address, then it shouldn't be probed. 7-bit
904 * addressing is assumed, 10-bit address devices are rare and should be
905 * explicitly enumerated. */
906static int i2c_check_addr_validity(unsigned short addr)
907{
908 /*
909 * Reserved addresses per I2C specification:
910 * 0x00 General call address / START byte
911 * 0x01 CBUS address
912 * 0x02 Reserved for different bus format
913 * 0x03 Reserved for future purposes
914 * 0x04-0x07 Hs-mode master code
915 * 0x78-0x7b 10-bit slave addressing
916 * 0x7c-0x7f Reserved for future purposes
917 */
918 if (addr < 0x08 || addr > 0x77)
919 return -EINVAL;
920 return 0;
921}
922
Jean Delvare3b5f7942010-06-03 11:33:55 +0200923static int __i2c_check_addr_busy(struct device *dev, void *addrp)
924{
925 struct i2c_client *client = i2c_verify_client(dev);
926 int addr = *(int *)addrp;
927
928 if (client && client->addr == addr)
929 return -EBUSY;
930 return 0;
931}
932
Michael Lawnick08263742010-08-11 18:21:02 +0200933/* walk up mux tree */
934static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
935{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200936 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200937 int result;
938
939 result = device_for_each_child(&adapter->dev, &addr,
940 __i2c_check_addr_busy);
941
Jean Delvare97cc4d42010-10-24 18:16:57 +0200942 if (!result && parent)
943 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200944
945 return result;
946}
947
948/* recurse down mux tree */
949static int i2c_check_mux_children(struct device *dev, void *addrp)
950{
951 int result;
952
953 if (dev->type == &i2c_adapter_type)
954 result = device_for_each_child(dev, addrp,
955 i2c_check_mux_children);
956 else
957 result = __i2c_check_addr_busy(dev, addrp);
958
959 return result;
960}
961
Jean Delvare3b5f7942010-06-03 11:33:55 +0200962static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
963{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200964 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200965 int result = 0;
966
Jean Delvare97cc4d42010-10-24 18:16:57 +0200967 if (parent)
968 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200969
970 if (!result)
971 result = device_for_each_child(&adapter->dev, &addr,
972 i2c_check_mux_children);
973
974 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200975}
976
David Brownell9c1600e2007-05-01 23:26:31 +0200977/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200978 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
979 * @adapter: Target I2C bus segment
980 */
981void i2c_lock_adapter(struct i2c_adapter *adapter)
982{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200983 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
984
985 if (parent)
986 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200987 else
988 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200989}
990EXPORT_SYMBOL_GPL(i2c_lock_adapter);
991
992/**
993 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
994 * @adapter: Target I2C bus segment
995 */
996static int i2c_trylock_adapter(struct i2c_adapter *adapter)
997{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200998 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
999
1000 if (parent)
1001 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001002 else
1003 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001004}
1005
1006/**
1007 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
1008 * @adapter: Target I2C bus segment
1009 */
1010void i2c_unlock_adapter(struct i2c_adapter *adapter)
1011{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001012 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1013
1014 if (parent)
1015 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001016 else
1017 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001018}
1019EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1020
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001021static void i2c_dev_set_name(struct i2c_adapter *adap,
1022 struct i2c_client *client)
1023{
1024 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1025
1026 if (adev) {
1027 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1028 return;
1029 }
1030
1031 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1032 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1033 client->addr | ((client->flags & I2C_CLIENT_TEN)
1034 ? 0xa000 : 0));
1035}
1036
Jean Delvarefe61e072010-08-11 18:20:58 +02001037/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001038 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001039 * @adap: the adapter managing the device
1040 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001041 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001042 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001043 * Create an i2c device. Binding is handled through driver model
1044 * probe()/remove() methods. A driver may be bound to this device when we
1045 * return from this function, or any later moment (e.g. maybe hotplugging will
1046 * load the driver module). This call is not appropriate for use by mainboard
1047 * initialization logic, which usually runs during an arch_initcall() long
1048 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001049 *
1050 * This returns the new i2c client, which may be saved for later use with
1051 * i2c_unregister_device(); or NULL to indicate an error.
1052 */
1053struct i2c_client *
1054i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1055{
1056 struct i2c_client *client;
1057 int status;
1058
1059 client = kzalloc(sizeof *client, GFP_KERNEL);
1060 if (!client)
1061 return NULL;
1062
1063 client->adapter = adap;
1064
1065 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001066
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001067 if (info->archdata)
1068 client->dev.archdata = *info->archdata;
1069
Marc Pignatee354252008-08-28 08:33:22 +02001070 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001071 client->addr = info->addr;
1072 client->irq = info->irq;
1073
David Brownell9c1600e2007-05-01 23:26:31 +02001074 strlcpy(client->name, info->type, sizeof(client->name));
1075
Jean Delvare3a89db52010-06-03 11:33:52 +02001076 /* Check for address validity */
1077 status = i2c_check_client_addr_validity(client);
1078 if (status) {
1079 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1080 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1081 goto out_err_silent;
1082 }
1083
Jean Delvaref8a227e2009-06-19 16:58:18 +02001084 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001085 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001086 if (status)
1087 goto out_err;
1088
1089 client->dev.parent = &client->adapter->dev;
1090 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001091 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001092 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001093 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001094
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001095 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001096 status = device_register(&client->dev);
1097 if (status)
1098 goto out_err;
1099
Jean Delvaref8a227e2009-06-19 16:58:18 +02001100 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1101 client->name, dev_name(&client->dev));
1102
David Brownell9c1600e2007-05-01 23:26:31 +02001103 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001104
1105out_err:
1106 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1107 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001108out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001109 kfree(client);
1110 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001111}
1112EXPORT_SYMBOL_GPL(i2c_new_device);
1113
1114
1115/**
1116 * i2c_unregister_device - reverse effect of i2c_new_device()
1117 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001118 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001119 */
1120void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001121{
David Brownella1d9e6e2007-05-01 23:26:30 +02001122 device_unregister(&client->dev);
1123}
David Brownell9c1600e2007-05-01 23:26:31 +02001124EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001125
1126
Jean Delvare60b129d2008-05-11 20:37:06 +02001127static const struct i2c_device_id dummy_id[] = {
1128 { "dummy", 0 },
1129 { },
1130};
1131
Jean Delvared2653e92008-04-29 23:11:39 +02001132static int dummy_probe(struct i2c_client *client,
1133 const struct i2c_device_id *id)
1134{
1135 return 0;
1136}
1137
1138static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001139{
1140 return 0;
1141}
1142
1143static struct i2c_driver dummy_driver = {
1144 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001145 .probe = dummy_probe,
1146 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001147 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001148};
1149
1150/**
1151 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1152 * @adapter: the adapter managing the device
1153 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001154 * Context: can sleep
1155 *
1156 * This returns an I2C client bound to the "dummy" driver, intended for use
1157 * with devices that consume multiple addresses. Examples of such chips
1158 * include various EEPROMS (like 24c04 and 24c08 models).
1159 *
1160 * These dummy devices have two main uses. First, most I2C and SMBus calls
1161 * except i2c_transfer() need a client handle; the dummy will be that handle.
1162 * And second, this prevents the specified address from being bound to a
1163 * different driver.
1164 *
1165 * This returns the new i2c client, which should be saved for later use with
1166 * i2c_unregister_device(); or NULL to indicate an error.
1167 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001168struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001169{
1170 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001171 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001172 };
1173
David Brownelle9f13732008-01-27 18:14:52 +01001174 return i2c_new_device(adapter, &info);
1175}
1176EXPORT_SYMBOL_GPL(i2c_new_dummy);
1177
David Brownellf37dd802007-02-13 22:09:00 +01001178/* ------------------------------------------------------------------------- */
1179
David Brownell16ffadf2007-05-01 23:26:28 +02001180/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1181
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001182static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
David Brownellef2c83212007-05-01 23:26:28 +02001184 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 complete(&adap->dev_released);
1186}
1187
Jean Delvare99cd8e22009-06-19 16:58:20 +02001188/*
Jean Delvare390946b2012-09-10 10:14:02 +02001189 * This function is only needed for mutex_lock_nested, so it is never
1190 * called unless locking correctness checking is enabled. Thus we
1191 * make it inline to avoid a compiler warning. That's what gcc ends up
1192 * doing anyway.
1193 */
1194static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1195{
1196 unsigned int depth = 0;
1197
1198 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1199 depth++;
1200
1201 return depth;
1202}
1203
1204/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001205 * Let users instantiate I2C devices through sysfs. This can be used when
1206 * platform initialization code doesn't contain the proper data for
1207 * whatever reason. Also useful for drivers that do device detection and
1208 * detection fails, either because the device uses an unexpected address,
1209 * or this is a compatible device with different ID register values.
1210 *
1211 * Parameter checking may look overzealous, but we really don't want
1212 * the user to provide incorrect parameters.
1213 */
1214static ssize_t
1215i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1216 const char *buf, size_t count)
1217{
1218 struct i2c_adapter *adap = to_i2c_adapter(dev);
1219 struct i2c_board_info info;
1220 struct i2c_client *client;
1221 char *blank, end;
1222 int res;
1223
Jean Delvare99cd8e22009-06-19 16:58:20 +02001224 memset(&info, 0, sizeof(struct i2c_board_info));
1225
1226 blank = strchr(buf, ' ');
1227 if (!blank) {
1228 dev_err(dev, "%s: Missing parameters\n", "new_device");
1229 return -EINVAL;
1230 }
1231 if (blank - buf > I2C_NAME_SIZE - 1) {
1232 dev_err(dev, "%s: Invalid device name\n", "new_device");
1233 return -EINVAL;
1234 }
1235 memcpy(info.type, buf, blank - buf);
1236
1237 /* Parse remaining parameters, reject extra parameters */
1238 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1239 if (res < 1) {
1240 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1241 return -EINVAL;
1242 }
1243 if (res > 1 && end != '\n') {
1244 dev_err(dev, "%s: Extra parameters\n", "new_device");
1245 return -EINVAL;
1246 }
1247
Jean Delvare99cd8e22009-06-19 16:58:20 +02001248 client = i2c_new_device(adap, &info);
1249 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001250 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001251
1252 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001253 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001254 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001255 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001256 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1257 info.type, info.addr);
1258
1259 return count;
1260}
1261
1262/*
1263 * And of course let the users delete the devices they instantiated, if
1264 * they got it wrong. This interface can only be used to delete devices
1265 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1266 * don't delete devices to which some kernel code still has references.
1267 *
1268 * Parameter checking may look overzealous, but we really don't want
1269 * the user to delete the wrong device.
1270 */
1271static ssize_t
1272i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1273 const char *buf, size_t count)
1274{
1275 struct i2c_adapter *adap = to_i2c_adapter(dev);
1276 struct i2c_client *client, *next;
1277 unsigned short addr;
1278 char end;
1279 int res;
1280
1281 /* Parse parameters, reject extra parameters */
1282 res = sscanf(buf, "%hi%c", &addr, &end);
1283 if (res < 1) {
1284 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1285 return -EINVAL;
1286 }
1287 if (res > 1 && end != '\n') {
1288 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1289 return -EINVAL;
1290 }
1291
1292 /* Make sure the device was added through sysfs */
1293 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001294 mutex_lock_nested(&adap->userspace_clients_lock,
1295 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001296 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1297 detected) {
1298 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001299 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1300 "delete_device", client->name, client->addr);
1301
1302 list_del(&client->detected);
1303 i2c_unregister_device(client);
1304 res = count;
1305 break;
1306 }
1307 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001308 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001309
1310 if (res < 0)
1311 dev_err(dev, "%s: Can't find device in list\n",
1312 "delete_device");
1313 return res;
1314}
1315
Jean Delvare4f8cf822009-09-18 22:45:46 +02001316static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001317static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1318 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001319
1320static struct attribute *i2c_adapter_attrs[] = {
1321 &dev_attr_name.attr,
1322 &dev_attr_new_device.attr,
1323 &dev_attr_delete_device.attr,
1324 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001325};
1326
Jean Delvare4f8cf822009-09-18 22:45:46 +02001327static struct attribute_group i2c_adapter_attr_group = {
1328 .attrs = i2c_adapter_attrs,
1329};
1330
1331static const struct attribute_group *i2c_adapter_attr_groups[] = {
1332 &i2c_adapter_attr_group,
1333 NULL
1334};
1335
Michael Lawnick08263742010-08-11 18:21:02 +02001336struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001337 .groups = i2c_adapter_attr_groups,
1338 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001339};
Michael Lawnick08263742010-08-11 18:21:02 +02001340EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Stephen Warren643dd092012-04-17 12:43:33 -06001342/**
1343 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1344 * @dev: device, probably from some driver model iterator
1345 *
1346 * When traversing the driver model tree, perhaps using driver model
1347 * iterators like @device_for_each_child(), you can't assume very much
1348 * about the nodes you find. Use this function to avoid oopses caused
1349 * by wrongly treating some non-I2C device as an i2c_adapter.
1350 */
1351struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1352{
1353 return (dev->type == &i2c_adapter_type)
1354 ? to_i2c_adapter(dev)
1355 : NULL;
1356}
1357EXPORT_SYMBOL(i2c_verify_adapter);
1358
Jean Delvare2bb50952009-09-18 22:45:46 +02001359#ifdef CONFIG_I2C_COMPAT
1360static struct class_compat *i2c_adapter_compat_class;
1361#endif
1362
David Brownell9c1600e2007-05-01 23:26:31 +02001363static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1364{
1365 struct i2c_devinfo *devinfo;
1366
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001367 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001368 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1369 if (devinfo->busnum == adapter->nr
1370 && !i2c_new_device(adapter,
1371 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001372 dev_err(&adapter->dev,
1373 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001374 devinfo->board_info.addr);
1375 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001376 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001377}
1378
Wolfram Sang687b81d2013-07-11 12:56:15 +01001379/* OF support code */
1380
1381#if IS_ENABLED(CONFIG_OF)
1382static void of_i2c_register_devices(struct i2c_adapter *adap)
1383{
1384 void *result;
1385 struct device_node *node;
1386
1387 /* Only register child devices if the adapter has a node pointer set */
1388 if (!adap->dev.of_node)
1389 return;
1390
1391 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1392
1393 for_each_available_child_of_node(adap->dev.of_node, node) {
1394 struct i2c_board_info info = {};
1395 struct dev_archdata dev_ad = {};
1396 const __be32 *addr;
1397 int len;
1398
1399 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1400
1401 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1402 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1403 node->full_name);
1404 continue;
1405 }
1406
1407 addr = of_get_property(node, "reg", &len);
1408 if (!addr || (len < sizeof(int))) {
1409 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1410 node->full_name);
1411 continue;
1412 }
1413
1414 info.addr = be32_to_cpup(addr);
1415 if (info.addr > (1 << 10) - 1) {
1416 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1417 info.addr, node->full_name);
1418 continue;
1419 }
1420
Wolfram Sang687b81d2013-07-11 12:56:15 +01001421 info.of_node = of_node_get(node);
1422 info.archdata = &dev_ad;
1423
1424 if (of_get_property(node, "wakeup-source", NULL))
1425 info.flags |= I2C_CLIENT_WAKE;
1426
1427 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1428
1429 result = i2c_new_device(adap, &info);
1430 if (result == NULL) {
1431 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1432 node->full_name);
1433 of_node_put(node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001434 continue;
1435 }
1436 }
1437}
1438
1439static int of_dev_node_match(struct device *dev, void *data)
1440{
1441 return dev->of_node == data;
1442}
1443
1444/* must call put_device() when done with returned i2c_client device */
1445struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1446{
1447 struct device *dev;
1448
1449 dev = bus_find_device(&i2c_bus_type, NULL, node,
1450 of_dev_node_match);
1451 if (!dev)
1452 return NULL;
1453
1454 return i2c_verify_client(dev);
1455}
1456EXPORT_SYMBOL(of_find_i2c_device_by_node);
1457
1458/* must call put_device() when done with returned i2c_adapter device */
1459struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1460{
1461 struct device *dev;
1462
1463 dev = bus_find_device(&i2c_bus_type, NULL, node,
1464 of_dev_node_match);
1465 if (!dev)
1466 return NULL;
1467
1468 return i2c_verify_adapter(dev);
1469}
1470EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1471#else
1472static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1473#endif /* CONFIG_OF */
1474
Jean Delvare69b00892009-12-06 17:06:27 +01001475static int i2c_do_add_adapter(struct i2c_driver *driver,
1476 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001477{
Jean Delvare4735c982008-07-14 22:38:36 +02001478 /* Detect supported devices on that bus, and instantiate them */
1479 i2c_detect(adap, driver);
1480
1481 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001482 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001483 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1484 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001485 dev_warn(&adap->dev, "Please use another way to instantiate "
1486 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001487 /* We ignore the return code; if it fails, too bad */
1488 driver->attach_adapter(adap);
1489 }
1490 return 0;
1491}
1492
Jean Delvare69b00892009-12-06 17:06:27 +01001493static int __process_new_adapter(struct device_driver *d, void *data)
1494{
1495 return i2c_do_add_adapter(to_i2c_driver(d), data);
1496}
1497
David Brownell6e13e642007-05-01 23:26:31 +02001498static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Jean Delvared6703282010-08-11 18:20:59 +02001500 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David Brownell1d0b19c2008-10-14 17:30:05 +02001502 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001503 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1504 res = -EAGAIN;
1505 goto out_list;
1506 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001507
Jean Delvare2236baa2010-11-15 22:40:38 +01001508 /* Sanity checks */
1509 if (unlikely(adap->name[0] == '\0')) {
1510 pr_err("i2c-core: Attempt to register an adapter with "
1511 "no name!\n");
1512 return -EINVAL;
1513 }
1514 if (unlikely(!adap->algo)) {
1515 pr_err("i2c-core: Attempt to register adapter '%s' with "
1516 "no algo!\n", adap->name);
1517 return -EINVAL;
1518 }
1519
Mika Kuoppala194684e2009-12-06 17:06:22 +01001520 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001521 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001522 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Jean Delvare8fcfef62009-03-28 21:34:43 +01001524 /* Set default timeout to 1 second if not already set */
1525 if (adap->timeout == 0)
1526 adap->timeout = HZ;
1527
Kay Sievers27d9c182009-01-07 14:29:16 +01001528 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001529 adap->dev.bus = &i2c_bus_type;
1530 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001531 res = device_register(&adap->dev);
1532 if (res)
1533 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001535 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1536
Jean Delvare2bb50952009-09-18 22:45:46 +02001537#ifdef CONFIG_I2C_COMPAT
1538 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1539 adap->dev.parent);
1540 if (res)
1541 dev_warn(&adap->dev,
1542 "Failed to create compatibility class link\n");
1543#endif
1544
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301545 /* bus recovery specific initialization */
1546 if (adap->bus_recovery_info) {
1547 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1548
1549 if (!bri->recover_bus) {
1550 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1551 adap->bus_recovery_info = NULL;
1552 goto exit_recovery;
1553 }
1554
1555 /* Generic GPIO recovery */
1556 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1557 if (!gpio_is_valid(bri->scl_gpio)) {
1558 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1559 adap->bus_recovery_info = NULL;
1560 goto exit_recovery;
1561 }
1562
1563 if (gpio_is_valid(bri->sda_gpio))
1564 bri->get_sda = get_sda_gpio_value;
1565 else
1566 bri->get_sda = NULL;
1567
1568 bri->get_scl = get_scl_gpio_value;
1569 bri->set_scl = set_scl_gpio_value;
1570 } else if (!bri->set_scl || !bri->get_scl) {
1571 /* Generic SCL recovery */
1572 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1573 adap->bus_recovery_info = NULL;
1574 }
1575 }
1576
1577exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001578 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001579 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001580 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001581 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001582
David Brownell6e13e642007-05-01 23:26:31 +02001583 if (adap->nr < __i2c_first_dynamic_bus_num)
1584 i2c_scan_static_board_info(adap);
1585
Jean Delvare4735c982008-07-14 22:38:36 +02001586 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001587 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001588 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001589 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001590
1591 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001592
Jean Delvareb119c6c2006-08-15 18:26:30 +02001593out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001594 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001595 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001596 mutex_unlock(&core_lock);
1597 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
David Brownell6e13e642007-05-01 23:26:31 +02001600/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001601 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1602 * @adap: the adapter to register (with adap->nr initialized)
1603 * Context: can sleep
1604 *
1605 * See i2c_add_numbered_adapter() for details.
1606 */
1607static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1608{
1609 int id;
1610
1611 mutex_lock(&core_lock);
1612 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1613 GFP_KERNEL);
1614 mutex_unlock(&core_lock);
1615 if (id < 0)
1616 return id == -ENOSPC ? -EBUSY : id;
1617
1618 return i2c_register_adapter(adap);
1619}
1620
1621/**
David Brownell6e13e642007-05-01 23:26:31 +02001622 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1623 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001624 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001625 *
1626 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001627 * doesn't matter or when its bus number is specified by an dt alias.
1628 * Examples of bases when the bus number doesn't matter: I2C adapters
1629 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001630 *
1631 * When this returns zero, a new bus number was allocated and stored
1632 * in adap->nr, and the specified adapter became available for clients.
1633 * Otherwise, a negative errno value is returned.
1634 */
1635int i2c_add_adapter(struct i2c_adapter *adapter)
1636{
Doug Andersonee5c2742013-03-01 08:57:31 -08001637 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001638 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001639
Doug Andersonee5c2742013-03-01 08:57:31 -08001640 if (dev->of_node) {
1641 id = of_alias_get_id(dev->of_node, "i2c");
1642 if (id >= 0) {
1643 adapter->nr = id;
1644 return __i2c_add_numbered_adapter(adapter);
1645 }
1646 }
1647
Jean Delvarecaada322008-01-27 18:14:49 +01001648 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001649 id = idr_alloc(&i2c_adapter_idr, adapter,
1650 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001651 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001652 if (id < 0)
1653 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001654
1655 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001656
David Brownell6e13e642007-05-01 23:26:31 +02001657 return i2c_register_adapter(adapter);
1658}
1659EXPORT_SYMBOL(i2c_add_adapter);
1660
1661/**
1662 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1663 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001664 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001665 *
1666 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001667 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1668 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001669 * is used to properly configure I2C devices.
1670 *
Grant Likely488bf312011-07-25 17:49:43 +02001671 * If the requested bus number is set to -1, then this function will behave
1672 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1673 *
David Brownell6e13e642007-05-01 23:26:31 +02001674 * If no devices have pre-been declared for this bus, then be sure to
1675 * register the adapter before any dynamically allocated ones. Otherwise
1676 * the required bus ID may not be available.
1677 *
1678 * When this returns zero, the specified adapter became available for
1679 * clients using the bus number provided in adap->nr. Also, the table
1680 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1681 * and the appropriate driver model device nodes are created. Otherwise, a
1682 * negative errno value is returned.
1683 */
1684int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1685{
Grant Likely488bf312011-07-25 17:49:43 +02001686 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1687 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001688
Doug Andersonee5c2742013-03-01 08:57:31 -08001689 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001690}
1691EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1692
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001693static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001694 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001695{
Jean Delvare4735c982008-07-14 22:38:36 +02001696 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001697
Jean Delvareacec2112009-03-28 21:34:40 +01001698 /* Remove the devices we created ourselves as the result of hardware
1699 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001700 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1701 if (client->adapter == adapter) {
1702 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1703 client->name, client->addr);
1704 list_del(&client->detected);
1705 i2c_unregister_device(client);
1706 }
1707 }
Jean Delvare026526f2008-01-27 18:14:49 +01001708}
1709
Jean Delvaree549c2b2009-06-19 16:58:19 +02001710static int __unregister_client(struct device *dev, void *dummy)
1711{
1712 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001713 if (client && strcmp(client->name, "dummy"))
1714 i2c_unregister_device(client);
1715 return 0;
1716}
1717
1718static int __unregister_dummy(struct device *dev, void *dummy)
1719{
1720 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001721 if (client)
1722 i2c_unregister_device(client);
1723 return 0;
1724}
1725
Jean Delvare69b00892009-12-06 17:06:27 +01001726static int __process_removed_adapter(struct device_driver *d, void *data)
1727{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001728 i2c_do_del_adapter(to_i2c_driver(d), data);
1729 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001730}
1731
David Brownelld64f73b2007-07-12 14:12:28 +02001732/**
1733 * i2c_del_adapter - unregister I2C adapter
1734 * @adap: the adapter being unregistered
1735 * Context: can sleep
1736 *
1737 * This unregisters an I2C adapter which was previously registered
1738 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1739 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001740void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001742 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001743 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001746 mutex_lock(&core_lock);
1747 found = idr_find(&i2c_adapter_idr, adap->nr);
1748 mutex_unlock(&core_lock);
1749 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001750 pr_debug("i2c-core: attempting to delete unregistered "
1751 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Lan Tianyu5d98e612014-05-20 20:59:23 +08001755 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001756 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001757 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001758 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001759 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001760 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001762 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001763 mutex_lock_nested(&adap->userspace_clients_lock,
1764 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001765 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1766 detected) {
1767 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1768 client->addr);
1769 list_del(&client->detected);
1770 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001771 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001772 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001773
Jean Delvaree549c2b2009-06-19 16:58:19 +02001774 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001775 * check the returned value. This is a two-pass process, because
1776 * we can't remove the dummy devices during the first pass: they
1777 * could have been instantiated by real devices wishing to clean
1778 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001779 device_for_each_child(&adap->dev, NULL, __unregister_client);
1780 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jean Delvare2bb50952009-09-18 22:45:46 +02001782#ifdef CONFIG_I2C_COMPAT
1783 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1784 adap->dev.parent);
1785#endif
1786
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001787 /* device name is gone after device_unregister */
1788 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* clean up the sysfs representation */
1791 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /* wait for sysfs to drop all references */
1795 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
David Brownell6e13e642007-05-01 23:26:31 +02001797 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001798 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001800 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001802 /* Clear the device structure in case this adapter is ever going to be
1803 added again */
1804 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
David Brownellc0564602007-05-01 23:26:31 +02001806EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
David Brownell7b4fbc52007-05-01 23:26:30 +02001808/* ------------------------------------------------------------------------- */
1809
Jean Delvare7ae31482011-03-20 14:50:52 +01001810int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1811{
1812 int res;
1813
1814 mutex_lock(&core_lock);
1815 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1816 mutex_unlock(&core_lock);
1817
1818 return res;
1819}
1820EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1821
Jean Delvare69b00892009-12-06 17:06:27 +01001822static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001823{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001824 if (dev->type != &i2c_adapter_type)
1825 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001826 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001827}
1828
David Brownell7b4fbc52007-05-01 23:26:30 +02001829/*
1830 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001831 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
1833
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001834int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001836 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
David Brownell1d0b19c2008-10-14 17:30:05 +02001838 /* Can't register until after driver model init */
1839 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1840 return -EAGAIN;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001843 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Jean Delvare729d6dd2009-06-19 16:58:18 +02001846 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001847 * will have called probe() for all matching-but-unbound devices.
1848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 res = driver_register(&driver->driver);
1850 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001851 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001852
Mark Brownf4e8db32011-01-14 22:03:50 +01001853 /* Drivers should switch to dev_pm_ops instead. */
1854 if (driver->suspend)
1855 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1856 driver->driver.name);
1857 if (driver->resume)
1858 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1859 driver->driver.name);
1860
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001861 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jean Delvare4735c982008-07-14 22:38:36 +02001863 INIT_LIST_HEAD(&driver->clients);
1864 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001865 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001866
Jean Delvare7eebcb72006-02-05 23:28:21 +01001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001869EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jean Delvare69b00892009-12-06 17:06:27 +01001871static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001872{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001873 if (dev->type == &i2c_adapter_type)
1874 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1875 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001876}
1877
David Brownella1d9e6e2007-05-01 23:26:30 +02001878/**
1879 * i2c_del_driver - unregister I2C driver
1880 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001881 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001882 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001883void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Jean Delvare7ae31482011-03-20 14:50:52 +01001885 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001888 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
David Brownellc0564602007-05-01 23:26:31 +02001890EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
David Brownell7b4fbc52007-05-01 23:26:30 +02001892/* ------------------------------------------------------------------------- */
1893
Jean Delvaree48d3312008-01-27 18:14:48 +01001894/**
1895 * i2c_use_client - increments the reference count of the i2c client structure
1896 * @client: the client being referenced
1897 *
1898 * Each live reference to a client should be refcounted. The driver model does
1899 * that automatically as part of driver binding, so that most drivers don't
1900 * need to do this explicitly: they hold a reference until they're unbound
1901 * from the device.
1902 *
1903 * A pointer to the client with the incremented reference counter is returned.
1904 */
1905struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
David Brownell6ea438e2008-07-14 22:38:24 +02001907 if (client && get_device(&client->dev))
1908 return client;
1909 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
David Brownellc0564602007-05-01 23:26:31 +02001911EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Jean Delvaree48d3312008-01-27 18:14:48 +01001913/**
1914 * i2c_release_client - release a use of the i2c client structure
1915 * @client: the client being no longer referenced
1916 *
1917 * Must be called when a user of a client is finished with it.
1918 */
1919void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
David Brownell6ea438e2008-07-14 22:38:24 +02001921 if (client)
1922 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
David Brownellc0564602007-05-01 23:26:31 +02001924EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
David Brownell9b766b82008-01-27 18:14:51 +01001926struct i2c_cmd_arg {
1927 unsigned cmd;
1928 void *arg;
1929};
1930
1931static int i2c_cmd(struct device *dev, void *_arg)
1932{
1933 struct i2c_client *client = i2c_verify_client(dev);
1934 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001935 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001936
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001937 if (!client || !client->dev.driver)
1938 return 0;
1939
1940 driver = to_i2c_driver(client->dev.driver);
1941 if (driver->command)
1942 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001943 return 0;
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1947{
David Brownell9b766b82008-01-27 18:14:51 +01001948 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
David Brownell9b766b82008-01-27 18:14:51 +01001950 cmd_arg.cmd = cmd;
1951 cmd_arg.arg = arg;
1952 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
David Brownellc0564602007-05-01 23:26:31 +02001954EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956static int __init i2c_init(void)
1957{
1958 int retval;
1959
1960 retval = bus_register(&i2c_bus_type);
1961 if (retval)
1962 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001963#ifdef CONFIG_I2C_COMPAT
1964 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1965 if (!i2c_adapter_compat_class) {
1966 retval = -ENOMEM;
1967 goto bus_err;
1968 }
1969#endif
David Brownelle9f13732008-01-27 18:14:52 +01001970 retval = i2c_add_driver(&dummy_driver);
1971 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001972 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001973 return 0;
1974
Jean Delvare2bb50952009-09-18 22:45:46 +02001975class_err:
1976#ifdef CONFIG_I2C_COMPAT
1977 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001978bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001979#endif
David Brownelle9f13732008-01-27 18:14:52 +01001980 bus_unregister(&i2c_bus_type);
1981 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984static void __exit i2c_exit(void)
1985{
David Brownelle9f13732008-01-27 18:14:52 +01001986 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001987#ifdef CONFIG_I2C_COMPAT
1988 class_compat_unregister(i2c_adapter_compat_class);
1989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001991 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
David Brownella10f9e72008-10-14 17:30:06 +02001994/* We must initialize early, because some subsystems register i2c drivers
1995 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1996 */
1997postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998module_exit(i2c_exit);
1999
2000/* ----------------------------------------------------
2001 * the functional interface to the i2c busses.
2002 * ----------------------------------------------------
2003 */
2004
David Brownella1cdeda2008-07-14 22:38:24 +02002005/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002006 * __i2c_transfer - unlocked flavor of i2c_transfer
2007 * @adap: Handle to I2C bus
2008 * @msgs: One or more messages to execute before STOP is issued to
2009 * terminate the operation; each message begins with a START.
2010 * @num: Number of messages to be executed.
2011 *
2012 * Returns negative errno, else the number of messages executed.
2013 *
2014 * Adapter lock must be held when calling this function. No debug logging
2015 * takes place. adap->algo->master_xfer existence isn't checked.
2016 */
2017int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2018{
2019 unsigned long orig_jiffies;
2020 int ret, try;
2021
David Howellsd9a83d62014-03-06 13:35:59 +00002022 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2023 * enabled. This is an efficient way of keeping the for-loop from
2024 * being executed when not needed.
2025 */
2026 if (static_key_false(&i2c_trace_msg)) {
2027 int i;
2028 for (i = 0; i < num; i++)
2029 if (msgs[i].flags & I2C_M_RD)
2030 trace_i2c_read(adap, &msgs[i], i);
2031 else
2032 trace_i2c_write(adap, &msgs[i], i);
2033 }
2034
Jean Delvareb37d2a32012-06-29 07:47:19 -03002035 /* Retry automatically on arbitration loss */
2036 orig_jiffies = jiffies;
2037 for (ret = 0, try = 0; try <= adap->retries; try++) {
2038 ret = adap->algo->master_xfer(adap, msgs, num);
2039 if (ret != -EAGAIN)
2040 break;
2041 if (time_after(jiffies, orig_jiffies + adap->timeout))
2042 break;
2043 }
2044
David Howellsd9a83d62014-03-06 13:35:59 +00002045 if (static_key_false(&i2c_trace_msg)) {
2046 int i;
2047 for (i = 0; i < ret; i++)
2048 if (msgs[i].flags & I2C_M_RD)
2049 trace_i2c_reply(adap, &msgs[i], i);
2050 trace_i2c_result(adap, i, ret);
2051 }
2052
Jean Delvareb37d2a32012-06-29 07:47:19 -03002053 return ret;
2054}
2055EXPORT_SYMBOL(__i2c_transfer);
2056
2057/**
David Brownella1cdeda2008-07-14 22:38:24 +02002058 * i2c_transfer - execute a single or combined I2C message
2059 * @adap: Handle to I2C bus
2060 * @msgs: One or more messages to execute before STOP is issued to
2061 * terminate the operation; each message begins with a START.
2062 * @num: Number of messages to be executed.
2063 *
2064 * Returns negative errno, else the number of messages executed.
2065 *
2066 * Note that there is no requirement that each message be sent to
2067 * the same slave address, although that is the most common model.
2068 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002069int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002071 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
David Brownella1cdeda2008-07-14 22:38:24 +02002073 /* REVISIT the fault reporting model here is weak:
2074 *
2075 * - When we get an error after receiving N bytes from a slave,
2076 * there is no way to report "N".
2077 *
2078 * - When we get a NAK after transmitting N bytes to a slave,
2079 * there is no way to report "N" ... or to let the master
2080 * continue executing the rest of this combined message, if
2081 * that's the appropriate response.
2082 *
2083 * - When for example "num" is two and we successfully complete
2084 * the first message but get an error part way through the
2085 * second, it's unclear whether that should be reported as
2086 * one (discarding status on the second message) or errno
2087 * (discarding status on the first one).
2088 */
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (adap->algo->master_xfer) {
2091#ifdef DEBUG
2092 for (ret = 0; ret < num; ret++) {
2093 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002094 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2095 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2096 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098#endif
2099
Mike Rapoportcea443a2008-01-27 18:14:50 +01002100 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002101 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002102 if (!ret)
2103 /* I2C activity is ongoing. */
2104 return -EAGAIN;
2105 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002106 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002107 }
2108
Jean Delvareb37d2a32012-06-29 07:47:19 -03002109 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002110 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 return ret;
2113 } else {
2114 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002115 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117}
David Brownellc0564602007-05-01 23:26:31 +02002118EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
David Brownella1cdeda2008-07-14 22:38:24 +02002120/**
2121 * i2c_master_send - issue a single I2C message in master transmit mode
2122 * @client: Handle to slave device
2123 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002124 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002125 *
2126 * Returns negative errno, or else the number of bytes written.
2127 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002128int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002131 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct i2c_msg msg;
2133
Jean Delvare815f55f2005-05-07 22:58:46 +02002134 msg.addr = client->addr;
2135 msg.flags = client->flags & I2C_M_TEN;
2136 msg.len = count;
2137 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002138
Jean Delvare815f55f2005-05-07 22:58:46 +02002139 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002141 /*
2142 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2143 * transmitted, else error code.
2144 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002145 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
David Brownellc0564602007-05-01 23:26:31 +02002147EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
David Brownella1cdeda2008-07-14 22:38:24 +02002149/**
2150 * i2c_master_recv - issue a single I2C message in master receive mode
2151 * @client: Handle to slave device
2152 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002153 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002154 *
2155 * Returns negative errno, or else the number of bytes read.
2156 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002157int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Farid Hammane7225acf2010-05-21 18:40:58 +02002159 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct i2c_msg msg;
2161 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Jean Delvare815f55f2005-05-07 22:58:46 +02002163 msg.addr = client->addr;
2164 msg.flags = client->flags & I2C_M_TEN;
2165 msg.flags |= I2C_M_RD;
2166 msg.len = count;
2167 msg.buf = buf;
2168
2169 ret = i2c_transfer(adap, &msg, 1);
2170
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002171 /*
2172 * If everything went ok (i.e. 1 msg received), return #bytes received,
2173 * else error code.
2174 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002175 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
David Brownellc0564602007-05-01 23:26:31 +02002177EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/* ----------------------------------------------------
2180 * the i2c address scanning function
2181 * Will not work for 10-bit addresses!
2182 * ----------------------------------------------------
2183 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002184
Jean Delvare63e4e802010-06-03 11:33:51 +02002185/*
2186 * Legacy default probe function, mostly relevant for SMBus. The default
2187 * probe method is a quick write, but it is known to corrupt the 24RF08
2188 * EEPROMs due to a state machine bug, and could also irreversibly
2189 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2190 * we use a short byte read instead. Also, some bus drivers don't implement
2191 * quick write, so we fallback to a byte read in that case too.
2192 * On x86, there is another special case for FSC hardware monitoring chips,
2193 * which want regular byte reads (address 0x73.) Fortunately, these are the
2194 * only known chips using this I2C address on PC hardware.
2195 * Returns 1 if probe succeeded, 0 if not.
2196 */
2197static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2198{
2199 int err;
2200 union i2c_smbus_data dummy;
2201
2202#ifdef CONFIG_X86
2203 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2204 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2205 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2206 I2C_SMBUS_BYTE_DATA, &dummy);
2207 else
2208#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002209 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2210 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002211 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2212 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002213 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2214 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2215 I2C_SMBUS_BYTE, &dummy);
2216 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002217 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2218 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002219 err = -EOPNOTSUPP;
2220 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002221
2222 return err >= 0;
2223}
2224
Jean Delvareccfbbd02009-12-06 17:06:25 +01002225static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002226 struct i2c_driver *driver)
2227{
2228 struct i2c_board_info info;
2229 struct i2c_adapter *adapter = temp_client->adapter;
2230 int addr = temp_client->addr;
2231 int err;
2232
2233 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002234 err = i2c_check_addr_validity(addr);
2235 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002236 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2237 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002238 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002239 }
2240
2241 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002242 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002243 return 0;
2244
Jean Delvareccfbbd02009-12-06 17:06:25 +01002245 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002246 if (!i2c_default_probe(adapter, addr))
2247 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002248
2249 /* Finally call the custom detection function */
2250 memset(&info, 0, sizeof(struct i2c_board_info));
2251 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002252 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002253 if (err) {
2254 /* -ENODEV is returned if the detection fails. We catch it
2255 here as this isn't an error. */
2256 return err == -ENODEV ? 0 : err;
2257 }
2258
2259 /* Consistency check */
2260 if (info.type[0] == '\0') {
2261 dev_err(&adapter->dev, "%s detection function provided "
2262 "no name for 0x%x\n", driver->driver.name,
2263 addr);
2264 } else {
2265 struct i2c_client *client;
2266
2267 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002268 if (adapter->class & I2C_CLASS_DEPRECATED)
2269 dev_warn(&adapter->dev,
2270 "This adapter will soon drop class based instantiation of devices. "
2271 "Please make sure client 0x%02x gets instantiated by other means. "
2272 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2273 info.addr);
2274
Jean Delvare4735c982008-07-14 22:38:36 +02002275 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2276 info.type, info.addr);
2277 client = i2c_new_device(adapter, &info);
2278 if (client)
2279 list_add_tail(&client->detected, &driver->clients);
2280 else
2281 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2282 info.type, info.addr);
2283 }
2284 return 0;
2285}
2286
2287static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2288{
Jean Delvarec3813d62009-12-14 21:17:25 +01002289 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002290 struct i2c_client *temp_client;
2291 int i, err = 0;
2292 int adap_id = i2c_adapter_id(adapter);
2293
Jean Delvarec3813d62009-12-14 21:17:25 +01002294 address_list = driver->address_list;
2295 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002296 return 0;
2297
Wolfram Sang45552272014-07-10 13:46:21 +02002298 /* Warn that the adapter lost class based instantiation */
2299 if (adapter->class == I2C_CLASS_DEPRECATED) {
2300 dev_dbg(&adapter->dev,
2301 "This adapter dropped support for I2C classes and "
2302 "won't auto-detect %s devices anymore. If you need it, check "
2303 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2304 driver->driver.name);
2305 return 0;
2306 }
2307
Jean Delvare51b54ba2010-10-24 18:16:58 +02002308 /* Stop here if the classes do not match */
2309 if (!(adapter->class & driver->class))
2310 return 0;
2311
Jean Delvare4735c982008-07-14 22:38:36 +02002312 /* Set up a temporary client to help detect callback */
2313 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2314 if (!temp_client)
2315 return -ENOMEM;
2316 temp_client->adapter = adapter;
2317
Jean Delvarec3813d62009-12-14 21:17:25 +01002318 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002319 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002320 "addr 0x%02x\n", adap_id, address_list[i]);
2321 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002322 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002323 if (unlikely(err))
2324 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002325 }
2326
Jean Delvare4735c982008-07-14 22:38:36 +02002327 kfree(temp_client);
2328 return err;
2329}
2330
Jean Delvared44f19d2010-08-11 18:20:57 +02002331int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2332{
2333 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2334 I2C_SMBUS_QUICK, NULL) >= 0;
2335}
2336EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2337
Jean Delvare12b5053a2007-05-01 23:26:31 +02002338struct i2c_client *
2339i2c_new_probed_device(struct i2c_adapter *adap,
2340 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002341 unsigned short const *addr_list,
2342 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002343{
2344 int i;
2345
Jean Delvare8031d792010-08-11 18:21:00 +02002346 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002347 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002348
Jean Delvare12b5053a2007-05-01 23:26:31 +02002349 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2350 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002351 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002352 dev_warn(&adap->dev, "Invalid 7-bit address "
2353 "0x%02x\n", addr_list[i]);
2354 continue;
2355 }
2356
2357 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002358 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002359 dev_dbg(&adap->dev, "Address 0x%02x already in "
2360 "use, not probing\n", addr_list[i]);
2361 continue;
2362 }
2363
Jean Delvare63e4e802010-06-03 11:33:51 +02002364 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002365 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002366 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002367 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002368
2369 if (addr_list[i] == I2C_CLIENT_END) {
2370 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2371 return NULL;
2372 }
2373
2374 info->addr = addr_list[i];
2375 return i2c_new_device(adap, info);
2376}
2377EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2378
Jean Delvared735b342011-03-20 14:50:52 +01002379struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002382
Jean Delvarecaada322008-01-27 18:14:49 +01002383 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002384 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002385 if (adapter && !try_module_get(adapter->owner))
2386 adapter = NULL;
2387
Jean Delvarecaada322008-01-27 18:14:49 +01002388 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002389 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
David Brownellc0564602007-05-01 23:26:31 +02002391EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393void i2c_put_adapter(struct i2c_adapter *adap)
2394{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002395 if (adap)
2396 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
David Brownellc0564602007-05-01 23:26:31 +02002398EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400/* The SMBus parts */
2401
David Brownell438d6c22006-12-10 21:21:31 +01002402#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002403static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002406
Farid Hammane7225acf2010-05-21 18:40:58 +02002407 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002408 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 data = data ^ POLY;
2410 data = data << 1;
2411 }
2412 return (u8)(data >> 8);
2413}
2414
Jean Delvare421ef472005-10-26 21:28:55 +02002415/* Incremental CRC8 over count bytes in the array pointed to by p */
2416static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 int i;
2419
Farid Hammane7225acf2010-05-21 18:40:58 +02002420 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002421 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return crc;
2423}
2424
Jean Delvare421ef472005-10-26 21:28:55 +02002425/* Assume a 7-bit address, which is reasonable for SMBus */
2426static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Jean Delvare421ef472005-10-26 21:28:55 +02002428 /* The address will be sent first */
2429 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2430 pec = i2c_smbus_pec(pec, &addr, 1);
2431
2432 /* The data buffer follows */
2433 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Jean Delvare421ef472005-10-26 21:28:55 +02002436/* Used for write only transactions */
2437static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Jean Delvare421ef472005-10-26 21:28:55 +02002439 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2440 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Jean Delvare421ef472005-10-26 21:28:55 +02002443/* Return <0 on CRC error
2444 If there was a write before this read (most cases) we need to take the
2445 partial CRC from the write part into account.
2446 Note that this function does modify the message (we need to decrease the
2447 message length to hide the CRC byte from the caller). */
2448static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Jean Delvare421ef472005-10-26 21:28:55 +02002450 u8 rpec = msg->buf[--msg->len];
2451 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (rpec != cpec) {
2454 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2455 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002456 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
David Brownell438d6c22006-12-10 21:21:31 +01002458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
David Brownella1cdeda2008-07-14 22:38:24 +02002461/**
2462 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2463 * @client: Handle to slave device
2464 *
2465 * This executes the SMBus "receive byte" protocol, returning negative errno
2466 * else the byte received from the device.
2467 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002468s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002471 int status;
2472
2473 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2474 I2C_SMBUS_READ, 0,
2475 I2C_SMBUS_BYTE, &data);
2476 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
David Brownellc0564602007-05-01 23:26:31 +02002478EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
David Brownella1cdeda2008-07-14 22:38:24 +02002480/**
2481 * i2c_smbus_write_byte - SMBus "send byte" protocol
2482 * @client: Handle to slave device
2483 * @value: Byte to be sent
2484 *
2485 * This executes the SMBus "send byte" protocol, returning negative errno
2486 * else zero on success.
2487 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002488s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Farid Hammane7225acf2010-05-21 18:40:58 +02002490 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002491 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
David Brownellc0564602007-05-01 23:26:31 +02002493EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
David Brownella1cdeda2008-07-14 22:38:24 +02002495/**
2496 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2497 * @client: Handle to slave device
2498 * @command: Byte interpreted by slave
2499 *
2500 * This executes the SMBus "read byte" protocol, returning negative errno
2501 * else a data byte received from the device.
2502 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002503s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002506 int status;
2507
2508 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2509 I2C_SMBUS_READ, command,
2510 I2C_SMBUS_BYTE_DATA, &data);
2511 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
David Brownellc0564602007-05-01 23:26:31 +02002513EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
David Brownella1cdeda2008-07-14 22:38:24 +02002515/**
2516 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2517 * @client: Handle to slave device
2518 * @command: Byte interpreted by slave
2519 * @value: Byte being written
2520 *
2521 * This executes the SMBus "write byte" protocol, returning negative errno
2522 * else zero on success.
2523 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002524s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2525 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 union i2c_smbus_data data;
2528 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002529 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2530 I2C_SMBUS_WRITE, command,
2531 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
David Brownellc0564602007-05-01 23:26:31 +02002533EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
David Brownella1cdeda2008-07-14 22:38:24 +02002535/**
2536 * i2c_smbus_read_word_data - SMBus "read word" protocol
2537 * @client: Handle to slave device
2538 * @command: Byte interpreted by slave
2539 *
2540 * This executes the SMBus "read word" protocol, returning negative errno
2541 * else a 16-bit unsigned "word" received from the device.
2542 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002543s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002546 int status;
2547
2548 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2549 I2C_SMBUS_READ, command,
2550 I2C_SMBUS_WORD_DATA, &data);
2551 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
David Brownellc0564602007-05-01 23:26:31 +02002553EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
David Brownella1cdeda2008-07-14 22:38:24 +02002555/**
2556 * i2c_smbus_write_word_data - SMBus "write word" protocol
2557 * @client: Handle to slave device
2558 * @command: Byte interpreted by slave
2559 * @value: 16-bit "word" being written
2560 *
2561 * This executes the SMBus "write word" protocol, returning negative errno
2562 * else zero on success.
2563 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002564s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2565 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 union i2c_smbus_data data;
2568 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002569 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2570 I2C_SMBUS_WRITE, command,
2571 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
David Brownellc0564602007-05-01 23:26:31 +02002573EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
David Brownella64ec072007-10-13 23:56:31 +02002575/**
David Brownella1cdeda2008-07-14 22:38:24 +02002576 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002577 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002578 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002579 * @values: Byte array into which data will be read; big enough to hold
2580 * the data returned by the slave. SMBus allows at most 32 bytes.
2581 *
David Brownella1cdeda2008-07-14 22:38:24 +02002582 * This executes the SMBus "block read" protocol, returning negative errno
2583 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002584 *
2585 * Note that using this function requires that the client's adapter support
2586 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2587 * support this; its emulation through I2C messaging relies on a specific
2588 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2589 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002590s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002591 u8 *values)
2592{
2593 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002594 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002595
David Brownell24a5bb72008-07-14 22:38:23 +02002596 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2597 I2C_SMBUS_READ, command,
2598 I2C_SMBUS_BLOCK_DATA, &data);
2599 if (status)
2600 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002601
2602 memcpy(values, &data.block[1], data.block[0]);
2603 return data.block[0];
2604}
2605EXPORT_SYMBOL(i2c_smbus_read_block_data);
2606
David Brownella1cdeda2008-07-14 22:38:24 +02002607/**
2608 * i2c_smbus_write_block_data - SMBus "block write" protocol
2609 * @client: Handle to slave device
2610 * @command: Byte interpreted by slave
2611 * @length: Size of data block; SMBus allows at most 32 bytes
2612 * @values: Byte array which will be written.
2613 *
2614 * This executes the SMBus "block write" protocol, returning negative errno
2615 * else zero on success.
2616 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002617s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002618 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
2620 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (length > I2C_SMBUS_BLOCK_MAX)
2623 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002625 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002626 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2627 I2C_SMBUS_WRITE, command,
2628 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
David Brownellc0564602007-05-01 23:26:31 +02002630EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002633s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002634 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002637 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002638
Jean Delvare4b2643d2007-07-12 14:12:29 +02002639 if (length > I2C_SMBUS_BLOCK_MAX)
2640 length = I2C_SMBUS_BLOCK_MAX;
2641 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002642 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2643 I2C_SMBUS_READ, command,
2644 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2645 if (status < 0)
2646 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002647
2648 memcpy(values, &data.block[1], data.block[0]);
2649 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
David Brownellc0564602007-05-01 23:26:31 +02002651EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Jean Delvare0cc43a12011-01-10 22:11:23 +01002653s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002654 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002655{
2656 union i2c_smbus_data data;
2657
2658 if (length > I2C_SMBUS_BLOCK_MAX)
2659 length = I2C_SMBUS_BLOCK_MAX;
2660 data.block[0] = length;
2661 memcpy(data.block + 1, values, length);
2662 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2663 I2C_SMBUS_WRITE, command,
2664 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2665}
David Brownellc0564602007-05-01 23:26:31 +02002666EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002667
David Brownell438d6c22006-12-10 21:21:31 +01002668/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002670static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2671 unsigned short flags,
2672 char read_write, u8 command, int size,
2673 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
2675 /* So we need to generate a series of msgs. In the case of writing, we
2676 need to use only one message; when reading, we need two. We initialize
2677 most things with sane defaults, to keep the code below somewhat
2678 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002679 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2680 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002681 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002683 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002684 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002685 struct i2c_msg msg[2] = {
2686 {
2687 .addr = addr,
2688 .flags = flags,
2689 .len = 1,
2690 .buf = msgbuf0,
2691 }, {
2692 .addr = addr,
2693 .flags = flags | I2C_M_RD,
2694 .len = 0,
2695 .buf = msgbuf1,
2696 },
2697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002700 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 case I2C_SMBUS_QUICK:
2702 msg[0].len = 0;
2703 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002704 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2705 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 num = 1;
2707 break;
2708 case I2C_SMBUS_BYTE:
2709 if (read_write == I2C_SMBUS_READ) {
2710 /* Special case: only a read! */
2711 msg[0].flags = I2C_M_RD | flags;
2712 num = 1;
2713 }
2714 break;
2715 case I2C_SMBUS_BYTE_DATA:
2716 if (read_write == I2C_SMBUS_READ)
2717 msg[1].len = 1;
2718 else {
2719 msg[0].len = 2;
2720 msgbuf0[1] = data->byte;
2721 }
2722 break;
2723 case I2C_SMBUS_WORD_DATA:
2724 if (read_write == I2C_SMBUS_READ)
2725 msg[1].len = 2;
2726 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002727 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002729 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731 break;
2732 case I2C_SMBUS_PROC_CALL:
2733 num = 2; /* Special case */
2734 read_write = I2C_SMBUS_READ;
2735 msg[0].len = 3;
2736 msg[1].len = 2;
2737 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002738 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 break;
2740 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002742 msg[1].flags |= I2C_M_RECV_LEN;
2743 msg[1].len = 1; /* block length will be added by
2744 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 } else {
2746 msg[0].len = data->block[0] + 2;
2747 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002748 dev_err(&adapter->dev,
2749 "Invalid block write size %d\n",
2750 data->block[0]);
2751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002753 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 msgbuf0[i] = data->block[i-1];
2755 }
2756 break;
2757 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002758 num = 2; /* Another special case */
2759 read_write = I2C_SMBUS_READ;
2760 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002761 dev_err(&adapter->dev,
2762 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002763 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002764 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002765 }
2766 msg[0].len = data->block[0] + 2;
2767 for (i = 1; i < msg[0].len; i++)
2768 msgbuf0[i] = data->block[i-1];
2769 msg[1].flags |= I2C_M_RECV_LEN;
2770 msg[1].len = 1; /* block length will be added by
2771 the underlying bus driver */
2772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 case I2C_SMBUS_I2C_BLOCK_DATA:
2774 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002775 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 } else {
2777 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002778 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002779 dev_err(&adapter->dev,
2780 "Invalid block write size %d\n",
2781 data->block[0]);
2782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
2784 for (i = 1; i <= data->block[0]; i++)
2785 msgbuf0[i] = data->block[i];
2786 }
2787 break;
2788 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002789 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2790 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792
Jean Delvare421ef472005-10-26 21:28:55 +02002793 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2794 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2795 if (i) {
2796 /* Compute PEC if first message is a write */
2797 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002798 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002799 i2c_smbus_add_pec(&msg[0]);
2800 else /* Write followed by read */
2801 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2802 }
2803 /* Ask for PEC if last message is a read */
2804 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002805 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002806 }
2807
David Brownell24a5bb72008-07-14 22:38:23 +02002808 status = i2c_transfer(adapter, msg, num);
2809 if (status < 0)
2810 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Jean Delvare421ef472005-10-26 21:28:55 +02002812 /* Check PEC if last message is a read */
2813 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002814 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2815 if (status < 0)
2816 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002817 }
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002820 switch (size) {
2821 case I2C_SMBUS_BYTE:
2822 data->byte = msgbuf0[0];
2823 break;
2824 case I2C_SMBUS_BYTE_DATA:
2825 data->byte = msgbuf1[0];
2826 break;
2827 case I2C_SMBUS_WORD_DATA:
2828 case I2C_SMBUS_PROC_CALL:
2829 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2830 break;
2831 case I2C_SMBUS_I2C_BLOCK_DATA:
2832 for (i = 0; i < data->block[0]; i++)
2833 data->block[i+1] = msgbuf1[i];
2834 break;
2835 case I2C_SMBUS_BLOCK_DATA:
2836 case I2C_SMBUS_BLOCK_PROC_CALL:
2837 for (i = 0; i < msgbuf1[0] + 1; i++)
2838 data->block[i] = msgbuf1[i];
2839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841 return 0;
2842}
2843
David Brownella1cdeda2008-07-14 22:38:24 +02002844/**
2845 * i2c_smbus_xfer - execute SMBus protocol operations
2846 * @adapter: Handle to I2C bus
2847 * @addr: Address of SMBus slave on that bus
2848 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2849 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2850 * @command: Byte interpreted by slave, for protocols which use such bytes
2851 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2852 * @data: Data to be read or written
2853 *
2854 * This executes an SMBus protocol operation, and returns a negative
2855 * errno code else zero on success.
2856 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002857s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002858 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002859 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002861 unsigned long orig_jiffies;
2862 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
David Howells8a325992014-03-06 13:36:06 +00002865 /* If enabled, the following two tracepoints are conditional on
2866 * read_write and protocol.
2867 */
2868 trace_smbus_write(adapter, addr, flags, read_write,
2869 command, protocol, data);
2870 trace_smbus_read(adapter, addr, flags, read_write,
2871 command, protocol);
2872
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002873 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002876 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002877
2878 /* Retry automatically on arbitration loss */
2879 orig_jiffies = jiffies;
2880 for (res = 0, try = 0; try <= adapter->retries; try++) {
2881 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2882 read_write, command,
2883 protocol, data);
2884 if (res != -EAGAIN)
2885 break;
2886 if (time_after(jiffies,
2887 orig_jiffies + adapter->timeout))
2888 break;
2889 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002890 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002892 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002893 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002894 /*
2895 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2896 * implement native support for the SMBus operation.
2897 */
2898 }
2899
David Howells8a325992014-03-06 13:36:06 +00002900 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2901 command, protocol, data);
2902
2903trace:
2904 /* If enabled, the reply tracepoint is conditional on read_write. */
2905 trace_smbus_reply(adapter, addr, flags, read_write,
2906 command, protocol, data);
2907 trace_smbus_result(adapter, addr, flags, read_write,
2908 command, protocol, res);
2909
2910 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2915MODULE_DESCRIPTION("I2C-Bus main module");
2916MODULE_LICENSE("GPL");