blob: ccfbbab82a157da532fb039f7032d0de38680d0f [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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
Jean Delvare5694f8a2012-03-26 21:47:19 +020017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* ------------------------------------------------------------------------- */
20
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020023 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010024 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020025 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010026 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020030 I2C ACPI code Copyright (C) 2014 Intel Corp
31 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053038#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/slab.h>
40#include <linux/i2c.h>
41#include <linux/init.h>
42#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010043#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010044#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060045#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010046#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020047#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010048#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010049#include <linux/hardirq.h>
50#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020051#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010052#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010053#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000054#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/uaccess.h>
56
David Brownell9c1600e2007-05-01 23:26:31 +020057#include "i2c-core.h"
58
David Howellsd9a83d62014-03-06 13:35:59 +000059#define CREATE_TRACE_POINTS
60#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jean Delvare6629dcf2010-05-04 11:09:28 +020062/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020063 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000064 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4f8cf822009-09-18 22:45:46 +020068static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020069static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010070
David Howellsd9a83d62014-03-06 13:35:59 +000071static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
72
73void i2c_transfer_trace_reg(void)
74{
75 static_key_slow_inc(&i2c_trace_msg);
76}
77
78void i2c_transfer_trace_unreg(void)
79{
80 static_key_slow_dec(&i2c_trace_msg);
81}
82
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020083#if defined(CONFIG_ACPI)
84struct acpi_i2c_handler_data {
85 struct acpi_connection_info info;
86 struct i2c_adapter *adapter;
87};
88
89struct gsb_buffer {
90 u8 status;
91 u8 len;
92 union {
93 u16 wdata;
94 u8 bdata;
95 u8 data[0];
96 };
97} __packed;
98
99static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
100{
101 struct i2c_board_info *info = data;
102
103 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
104 struct acpi_resource_i2c_serialbus *sb;
105
106 sb = &ares->data.i2c_serial_bus;
107 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
108 info->addr = sb->slave_address;
109 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
110 info->flags |= I2C_CLIENT_TEN;
111 }
112 } else if (info->irq < 0) {
113 struct resource r;
114
115 if (acpi_dev_resource_interrupt(ares, 0, &r))
116 info->irq = r.start;
117 }
118
119 /* Tell the ACPI core to skip this resource */
120 return 1;
121}
122
123static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
124 void *data, void **return_value)
125{
126 struct i2c_adapter *adapter = data;
127 struct list_head resource_list;
128 struct i2c_board_info info;
129 struct acpi_device *adev;
130 int ret;
131
132 if (acpi_bus_get_device(handle, &adev))
133 return AE_OK;
134 if (acpi_bus_get_status(adev) || !adev->status.present)
135 return AE_OK;
136
137 memset(&info, 0, sizeof(info));
138 info.acpi_node.companion = adev;
139 info.irq = -1;
140
141 INIT_LIST_HEAD(&resource_list);
142 ret = acpi_dev_get_resources(adev, &resource_list,
143 acpi_i2c_add_resource, &info);
144 acpi_dev_free_resource_list(&resource_list);
145
146 if (ret < 0 || !info.addr)
147 return AE_OK;
148
149 adev->power.flags.ignore_parent = true;
150 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
151 if (!i2c_new_device(adapter, &info)) {
152 adev->power.flags.ignore_parent = false;
153 dev_err(&adapter->dev,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev->dev));
156 }
157
158 return AE_OK;
159}
160
161/**
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
164 *
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
168 */
169static void acpi_i2c_register_devices(struct i2c_adapter *adap)
170{
171 acpi_handle handle;
172 acpi_status status;
173
174 if (!adap->dev.parent)
175 return;
176
177 handle = ACPI_HANDLE(adap->dev.parent);
178 if (!handle)
179 return;
180
181 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
182 acpi_i2c_add_device, NULL,
183 adap, NULL);
184 if (ACPI_FAILURE(status))
185 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
186}
187
188#else /* CONFIG_ACPI */
189static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
190#endif /* CONFIG_ACPI */
191
192#ifdef CONFIG_ACPI_I2C_OPREGION
193static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
194 u8 cmd, u8 *data, u8 data_len)
195{
196
197 struct i2c_msg msgs[2];
198 int ret;
199 u8 *buffer;
200
201 buffer = kzalloc(data_len, GFP_KERNEL);
202 if (!buffer)
203 return AE_NO_MEMORY;
204
205 msgs[0].addr = client->addr;
206 msgs[0].flags = client->flags;
207 msgs[0].len = 1;
208 msgs[0].buf = &cmd;
209
210 msgs[1].addr = client->addr;
211 msgs[1].flags = client->flags | I2C_M_RD;
212 msgs[1].len = data_len;
213 msgs[1].buf = buffer;
214
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
216 if (ret < 0)
217 dev_err(&client->adapter->dev, "i2c read failed\n");
218 else
219 memcpy(data, buffer, data_len);
220
221 kfree(buffer);
222 return ret;
223}
224
225static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
226 u8 cmd, u8 *data, u8 data_len)
227{
228
229 struct i2c_msg msgs[1];
230 u8 *buffer;
231 int ret = AE_OK;
232
233 buffer = kzalloc(data_len + 1, GFP_KERNEL);
234 if (!buffer)
235 return AE_NO_MEMORY;
236
237 buffer[0] = cmd;
238 memcpy(buffer + 1, data, data_len);
239
240 msgs[0].addr = client->addr;
241 msgs[0].flags = client->flags;
242 msgs[0].len = data_len + 1;
243 msgs[0].buf = buffer;
244
245 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
246 if (ret < 0)
247 dev_err(&client->adapter->dev, "i2c write failed\n");
248
249 kfree(buffer);
250 return ret;
251}
252
253static acpi_status
254acpi_i2c_space_handler(u32 function, acpi_physical_address command,
255 u32 bits, u64 *value64,
256 void *handler_context, void *region_context)
257{
258 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
259 struct acpi_i2c_handler_data *data = handler_context;
260 struct acpi_connection_info *info = &data->info;
261 struct acpi_resource_i2c_serialbus *sb;
262 struct i2c_adapter *adapter = data->adapter;
263 struct i2c_client client;
264 struct acpi_resource *ares;
265 u32 accessor_type = function >> 16;
266 u8 action = function & ACPI_IO_MASK;
267 acpi_status ret = AE_OK;
268 int status;
269
270 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
271 if (ACPI_FAILURE(ret))
272 return ret;
273
274 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
275 ret = AE_BAD_PARAMETER;
276 goto err;
277 }
278
279 sb = &ares->data.i2c_serial_bus;
280 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
281 ret = AE_BAD_PARAMETER;
282 goto err;
283 }
284
285 memset(&client, 0, sizeof(client));
286 client.adapter = adapter;
287 client.addr = sb->slave_address;
288 client.flags = 0;
289
290 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
291 client.flags |= I2C_CLIENT_TEN;
292
293 switch (accessor_type) {
294 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
295 if (action == ACPI_READ) {
296 status = i2c_smbus_read_byte(&client);
297 if (status >= 0) {
298 gsb->bdata = status;
299 status = 0;
300 }
301 } else {
302 status = i2c_smbus_write_byte(&client, gsb->bdata);
303 }
304 break;
305
306 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
307 if (action == ACPI_READ) {
308 status = i2c_smbus_read_byte_data(&client, command);
309 if (status >= 0) {
310 gsb->bdata = status;
311 status = 0;
312 }
313 } else {
314 status = i2c_smbus_write_byte_data(&client, command,
315 gsb->bdata);
316 }
317 break;
318
319 case ACPI_GSB_ACCESS_ATTRIB_WORD:
320 if (action == ACPI_READ) {
321 status = i2c_smbus_read_word_data(&client, command);
322 if (status >= 0) {
323 gsb->wdata = status;
324 status = 0;
325 }
326 } else {
327 status = i2c_smbus_write_word_data(&client, command,
328 gsb->wdata);
329 }
330 break;
331
332 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
333 if (action == ACPI_READ) {
334 status = i2c_smbus_read_block_data(&client, command,
335 gsb->data);
336 if (status >= 0) {
337 gsb->len = status;
338 status = 0;
339 }
340 } else {
341 status = i2c_smbus_write_block_data(&client, command,
342 gsb->len, gsb->data);
343 }
344 break;
345
346 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
347 if (action == ACPI_READ) {
348 status = acpi_gsb_i2c_read_bytes(&client, command,
349 gsb->data, info->access_length);
350 if (status > 0)
351 status = 0;
352 } else {
353 status = acpi_gsb_i2c_write_bytes(&client, command,
354 gsb->data, info->access_length);
355 }
356 break;
357
358 default:
359 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
360 ret = AE_BAD_PARAMETER;
361 goto err;
362 }
363
364 gsb->status = status;
365
366 err:
367 ACPI_FREE(ares);
368 return ret;
369}
370
371
372static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
373{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200374 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200375 struct acpi_i2c_handler_data *data;
376 acpi_status status;
377
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200378 if (!adapter->dev.parent)
379 return -ENODEV;
380
381 handle = ACPI_HANDLE(adapter->dev.parent);
382
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200383 if (!handle)
384 return -ENODEV;
385
386 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
387 GFP_KERNEL);
388 if (!data)
389 return -ENOMEM;
390
391 data->adapter = adapter;
392 status = acpi_bus_attach_private_data(handle, (void *)data);
393 if (ACPI_FAILURE(status)) {
394 kfree(data);
395 return -ENOMEM;
396 }
397
398 status = acpi_install_address_space_handler(handle,
399 ACPI_ADR_SPACE_GSBUS,
400 &acpi_i2c_space_handler,
401 NULL,
402 data);
403 if (ACPI_FAILURE(status)) {
404 dev_err(&adapter->dev, "Error installing i2c space handler\n");
405 acpi_bus_detach_private_data(handle);
406 kfree(data);
407 return -ENOMEM;
408 }
409
410 return 0;
411}
412
413static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
414{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200415 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200416 struct acpi_i2c_handler_data *data;
417 acpi_status status;
418
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200419 if (!adapter->dev.parent)
420 return;
421
422 handle = ACPI_HANDLE(adapter->dev.parent);
423
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200424 if (!handle)
425 return;
426
427 acpi_remove_address_space_handler(handle,
428 ACPI_ADR_SPACE_GSBUS,
429 &acpi_i2c_space_handler);
430
431 status = acpi_bus_get_private_data(handle, (void **)&data);
432 if (ACPI_SUCCESS(status))
433 kfree(data);
434
435 acpi_bus_detach_private_data(handle);
436}
437#else /* CONFIG_ACPI_I2C_OPREGION */
438static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
439{ }
440
441static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
442{ return 0; }
443#endif /* CONFIG_ACPI_I2C_OPREGION */
444
David Brownellf37dd802007-02-13 22:09:00 +0100445/* ------------------------------------------------------------------------- */
446
Jean Delvared2653e92008-04-29 23:11:39 +0200447static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
448 const struct i2c_client *client)
449{
450 while (id->name[0]) {
451 if (strcmp(client->name, id->name) == 0)
452 return id;
453 id++;
454 }
455 return NULL;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static int i2c_device_match(struct device *dev, struct device_driver *drv)
459{
Jean Delvare51298d12009-09-18 22:45:45 +0200460 struct i2c_client *client = i2c_verify_client(dev);
461 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200462
Jean Delvare51298d12009-09-18 22:45:45 +0200463 if (!client)
464 return 0;
465
Grant Likely959e85f2010-06-08 07:48:19 -0600466 /* Attempt an OF style match */
467 if (of_driver_match_device(dev, drv))
468 return 1;
469
Mika Westerberg907ddf82012-11-23 12:23:40 +0100470 /* Then ACPI style match */
471 if (acpi_driver_match_device(dev, drv))
472 return 1;
473
Jean Delvare51298d12009-09-18 22:45:45 +0200474 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200475 /* match on an id table if there is one */
476 if (driver->id_table)
477 return i2c_match_id(driver->id_table, client) != NULL;
478
Jean Delvareeb8a7902008-05-18 20:49:41 +0200479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
David Brownell7b4fbc52007-05-01 23:26:30 +0200482
483/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200484static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200485{
486 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800487 int rc;
488
489 rc = acpi_device_uevent_modalias(dev, env);
490 if (rc != -ENODEV)
491 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200492
Jean Delvareeb8a7902008-05-18 20:49:41 +0200493 if (add_uevent_var(env, "MODALIAS=%s%s",
494 I2C_MODULE_PREFIX, client->name))
495 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200496 dev_dbg(dev, "uevent\n");
497 return 0;
498}
499
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530500/* i2c bus recovery routines */
501static int get_scl_gpio_value(struct i2c_adapter *adap)
502{
503 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
504}
505
506static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
507{
508 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
509}
510
511static int get_sda_gpio_value(struct i2c_adapter *adap)
512{
513 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
514}
515
516static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
517{
518 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
519 struct device *dev = &adap->dev;
520 int ret = 0;
521
522 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
523 GPIOF_OUT_INIT_HIGH, "i2c-scl");
524 if (ret) {
525 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
526 return ret;
527 }
528
529 if (bri->get_sda) {
530 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
531 /* work without SDA polling */
532 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
533 bri->sda_gpio);
534 bri->get_sda = NULL;
535 }
536 }
537
538 return ret;
539}
540
541static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
542{
543 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
544
545 if (bri->get_sda)
546 gpio_free(bri->sda_gpio);
547
548 gpio_free(bri->scl_gpio);
549}
550
551/*
552 * We are generating clock pulses. ndelay() determines durating of clk pulses.
553 * We will generate clock with rate 100 KHz and so duration of both clock levels
554 * is: delay in ns = (10^6 / 100) / 2
555 */
556#define RECOVERY_NDELAY 5000
557#define RECOVERY_CLK_CNT 9
558
559static int i2c_generic_recovery(struct i2c_adapter *adap)
560{
561 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
562 int i = 0, val = 1, ret = 0;
563
564 if (bri->prepare_recovery)
565 bri->prepare_recovery(bri);
566
567 /*
568 * By this time SCL is high, as we need to give 9 falling-rising edges
569 */
570 while (i++ < RECOVERY_CLK_CNT * 2) {
571 if (val) {
572 /* Break if SDA is high */
573 if (bri->get_sda && bri->get_sda(adap))
574 break;
575 /* SCL shouldn't be low here */
576 if (!bri->get_scl(adap)) {
577 dev_err(&adap->dev,
578 "SCL is stuck low, exit recovery\n");
579 ret = -EBUSY;
580 break;
581 }
582 }
583
584 val = !val;
585 bri->set_scl(adap, val);
586 ndelay(RECOVERY_NDELAY);
587 }
588
589 if (bri->unprepare_recovery)
590 bri->unprepare_recovery(bri);
591
592 return ret;
593}
594
595int i2c_generic_scl_recovery(struct i2c_adapter *adap)
596{
597 adap->bus_recovery_info->set_scl(adap, 1);
598 return i2c_generic_recovery(adap);
599}
600
601int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
602{
603 int ret;
604
605 ret = i2c_get_gpios_for_recovery(adap);
606 if (ret)
607 return ret;
608
609 ret = i2c_generic_recovery(adap);
610 i2c_put_gpios_for_recovery(adap);
611
612 return ret;
613}
614
615int i2c_recover_bus(struct i2c_adapter *adap)
616{
617 if (!adap->bus_recovery_info)
618 return -EOPNOTSUPP;
619
620 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
621 return adap->bus_recovery_info->recover_bus(adap);
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static int i2c_device_probe(struct device *dev)
625{
Jean Delvare51298d12009-09-18 22:45:45 +0200626 struct i2c_client *client = i2c_verify_client(dev);
627 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100628 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200629
Jean Delvare51298d12009-09-18 22:45:45 +0200630 if (!client)
631 return 0;
632
633 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200634 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200635 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200636
Marc Pignatee354252008-08-28 08:33:22 +0200637 if (!device_can_wakeup(&client->dev))
638 device_init_wakeup(&client->dev,
639 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200640 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200641
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200642 status = of_clk_set_defaults(dev->of_node, false);
643 if (status < 0)
644 return status;
645
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300646 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200647 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sang72fa8182014-01-21 17:48:34 +0100648 if (status)
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300649 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sang72fa8182014-01-21 17:48:34 +0100650
Hans Verkuil50c33042008-03-12 14:15:00 +0100651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static int i2c_device_remove(struct device *dev)
655{
Jean Delvare51298d12009-09-18 22:45:45 +0200656 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200657 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100658 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200659
Jean Delvare51298d12009-09-18 22:45:45 +0200660 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200661 return 0;
662
663 driver = to_i2c_driver(dev->driver);
664 if (driver->remove) {
665 dev_dbg(dev, "remove\n");
666 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200667 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100668
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300669 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200670 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
David Brownellf37dd802007-02-13 22:09:00 +0100673static void i2c_device_shutdown(struct device *dev)
674{
Jean Delvare51298d12009-09-18 22:45:45 +0200675 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100676 struct i2c_driver *driver;
677
Jean Delvare51298d12009-09-18 22:45:45 +0200678 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100679 return;
680 driver = to_i2c_driver(dev->driver);
681 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200682 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100683}
684
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200685#ifdef CONFIG_PM_SLEEP
686static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100687{
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 0;
693 driver = to_i2c_driver(dev->driver);
694 if (!driver->suspend)
695 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200696 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100697}
698
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200699static int i2c_legacy_resume(struct device *dev)
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->resume)
708 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200709 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100710}
711
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200712static int i2c_device_pm_suspend(struct device *dev)
713{
714 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
715
Mark Brownd529de22011-01-14 22:03:49 +0100716 if (pm)
717 return pm_generic_suspend(dev);
718 else
719 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200720}
721
722static int i2c_device_pm_resume(struct device *dev)
723{
724 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200725
726 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100727 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200728 else
Mark Brownd529de22011-01-14 22:03:49 +0100729 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200730}
731
732static int i2c_device_pm_freeze(struct device *dev)
733{
734 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
735
Mark Brownd529de22011-01-14 22:03:49 +0100736 if (pm)
737 return pm_generic_freeze(dev);
738 else
739 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200740}
741
742static int i2c_device_pm_thaw(struct device *dev)
743{
744 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
745
Mark Brownd529de22011-01-14 22:03:49 +0100746 if (pm)
747 return pm_generic_thaw(dev);
748 else
749 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200750}
751
752static int i2c_device_pm_poweroff(struct device *dev)
753{
754 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
755
Mark Brownd529de22011-01-14 22:03:49 +0100756 if (pm)
757 return pm_generic_poweroff(dev);
758 else
759 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200760}
761
762static int i2c_device_pm_restore(struct device *dev)
763{
764 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200765
766 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100767 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200768 else
Mark Brownd529de22011-01-14 22:03:49 +0100769 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200770}
771#else /* !CONFIG_PM_SLEEP */
772#define i2c_device_pm_suspend NULL
773#define i2c_device_pm_resume NULL
774#define i2c_device_pm_freeze NULL
775#define i2c_device_pm_thaw NULL
776#define i2c_device_pm_poweroff NULL
777#define i2c_device_pm_restore NULL
778#endif /* !CONFIG_PM_SLEEP */
779
David Brownell9c1600e2007-05-01 23:26:31 +0200780static void i2c_client_dev_release(struct device *dev)
781{
782 kfree(to_i2c_client(dev));
783}
784
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100785static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200786show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200787{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200788 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
789 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200790}
791
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100792static ssize_t
793show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200794{
795 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800796 int len;
797
798 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
799 if (len != -ENODEV)
800 return len;
801
Jean Delvareeb8a7902008-05-18 20:49:41 +0200802 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200803}
804
Jean Delvare4f8cf822009-09-18 22:45:46 +0200805static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200806static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
807
808static struct attribute *i2c_dev_attrs[] = {
809 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200810 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200811 &dev_attr_modalias.attr,
812 NULL
813};
814
815static struct attribute_group i2c_dev_attr_group = {
816 .attrs = i2c_dev_attrs,
817};
818
819static const struct attribute_group *i2c_dev_attr_groups[] = {
820 &i2c_dev_attr_group,
821 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200822};
823
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100824static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100825 .suspend = i2c_device_pm_suspend,
826 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200827 .freeze = i2c_device_pm_freeze,
828 .thaw = i2c_device_pm_thaw,
829 .poweroff = i2c_device_pm_poweroff,
830 .restore = i2c_device_pm_restore,
831 SET_RUNTIME_PM_OPS(
832 pm_generic_runtime_suspend,
833 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200834 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200835 )
sonic zhang54067ee2009-12-14 21:17:30 +0100836};
837
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200838struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100839 .name = "i2c",
840 .match = i2c_device_match,
841 .probe = i2c_device_probe,
842 .remove = i2c_device_remove,
843 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100844 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000845};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200846EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000847
Jean Delvare51298d12009-09-18 22:45:45 +0200848static struct device_type i2c_client_type = {
849 .groups = i2c_dev_attr_groups,
850 .uevent = i2c_device_uevent,
851 .release = i2c_client_dev_release,
852};
853
David Brownell9b766b82008-01-27 18:14:51 +0100854
855/**
856 * i2c_verify_client - return parameter as i2c_client, or NULL
857 * @dev: device, probably from some driver model iterator
858 *
859 * When traversing the driver model tree, perhaps using driver model
860 * iterators like @device_for_each_child(), you can't assume very much
861 * about the nodes you find. Use this function to avoid oopses caused
862 * by wrongly treating some non-I2C device as an i2c_client.
863 */
864struct i2c_client *i2c_verify_client(struct device *dev)
865{
Jean Delvare51298d12009-09-18 22:45:45 +0200866 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100867 ? to_i2c_client(dev)
868 : NULL;
869}
870EXPORT_SYMBOL(i2c_verify_client);
871
872
Jean Delvare3a89db52010-06-03 11:33:52 +0200873/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300874 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200875static int i2c_check_client_addr_validity(const struct i2c_client *client)
876{
877 if (client->flags & I2C_CLIENT_TEN) {
878 /* 10-bit address, all values are valid */
879 if (client->addr > 0x3ff)
880 return -EINVAL;
881 } else {
882 /* 7-bit address, reject the general call address */
883 if (client->addr == 0x00 || client->addr > 0x7f)
884 return -EINVAL;
885 }
886 return 0;
887}
888
Jean Delvare656b8762010-06-03 11:33:53 +0200889/* And this is a strict address validity check, used when probing. If a
890 * device uses a reserved address, then it shouldn't be probed. 7-bit
891 * addressing is assumed, 10-bit address devices are rare and should be
892 * explicitly enumerated. */
893static int i2c_check_addr_validity(unsigned short addr)
894{
895 /*
896 * Reserved addresses per I2C specification:
897 * 0x00 General call address / START byte
898 * 0x01 CBUS address
899 * 0x02 Reserved for different bus format
900 * 0x03 Reserved for future purposes
901 * 0x04-0x07 Hs-mode master code
902 * 0x78-0x7b 10-bit slave addressing
903 * 0x7c-0x7f Reserved for future purposes
904 */
905 if (addr < 0x08 || addr > 0x77)
906 return -EINVAL;
907 return 0;
908}
909
Jean Delvare3b5f7942010-06-03 11:33:55 +0200910static int __i2c_check_addr_busy(struct device *dev, void *addrp)
911{
912 struct i2c_client *client = i2c_verify_client(dev);
913 int addr = *(int *)addrp;
914
915 if (client && client->addr == addr)
916 return -EBUSY;
917 return 0;
918}
919
Michael Lawnick08263742010-08-11 18:21:02 +0200920/* walk up mux tree */
921static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
922{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200923 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200924 int result;
925
926 result = device_for_each_child(&adapter->dev, &addr,
927 __i2c_check_addr_busy);
928
Jean Delvare97cc4d42010-10-24 18:16:57 +0200929 if (!result && parent)
930 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200931
932 return result;
933}
934
935/* recurse down mux tree */
936static int i2c_check_mux_children(struct device *dev, void *addrp)
937{
938 int result;
939
940 if (dev->type == &i2c_adapter_type)
941 result = device_for_each_child(dev, addrp,
942 i2c_check_mux_children);
943 else
944 result = __i2c_check_addr_busy(dev, addrp);
945
946 return result;
947}
948
Jean Delvare3b5f7942010-06-03 11:33:55 +0200949static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
950{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200951 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200952 int result = 0;
953
Jean Delvare97cc4d42010-10-24 18:16:57 +0200954 if (parent)
955 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200956
957 if (!result)
958 result = device_for_each_child(&adapter->dev, &addr,
959 i2c_check_mux_children);
960
961 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200962}
963
David Brownell9c1600e2007-05-01 23:26:31 +0200964/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200965 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
966 * @adapter: Target I2C bus segment
967 */
968void i2c_lock_adapter(struct i2c_adapter *adapter)
969{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200970 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
971
972 if (parent)
973 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200974 else
975 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200976}
977EXPORT_SYMBOL_GPL(i2c_lock_adapter);
978
979/**
980 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
981 * @adapter: Target I2C bus segment
982 */
983static int i2c_trylock_adapter(struct i2c_adapter *adapter)
984{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200985 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
986
987 if (parent)
988 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200989 else
990 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200991}
992
993/**
994 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
995 * @adapter: Target I2C bus segment
996 */
997void i2c_unlock_adapter(struct i2c_adapter *adapter)
998{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200999 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1000
1001 if (parent)
1002 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001003 else
1004 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001005}
1006EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1007
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001008static void i2c_dev_set_name(struct i2c_adapter *adap,
1009 struct i2c_client *client)
1010{
1011 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1012
1013 if (adev) {
1014 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1015 return;
1016 }
1017
1018 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1019 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1020 client->addr | ((client->flags & I2C_CLIENT_TEN)
1021 ? 0xa000 : 0));
1022}
1023
Jean Delvarefe61e072010-08-11 18:20:58 +02001024/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001025 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001026 * @adap: the adapter managing the device
1027 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001028 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001029 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001030 * Create an i2c device. Binding is handled through driver model
1031 * probe()/remove() methods. A driver may be bound to this device when we
1032 * return from this function, or any later moment (e.g. maybe hotplugging will
1033 * load the driver module). This call is not appropriate for use by mainboard
1034 * initialization logic, which usually runs during an arch_initcall() long
1035 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001036 *
1037 * This returns the new i2c client, which may be saved for later use with
1038 * i2c_unregister_device(); or NULL to indicate an error.
1039 */
1040struct i2c_client *
1041i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1042{
1043 struct i2c_client *client;
1044 int status;
1045
1046 client = kzalloc(sizeof *client, GFP_KERNEL);
1047 if (!client)
1048 return NULL;
1049
1050 client->adapter = adap;
1051
1052 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001053
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001054 if (info->archdata)
1055 client->dev.archdata = *info->archdata;
1056
Marc Pignatee354252008-08-28 08:33:22 +02001057 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001058 client->addr = info->addr;
1059 client->irq = info->irq;
1060
David Brownell9c1600e2007-05-01 23:26:31 +02001061 strlcpy(client->name, info->type, sizeof(client->name));
1062
Jean Delvare3a89db52010-06-03 11:33:52 +02001063 /* Check for address validity */
1064 status = i2c_check_client_addr_validity(client);
1065 if (status) {
1066 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1067 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1068 goto out_err_silent;
1069 }
1070
Jean Delvaref8a227e2009-06-19 16:58:18 +02001071 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001072 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001073 if (status)
1074 goto out_err;
1075
1076 client->dev.parent = &client->adapter->dev;
1077 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001078 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001079 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001080 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001081
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001082 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001083 status = device_register(&client->dev);
1084 if (status)
1085 goto out_err;
1086
Jean Delvaref8a227e2009-06-19 16:58:18 +02001087 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1088 client->name, dev_name(&client->dev));
1089
David Brownell9c1600e2007-05-01 23:26:31 +02001090 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001091
1092out_err:
1093 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1094 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001095out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001096 kfree(client);
1097 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001098}
1099EXPORT_SYMBOL_GPL(i2c_new_device);
1100
1101
1102/**
1103 * i2c_unregister_device - reverse effect of i2c_new_device()
1104 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001105 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001106 */
1107void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001108{
David Brownella1d9e6e2007-05-01 23:26:30 +02001109 device_unregister(&client->dev);
1110}
David Brownell9c1600e2007-05-01 23:26:31 +02001111EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001112
1113
Jean Delvare60b129d2008-05-11 20:37:06 +02001114static const struct i2c_device_id dummy_id[] = {
1115 { "dummy", 0 },
1116 { },
1117};
1118
Jean Delvared2653e92008-04-29 23:11:39 +02001119static int dummy_probe(struct i2c_client *client,
1120 const struct i2c_device_id *id)
1121{
1122 return 0;
1123}
1124
1125static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001126{
1127 return 0;
1128}
1129
1130static struct i2c_driver dummy_driver = {
1131 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001132 .probe = dummy_probe,
1133 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001134 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001135};
1136
1137/**
1138 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1139 * @adapter: the adapter managing the device
1140 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001141 * Context: can sleep
1142 *
1143 * This returns an I2C client bound to the "dummy" driver, intended for use
1144 * with devices that consume multiple addresses. Examples of such chips
1145 * include various EEPROMS (like 24c04 and 24c08 models).
1146 *
1147 * These dummy devices have two main uses. First, most I2C and SMBus calls
1148 * except i2c_transfer() need a client handle; the dummy will be that handle.
1149 * And second, this prevents the specified address from being bound to a
1150 * different driver.
1151 *
1152 * This returns the new i2c client, which should be saved for later use with
1153 * i2c_unregister_device(); or NULL to indicate an error.
1154 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001155struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001156{
1157 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001158 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001159 };
1160
David Brownelle9f13732008-01-27 18:14:52 +01001161 return i2c_new_device(adapter, &info);
1162}
1163EXPORT_SYMBOL_GPL(i2c_new_dummy);
1164
David Brownellf37dd802007-02-13 22:09:00 +01001165/* ------------------------------------------------------------------------- */
1166
David Brownell16ffadf2007-05-01 23:26:28 +02001167/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1168
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001169static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
David Brownellef2c83212007-05-01 23:26:28 +02001171 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 complete(&adap->dev_released);
1173}
1174
Jean Delvare99cd8e22009-06-19 16:58:20 +02001175/*
Jean Delvare390946b2012-09-10 10:14:02 +02001176 * This function is only needed for mutex_lock_nested, so it is never
1177 * called unless locking correctness checking is enabled. Thus we
1178 * make it inline to avoid a compiler warning. That's what gcc ends up
1179 * doing anyway.
1180 */
1181static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1182{
1183 unsigned int depth = 0;
1184
1185 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1186 depth++;
1187
1188 return depth;
1189}
1190
1191/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001192 * Let users instantiate I2C devices through sysfs. This can be used when
1193 * platform initialization code doesn't contain the proper data for
1194 * whatever reason. Also useful for drivers that do device detection and
1195 * detection fails, either because the device uses an unexpected address,
1196 * or this is a compatible device with different ID register values.
1197 *
1198 * Parameter checking may look overzealous, but we really don't want
1199 * the user to provide incorrect parameters.
1200 */
1201static ssize_t
1202i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1203 const char *buf, size_t count)
1204{
1205 struct i2c_adapter *adap = to_i2c_adapter(dev);
1206 struct i2c_board_info info;
1207 struct i2c_client *client;
1208 char *blank, end;
1209 int res;
1210
Jean Delvare99cd8e22009-06-19 16:58:20 +02001211 memset(&info, 0, sizeof(struct i2c_board_info));
1212
1213 blank = strchr(buf, ' ');
1214 if (!blank) {
1215 dev_err(dev, "%s: Missing parameters\n", "new_device");
1216 return -EINVAL;
1217 }
1218 if (blank - buf > I2C_NAME_SIZE - 1) {
1219 dev_err(dev, "%s: Invalid device name\n", "new_device");
1220 return -EINVAL;
1221 }
1222 memcpy(info.type, buf, blank - buf);
1223
1224 /* Parse remaining parameters, reject extra parameters */
1225 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1226 if (res < 1) {
1227 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1228 return -EINVAL;
1229 }
1230 if (res > 1 && end != '\n') {
1231 dev_err(dev, "%s: Extra parameters\n", "new_device");
1232 return -EINVAL;
1233 }
1234
Jean Delvare99cd8e22009-06-19 16:58:20 +02001235 client = i2c_new_device(adap, &info);
1236 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001237 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001238
1239 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001240 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001241 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001242 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001243 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1244 info.type, info.addr);
1245
1246 return count;
1247}
1248
1249/*
1250 * And of course let the users delete the devices they instantiated, if
1251 * they got it wrong. This interface can only be used to delete devices
1252 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1253 * don't delete devices to which some kernel code still has references.
1254 *
1255 * Parameter checking may look overzealous, but we really don't want
1256 * the user to delete the wrong device.
1257 */
1258static ssize_t
1259i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1260 const char *buf, size_t count)
1261{
1262 struct i2c_adapter *adap = to_i2c_adapter(dev);
1263 struct i2c_client *client, *next;
1264 unsigned short addr;
1265 char end;
1266 int res;
1267
1268 /* Parse parameters, reject extra parameters */
1269 res = sscanf(buf, "%hi%c", &addr, &end);
1270 if (res < 1) {
1271 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1272 return -EINVAL;
1273 }
1274 if (res > 1 && end != '\n') {
1275 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1276 return -EINVAL;
1277 }
1278
1279 /* Make sure the device was added through sysfs */
1280 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001281 mutex_lock_nested(&adap->userspace_clients_lock,
1282 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001283 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1284 detected) {
1285 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001286 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1287 "delete_device", client->name, client->addr);
1288
1289 list_del(&client->detected);
1290 i2c_unregister_device(client);
1291 res = count;
1292 break;
1293 }
1294 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001295 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001296
1297 if (res < 0)
1298 dev_err(dev, "%s: Can't find device in list\n",
1299 "delete_device");
1300 return res;
1301}
1302
Jean Delvare4f8cf822009-09-18 22:45:46 +02001303static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001304static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1305 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001306
1307static struct attribute *i2c_adapter_attrs[] = {
1308 &dev_attr_name.attr,
1309 &dev_attr_new_device.attr,
1310 &dev_attr_delete_device.attr,
1311 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001312};
1313
Jean Delvare4f8cf822009-09-18 22:45:46 +02001314static struct attribute_group i2c_adapter_attr_group = {
1315 .attrs = i2c_adapter_attrs,
1316};
1317
1318static const struct attribute_group *i2c_adapter_attr_groups[] = {
1319 &i2c_adapter_attr_group,
1320 NULL
1321};
1322
Michael Lawnick08263742010-08-11 18:21:02 +02001323struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001324 .groups = i2c_adapter_attr_groups,
1325 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001326};
Michael Lawnick08263742010-08-11 18:21:02 +02001327EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Stephen Warren643dd092012-04-17 12:43:33 -06001329/**
1330 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1331 * @dev: device, probably from some driver model iterator
1332 *
1333 * When traversing the driver model tree, perhaps using driver model
1334 * iterators like @device_for_each_child(), you can't assume very much
1335 * about the nodes you find. Use this function to avoid oopses caused
1336 * by wrongly treating some non-I2C device as an i2c_adapter.
1337 */
1338struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1339{
1340 return (dev->type == &i2c_adapter_type)
1341 ? to_i2c_adapter(dev)
1342 : NULL;
1343}
1344EXPORT_SYMBOL(i2c_verify_adapter);
1345
Jean Delvare2bb50952009-09-18 22:45:46 +02001346#ifdef CONFIG_I2C_COMPAT
1347static struct class_compat *i2c_adapter_compat_class;
1348#endif
1349
David Brownell9c1600e2007-05-01 23:26:31 +02001350static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1351{
1352 struct i2c_devinfo *devinfo;
1353
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001354 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001355 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1356 if (devinfo->busnum == adapter->nr
1357 && !i2c_new_device(adapter,
1358 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001359 dev_err(&adapter->dev,
1360 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001361 devinfo->board_info.addr);
1362 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001363 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001364}
1365
Wolfram Sang687b81d2013-07-11 12:56:15 +01001366/* OF support code */
1367
1368#if IS_ENABLED(CONFIG_OF)
1369static void of_i2c_register_devices(struct i2c_adapter *adap)
1370{
1371 void *result;
1372 struct device_node *node;
1373
1374 /* Only register child devices if the adapter has a node pointer set */
1375 if (!adap->dev.of_node)
1376 return;
1377
1378 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1379
1380 for_each_available_child_of_node(adap->dev.of_node, node) {
1381 struct i2c_board_info info = {};
1382 struct dev_archdata dev_ad = {};
1383 const __be32 *addr;
1384 int len;
1385
1386 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1387
1388 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1389 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1390 node->full_name);
1391 continue;
1392 }
1393
1394 addr = of_get_property(node, "reg", &len);
1395 if (!addr || (len < sizeof(int))) {
1396 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1397 node->full_name);
1398 continue;
1399 }
1400
1401 info.addr = be32_to_cpup(addr);
1402 if (info.addr > (1 << 10) - 1) {
1403 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1404 info.addr, node->full_name);
1405 continue;
1406 }
1407
1408 info.irq = irq_of_parse_and_map(node, 0);
1409 info.of_node = of_node_get(node);
1410 info.archdata = &dev_ad;
1411
1412 if (of_get_property(node, "wakeup-source", NULL))
1413 info.flags |= I2C_CLIENT_WAKE;
1414
1415 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1416
1417 result = i2c_new_device(adap, &info);
1418 if (result == NULL) {
1419 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1420 node->full_name);
1421 of_node_put(node);
1422 irq_dispose_mapping(info.irq);
1423 continue;
1424 }
1425 }
1426}
1427
1428static int of_dev_node_match(struct device *dev, void *data)
1429{
1430 return dev->of_node == data;
1431}
1432
1433/* must call put_device() when done with returned i2c_client device */
1434struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1435{
1436 struct device *dev;
1437
1438 dev = bus_find_device(&i2c_bus_type, NULL, node,
1439 of_dev_node_match);
1440 if (!dev)
1441 return NULL;
1442
1443 return i2c_verify_client(dev);
1444}
1445EXPORT_SYMBOL(of_find_i2c_device_by_node);
1446
1447/* must call put_device() when done with returned i2c_adapter device */
1448struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1449{
1450 struct device *dev;
1451
1452 dev = bus_find_device(&i2c_bus_type, NULL, node,
1453 of_dev_node_match);
1454 if (!dev)
1455 return NULL;
1456
1457 return i2c_verify_adapter(dev);
1458}
1459EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1460#else
1461static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1462#endif /* CONFIG_OF */
1463
Jean Delvare69b00892009-12-06 17:06:27 +01001464static int i2c_do_add_adapter(struct i2c_driver *driver,
1465 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001466{
Jean Delvare4735c982008-07-14 22:38:36 +02001467 /* Detect supported devices on that bus, and instantiate them */
1468 i2c_detect(adap, driver);
1469
1470 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001471 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001472 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1473 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001474 dev_warn(&adap->dev, "Please use another way to instantiate "
1475 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001476 /* We ignore the return code; if it fails, too bad */
1477 driver->attach_adapter(adap);
1478 }
1479 return 0;
1480}
1481
Jean Delvare69b00892009-12-06 17:06:27 +01001482static int __process_new_adapter(struct device_driver *d, void *data)
1483{
1484 return i2c_do_add_adapter(to_i2c_driver(d), data);
1485}
1486
David Brownell6e13e642007-05-01 23:26:31 +02001487static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Jean Delvared6703282010-08-11 18:20:59 +02001489 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
David Brownell1d0b19c2008-10-14 17:30:05 +02001491 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001492 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1493 res = -EAGAIN;
1494 goto out_list;
1495 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001496
Jean Delvare2236baa2010-11-15 22:40:38 +01001497 /* Sanity checks */
1498 if (unlikely(adap->name[0] == '\0')) {
1499 pr_err("i2c-core: Attempt to register an adapter with "
1500 "no name!\n");
1501 return -EINVAL;
1502 }
1503 if (unlikely(!adap->algo)) {
1504 pr_err("i2c-core: Attempt to register adapter '%s' with "
1505 "no algo!\n", adap->name);
1506 return -EINVAL;
1507 }
1508
Mika Kuoppala194684e2009-12-06 17:06:22 +01001509 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001510 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001511 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Jean Delvare8fcfef62009-03-28 21:34:43 +01001513 /* Set default timeout to 1 second if not already set */
1514 if (adap->timeout == 0)
1515 adap->timeout = HZ;
1516
Kay Sievers27d9c182009-01-07 14:29:16 +01001517 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001518 adap->dev.bus = &i2c_bus_type;
1519 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001520 res = device_register(&adap->dev);
1521 if (res)
1522 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001524 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1525
Jean Delvare2bb50952009-09-18 22:45:46 +02001526#ifdef CONFIG_I2C_COMPAT
1527 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1528 adap->dev.parent);
1529 if (res)
1530 dev_warn(&adap->dev,
1531 "Failed to create compatibility class link\n");
1532#endif
1533
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301534 /* bus recovery specific initialization */
1535 if (adap->bus_recovery_info) {
1536 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1537
1538 if (!bri->recover_bus) {
1539 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1540 adap->bus_recovery_info = NULL;
1541 goto exit_recovery;
1542 }
1543
1544 /* Generic GPIO recovery */
1545 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1546 if (!gpio_is_valid(bri->scl_gpio)) {
1547 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1548 adap->bus_recovery_info = NULL;
1549 goto exit_recovery;
1550 }
1551
1552 if (gpio_is_valid(bri->sda_gpio))
1553 bri->get_sda = get_sda_gpio_value;
1554 else
1555 bri->get_sda = NULL;
1556
1557 bri->get_scl = get_scl_gpio_value;
1558 bri->set_scl = set_scl_gpio_value;
1559 } else if (!bri->set_scl || !bri->get_scl) {
1560 /* Generic SCL recovery */
1561 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1562 adap->bus_recovery_info = NULL;
1563 }
1564 }
1565
1566exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001567 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001568 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001569 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001570 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001571
David Brownell6e13e642007-05-01 23:26:31 +02001572 if (adap->nr < __i2c_first_dynamic_bus_num)
1573 i2c_scan_static_board_info(adap);
1574
Jean Delvare4735c982008-07-14 22:38:36 +02001575 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001576 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001577 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001578 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001579
1580 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001581
Jean Delvareb119c6c2006-08-15 18:26:30 +02001582out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001583 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001584 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001585 mutex_unlock(&core_lock);
1586 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
David Brownell6e13e642007-05-01 23:26:31 +02001589/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001590 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1591 * @adap: the adapter to register (with adap->nr initialized)
1592 * Context: can sleep
1593 *
1594 * See i2c_add_numbered_adapter() for details.
1595 */
1596static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1597{
1598 int id;
1599
1600 mutex_lock(&core_lock);
1601 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1602 GFP_KERNEL);
1603 mutex_unlock(&core_lock);
1604 if (id < 0)
1605 return id == -ENOSPC ? -EBUSY : id;
1606
1607 return i2c_register_adapter(adap);
1608}
1609
1610/**
David Brownell6e13e642007-05-01 23:26:31 +02001611 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1612 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001613 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001614 *
1615 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001616 * doesn't matter or when its bus number is specified by an dt alias.
1617 * Examples of bases when the bus number doesn't matter: I2C adapters
1618 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001619 *
1620 * When this returns zero, a new bus number was allocated and stored
1621 * in adap->nr, and the specified adapter became available for clients.
1622 * Otherwise, a negative errno value is returned.
1623 */
1624int i2c_add_adapter(struct i2c_adapter *adapter)
1625{
Doug Andersonee5c2742013-03-01 08:57:31 -08001626 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001627 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001628
Doug Andersonee5c2742013-03-01 08:57:31 -08001629 if (dev->of_node) {
1630 id = of_alias_get_id(dev->of_node, "i2c");
1631 if (id >= 0) {
1632 adapter->nr = id;
1633 return __i2c_add_numbered_adapter(adapter);
1634 }
1635 }
1636
Jean Delvarecaada322008-01-27 18:14:49 +01001637 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001638 id = idr_alloc(&i2c_adapter_idr, adapter,
1639 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001640 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001641 if (id < 0)
1642 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001643
1644 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001645
David Brownell6e13e642007-05-01 23:26:31 +02001646 return i2c_register_adapter(adapter);
1647}
1648EXPORT_SYMBOL(i2c_add_adapter);
1649
1650/**
1651 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1652 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001653 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001654 *
1655 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001656 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1657 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001658 * is used to properly configure I2C devices.
1659 *
Grant Likely488bf312011-07-25 17:49:43 +02001660 * If the requested bus number is set to -1, then this function will behave
1661 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1662 *
David Brownell6e13e642007-05-01 23:26:31 +02001663 * If no devices have pre-been declared for this bus, then be sure to
1664 * register the adapter before any dynamically allocated ones. Otherwise
1665 * the required bus ID may not be available.
1666 *
1667 * When this returns zero, the specified adapter became available for
1668 * clients using the bus number provided in adap->nr. Also, the table
1669 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1670 * and the appropriate driver model device nodes are created. Otherwise, a
1671 * negative errno value is returned.
1672 */
1673int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1674{
Grant Likely488bf312011-07-25 17:49:43 +02001675 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1676 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001677
Doug Andersonee5c2742013-03-01 08:57:31 -08001678 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001679}
1680EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1681
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001682static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001683 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001684{
Jean Delvare4735c982008-07-14 22:38:36 +02001685 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001686
Jean Delvareacec2112009-03-28 21:34:40 +01001687 /* Remove the devices we created ourselves as the result of hardware
1688 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001689 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1690 if (client->adapter == adapter) {
1691 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1692 client->name, client->addr);
1693 list_del(&client->detected);
1694 i2c_unregister_device(client);
1695 }
1696 }
Jean Delvare026526f2008-01-27 18:14:49 +01001697}
1698
Jean Delvaree549c2b2009-06-19 16:58:19 +02001699static int __unregister_client(struct device *dev, void *dummy)
1700{
1701 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001702 if (client && strcmp(client->name, "dummy"))
1703 i2c_unregister_device(client);
1704 return 0;
1705}
1706
1707static int __unregister_dummy(struct device *dev, void *dummy)
1708{
1709 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001710 if (client)
1711 i2c_unregister_device(client);
1712 return 0;
1713}
1714
Jean Delvare69b00892009-12-06 17:06:27 +01001715static int __process_removed_adapter(struct device_driver *d, void *data)
1716{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001717 i2c_do_del_adapter(to_i2c_driver(d), data);
1718 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001719}
1720
David Brownelld64f73b2007-07-12 14:12:28 +02001721/**
1722 * i2c_del_adapter - unregister I2C adapter
1723 * @adap: the adapter being unregistered
1724 * Context: can sleep
1725 *
1726 * This unregisters an I2C adapter which was previously registered
1727 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1728 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001729void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001731 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001732 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001735 mutex_lock(&core_lock);
1736 found = idr_find(&i2c_adapter_idr, adap->nr);
1737 mutex_unlock(&core_lock);
1738 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001739 pr_debug("i2c-core: attempting to delete unregistered "
1740 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001741 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
Lan Tianyu5d98e612014-05-20 20:59:23 +08001744 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001745 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001746 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001747 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001748 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001749 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001751 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001752 mutex_lock_nested(&adap->userspace_clients_lock,
1753 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001754 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1755 detected) {
1756 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1757 client->addr);
1758 list_del(&client->detected);
1759 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001760 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001761 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001762
Jean Delvaree549c2b2009-06-19 16:58:19 +02001763 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001764 * check the returned value. This is a two-pass process, because
1765 * we can't remove the dummy devices during the first pass: they
1766 * could have been instantiated by real devices wishing to clean
1767 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001768 device_for_each_child(&adap->dev, NULL, __unregister_client);
1769 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jean Delvare2bb50952009-09-18 22:45:46 +02001771#ifdef CONFIG_I2C_COMPAT
1772 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1773 adap->dev.parent);
1774#endif
1775
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001776 /* device name is gone after device_unregister */
1777 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /* clean up the sysfs representation */
1780 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /* wait for sysfs to drop all references */
1784 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
David Brownell6e13e642007-05-01 23:26:31 +02001786 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001787 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001789 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001791 /* Clear the device structure in case this adapter is ever going to be
1792 added again */
1793 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
David Brownellc0564602007-05-01 23:26:31 +02001795EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
David Brownell7b4fbc52007-05-01 23:26:30 +02001797/* ------------------------------------------------------------------------- */
1798
Jean Delvare7ae31482011-03-20 14:50:52 +01001799int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1800{
1801 int res;
1802
1803 mutex_lock(&core_lock);
1804 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1805 mutex_unlock(&core_lock);
1806
1807 return res;
1808}
1809EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1810
Jean Delvare69b00892009-12-06 17:06:27 +01001811static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001812{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001813 if (dev->type != &i2c_adapter_type)
1814 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001815 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001816}
1817
David Brownell7b4fbc52007-05-01 23:26:30 +02001818/*
1819 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001820 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
1822
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001823int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001825 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
David Brownell1d0b19c2008-10-14 17:30:05 +02001827 /* Can't register until after driver model init */
1828 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1829 return -EAGAIN;
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001832 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jean Delvare729d6dd2009-06-19 16:58:18 +02001835 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001836 * will have called probe() for all matching-but-unbound devices.
1837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 res = driver_register(&driver->driver);
1839 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001840 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001841
Mark Brownf4e8db32011-01-14 22:03:50 +01001842 /* Drivers should switch to dev_pm_ops instead. */
1843 if (driver->suspend)
1844 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1845 driver->driver.name);
1846 if (driver->resume)
1847 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1848 driver->driver.name);
1849
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001850 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Jean Delvare4735c982008-07-14 22:38:36 +02001852 INIT_LIST_HEAD(&driver->clients);
1853 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001854 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001855
Jean Delvare7eebcb72006-02-05 23:28:21 +01001856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001858EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jean Delvare69b00892009-12-06 17:06:27 +01001860static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001861{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001862 if (dev->type == &i2c_adapter_type)
1863 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1864 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001865}
1866
David Brownella1d9e6e2007-05-01 23:26:30 +02001867/**
1868 * i2c_del_driver - unregister I2C driver
1869 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001870 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001871 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001872void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Jean Delvare7ae31482011-03-20 14:50:52 +01001874 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001877 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
David Brownellc0564602007-05-01 23:26:31 +02001879EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
David Brownell7b4fbc52007-05-01 23:26:30 +02001881/* ------------------------------------------------------------------------- */
1882
Jean Delvaree48d3312008-01-27 18:14:48 +01001883/**
1884 * i2c_use_client - increments the reference count of the i2c client structure
1885 * @client: the client being referenced
1886 *
1887 * Each live reference to a client should be refcounted. The driver model does
1888 * that automatically as part of driver binding, so that most drivers don't
1889 * need to do this explicitly: they hold a reference until they're unbound
1890 * from the device.
1891 *
1892 * A pointer to the client with the incremented reference counter is returned.
1893 */
1894struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
David Brownell6ea438e2008-07-14 22:38:24 +02001896 if (client && get_device(&client->dev))
1897 return client;
1898 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
David Brownellc0564602007-05-01 23:26:31 +02001900EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Jean Delvaree48d3312008-01-27 18:14:48 +01001902/**
1903 * i2c_release_client - release a use of the i2c client structure
1904 * @client: the client being no longer referenced
1905 *
1906 * Must be called when a user of a client is finished with it.
1907 */
1908void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
David Brownell6ea438e2008-07-14 22:38:24 +02001910 if (client)
1911 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
David Brownellc0564602007-05-01 23:26:31 +02001913EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
David Brownell9b766b82008-01-27 18:14:51 +01001915struct i2c_cmd_arg {
1916 unsigned cmd;
1917 void *arg;
1918};
1919
1920static int i2c_cmd(struct device *dev, void *_arg)
1921{
1922 struct i2c_client *client = i2c_verify_client(dev);
1923 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001924 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001925
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001926 if (!client || !client->dev.driver)
1927 return 0;
1928
1929 driver = to_i2c_driver(client->dev.driver);
1930 if (driver->command)
1931 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001932 return 0;
1933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1936{
David Brownell9b766b82008-01-27 18:14:51 +01001937 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
David Brownell9b766b82008-01-27 18:14:51 +01001939 cmd_arg.cmd = cmd;
1940 cmd_arg.arg = arg;
1941 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
David Brownellc0564602007-05-01 23:26:31 +02001943EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945static int __init i2c_init(void)
1946{
1947 int retval;
1948
1949 retval = bus_register(&i2c_bus_type);
1950 if (retval)
1951 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001952#ifdef CONFIG_I2C_COMPAT
1953 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1954 if (!i2c_adapter_compat_class) {
1955 retval = -ENOMEM;
1956 goto bus_err;
1957 }
1958#endif
David Brownelle9f13732008-01-27 18:14:52 +01001959 retval = i2c_add_driver(&dummy_driver);
1960 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001961 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001962 return 0;
1963
Jean Delvare2bb50952009-09-18 22:45:46 +02001964class_err:
1965#ifdef CONFIG_I2C_COMPAT
1966 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001967bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001968#endif
David Brownelle9f13732008-01-27 18:14:52 +01001969 bus_unregister(&i2c_bus_type);
1970 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static void __exit i2c_exit(void)
1974{
David Brownelle9f13732008-01-27 18:14:52 +01001975 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001976#ifdef CONFIG_I2C_COMPAT
1977 class_compat_unregister(i2c_adapter_compat_class);
1978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001980 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
David Brownella10f9e72008-10-14 17:30:06 +02001983/* We must initialize early, because some subsystems register i2c drivers
1984 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1985 */
1986postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987module_exit(i2c_exit);
1988
1989/* ----------------------------------------------------
1990 * the functional interface to the i2c busses.
1991 * ----------------------------------------------------
1992 */
1993
David Brownella1cdeda2008-07-14 22:38:24 +02001994/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001995 * __i2c_transfer - unlocked flavor of i2c_transfer
1996 * @adap: Handle to I2C bus
1997 * @msgs: One or more messages to execute before STOP is issued to
1998 * terminate the operation; each message begins with a START.
1999 * @num: Number of messages to be executed.
2000 *
2001 * Returns negative errno, else the number of messages executed.
2002 *
2003 * Adapter lock must be held when calling this function. No debug logging
2004 * takes place. adap->algo->master_xfer existence isn't checked.
2005 */
2006int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2007{
2008 unsigned long orig_jiffies;
2009 int ret, try;
2010
David Howellsd9a83d62014-03-06 13:35:59 +00002011 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2012 * enabled. This is an efficient way of keeping the for-loop from
2013 * being executed when not needed.
2014 */
2015 if (static_key_false(&i2c_trace_msg)) {
2016 int i;
2017 for (i = 0; i < num; i++)
2018 if (msgs[i].flags & I2C_M_RD)
2019 trace_i2c_read(adap, &msgs[i], i);
2020 else
2021 trace_i2c_write(adap, &msgs[i], i);
2022 }
2023
Jean Delvareb37d2a32012-06-29 07:47:19 -03002024 /* Retry automatically on arbitration loss */
2025 orig_jiffies = jiffies;
2026 for (ret = 0, try = 0; try <= adap->retries; try++) {
2027 ret = adap->algo->master_xfer(adap, msgs, num);
2028 if (ret != -EAGAIN)
2029 break;
2030 if (time_after(jiffies, orig_jiffies + adap->timeout))
2031 break;
2032 }
2033
David Howellsd9a83d62014-03-06 13:35:59 +00002034 if (static_key_false(&i2c_trace_msg)) {
2035 int i;
2036 for (i = 0; i < ret; i++)
2037 if (msgs[i].flags & I2C_M_RD)
2038 trace_i2c_reply(adap, &msgs[i], i);
2039 trace_i2c_result(adap, i, ret);
2040 }
2041
Jean Delvareb37d2a32012-06-29 07:47:19 -03002042 return ret;
2043}
2044EXPORT_SYMBOL(__i2c_transfer);
2045
2046/**
David Brownella1cdeda2008-07-14 22:38:24 +02002047 * i2c_transfer - execute a single or combined I2C message
2048 * @adap: Handle to I2C bus
2049 * @msgs: One or more messages to execute before STOP is issued to
2050 * terminate the operation; each message begins with a START.
2051 * @num: Number of messages to be executed.
2052 *
2053 * Returns negative errno, else the number of messages executed.
2054 *
2055 * Note that there is no requirement that each message be sent to
2056 * the same slave address, although that is the most common model.
2057 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002058int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002060 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
David Brownella1cdeda2008-07-14 22:38:24 +02002062 /* REVISIT the fault reporting model here is weak:
2063 *
2064 * - When we get an error after receiving N bytes from a slave,
2065 * there is no way to report "N".
2066 *
2067 * - When we get a NAK after transmitting N bytes to a slave,
2068 * there is no way to report "N" ... or to let the master
2069 * continue executing the rest of this combined message, if
2070 * that's the appropriate response.
2071 *
2072 * - When for example "num" is two and we successfully complete
2073 * the first message but get an error part way through the
2074 * second, it's unclear whether that should be reported as
2075 * one (discarding status on the second message) or errno
2076 * (discarding status on the first one).
2077 */
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (adap->algo->master_xfer) {
2080#ifdef DEBUG
2081 for (ret = 0; ret < num; ret++) {
2082 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002083 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2084 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2085 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087#endif
2088
Mike Rapoportcea443a82008-01-27 18:14:50 +01002089 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002090 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002091 if (!ret)
2092 /* I2C activity is ongoing. */
2093 return -EAGAIN;
2094 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002095 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002096 }
2097
Jean Delvareb37d2a32012-06-29 07:47:19 -03002098 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002099 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 return ret;
2102 } else {
2103 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002104 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106}
David Brownellc0564602007-05-01 23:26:31 +02002107EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
David Brownella1cdeda2008-07-14 22:38:24 +02002109/**
2110 * i2c_master_send - issue a single I2C message in master transmit mode
2111 * @client: Handle to slave device
2112 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002113 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002114 *
2115 * Returns negative errno, or else the number of bytes written.
2116 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002117int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002120 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct i2c_msg msg;
2122
Jean Delvare815f55f2005-05-07 22:58:46 +02002123 msg.addr = client->addr;
2124 msg.flags = client->flags & I2C_M_TEN;
2125 msg.len = count;
2126 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002127
Jean Delvare815f55f2005-05-07 22:58:46 +02002128 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002130 /*
2131 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2132 * transmitted, else error code.
2133 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002134 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
David Brownellc0564602007-05-01 23:26:31 +02002136EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
David Brownella1cdeda2008-07-14 22:38:24 +02002138/**
2139 * i2c_master_recv - issue a single I2C message in master receive mode
2140 * @client: Handle to slave device
2141 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002142 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002143 *
2144 * Returns negative errno, or else the number of bytes read.
2145 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002146int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Farid Hammane7225acf2010-05-21 18:40:58 +02002148 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct i2c_msg msg;
2150 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Jean Delvare815f55f2005-05-07 22:58:46 +02002152 msg.addr = client->addr;
2153 msg.flags = client->flags & I2C_M_TEN;
2154 msg.flags |= I2C_M_RD;
2155 msg.len = count;
2156 msg.buf = buf;
2157
2158 ret = i2c_transfer(adap, &msg, 1);
2159
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002160 /*
2161 * If everything went ok (i.e. 1 msg received), return #bytes received,
2162 * else error code.
2163 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002164 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
David Brownellc0564602007-05-01 23:26:31 +02002166EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168/* ----------------------------------------------------
2169 * the i2c address scanning function
2170 * Will not work for 10-bit addresses!
2171 * ----------------------------------------------------
2172 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002173
Jean Delvare63e4e802010-06-03 11:33:51 +02002174/*
2175 * Legacy default probe function, mostly relevant for SMBus. The default
2176 * probe method is a quick write, but it is known to corrupt the 24RF08
2177 * EEPROMs due to a state machine bug, and could also irreversibly
2178 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2179 * we use a short byte read instead. Also, some bus drivers don't implement
2180 * quick write, so we fallback to a byte read in that case too.
2181 * On x86, there is another special case for FSC hardware monitoring chips,
2182 * which want regular byte reads (address 0x73.) Fortunately, these are the
2183 * only known chips using this I2C address on PC hardware.
2184 * Returns 1 if probe succeeded, 0 if not.
2185 */
2186static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2187{
2188 int err;
2189 union i2c_smbus_data dummy;
2190
2191#ifdef CONFIG_X86
2192 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2193 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2194 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2195 I2C_SMBUS_BYTE_DATA, &dummy);
2196 else
2197#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002198 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2199 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002200 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2201 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002202 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2203 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2204 I2C_SMBUS_BYTE, &dummy);
2205 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002206 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2207 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002208 err = -EOPNOTSUPP;
2209 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002210
2211 return err >= 0;
2212}
2213
Jean Delvareccfbbd02009-12-06 17:06:25 +01002214static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002215 struct i2c_driver *driver)
2216{
2217 struct i2c_board_info info;
2218 struct i2c_adapter *adapter = temp_client->adapter;
2219 int addr = temp_client->addr;
2220 int err;
2221
2222 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002223 err = i2c_check_addr_validity(addr);
2224 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002225 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2226 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002227 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002228 }
2229
2230 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002231 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002232 return 0;
2233
Jean Delvareccfbbd02009-12-06 17:06:25 +01002234 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002235 if (!i2c_default_probe(adapter, addr))
2236 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002237
2238 /* Finally call the custom detection function */
2239 memset(&info, 0, sizeof(struct i2c_board_info));
2240 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002241 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002242 if (err) {
2243 /* -ENODEV is returned if the detection fails. We catch it
2244 here as this isn't an error. */
2245 return err == -ENODEV ? 0 : err;
2246 }
2247
2248 /* Consistency check */
2249 if (info.type[0] == '\0') {
2250 dev_err(&adapter->dev, "%s detection function provided "
2251 "no name for 0x%x\n", driver->driver.name,
2252 addr);
2253 } else {
2254 struct i2c_client *client;
2255
2256 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002257 if (adapter->class & I2C_CLASS_DEPRECATED)
2258 dev_warn(&adapter->dev,
2259 "This adapter will soon drop class based instantiation of devices. "
2260 "Please make sure client 0x%02x gets instantiated by other means. "
2261 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2262 info.addr);
2263
Jean Delvare4735c982008-07-14 22:38:36 +02002264 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2265 info.type, info.addr);
2266 client = i2c_new_device(adapter, &info);
2267 if (client)
2268 list_add_tail(&client->detected, &driver->clients);
2269 else
2270 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2271 info.type, info.addr);
2272 }
2273 return 0;
2274}
2275
2276static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2277{
Jean Delvarec3813d62009-12-14 21:17:25 +01002278 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002279 struct i2c_client *temp_client;
2280 int i, err = 0;
2281 int adap_id = i2c_adapter_id(adapter);
2282
Jean Delvarec3813d62009-12-14 21:17:25 +01002283 address_list = driver->address_list;
2284 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002285 return 0;
2286
Wolfram Sang45552272014-07-10 13:46:21 +02002287 /* Warn that the adapter lost class based instantiation */
2288 if (adapter->class == I2C_CLASS_DEPRECATED) {
2289 dev_dbg(&adapter->dev,
2290 "This adapter dropped support for I2C classes and "
2291 "won't auto-detect %s devices anymore. If you need it, check "
2292 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2293 driver->driver.name);
2294 return 0;
2295 }
2296
Jean Delvare51b54ba2010-10-24 18:16:58 +02002297 /* Stop here if the classes do not match */
2298 if (!(adapter->class & driver->class))
2299 return 0;
2300
Jean Delvare4735c982008-07-14 22:38:36 +02002301 /* Set up a temporary client to help detect callback */
2302 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2303 if (!temp_client)
2304 return -ENOMEM;
2305 temp_client->adapter = adapter;
2306
Jean Delvarec3813d62009-12-14 21:17:25 +01002307 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002308 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002309 "addr 0x%02x\n", adap_id, address_list[i]);
2310 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002311 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002312 if (unlikely(err))
2313 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002314 }
2315
Jean Delvare4735c982008-07-14 22:38:36 +02002316 kfree(temp_client);
2317 return err;
2318}
2319
Jean Delvared44f19d2010-08-11 18:20:57 +02002320int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2321{
2322 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2323 I2C_SMBUS_QUICK, NULL) >= 0;
2324}
2325EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2326
Jean Delvare12b5053a2007-05-01 23:26:31 +02002327struct i2c_client *
2328i2c_new_probed_device(struct i2c_adapter *adap,
2329 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002330 unsigned short const *addr_list,
2331 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002332{
2333 int i;
2334
Jean Delvare8031d792010-08-11 18:21:00 +02002335 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002336 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002337
Jean Delvare12b5053a2007-05-01 23:26:31 +02002338 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2339 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002340 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002341 dev_warn(&adap->dev, "Invalid 7-bit address "
2342 "0x%02x\n", addr_list[i]);
2343 continue;
2344 }
2345
2346 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002347 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002348 dev_dbg(&adap->dev, "Address 0x%02x already in "
2349 "use, not probing\n", addr_list[i]);
2350 continue;
2351 }
2352
Jean Delvare63e4e802010-06-03 11:33:51 +02002353 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002354 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002355 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002356 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002357
2358 if (addr_list[i] == I2C_CLIENT_END) {
2359 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2360 return NULL;
2361 }
2362
2363 info->addr = addr_list[i];
2364 return i2c_new_device(adap, info);
2365}
2366EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2367
Jean Delvared735b342011-03-20 14:50:52 +01002368struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002371
Jean Delvarecaada322008-01-27 18:14:49 +01002372 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002373 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002374 if (adapter && !try_module_get(adapter->owner))
2375 adapter = NULL;
2376
Jean Delvarecaada322008-01-27 18:14:49 +01002377 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002378 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
David Brownellc0564602007-05-01 23:26:31 +02002380EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382void i2c_put_adapter(struct i2c_adapter *adap)
2383{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002384 if (adap)
2385 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
David Brownellc0564602007-05-01 23:26:31 +02002387EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389/* The SMBus parts */
2390
David Brownell438d6c22006-12-10 21:21:31 +01002391#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002392static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002395
Farid Hammane7225acf2010-05-21 18:40:58 +02002396 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002397 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 data = data ^ POLY;
2399 data = data << 1;
2400 }
2401 return (u8)(data >> 8);
2402}
2403
Jean Delvare421ef472005-10-26 21:28:55 +02002404/* Incremental CRC8 over count bytes in the array pointed to by p */
2405static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 int i;
2408
Farid Hammane7225acf2010-05-21 18:40:58 +02002409 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002410 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return crc;
2412}
2413
Jean Delvare421ef472005-10-26 21:28:55 +02002414/* Assume a 7-bit address, which is reasonable for SMBus */
2415static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Jean Delvare421ef472005-10-26 21:28:55 +02002417 /* The address will be sent first */
2418 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2419 pec = i2c_smbus_pec(pec, &addr, 1);
2420
2421 /* The data buffer follows */
2422 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Jean Delvare421ef472005-10-26 21:28:55 +02002425/* Used for write only transactions */
2426static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Jean Delvare421ef472005-10-26 21:28:55 +02002428 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2429 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Jean Delvare421ef472005-10-26 21:28:55 +02002432/* Return <0 on CRC error
2433 If there was a write before this read (most cases) we need to take the
2434 partial CRC from the write part into account.
2435 Note that this function does modify the message (we need to decrease the
2436 message length to hide the CRC byte from the caller). */
2437static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Jean Delvare421ef472005-10-26 21:28:55 +02002439 u8 rpec = msg->buf[--msg->len];
2440 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (rpec != cpec) {
2443 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2444 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002445 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
David Brownell438d6c22006-12-10 21:21:31 +01002447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
David Brownella1cdeda2008-07-14 22:38:24 +02002450/**
2451 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2452 * @client: Handle to slave device
2453 *
2454 * This executes the SMBus "receive byte" protocol, returning negative errno
2455 * else the byte received from the device.
2456 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002457s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002460 int status;
2461
2462 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2463 I2C_SMBUS_READ, 0,
2464 I2C_SMBUS_BYTE, &data);
2465 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
David Brownellc0564602007-05-01 23:26:31 +02002467EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
David Brownella1cdeda2008-07-14 22:38:24 +02002469/**
2470 * i2c_smbus_write_byte - SMBus "send byte" protocol
2471 * @client: Handle to slave device
2472 * @value: Byte to be sent
2473 *
2474 * This executes the SMBus "send byte" protocol, returning negative errno
2475 * else zero on success.
2476 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002477s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Farid Hammane7225acf2010-05-21 18:40:58 +02002479 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002480 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
David Brownellc0564602007-05-01 23:26:31 +02002482EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
David Brownella1cdeda2008-07-14 22:38:24 +02002484/**
2485 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2486 * @client: Handle to slave device
2487 * @command: Byte interpreted by slave
2488 *
2489 * This executes the SMBus "read byte" protocol, returning negative errno
2490 * else a data byte received from the device.
2491 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002492s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
2494 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002495 int status;
2496
2497 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2498 I2C_SMBUS_READ, command,
2499 I2C_SMBUS_BYTE_DATA, &data);
2500 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
David Brownellc0564602007-05-01 23:26:31 +02002502EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
David Brownella1cdeda2008-07-14 22:38:24 +02002504/**
2505 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2506 * @client: Handle to slave device
2507 * @command: Byte interpreted by slave
2508 * @value: Byte being written
2509 *
2510 * This executes the SMBus "write byte" protocol, returning negative errno
2511 * else zero on success.
2512 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002513s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2514 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 union i2c_smbus_data data;
2517 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002518 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2519 I2C_SMBUS_WRITE, command,
2520 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
David Brownellc0564602007-05-01 23:26:31 +02002522EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
David Brownella1cdeda2008-07-14 22:38:24 +02002524/**
2525 * i2c_smbus_read_word_data - SMBus "read word" protocol
2526 * @client: Handle to slave device
2527 * @command: Byte interpreted by slave
2528 *
2529 * This executes the SMBus "read word" protocol, returning negative errno
2530 * else a 16-bit unsigned "word" received from the device.
2531 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002532s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002535 int status;
2536
2537 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2538 I2C_SMBUS_READ, command,
2539 I2C_SMBUS_WORD_DATA, &data);
2540 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
David Brownellc0564602007-05-01 23:26:31 +02002542EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
David Brownella1cdeda2008-07-14 22:38:24 +02002544/**
2545 * i2c_smbus_write_word_data - SMBus "write word" protocol
2546 * @client: Handle to slave device
2547 * @command: Byte interpreted by slave
2548 * @value: 16-bit "word" being written
2549 *
2550 * This executes the SMBus "write word" protocol, returning negative errno
2551 * else zero on success.
2552 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002553s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2554 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 union i2c_smbus_data data;
2557 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002558 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2559 I2C_SMBUS_WRITE, command,
2560 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
David Brownellc0564602007-05-01 23:26:31 +02002562EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
David Brownella64ec072007-10-13 23:56:31 +02002564/**
David Brownella1cdeda2008-07-14 22:38:24 +02002565 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002566 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002567 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002568 * @values: Byte array into which data will be read; big enough to hold
2569 * the data returned by the slave. SMBus allows at most 32 bytes.
2570 *
David Brownella1cdeda2008-07-14 22:38:24 +02002571 * This executes the SMBus "block read" protocol, returning negative errno
2572 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002573 *
2574 * Note that using this function requires that the client's adapter support
2575 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2576 * support this; its emulation through I2C messaging relies on a specific
2577 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2578 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002579s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002580 u8 *values)
2581{
2582 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002583 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002584
David Brownell24a5bb72008-07-14 22:38:23 +02002585 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2586 I2C_SMBUS_READ, command,
2587 I2C_SMBUS_BLOCK_DATA, &data);
2588 if (status)
2589 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002590
2591 memcpy(values, &data.block[1], data.block[0]);
2592 return data.block[0];
2593}
2594EXPORT_SYMBOL(i2c_smbus_read_block_data);
2595
David Brownella1cdeda2008-07-14 22:38:24 +02002596/**
2597 * i2c_smbus_write_block_data - SMBus "block write" protocol
2598 * @client: Handle to slave device
2599 * @command: Byte interpreted by slave
2600 * @length: Size of data block; SMBus allows at most 32 bytes
2601 * @values: Byte array which will be written.
2602 *
2603 * This executes the SMBus "block write" protocol, returning negative errno
2604 * else zero on success.
2605 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002606s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002607 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (length > I2C_SMBUS_BLOCK_MAX)
2612 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002614 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002615 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2616 I2C_SMBUS_WRITE, command,
2617 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
David Brownellc0564602007-05-01 23:26:31 +02002619EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002622s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002623 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002626 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002627
Jean Delvare4b2643d2007-07-12 14:12:29 +02002628 if (length > I2C_SMBUS_BLOCK_MAX)
2629 length = I2C_SMBUS_BLOCK_MAX;
2630 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002631 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2632 I2C_SMBUS_READ, command,
2633 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2634 if (status < 0)
2635 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002636
2637 memcpy(values, &data.block[1], data.block[0]);
2638 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
David Brownellc0564602007-05-01 23:26:31 +02002640EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Jean Delvare0cc43a12011-01-10 22:11:23 +01002642s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002643 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002644{
2645 union i2c_smbus_data data;
2646
2647 if (length > I2C_SMBUS_BLOCK_MAX)
2648 length = I2C_SMBUS_BLOCK_MAX;
2649 data.block[0] = length;
2650 memcpy(data.block + 1, values, length);
2651 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2652 I2C_SMBUS_WRITE, command,
2653 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2654}
David Brownellc0564602007-05-01 23:26:31 +02002655EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002656
David Brownell438d6c22006-12-10 21:21:31 +01002657/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002659static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2660 unsigned short flags,
2661 char read_write, u8 command, int size,
2662 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 /* So we need to generate a series of msgs. In the case of writing, we
2665 need to use only one message; when reading, we need two. We initialize
2666 most things with sane defaults, to keep the code below somewhat
2667 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002668 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2669 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002670 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002672 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002673 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002674 struct i2c_msg msg[2] = {
2675 {
2676 .addr = addr,
2677 .flags = flags,
2678 .len = 1,
2679 .buf = msgbuf0,
2680 }, {
2681 .addr = addr,
2682 .flags = flags | I2C_M_RD,
2683 .len = 0,
2684 .buf = msgbuf1,
2685 },
2686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002689 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 case I2C_SMBUS_QUICK:
2691 msg[0].len = 0;
2692 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002693 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2694 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 num = 1;
2696 break;
2697 case I2C_SMBUS_BYTE:
2698 if (read_write == I2C_SMBUS_READ) {
2699 /* Special case: only a read! */
2700 msg[0].flags = I2C_M_RD | flags;
2701 num = 1;
2702 }
2703 break;
2704 case I2C_SMBUS_BYTE_DATA:
2705 if (read_write == I2C_SMBUS_READ)
2706 msg[1].len = 1;
2707 else {
2708 msg[0].len = 2;
2709 msgbuf0[1] = data->byte;
2710 }
2711 break;
2712 case I2C_SMBUS_WORD_DATA:
2713 if (read_write == I2C_SMBUS_READ)
2714 msg[1].len = 2;
2715 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002716 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002718 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720 break;
2721 case I2C_SMBUS_PROC_CALL:
2722 num = 2; /* Special case */
2723 read_write = I2C_SMBUS_READ;
2724 msg[0].len = 3;
2725 msg[1].len = 2;
2726 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002727 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 break;
2729 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002731 msg[1].flags |= I2C_M_RECV_LEN;
2732 msg[1].len = 1; /* block length will be added by
2733 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 } else {
2735 msg[0].len = data->block[0] + 2;
2736 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002737 dev_err(&adapter->dev,
2738 "Invalid block write size %d\n",
2739 data->block[0]);
2740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002742 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 msgbuf0[i] = data->block[i-1];
2744 }
2745 break;
2746 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002747 num = 2; /* Another special case */
2748 read_write = I2C_SMBUS_READ;
2749 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002750 dev_err(&adapter->dev,
2751 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002752 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002753 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002754 }
2755 msg[0].len = data->block[0] + 2;
2756 for (i = 1; i < msg[0].len; i++)
2757 msgbuf0[i] = data->block[i-1];
2758 msg[1].flags |= I2C_M_RECV_LEN;
2759 msg[1].len = 1; /* block length will be added by
2760 the underlying bus driver */
2761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 case I2C_SMBUS_I2C_BLOCK_DATA:
2763 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002764 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 } else {
2766 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002767 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002768 dev_err(&adapter->dev,
2769 "Invalid block write size %d\n",
2770 data->block[0]);
2771 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773 for (i = 1; i <= data->block[0]; i++)
2774 msgbuf0[i] = data->block[i];
2775 }
2776 break;
2777 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002778 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2779 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781
Jean Delvare421ef472005-10-26 21:28:55 +02002782 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2783 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2784 if (i) {
2785 /* Compute PEC if first message is a write */
2786 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002787 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002788 i2c_smbus_add_pec(&msg[0]);
2789 else /* Write followed by read */
2790 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2791 }
2792 /* Ask for PEC if last message is a read */
2793 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002794 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002795 }
2796
David Brownell24a5bb72008-07-14 22:38:23 +02002797 status = i2c_transfer(adapter, msg, num);
2798 if (status < 0)
2799 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Jean Delvare421ef472005-10-26 21:28:55 +02002801 /* Check PEC if last message is a read */
2802 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002803 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2804 if (status < 0)
2805 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002806 }
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002809 switch (size) {
2810 case I2C_SMBUS_BYTE:
2811 data->byte = msgbuf0[0];
2812 break;
2813 case I2C_SMBUS_BYTE_DATA:
2814 data->byte = msgbuf1[0];
2815 break;
2816 case I2C_SMBUS_WORD_DATA:
2817 case I2C_SMBUS_PROC_CALL:
2818 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2819 break;
2820 case I2C_SMBUS_I2C_BLOCK_DATA:
2821 for (i = 0; i < data->block[0]; i++)
2822 data->block[i+1] = msgbuf1[i];
2823 break;
2824 case I2C_SMBUS_BLOCK_DATA:
2825 case I2C_SMBUS_BLOCK_PROC_CALL:
2826 for (i = 0; i < msgbuf1[0] + 1; i++)
2827 data->block[i] = msgbuf1[i];
2828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830 return 0;
2831}
2832
David Brownella1cdeda2008-07-14 22:38:24 +02002833/**
2834 * i2c_smbus_xfer - execute SMBus protocol operations
2835 * @adapter: Handle to I2C bus
2836 * @addr: Address of SMBus slave on that bus
2837 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2838 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2839 * @command: Byte interpreted by slave, for protocols which use such bytes
2840 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2841 * @data: Data to be read or written
2842 *
2843 * This executes an SMBus protocol operation, and returns a negative
2844 * errno code else zero on success.
2845 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002846s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002847 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002848 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002850 unsigned long orig_jiffies;
2851 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
David Howells8a325992014-03-06 13:36:06 +00002854 /* If enabled, the following two tracepoints are conditional on
2855 * read_write and protocol.
2856 */
2857 trace_smbus_write(adapter, addr, flags, read_write,
2858 command, protocol, data);
2859 trace_smbus_read(adapter, addr, flags, read_write,
2860 command, protocol);
2861
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002862 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002865 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002866
2867 /* Retry automatically on arbitration loss */
2868 orig_jiffies = jiffies;
2869 for (res = 0, try = 0; try <= adapter->retries; try++) {
2870 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2871 read_write, command,
2872 protocol, data);
2873 if (res != -EAGAIN)
2874 break;
2875 if (time_after(jiffies,
2876 orig_jiffies + adapter->timeout))
2877 break;
2878 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002879 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002881 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002882 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002883 /*
2884 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2885 * implement native support for the SMBus operation.
2886 */
2887 }
2888
David Howells8a325992014-03-06 13:36:06 +00002889 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2890 command, protocol, data);
2891
2892trace:
2893 /* If enabled, the reply tracepoint is conditional on read_write. */
2894 trace_smbus_reply(adapter, addr, flags, read_write,
2895 command, protocol, data);
2896 trace_smbus_result(adapter, addr, flags, read_write,
2897 command, protocol, res);
2898
2899 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2904MODULE_DESCRIPTION("I2C-Bus main module");
2905MODULE_LICENSE("GPL");