blob: 2f90ac6a7f794ad8e79577a2c5e59ed853bf413f [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 Delvare7c81c602014-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 Rapoportcea443a2008-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>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020053#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010054#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000055#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/uaccess.h>
57
David Brownell9c1600e2007-05-01 23:26:31 +020058#include "i2c-core.h"
59
David Howellsd9a83d62014-03-06 13:35:59 +000060#define CREATE_TRACE_POINTS
61#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jean Delvare6629dcf2010-05-04 11:09:28 +020063/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020064 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000065 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010066static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_IDR(i2c_adapter_idr);
68
Jean Delvare4f8cf822009-09-18 22:45:46 +020069static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020070static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010071
David Howellsd9a83d62014-03-06 13:35:59 +000072static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
73
74void i2c_transfer_trace_reg(void)
75{
76 static_key_slow_inc(&i2c_trace_msg);
77}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020084#if defined(CONFIG_ACPI)
85struct acpi_i2c_handler_data {
86 struct acpi_connection_info info;
87 struct i2c_adapter *adapter;
88};
89
90struct gsb_buffer {
91 u8 status;
92 u8 len;
93 union {
94 u16 wdata;
95 u8 bdata;
96 u8 data[0];
97 };
98} __packed;
99
100static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
101{
102 struct i2c_board_info *info = data;
103
104 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
105 struct acpi_resource_i2c_serialbus *sb;
106
107 sb = &ares->data.i2c_serial_bus;
108 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
109 info->addr = sb->slave_address;
110 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
111 info->flags |= I2C_CLIENT_TEN;
112 }
113 } else if (info->irq < 0) {
114 struct resource r;
115
116 if (acpi_dev_resource_interrupt(ares, 0, &r))
117 info->irq = r.start;
118 }
119
120 /* Tell the ACPI core to skip this resource */
121 return 1;
122}
123
124static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
125 void *data, void **return_value)
126{
127 struct i2c_adapter *adapter = data;
128 struct list_head resource_list;
129 struct i2c_board_info info;
130 struct acpi_device *adev;
131 int ret;
132
133 if (acpi_bus_get_device(handle, &adev))
134 return AE_OK;
135 if (acpi_bus_get_status(adev) || !adev->status.present)
136 return AE_OK;
137
138 memset(&info, 0, sizeof(info));
139 info.acpi_node.companion = adev;
140 info.irq = -1;
141
142 INIT_LIST_HEAD(&resource_list);
143 ret = acpi_dev_get_resources(adev, &resource_list,
144 acpi_i2c_add_resource, &info);
145 acpi_dev_free_resource_list(&resource_list);
146
147 if (ret < 0 || !info.addr)
148 return AE_OK;
149
150 adev->power.flags.ignore_parent = true;
151 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
152 if (!i2c_new_device(adapter, &info)) {
153 adev->power.flags.ignore_parent = false;
154 dev_err(&adapter->dev,
155 "failed to add I2C device %s from ACPI\n",
156 dev_name(&adev->dev));
157 }
158
159 return AE_OK;
160}
161
162/**
163 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
164 * @adap: pointer to adapter
165 *
166 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
167 * namespace. When a device is found it will be added to the Linux device
168 * model and bound to the corresponding ACPI handle.
169 */
170static void acpi_i2c_register_devices(struct i2c_adapter *adap)
171{
172 acpi_handle handle;
173 acpi_status status;
174
175 if (!adap->dev.parent)
176 return;
177
178 handle = ACPI_HANDLE(adap->dev.parent);
179 if (!handle)
180 return;
181
182 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
183 acpi_i2c_add_device, NULL,
184 adap, NULL);
185 if (ACPI_FAILURE(status))
186 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
187}
188
189#else /* CONFIG_ACPI */
190static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
191#endif /* CONFIG_ACPI */
192
193#ifdef CONFIG_ACPI_I2C_OPREGION
194static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
195 u8 cmd, u8 *data, u8 data_len)
196{
197
198 struct i2c_msg msgs[2];
199 int ret;
200 u8 *buffer;
201
202 buffer = kzalloc(data_len, GFP_KERNEL);
203 if (!buffer)
204 return AE_NO_MEMORY;
205
206 msgs[0].addr = client->addr;
207 msgs[0].flags = client->flags;
208 msgs[0].len = 1;
209 msgs[0].buf = &cmd;
210
211 msgs[1].addr = client->addr;
212 msgs[1].flags = client->flags | I2C_M_RD;
213 msgs[1].len = data_len;
214 msgs[1].buf = buffer;
215
216 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
217 if (ret < 0)
218 dev_err(&client->adapter->dev, "i2c read failed\n");
219 else
220 memcpy(data, buffer, data_len);
221
222 kfree(buffer);
223 return ret;
224}
225
226static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
227 u8 cmd, u8 *data, u8 data_len)
228{
229
230 struct i2c_msg msgs[1];
231 u8 *buffer;
232 int ret = AE_OK;
233
234 buffer = kzalloc(data_len + 1, GFP_KERNEL);
235 if (!buffer)
236 return AE_NO_MEMORY;
237
238 buffer[0] = cmd;
239 memcpy(buffer + 1, data, data_len);
240
241 msgs[0].addr = client->addr;
242 msgs[0].flags = client->flags;
243 msgs[0].len = data_len + 1;
244 msgs[0].buf = buffer;
245
246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
247 if (ret < 0)
248 dev_err(&client->adapter->dev, "i2c write failed\n");
249
250 kfree(buffer);
251 return ret;
252}
253
254static acpi_status
255acpi_i2c_space_handler(u32 function, acpi_physical_address command,
256 u32 bits, u64 *value64,
257 void *handler_context, void *region_context)
258{
259 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
260 struct acpi_i2c_handler_data *data = handler_context;
261 struct acpi_connection_info *info = &data->info;
262 struct acpi_resource_i2c_serialbus *sb;
263 struct i2c_adapter *adapter = data->adapter;
264 struct i2c_client client;
265 struct acpi_resource *ares;
266 u32 accessor_type = function >> 16;
267 u8 action = function & ACPI_IO_MASK;
268 acpi_status ret = AE_OK;
269 int status;
270
271 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
272 if (ACPI_FAILURE(ret))
273 return ret;
274
275 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
276 ret = AE_BAD_PARAMETER;
277 goto err;
278 }
279
280 sb = &ares->data.i2c_serial_bus;
281 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
282 ret = AE_BAD_PARAMETER;
283 goto err;
284 }
285
286 memset(&client, 0, sizeof(client));
287 client.adapter = adapter;
288 client.addr = sb->slave_address;
289 client.flags = 0;
290
291 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
292 client.flags |= I2C_CLIENT_TEN;
293
294 switch (accessor_type) {
295 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
296 if (action == ACPI_READ) {
297 status = i2c_smbus_read_byte(&client);
298 if (status >= 0) {
299 gsb->bdata = status;
300 status = 0;
301 }
302 } else {
303 status = i2c_smbus_write_byte(&client, gsb->bdata);
304 }
305 break;
306
307 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
308 if (action == ACPI_READ) {
309 status = i2c_smbus_read_byte_data(&client, command);
310 if (status >= 0) {
311 gsb->bdata = status;
312 status = 0;
313 }
314 } else {
315 status = i2c_smbus_write_byte_data(&client, command,
316 gsb->bdata);
317 }
318 break;
319
320 case ACPI_GSB_ACCESS_ATTRIB_WORD:
321 if (action == ACPI_READ) {
322 status = i2c_smbus_read_word_data(&client, command);
323 if (status >= 0) {
324 gsb->wdata = status;
325 status = 0;
326 }
327 } else {
328 status = i2c_smbus_write_word_data(&client, command,
329 gsb->wdata);
330 }
331 break;
332
333 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
334 if (action == ACPI_READ) {
335 status = i2c_smbus_read_block_data(&client, command,
336 gsb->data);
337 if (status >= 0) {
338 gsb->len = status;
339 status = 0;
340 }
341 } else {
342 status = i2c_smbus_write_block_data(&client, command,
343 gsb->len, gsb->data);
344 }
345 break;
346
347 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
348 if (action == ACPI_READ) {
349 status = acpi_gsb_i2c_read_bytes(&client, command,
350 gsb->data, info->access_length);
351 if (status > 0)
352 status = 0;
353 } else {
354 status = acpi_gsb_i2c_write_bytes(&client, command,
355 gsb->data, info->access_length);
356 }
357 break;
358
359 default:
360 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
361 ret = AE_BAD_PARAMETER;
362 goto err;
363 }
364
365 gsb->status = status;
366
367 err:
368 ACPI_FREE(ares);
369 return ret;
370}
371
372
373static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
374{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200375 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200376 struct acpi_i2c_handler_data *data;
377 acpi_status status;
378
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200379 if (!adapter->dev.parent)
380 return -ENODEV;
381
382 handle = ACPI_HANDLE(adapter->dev.parent);
383
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200384 if (!handle)
385 return -ENODEV;
386
387 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
388 GFP_KERNEL);
389 if (!data)
390 return -ENOMEM;
391
392 data->adapter = adapter;
393 status = acpi_bus_attach_private_data(handle, (void *)data);
394 if (ACPI_FAILURE(status)) {
395 kfree(data);
396 return -ENOMEM;
397 }
398
399 status = acpi_install_address_space_handler(handle,
400 ACPI_ADR_SPACE_GSBUS,
401 &acpi_i2c_space_handler,
402 NULL,
403 data);
404 if (ACPI_FAILURE(status)) {
405 dev_err(&adapter->dev, "Error installing i2c space handler\n");
406 acpi_bus_detach_private_data(handle);
407 kfree(data);
408 return -ENOMEM;
409 }
410
411 return 0;
412}
413
414static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
415{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200416 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200417 struct acpi_i2c_handler_data *data;
418 acpi_status status;
419
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200420 if (!adapter->dev.parent)
421 return;
422
423 handle = ACPI_HANDLE(adapter->dev.parent);
424
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200425 if (!handle)
426 return;
427
428 acpi_remove_address_space_handler(handle,
429 ACPI_ADR_SPACE_GSBUS,
430 &acpi_i2c_space_handler);
431
432 status = acpi_bus_get_private_data(handle, (void **)&data);
433 if (ACPI_SUCCESS(status))
434 kfree(data);
435
436 acpi_bus_detach_private_data(handle);
437}
438#else /* CONFIG_ACPI_I2C_OPREGION */
439static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
440{ }
441
442static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
443{ return 0; }
444#endif /* CONFIG_ACPI_I2C_OPREGION */
445
David Brownellf37dd802007-02-13 22:09:00 +0100446/* ------------------------------------------------------------------------- */
447
Jean Delvared2653e92008-04-29 23:11:39 +0200448static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
449 const struct i2c_client *client)
450{
451 while (id->name[0]) {
452 if (strcmp(client->name, id->name) == 0)
453 return id;
454 id++;
455 }
456 return NULL;
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static int i2c_device_match(struct device *dev, struct device_driver *drv)
460{
Jean Delvare51298d12009-09-18 22:45:45 +0200461 struct i2c_client *client = i2c_verify_client(dev);
462 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200463
Jean Delvare51298d12009-09-18 22:45:45 +0200464 if (!client)
465 return 0;
466
Grant Likely959e85f2010-06-08 07:48:19 -0600467 /* Attempt an OF style match */
468 if (of_driver_match_device(dev, drv))
469 return 1;
470
Mika Westerberg907ddf82012-11-23 12:23:40 +0100471 /* Then ACPI style match */
472 if (acpi_driver_match_device(dev, drv))
473 return 1;
474
Jean Delvare51298d12009-09-18 22:45:45 +0200475 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200476 /* match on an id table if there is one */
477 if (driver->id_table)
478 return i2c_match_id(driver->id_table, client) != NULL;
479
Jean Delvareeb8a7902008-05-18 20:49:41 +0200480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
David Brownell7b4fbc52007-05-01 23:26:30 +0200483
484/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200485static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200486{
487 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800488 int rc;
489
490 rc = acpi_device_uevent_modalias(dev, env);
491 if (rc != -ENODEV)
492 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200493
Jean Delvareeb8a7902008-05-18 20:49:41 +0200494 if (add_uevent_var(env, "MODALIAS=%s%s",
495 I2C_MODULE_PREFIX, client->name))
496 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200497 dev_dbg(dev, "uevent\n");
498 return 0;
499}
500
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530501/* i2c bus recovery routines */
502static int get_scl_gpio_value(struct i2c_adapter *adap)
503{
504 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
505}
506
507static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
508{
509 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
510}
511
512static int get_sda_gpio_value(struct i2c_adapter *adap)
513{
514 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
515}
516
517static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
518{
519 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
520 struct device *dev = &adap->dev;
521 int ret = 0;
522
523 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
524 GPIOF_OUT_INIT_HIGH, "i2c-scl");
525 if (ret) {
526 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
527 return ret;
528 }
529
530 if (bri->get_sda) {
531 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
532 /* work without SDA polling */
533 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
534 bri->sda_gpio);
535 bri->get_sda = NULL;
536 }
537 }
538
539 return ret;
540}
541
542static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
543{
544 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
545
546 if (bri->get_sda)
547 gpio_free(bri->sda_gpio);
548
549 gpio_free(bri->scl_gpio);
550}
551
552/*
553 * We are generating clock pulses. ndelay() determines durating of clk pulses.
554 * We will generate clock with rate 100 KHz and so duration of both clock levels
555 * is: delay in ns = (10^6 / 100) / 2
556 */
557#define RECOVERY_NDELAY 5000
558#define RECOVERY_CLK_CNT 9
559
560static int i2c_generic_recovery(struct i2c_adapter *adap)
561{
562 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
563 int i = 0, val = 1, ret = 0;
564
565 if (bri->prepare_recovery)
566 bri->prepare_recovery(bri);
567
568 /*
569 * By this time SCL is high, as we need to give 9 falling-rising edges
570 */
571 while (i++ < RECOVERY_CLK_CNT * 2) {
572 if (val) {
573 /* Break if SDA is high */
574 if (bri->get_sda && bri->get_sda(adap))
575 break;
576 /* SCL shouldn't be low here */
577 if (!bri->get_scl(adap)) {
578 dev_err(&adap->dev,
579 "SCL is stuck low, exit recovery\n");
580 ret = -EBUSY;
581 break;
582 }
583 }
584
585 val = !val;
586 bri->set_scl(adap, val);
587 ndelay(RECOVERY_NDELAY);
588 }
589
590 if (bri->unprepare_recovery)
591 bri->unprepare_recovery(bri);
592
593 return ret;
594}
595
596int i2c_generic_scl_recovery(struct i2c_adapter *adap)
597{
598 adap->bus_recovery_info->set_scl(adap, 1);
599 return i2c_generic_recovery(adap);
600}
601
602int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
603{
604 int ret;
605
606 ret = i2c_get_gpios_for_recovery(adap);
607 if (ret)
608 return ret;
609
610 ret = i2c_generic_recovery(adap);
611 i2c_put_gpios_for_recovery(adap);
612
613 return ret;
614}
615
616int i2c_recover_bus(struct i2c_adapter *adap)
617{
618 if (!adap->bus_recovery_info)
619 return -EOPNOTSUPP;
620
621 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
622 return adap->bus_recovery_info->recover_bus(adap);
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static int i2c_device_probe(struct device *dev)
626{
Jean Delvare51298d12009-09-18 22:45:45 +0200627 struct i2c_client *client = i2c_verify_client(dev);
628 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100629 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200630
Jean Delvare51298d12009-09-18 22:45:45 +0200631 if (!client)
632 return 0;
633
634 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200635 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200636 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200637
Marc Pignatee354252008-08-28 08:33:22 +0200638 if (!device_can_wakeup(&client->dev))
639 device_init_wakeup(&client->dev,
640 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200641 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200642
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200643 status = of_clk_set_defaults(dev->of_node, false);
644 if (status < 0)
645 return status;
646
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200647 status = dev_pm_domain_attach(&client->dev, true);
648 if (status != -EPROBE_DEFER) {
649 status = driver->probe(client, i2c_match_id(driver->id_table,
650 client));
651 if (status)
652 dev_pm_domain_detach(&client->dev, true);
653 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100654
Hans Verkuil50c33042008-03-12 14:15:00 +0100655 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static int i2c_device_remove(struct device *dev)
659{
Jean Delvare51298d12009-09-18 22:45:45 +0200660 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200661 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100662 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200663
Jean Delvare51298d12009-09-18 22:45:45 +0200664 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200665 return 0;
666
667 driver = to_i2c_driver(dev->driver);
668 if (driver->remove) {
669 dev_dbg(dev, "remove\n");
670 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200671 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100672
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200673 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200674 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
David Brownellf37dd802007-02-13 22:09:00 +0100677static void i2c_device_shutdown(struct device *dev)
678{
Jean Delvare51298d12009-09-18 22:45:45 +0200679 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100680 struct i2c_driver *driver;
681
Jean Delvare51298d12009-09-18 22:45:45 +0200682 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100683 return;
684 driver = to_i2c_driver(dev->driver);
685 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200686 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100687}
688
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200689#ifdef CONFIG_PM_SLEEP
690static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100691{
Jean Delvare51298d12009-09-18 22:45:45 +0200692 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100693 struct i2c_driver *driver;
694
Jean Delvare51298d12009-09-18 22:45:45 +0200695 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100696 return 0;
697 driver = to_i2c_driver(dev->driver);
698 if (!driver->suspend)
699 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200700 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100701}
702
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200703static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100704{
Jean Delvare51298d12009-09-18 22:45:45 +0200705 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100706 struct i2c_driver *driver;
707
Jean Delvare51298d12009-09-18 22:45:45 +0200708 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100709 return 0;
710 driver = to_i2c_driver(dev->driver);
711 if (!driver->resume)
712 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200713 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100714}
715
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200716static int i2c_device_pm_suspend(struct device *dev)
717{
718 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
719
Mark Brownd529de22011-01-14 22:03:49 +0100720 if (pm)
721 return pm_generic_suspend(dev);
722 else
723 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200724}
725
726static int i2c_device_pm_resume(struct device *dev)
727{
728 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200729
730 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100731 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200732 else
Mark Brownd529de22011-01-14 22:03:49 +0100733 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200734}
735
736static int i2c_device_pm_freeze(struct device *dev)
737{
738 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
739
Mark Brownd529de22011-01-14 22:03:49 +0100740 if (pm)
741 return pm_generic_freeze(dev);
742 else
743 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200744}
745
746static int i2c_device_pm_thaw(struct device *dev)
747{
748 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
749
Mark Brownd529de22011-01-14 22:03:49 +0100750 if (pm)
751 return pm_generic_thaw(dev);
752 else
753 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200754}
755
756static int i2c_device_pm_poweroff(struct device *dev)
757{
758 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
759
Mark Brownd529de22011-01-14 22:03:49 +0100760 if (pm)
761 return pm_generic_poweroff(dev);
762 else
763 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200764}
765
766static int i2c_device_pm_restore(struct device *dev)
767{
768 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200769
770 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100771 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200772 else
Mark Brownd529de22011-01-14 22:03:49 +0100773 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200774}
775#else /* !CONFIG_PM_SLEEP */
776#define i2c_device_pm_suspend NULL
777#define i2c_device_pm_resume NULL
778#define i2c_device_pm_freeze NULL
779#define i2c_device_pm_thaw NULL
780#define i2c_device_pm_poweroff NULL
781#define i2c_device_pm_restore NULL
782#endif /* !CONFIG_PM_SLEEP */
783
David Brownell9c1600e2007-05-01 23:26:31 +0200784static void i2c_client_dev_release(struct device *dev)
785{
786 kfree(to_i2c_client(dev));
787}
788
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100789static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200790show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200791{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200792 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
793 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200794}
795
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100796static ssize_t
797show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200798{
799 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800800 int len;
801
802 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
803 if (len != -ENODEV)
804 return len;
805
Jean Delvareeb8a7902008-05-18 20:49:41 +0200806 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200807}
808
Jean Delvare4f8cf822009-09-18 22:45:46 +0200809static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200810static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
811
812static struct attribute *i2c_dev_attrs[] = {
813 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200814 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200815 &dev_attr_modalias.attr,
816 NULL
817};
818
819static struct attribute_group i2c_dev_attr_group = {
820 .attrs = i2c_dev_attrs,
821};
822
823static const struct attribute_group *i2c_dev_attr_groups[] = {
824 &i2c_dev_attr_group,
825 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200826};
827
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100828static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100829 .suspend = i2c_device_pm_suspend,
830 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200831 .freeze = i2c_device_pm_freeze,
832 .thaw = i2c_device_pm_thaw,
833 .poweroff = i2c_device_pm_poweroff,
834 .restore = i2c_device_pm_restore,
835 SET_RUNTIME_PM_OPS(
836 pm_generic_runtime_suspend,
837 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200838 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200839 )
sonic zhang54067ee2009-12-14 21:17:30 +0100840};
841
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200842struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100843 .name = "i2c",
844 .match = i2c_device_match,
845 .probe = i2c_device_probe,
846 .remove = i2c_device_remove,
847 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100848 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000849};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200850EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000851
Jean Delvare51298d12009-09-18 22:45:45 +0200852static struct device_type i2c_client_type = {
853 .groups = i2c_dev_attr_groups,
854 .uevent = i2c_device_uevent,
855 .release = i2c_client_dev_release,
856};
857
David Brownell9b766b82008-01-27 18:14:51 +0100858
859/**
860 * i2c_verify_client - return parameter as i2c_client, or NULL
861 * @dev: device, probably from some driver model iterator
862 *
863 * When traversing the driver model tree, perhaps using driver model
864 * iterators like @device_for_each_child(), you can't assume very much
865 * about the nodes you find. Use this function to avoid oopses caused
866 * by wrongly treating some non-I2C device as an i2c_client.
867 */
868struct i2c_client *i2c_verify_client(struct device *dev)
869{
Jean Delvare51298d12009-09-18 22:45:45 +0200870 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100871 ? to_i2c_client(dev)
872 : NULL;
873}
874EXPORT_SYMBOL(i2c_verify_client);
875
876
Jean Delvare3a89db52010-06-03 11:33:52 +0200877/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300878 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200879static int i2c_check_client_addr_validity(const struct i2c_client *client)
880{
881 if (client->flags & I2C_CLIENT_TEN) {
882 /* 10-bit address, all values are valid */
883 if (client->addr > 0x3ff)
884 return -EINVAL;
885 } else {
886 /* 7-bit address, reject the general call address */
887 if (client->addr == 0x00 || client->addr > 0x7f)
888 return -EINVAL;
889 }
890 return 0;
891}
892
Jean Delvare656b8762010-06-03 11:33:53 +0200893/* And this is a strict address validity check, used when probing. If a
894 * device uses a reserved address, then it shouldn't be probed. 7-bit
895 * addressing is assumed, 10-bit address devices are rare and should be
896 * explicitly enumerated. */
897static int i2c_check_addr_validity(unsigned short addr)
898{
899 /*
900 * Reserved addresses per I2C specification:
901 * 0x00 General call address / START byte
902 * 0x01 CBUS address
903 * 0x02 Reserved for different bus format
904 * 0x03 Reserved for future purposes
905 * 0x04-0x07 Hs-mode master code
906 * 0x78-0x7b 10-bit slave addressing
907 * 0x7c-0x7f Reserved for future purposes
908 */
909 if (addr < 0x08 || addr > 0x77)
910 return -EINVAL;
911 return 0;
912}
913
Jean Delvare3b5f7942010-06-03 11:33:55 +0200914static int __i2c_check_addr_busy(struct device *dev, void *addrp)
915{
916 struct i2c_client *client = i2c_verify_client(dev);
917 int addr = *(int *)addrp;
918
919 if (client && client->addr == addr)
920 return -EBUSY;
921 return 0;
922}
923
Michael Lawnick08263742010-08-11 18:21:02 +0200924/* walk up mux tree */
925static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
926{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200927 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200928 int result;
929
930 result = device_for_each_child(&adapter->dev, &addr,
931 __i2c_check_addr_busy);
932
Jean Delvare97cc4d42010-10-24 18:16:57 +0200933 if (!result && parent)
934 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200935
936 return result;
937}
938
939/* recurse down mux tree */
940static int i2c_check_mux_children(struct device *dev, void *addrp)
941{
942 int result;
943
944 if (dev->type == &i2c_adapter_type)
945 result = device_for_each_child(dev, addrp,
946 i2c_check_mux_children);
947 else
948 result = __i2c_check_addr_busy(dev, addrp);
949
950 return result;
951}
952
Jean Delvare3b5f7942010-06-03 11:33:55 +0200953static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
954{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200955 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200956 int result = 0;
957
Jean Delvare97cc4d42010-10-24 18:16:57 +0200958 if (parent)
959 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200960
961 if (!result)
962 result = device_for_each_child(&adapter->dev, &addr,
963 i2c_check_mux_children);
964
965 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200966}
967
David Brownell9c1600e2007-05-01 23:26:31 +0200968/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200969 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
970 * @adapter: Target I2C bus segment
971 */
972void i2c_lock_adapter(struct i2c_adapter *adapter)
973{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200974 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
975
976 if (parent)
977 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200978 else
979 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200980}
981EXPORT_SYMBOL_GPL(i2c_lock_adapter);
982
983/**
984 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
985 * @adapter: Target I2C bus segment
986 */
987static int i2c_trylock_adapter(struct i2c_adapter *adapter)
988{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200989 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
990
991 if (parent)
992 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200993 else
994 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200995}
996
997/**
998 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
999 * @adapter: Target I2C bus segment
1000 */
1001void i2c_unlock_adapter(struct i2c_adapter *adapter)
1002{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001003 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1004
1005 if (parent)
1006 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001007 else
1008 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001009}
1010EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1011
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001012static void i2c_dev_set_name(struct i2c_adapter *adap,
1013 struct i2c_client *client)
1014{
1015 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1016
1017 if (adev) {
1018 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1019 return;
1020 }
1021
1022 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1023 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1024 client->addr | ((client->flags & I2C_CLIENT_TEN)
1025 ? 0xa000 : 0));
1026}
1027
Jean Delvarefe61e072010-08-11 18:20:58 +02001028/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001029 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001030 * @adap: the adapter managing the device
1031 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001032 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001033 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001034 * Create an i2c device. Binding is handled through driver model
1035 * probe()/remove() methods. A driver may be bound to this device when we
1036 * return from this function, or any later moment (e.g. maybe hotplugging will
1037 * load the driver module). This call is not appropriate for use by mainboard
1038 * initialization logic, which usually runs during an arch_initcall() long
1039 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001040 *
1041 * This returns the new i2c client, which may be saved for later use with
1042 * i2c_unregister_device(); or NULL to indicate an error.
1043 */
1044struct i2c_client *
1045i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1046{
1047 struct i2c_client *client;
1048 int status;
1049
1050 client = kzalloc(sizeof *client, GFP_KERNEL);
1051 if (!client)
1052 return NULL;
1053
1054 client->adapter = adap;
1055
1056 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001057
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001058 if (info->archdata)
1059 client->dev.archdata = *info->archdata;
1060
Marc Pignatee354252008-08-28 08:33:22 +02001061 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001062 client->addr = info->addr;
1063 client->irq = info->irq;
1064
David Brownell9c1600e2007-05-01 23:26:31 +02001065 strlcpy(client->name, info->type, sizeof(client->name));
1066
Jean Delvare3a89db52010-06-03 11:33:52 +02001067 /* Check for address validity */
1068 status = i2c_check_client_addr_validity(client);
1069 if (status) {
1070 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1071 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1072 goto out_err_silent;
1073 }
1074
Jean Delvaref8a227e2009-06-19 16:58:18 +02001075 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001076 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001077 if (status)
1078 goto out_err;
1079
1080 client->dev.parent = &client->adapter->dev;
1081 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001082 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001083 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001084 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001085
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001086 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001087 status = device_register(&client->dev);
1088 if (status)
1089 goto out_err;
1090
Jean Delvaref8a227e2009-06-19 16:58:18 +02001091 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1092 client->name, dev_name(&client->dev));
1093
David Brownell9c1600e2007-05-01 23:26:31 +02001094 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001095
1096out_err:
1097 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1098 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001099out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001100 kfree(client);
1101 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001102}
1103EXPORT_SYMBOL_GPL(i2c_new_device);
1104
1105
1106/**
1107 * i2c_unregister_device - reverse effect of i2c_new_device()
1108 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001109 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001110 */
1111void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001112{
David Brownella1d9e6e2007-05-01 23:26:30 +02001113 device_unregister(&client->dev);
1114}
David Brownell9c1600e2007-05-01 23:26:31 +02001115EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001116
1117
Jean Delvare60b129d2008-05-11 20:37:06 +02001118static const struct i2c_device_id dummy_id[] = {
1119 { "dummy", 0 },
1120 { },
1121};
1122
Jean Delvared2653e92008-04-29 23:11:39 +02001123static int dummy_probe(struct i2c_client *client,
1124 const struct i2c_device_id *id)
1125{
1126 return 0;
1127}
1128
1129static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001130{
1131 return 0;
1132}
1133
1134static struct i2c_driver dummy_driver = {
1135 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001136 .probe = dummy_probe,
1137 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001138 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001139};
1140
1141/**
1142 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1143 * @adapter: the adapter managing the device
1144 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001145 * Context: can sleep
1146 *
1147 * This returns an I2C client bound to the "dummy" driver, intended for use
1148 * with devices that consume multiple addresses. Examples of such chips
1149 * include various EEPROMS (like 24c04 and 24c08 models).
1150 *
1151 * These dummy devices have two main uses. First, most I2C and SMBus calls
1152 * except i2c_transfer() need a client handle; the dummy will be that handle.
1153 * And second, this prevents the specified address from being bound to a
1154 * different driver.
1155 *
1156 * This returns the new i2c client, which should be saved for later use with
1157 * i2c_unregister_device(); or NULL to indicate an error.
1158 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001159struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001160{
1161 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001162 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001163 };
1164
David Brownelle9f13732008-01-27 18:14:52 +01001165 return i2c_new_device(adapter, &info);
1166}
1167EXPORT_SYMBOL_GPL(i2c_new_dummy);
1168
David Brownellf37dd802007-02-13 22:09:00 +01001169/* ------------------------------------------------------------------------- */
1170
David Brownell16ffadf2007-05-01 23:26:28 +02001171/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1172
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001173static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
David Brownellef2c83212007-05-01 23:26:28 +02001175 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 complete(&adap->dev_released);
1177}
1178
Jean Delvare99cd8e22009-06-19 16:58:20 +02001179/*
Jean Delvare390946b2012-09-10 10:14:02 +02001180 * This function is only needed for mutex_lock_nested, so it is never
1181 * called unless locking correctness checking is enabled. Thus we
1182 * make it inline to avoid a compiler warning. That's what gcc ends up
1183 * doing anyway.
1184 */
1185static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1186{
1187 unsigned int depth = 0;
1188
1189 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1190 depth++;
1191
1192 return depth;
1193}
1194
1195/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001196 * Let users instantiate I2C devices through sysfs. This can be used when
1197 * platform initialization code doesn't contain the proper data for
1198 * whatever reason. Also useful for drivers that do device detection and
1199 * detection fails, either because the device uses an unexpected address,
1200 * or this is a compatible device with different ID register values.
1201 *
1202 * Parameter checking may look overzealous, but we really don't want
1203 * the user to provide incorrect parameters.
1204 */
1205static ssize_t
1206i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1207 const char *buf, size_t count)
1208{
1209 struct i2c_adapter *adap = to_i2c_adapter(dev);
1210 struct i2c_board_info info;
1211 struct i2c_client *client;
1212 char *blank, end;
1213 int res;
1214
Jean Delvare99cd8e22009-06-19 16:58:20 +02001215 memset(&info, 0, sizeof(struct i2c_board_info));
1216
1217 blank = strchr(buf, ' ');
1218 if (!blank) {
1219 dev_err(dev, "%s: Missing parameters\n", "new_device");
1220 return -EINVAL;
1221 }
1222 if (blank - buf > I2C_NAME_SIZE - 1) {
1223 dev_err(dev, "%s: Invalid device name\n", "new_device");
1224 return -EINVAL;
1225 }
1226 memcpy(info.type, buf, blank - buf);
1227
1228 /* Parse remaining parameters, reject extra parameters */
1229 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1230 if (res < 1) {
1231 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1232 return -EINVAL;
1233 }
1234 if (res > 1 && end != '\n') {
1235 dev_err(dev, "%s: Extra parameters\n", "new_device");
1236 return -EINVAL;
1237 }
1238
Jean Delvare99cd8e22009-06-19 16:58:20 +02001239 client = i2c_new_device(adap, &info);
1240 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001241 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001242
1243 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001244 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001245 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001246 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001247 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1248 info.type, info.addr);
1249
1250 return count;
1251}
1252
1253/*
1254 * And of course let the users delete the devices they instantiated, if
1255 * they got it wrong. This interface can only be used to delete devices
1256 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1257 * don't delete devices to which some kernel code still has references.
1258 *
1259 * Parameter checking may look overzealous, but we really don't want
1260 * the user to delete the wrong device.
1261 */
1262static ssize_t
1263i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1264 const char *buf, size_t count)
1265{
1266 struct i2c_adapter *adap = to_i2c_adapter(dev);
1267 struct i2c_client *client, *next;
1268 unsigned short addr;
1269 char end;
1270 int res;
1271
1272 /* Parse parameters, reject extra parameters */
1273 res = sscanf(buf, "%hi%c", &addr, &end);
1274 if (res < 1) {
1275 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1276 return -EINVAL;
1277 }
1278 if (res > 1 && end != '\n') {
1279 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1280 return -EINVAL;
1281 }
1282
1283 /* Make sure the device was added through sysfs */
1284 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001285 mutex_lock_nested(&adap->userspace_clients_lock,
1286 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001287 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1288 detected) {
1289 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001290 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1291 "delete_device", client->name, client->addr);
1292
1293 list_del(&client->detected);
1294 i2c_unregister_device(client);
1295 res = count;
1296 break;
1297 }
1298 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001299 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001300
1301 if (res < 0)
1302 dev_err(dev, "%s: Can't find device in list\n",
1303 "delete_device");
1304 return res;
1305}
1306
Jean Delvare4f8cf822009-09-18 22:45:46 +02001307static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001308static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1309 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001310
1311static struct attribute *i2c_adapter_attrs[] = {
1312 &dev_attr_name.attr,
1313 &dev_attr_new_device.attr,
1314 &dev_attr_delete_device.attr,
1315 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001316};
1317
Jean Delvare4f8cf822009-09-18 22:45:46 +02001318static struct attribute_group i2c_adapter_attr_group = {
1319 .attrs = i2c_adapter_attrs,
1320};
1321
1322static const struct attribute_group *i2c_adapter_attr_groups[] = {
1323 &i2c_adapter_attr_group,
1324 NULL
1325};
1326
Michael Lawnick08263742010-08-11 18:21:02 +02001327struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001328 .groups = i2c_adapter_attr_groups,
1329 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001330};
Michael Lawnick08263742010-08-11 18:21:02 +02001331EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Stephen Warren643dd092012-04-17 12:43:33 -06001333/**
1334 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1335 * @dev: device, probably from some driver model iterator
1336 *
1337 * When traversing the driver model tree, perhaps using driver model
1338 * iterators like @device_for_each_child(), you can't assume very much
1339 * about the nodes you find. Use this function to avoid oopses caused
1340 * by wrongly treating some non-I2C device as an i2c_adapter.
1341 */
1342struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1343{
1344 return (dev->type == &i2c_adapter_type)
1345 ? to_i2c_adapter(dev)
1346 : NULL;
1347}
1348EXPORT_SYMBOL(i2c_verify_adapter);
1349
Jean Delvare2bb50952009-09-18 22:45:46 +02001350#ifdef CONFIG_I2C_COMPAT
1351static struct class_compat *i2c_adapter_compat_class;
1352#endif
1353
David Brownell9c1600e2007-05-01 23:26:31 +02001354static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1355{
1356 struct i2c_devinfo *devinfo;
1357
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001358 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001359 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1360 if (devinfo->busnum == adapter->nr
1361 && !i2c_new_device(adapter,
1362 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001363 dev_err(&adapter->dev,
1364 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001365 devinfo->board_info.addr);
1366 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001367 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001368}
1369
Wolfram Sang687b81d2013-07-11 12:56:15 +01001370/* OF support code */
1371
1372#if IS_ENABLED(CONFIG_OF)
1373static void of_i2c_register_devices(struct i2c_adapter *adap)
1374{
1375 void *result;
1376 struct device_node *node;
1377
1378 /* Only register child devices if the adapter has a node pointer set */
1379 if (!adap->dev.of_node)
1380 return;
1381
1382 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1383
1384 for_each_available_child_of_node(adap->dev.of_node, node) {
1385 struct i2c_board_info info = {};
1386 struct dev_archdata dev_ad = {};
1387 const __be32 *addr;
1388 int len;
1389
1390 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1391
1392 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1393 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1394 node->full_name);
1395 continue;
1396 }
1397
1398 addr = of_get_property(node, "reg", &len);
1399 if (!addr || (len < sizeof(int))) {
1400 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1401 node->full_name);
1402 continue;
1403 }
1404
1405 info.addr = be32_to_cpup(addr);
1406 if (info.addr > (1 << 10) - 1) {
1407 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1408 info.addr, node->full_name);
1409 continue;
1410 }
1411
1412 info.irq = irq_of_parse_and_map(node, 0);
1413 info.of_node = of_node_get(node);
1414 info.archdata = &dev_ad;
1415
1416 if (of_get_property(node, "wakeup-source", NULL))
1417 info.flags |= I2C_CLIENT_WAKE;
1418
1419 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1420
1421 result = i2c_new_device(adap, &info);
1422 if (result == NULL) {
1423 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1424 node->full_name);
1425 of_node_put(node);
1426 irq_dispose_mapping(info.irq);
1427 continue;
1428 }
1429 }
1430}
1431
1432static int of_dev_node_match(struct device *dev, void *data)
1433{
1434 return dev->of_node == data;
1435}
1436
1437/* must call put_device() when done with returned i2c_client device */
1438struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1439{
1440 struct device *dev;
1441
1442 dev = bus_find_device(&i2c_bus_type, NULL, node,
1443 of_dev_node_match);
1444 if (!dev)
1445 return NULL;
1446
1447 return i2c_verify_client(dev);
1448}
1449EXPORT_SYMBOL(of_find_i2c_device_by_node);
1450
1451/* must call put_device() when done with returned i2c_adapter device */
1452struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1453{
1454 struct device *dev;
1455
1456 dev = bus_find_device(&i2c_bus_type, NULL, node,
1457 of_dev_node_match);
1458 if (!dev)
1459 return NULL;
1460
1461 return i2c_verify_adapter(dev);
1462}
1463EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1464#else
1465static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1466#endif /* CONFIG_OF */
1467
Jean Delvare69b00892009-12-06 17:06:27 +01001468static int i2c_do_add_adapter(struct i2c_driver *driver,
1469 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001470{
Jean Delvare4735c982008-07-14 22:38:36 +02001471 /* Detect supported devices on that bus, and instantiate them */
1472 i2c_detect(adap, driver);
1473
1474 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001475 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001476 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1477 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001478 dev_warn(&adap->dev, "Please use another way to instantiate "
1479 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001480 /* We ignore the return code; if it fails, too bad */
1481 driver->attach_adapter(adap);
1482 }
1483 return 0;
1484}
1485
Jean Delvare69b00892009-12-06 17:06:27 +01001486static int __process_new_adapter(struct device_driver *d, void *data)
1487{
1488 return i2c_do_add_adapter(to_i2c_driver(d), data);
1489}
1490
David Brownell6e13e642007-05-01 23:26:31 +02001491static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Jean Delvared6703282010-08-11 18:20:59 +02001493 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
David Brownell1d0b19c2008-10-14 17:30:05 +02001495 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001496 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1497 res = -EAGAIN;
1498 goto out_list;
1499 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001500
Jean Delvare2236baa2010-11-15 22:40:38 +01001501 /* Sanity checks */
1502 if (unlikely(adap->name[0] == '\0')) {
1503 pr_err("i2c-core: Attempt to register an adapter with "
1504 "no name!\n");
1505 return -EINVAL;
1506 }
1507 if (unlikely(!adap->algo)) {
1508 pr_err("i2c-core: Attempt to register adapter '%s' with "
1509 "no algo!\n", adap->name);
1510 return -EINVAL;
1511 }
1512
Mika Kuoppala194684e2009-12-06 17:06:22 +01001513 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001514 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001515 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Jean Delvare8fcfef62009-03-28 21:34:43 +01001517 /* Set default timeout to 1 second if not already set */
1518 if (adap->timeout == 0)
1519 adap->timeout = HZ;
1520
Kay Sievers27d9c182009-01-07 14:29:16 +01001521 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001522 adap->dev.bus = &i2c_bus_type;
1523 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001524 res = device_register(&adap->dev);
1525 if (res)
1526 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001528 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1529
Jean Delvare2bb50952009-09-18 22:45:46 +02001530#ifdef CONFIG_I2C_COMPAT
1531 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1532 adap->dev.parent);
1533 if (res)
1534 dev_warn(&adap->dev,
1535 "Failed to create compatibility class link\n");
1536#endif
1537
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301538 /* bus recovery specific initialization */
1539 if (adap->bus_recovery_info) {
1540 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1541
1542 if (!bri->recover_bus) {
1543 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1544 adap->bus_recovery_info = NULL;
1545 goto exit_recovery;
1546 }
1547
1548 /* Generic GPIO recovery */
1549 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1550 if (!gpio_is_valid(bri->scl_gpio)) {
1551 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1552 adap->bus_recovery_info = NULL;
1553 goto exit_recovery;
1554 }
1555
1556 if (gpio_is_valid(bri->sda_gpio))
1557 bri->get_sda = get_sda_gpio_value;
1558 else
1559 bri->get_sda = NULL;
1560
1561 bri->get_scl = get_scl_gpio_value;
1562 bri->set_scl = set_scl_gpio_value;
1563 } else if (!bri->set_scl || !bri->get_scl) {
1564 /* Generic SCL recovery */
1565 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1566 adap->bus_recovery_info = NULL;
1567 }
1568 }
1569
1570exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001571 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001572 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001573 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001574 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001575
David Brownell6e13e642007-05-01 23:26:31 +02001576 if (adap->nr < __i2c_first_dynamic_bus_num)
1577 i2c_scan_static_board_info(adap);
1578
Jean Delvare4735c982008-07-14 22:38:36 +02001579 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001580 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001581 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001582 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001583
1584 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001585
Jean Delvareb119c6c2006-08-15 18:26:30 +02001586out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001587 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001588 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001589 mutex_unlock(&core_lock);
1590 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
David Brownell6e13e642007-05-01 23:26:31 +02001593/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001594 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1595 * @adap: the adapter to register (with adap->nr initialized)
1596 * Context: can sleep
1597 *
1598 * See i2c_add_numbered_adapter() for details.
1599 */
1600static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1601{
1602 int id;
1603
1604 mutex_lock(&core_lock);
1605 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1606 GFP_KERNEL);
1607 mutex_unlock(&core_lock);
1608 if (id < 0)
1609 return id == -ENOSPC ? -EBUSY : id;
1610
1611 return i2c_register_adapter(adap);
1612}
1613
1614/**
David Brownell6e13e642007-05-01 23:26:31 +02001615 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1616 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001617 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001618 *
1619 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001620 * doesn't matter or when its bus number is specified by an dt alias.
1621 * Examples of bases when the bus number doesn't matter: I2C adapters
1622 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001623 *
1624 * When this returns zero, a new bus number was allocated and stored
1625 * in adap->nr, and the specified adapter became available for clients.
1626 * Otherwise, a negative errno value is returned.
1627 */
1628int i2c_add_adapter(struct i2c_adapter *adapter)
1629{
Doug Andersonee5c2742013-03-01 08:57:31 -08001630 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001631 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001632
Doug Andersonee5c2742013-03-01 08:57:31 -08001633 if (dev->of_node) {
1634 id = of_alias_get_id(dev->of_node, "i2c");
1635 if (id >= 0) {
1636 adapter->nr = id;
1637 return __i2c_add_numbered_adapter(adapter);
1638 }
1639 }
1640
Jean Delvarecaada322008-01-27 18:14:49 +01001641 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001642 id = idr_alloc(&i2c_adapter_idr, adapter,
1643 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001644 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001645 if (id < 0)
1646 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001647
1648 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001649
David Brownell6e13e642007-05-01 23:26:31 +02001650 return i2c_register_adapter(adapter);
1651}
1652EXPORT_SYMBOL(i2c_add_adapter);
1653
1654/**
1655 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1656 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001657 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001658 *
1659 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001660 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1661 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001662 * is used to properly configure I2C devices.
1663 *
Grant Likely488bf312011-07-25 17:49:43 +02001664 * If the requested bus number is set to -1, then this function will behave
1665 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1666 *
David Brownell6e13e642007-05-01 23:26:31 +02001667 * If no devices have pre-been declared for this bus, then be sure to
1668 * register the adapter before any dynamically allocated ones. Otherwise
1669 * the required bus ID may not be available.
1670 *
1671 * When this returns zero, the specified adapter became available for
1672 * clients using the bus number provided in adap->nr. Also, the table
1673 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1674 * and the appropriate driver model device nodes are created. Otherwise, a
1675 * negative errno value is returned.
1676 */
1677int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1678{
Grant Likely488bf312011-07-25 17:49:43 +02001679 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1680 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001681
Doug Andersonee5c2742013-03-01 08:57:31 -08001682 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001683}
1684EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1685
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001686static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001687 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001688{
Jean Delvare4735c982008-07-14 22:38:36 +02001689 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001690
Jean Delvareacec2112009-03-28 21:34:40 +01001691 /* Remove the devices we created ourselves as the result of hardware
1692 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001693 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1694 if (client->adapter == adapter) {
1695 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1696 client->name, client->addr);
1697 list_del(&client->detected);
1698 i2c_unregister_device(client);
1699 }
1700 }
Jean Delvare026526f2008-01-27 18:14:49 +01001701}
1702
Jean Delvaree549c2b2009-06-19 16:58:19 +02001703static int __unregister_client(struct device *dev, void *dummy)
1704{
1705 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001706 if (client && strcmp(client->name, "dummy"))
1707 i2c_unregister_device(client);
1708 return 0;
1709}
1710
1711static int __unregister_dummy(struct device *dev, void *dummy)
1712{
1713 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001714 if (client)
1715 i2c_unregister_device(client);
1716 return 0;
1717}
1718
Jean Delvare69b00892009-12-06 17:06:27 +01001719static int __process_removed_adapter(struct device_driver *d, void *data)
1720{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001721 i2c_do_del_adapter(to_i2c_driver(d), data);
1722 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001723}
1724
David Brownelld64f73b2007-07-12 14:12:28 +02001725/**
1726 * i2c_del_adapter - unregister I2C adapter
1727 * @adap: the adapter being unregistered
1728 * Context: can sleep
1729 *
1730 * This unregisters an I2C adapter which was previously registered
1731 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1732 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001733void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001735 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001736 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001739 mutex_lock(&core_lock);
1740 found = idr_find(&i2c_adapter_idr, adap->nr);
1741 mutex_unlock(&core_lock);
1742 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001743 pr_debug("i2c-core: attempting to delete unregistered "
1744 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001745 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Lan Tianyu5d98e612014-05-20 20:59:23 +08001748 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001749 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001750 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001751 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001752 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001753 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001755 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001756 mutex_lock_nested(&adap->userspace_clients_lock,
1757 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001758 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1759 detected) {
1760 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1761 client->addr);
1762 list_del(&client->detected);
1763 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001764 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001765 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001766
Jean Delvaree549c2b2009-06-19 16:58:19 +02001767 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001768 * check the returned value. This is a two-pass process, because
1769 * we can't remove the dummy devices during the first pass: they
1770 * could have been instantiated by real devices wishing to clean
1771 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001772 device_for_each_child(&adap->dev, NULL, __unregister_client);
1773 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Jean Delvare2bb50952009-09-18 22:45:46 +02001775#ifdef CONFIG_I2C_COMPAT
1776 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1777 adap->dev.parent);
1778#endif
1779
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001780 /* device name is gone after device_unregister */
1781 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* clean up the sysfs representation */
1784 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /* wait for sysfs to drop all references */
1788 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
David Brownell6e13e642007-05-01 23:26:31 +02001790 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001791 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001793 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001795 /* Clear the device structure in case this adapter is ever going to be
1796 added again */
1797 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
David Brownellc0564602007-05-01 23:26:31 +02001799EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
David Brownell7b4fbc52007-05-01 23:26:30 +02001801/* ------------------------------------------------------------------------- */
1802
Jean Delvare7ae31482011-03-20 14:50:52 +01001803int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1804{
1805 int res;
1806
1807 mutex_lock(&core_lock);
1808 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1809 mutex_unlock(&core_lock);
1810
1811 return res;
1812}
1813EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1814
Jean Delvare69b00892009-12-06 17:06:27 +01001815static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001816{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001817 if (dev->type != &i2c_adapter_type)
1818 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001819 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001820}
1821
David Brownell7b4fbc52007-05-01 23:26:30 +02001822/*
1823 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001824 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
1826
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001827int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001829 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
David Brownell1d0b19c2008-10-14 17:30:05 +02001831 /* Can't register until after driver model init */
1832 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1833 return -EAGAIN;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001836 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Jean Delvare729d6dd2009-06-19 16:58:18 +02001839 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001840 * will have called probe() for all matching-but-unbound devices.
1841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 res = driver_register(&driver->driver);
1843 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001844 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001845
Mark Brownf4e8db32011-01-14 22:03:50 +01001846 /* Drivers should switch to dev_pm_ops instead. */
1847 if (driver->suspend)
1848 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1849 driver->driver.name);
1850 if (driver->resume)
1851 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1852 driver->driver.name);
1853
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001854 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jean Delvare4735c982008-07-14 22:38:36 +02001856 INIT_LIST_HEAD(&driver->clients);
1857 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001858 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001859
Jean Delvare7eebcb72006-02-05 23:28:21 +01001860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001862EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Jean Delvare69b00892009-12-06 17:06:27 +01001864static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001865{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001866 if (dev->type == &i2c_adapter_type)
1867 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1868 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001869}
1870
David Brownella1d9e6e2007-05-01 23:26:30 +02001871/**
1872 * i2c_del_driver - unregister I2C driver
1873 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001874 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001875 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001876void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Jean Delvare7ae31482011-03-20 14:50:52 +01001878 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001881 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
David Brownellc0564602007-05-01 23:26:31 +02001883EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
David Brownell7b4fbc52007-05-01 23:26:30 +02001885/* ------------------------------------------------------------------------- */
1886
Jean Delvaree48d3312008-01-27 18:14:48 +01001887/**
1888 * i2c_use_client - increments the reference count of the i2c client structure
1889 * @client: the client being referenced
1890 *
1891 * Each live reference to a client should be refcounted. The driver model does
1892 * that automatically as part of driver binding, so that most drivers don't
1893 * need to do this explicitly: they hold a reference until they're unbound
1894 * from the device.
1895 *
1896 * A pointer to the client with the incremented reference counter is returned.
1897 */
1898struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
David Brownell6ea438e2008-07-14 22:38:24 +02001900 if (client && get_device(&client->dev))
1901 return client;
1902 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
David Brownellc0564602007-05-01 23:26:31 +02001904EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jean Delvaree48d3312008-01-27 18:14:48 +01001906/**
1907 * i2c_release_client - release a use of the i2c client structure
1908 * @client: the client being no longer referenced
1909 *
1910 * Must be called when a user of a client is finished with it.
1911 */
1912void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
David Brownell6ea438e2008-07-14 22:38:24 +02001914 if (client)
1915 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
David Brownellc0564602007-05-01 23:26:31 +02001917EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
David Brownell9b766b82008-01-27 18:14:51 +01001919struct i2c_cmd_arg {
1920 unsigned cmd;
1921 void *arg;
1922};
1923
1924static int i2c_cmd(struct device *dev, void *_arg)
1925{
1926 struct i2c_client *client = i2c_verify_client(dev);
1927 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001928 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001929
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001930 if (!client || !client->dev.driver)
1931 return 0;
1932
1933 driver = to_i2c_driver(client->dev.driver);
1934 if (driver->command)
1935 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001936 return 0;
1937}
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1940{
David Brownell9b766b82008-01-27 18:14:51 +01001941 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
David Brownell9b766b82008-01-27 18:14:51 +01001943 cmd_arg.cmd = cmd;
1944 cmd_arg.arg = arg;
1945 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
David Brownellc0564602007-05-01 23:26:31 +02001947EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949static int __init i2c_init(void)
1950{
1951 int retval;
1952
1953 retval = bus_register(&i2c_bus_type);
1954 if (retval)
1955 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001956#ifdef CONFIG_I2C_COMPAT
1957 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1958 if (!i2c_adapter_compat_class) {
1959 retval = -ENOMEM;
1960 goto bus_err;
1961 }
1962#endif
David Brownelle9f13732008-01-27 18:14:52 +01001963 retval = i2c_add_driver(&dummy_driver);
1964 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001965 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001966 return 0;
1967
Jean Delvare2bb50952009-09-18 22:45:46 +02001968class_err:
1969#ifdef CONFIG_I2C_COMPAT
1970 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001971bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001972#endif
David Brownelle9f13732008-01-27 18:14:52 +01001973 bus_unregister(&i2c_bus_type);
1974 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977static void __exit i2c_exit(void)
1978{
David Brownelle9f13732008-01-27 18:14:52 +01001979 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001980#ifdef CONFIG_I2C_COMPAT
1981 class_compat_unregister(i2c_adapter_compat_class);
1982#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001984 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
David Brownella10f9e72008-10-14 17:30:06 +02001987/* We must initialize early, because some subsystems register i2c drivers
1988 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1989 */
1990postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991module_exit(i2c_exit);
1992
1993/* ----------------------------------------------------
1994 * the functional interface to the i2c busses.
1995 * ----------------------------------------------------
1996 */
1997
David Brownella1cdeda2008-07-14 22:38:24 +02001998/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001999 * __i2c_transfer - unlocked flavor of i2c_transfer
2000 * @adap: Handle to I2C bus
2001 * @msgs: One or more messages to execute before STOP is issued to
2002 * terminate the operation; each message begins with a START.
2003 * @num: Number of messages to be executed.
2004 *
2005 * Returns negative errno, else the number of messages executed.
2006 *
2007 * Adapter lock must be held when calling this function. No debug logging
2008 * takes place. adap->algo->master_xfer existence isn't checked.
2009 */
2010int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2011{
2012 unsigned long orig_jiffies;
2013 int ret, try;
2014
David Howellsd9a83d62014-03-06 13:35:59 +00002015 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2016 * enabled. This is an efficient way of keeping the for-loop from
2017 * being executed when not needed.
2018 */
2019 if (static_key_false(&i2c_trace_msg)) {
2020 int i;
2021 for (i = 0; i < num; i++)
2022 if (msgs[i].flags & I2C_M_RD)
2023 trace_i2c_read(adap, &msgs[i], i);
2024 else
2025 trace_i2c_write(adap, &msgs[i], i);
2026 }
2027
Jean Delvareb37d2a32012-06-29 07:47:19 -03002028 /* Retry automatically on arbitration loss */
2029 orig_jiffies = jiffies;
2030 for (ret = 0, try = 0; try <= adap->retries; try++) {
2031 ret = adap->algo->master_xfer(adap, msgs, num);
2032 if (ret != -EAGAIN)
2033 break;
2034 if (time_after(jiffies, orig_jiffies + adap->timeout))
2035 break;
2036 }
2037
David Howellsd9a83d62014-03-06 13:35:59 +00002038 if (static_key_false(&i2c_trace_msg)) {
2039 int i;
2040 for (i = 0; i < ret; i++)
2041 if (msgs[i].flags & I2C_M_RD)
2042 trace_i2c_reply(adap, &msgs[i], i);
2043 trace_i2c_result(adap, i, ret);
2044 }
2045
Jean Delvareb37d2a32012-06-29 07:47:19 -03002046 return ret;
2047}
2048EXPORT_SYMBOL(__i2c_transfer);
2049
2050/**
David Brownella1cdeda2008-07-14 22:38:24 +02002051 * i2c_transfer - execute a single or combined I2C message
2052 * @adap: Handle to I2C bus
2053 * @msgs: One or more messages to execute before STOP is issued to
2054 * terminate the operation; each message begins with a START.
2055 * @num: Number of messages to be executed.
2056 *
2057 * Returns negative errno, else the number of messages executed.
2058 *
2059 * Note that there is no requirement that each message be sent to
2060 * the same slave address, although that is the most common model.
2061 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002062int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002064 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
David Brownella1cdeda2008-07-14 22:38:24 +02002066 /* REVISIT the fault reporting model here is weak:
2067 *
2068 * - When we get an error after receiving N bytes from a slave,
2069 * there is no way to report "N".
2070 *
2071 * - When we get a NAK after transmitting N bytes to a slave,
2072 * there is no way to report "N" ... or to let the master
2073 * continue executing the rest of this combined message, if
2074 * that's the appropriate response.
2075 *
2076 * - When for example "num" is two and we successfully complete
2077 * the first message but get an error part way through the
2078 * second, it's unclear whether that should be reported as
2079 * one (discarding status on the second message) or errno
2080 * (discarding status on the first one).
2081 */
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (adap->algo->master_xfer) {
2084#ifdef DEBUG
2085 for (ret = 0; ret < num; ret++) {
2086 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002087 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2088 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2089 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091#endif
2092
Mike Rapoportcea443a2008-01-27 18:14:50 +01002093 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002094 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002095 if (!ret)
2096 /* I2C activity is ongoing. */
2097 return -EAGAIN;
2098 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002099 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002100 }
2101
Jean Delvareb37d2a32012-06-29 07:47:19 -03002102 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002103 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 return ret;
2106 } else {
2107 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002108 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110}
David Brownellc0564602007-05-01 23:26:31 +02002111EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
David Brownella1cdeda2008-07-14 22:38:24 +02002113/**
2114 * i2c_master_send - issue a single I2C message in master transmit mode
2115 * @client: Handle to slave device
2116 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002117 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002118 *
2119 * Returns negative errno, or else the number of bytes written.
2120 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002121int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002124 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct i2c_msg msg;
2126
Jean Delvare815f55f2005-05-07 22:58:46 +02002127 msg.addr = client->addr;
2128 msg.flags = client->flags & I2C_M_TEN;
2129 msg.len = count;
2130 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002131
Jean Delvare815f55f2005-05-07 22:58:46 +02002132 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002134 /*
2135 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2136 * transmitted, else error code.
2137 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002138 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
David Brownellc0564602007-05-01 23:26:31 +02002140EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
David Brownella1cdeda2008-07-14 22:38:24 +02002142/**
2143 * i2c_master_recv - issue a single I2C message in master receive mode
2144 * @client: Handle to slave device
2145 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002146 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002147 *
2148 * Returns negative errno, or else the number of bytes read.
2149 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002150int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Farid Hammane7225acf2010-05-21 18:40:58 +02002152 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct i2c_msg msg;
2154 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jean Delvare815f55f2005-05-07 22:58:46 +02002156 msg.addr = client->addr;
2157 msg.flags = client->flags & I2C_M_TEN;
2158 msg.flags |= I2C_M_RD;
2159 msg.len = count;
2160 msg.buf = buf;
2161
2162 ret = i2c_transfer(adap, &msg, 1);
2163
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002164 /*
2165 * If everything went ok (i.e. 1 msg received), return #bytes received,
2166 * else error code.
2167 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002168 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
David Brownellc0564602007-05-01 23:26:31 +02002170EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172/* ----------------------------------------------------
2173 * the i2c address scanning function
2174 * Will not work for 10-bit addresses!
2175 * ----------------------------------------------------
2176 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002177
Jean Delvare63e4e802010-06-03 11:33:51 +02002178/*
2179 * Legacy default probe function, mostly relevant for SMBus. The default
2180 * probe method is a quick write, but it is known to corrupt the 24RF08
2181 * EEPROMs due to a state machine bug, and could also irreversibly
2182 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2183 * we use a short byte read instead. Also, some bus drivers don't implement
2184 * quick write, so we fallback to a byte read in that case too.
2185 * On x86, there is another special case for FSC hardware monitoring chips,
2186 * which want regular byte reads (address 0x73.) Fortunately, these are the
2187 * only known chips using this I2C address on PC hardware.
2188 * Returns 1 if probe succeeded, 0 if not.
2189 */
2190static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2191{
2192 int err;
2193 union i2c_smbus_data dummy;
2194
2195#ifdef CONFIG_X86
2196 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2197 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2198 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2199 I2C_SMBUS_BYTE_DATA, &dummy);
2200 else
2201#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002202 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2203 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002204 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2205 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002206 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2207 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2208 I2C_SMBUS_BYTE, &dummy);
2209 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002210 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2211 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002212 err = -EOPNOTSUPP;
2213 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002214
2215 return err >= 0;
2216}
2217
Jean Delvareccfbbd02009-12-06 17:06:25 +01002218static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002219 struct i2c_driver *driver)
2220{
2221 struct i2c_board_info info;
2222 struct i2c_adapter *adapter = temp_client->adapter;
2223 int addr = temp_client->addr;
2224 int err;
2225
2226 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002227 err = i2c_check_addr_validity(addr);
2228 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002229 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2230 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002231 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002232 }
2233
2234 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002235 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002236 return 0;
2237
Jean Delvareccfbbd02009-12-06 17:06:25 +01002238 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002239 if (!i2c_default_probe(adapter, addr))
2240 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002241
2242 /* Finally call the custom detection function */
2243 memset(&info, 0, sizeof(struct i2c_board_info));
2244 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002245 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002246 if (err) {
2247 /* -ENODEV is returned if the detection fails. We catch it
2248 here as this isn't an error. */
2249 return err == -ENODEV ? 0 : err;
2250 }
2251
2252 /* Consistency check */
2253 if (info.type[0] == '\0') {
2254 dev_err(&adapter->dev, "%s detection function provided "
2255 "no name for 0x%x\n", driver->driver.name,
2256 addr);
2257 } else {
2258 struct i2c_client *client;
2259
2260 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002261 if (adapter->class & I2C_CLASS_DEPRECATED)
2262 dev_warn(&adapter->dev,
2263 "This adapter will soon drop class based instantiation of devices. "
2264 "Please make sure client 0x%02x gets instantiated by other means. "
2265 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2266 info.addr);
2267
Jean Delvare4735c982008-07-14 22:38:36 +02002268 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2269 info.type, info.addr);
2270 client = i2c_new_device(adapter, &info);
2271 if (client)
2272 list_add_tail(&client->detected, &driver->clients);
2273 else
2274 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2275 info.type, info.addr);
2276 }
2277 return 0;
2278}
2279
2280static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2281{
Jean Delvarec3813d62009-12-14 21:17:25 +01002282 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002283 struct i2c_client *temp_client;
2284 int i, err = 0;
2285 int adap_id = i2c_adapter_id(adapter);
2286
Jean Delvarec3813d62009-12-14 21:17:25 +01002287 address_list = driver->address_list;
2288 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002289 return 0;
2290
Wolfram Sang45552272014-07-10 13:46:21 +02002291 /* Warn that the adapter lost class based instantiation */
2292 if (adapter->class == I2C_CLASS_DEPRECATED) {
2293 dev_dbg(&adapter->dev,
2294 "This adapter dropped support for I2C classes and "
2295 "won't auto-detect %s devices anymore. If you need it, check "
2296 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2297 driver->driver.name);
2298 return 0;
2299 }
2300
Jean Delvare51b54ba2010-10-24 18:16:58 +02002301 /* Stop here if the classes do not match */
2302 if (!(adapter->class & driver->class))
2303 return 0;
2304
Jean Delvare4735c982008-07-14 22:38:36 +02002305 /* Set up a temporary client to help detect callback */
2306 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2307 if (!temp_client)
2308 return -ENOMEM;
2309 temp_client->adapter = adapter;
2310
Jean Delvarec3813d62009-12-14 21:17:25 +01002311 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002312 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002313 "addr 0x%02x\n", adap_id, address_list[i]);
2314 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002315 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002316 if (unlikely(err))
2317 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002318 }
2319
Jean Delvare4735c982008-07-14 22:38:36 +02002320 kfree(temp_client);
2321 return err;
2322}
2323
Jean Delvared44f19d2010-08-11 18:20:57 +02002324int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2325{
2326 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2327 I2C_SMBUS_QUICK, NULL) >= 0;
2328}
2329EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2330
Jean Delvare12b5053a2007-05-01 23:26:31 +02002331struct i2c_client *
2332i2c_new_probed_device(struct i2c_adapter *adap,
2333 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002334 unsigned short const *addr_list,
2335 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002336{
2337 int i;
2338
Jean Delvare8031d792010-08-11 18:21:00 +02002339 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002340 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002341
Jean Delvare12b5053a2007-05-01 23:26:31 +02002342 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2343 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002344 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002345 dev_warn(&adap->dev, "Invalid 7-bit address "
2346 "0x%02x\n", addr_list[i]);
2347 continue;
2348 }
2349
2350 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002351 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002352 dev_dbg(&adap->dev, "Address 0x%02x already in "
2353 "use, not probing\n", addr_list[i]);
2354 continue;
2355 }
2356
Jean Delvare63e4e802010-06-03 11:33:51 +02002357 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002358 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002359 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002360 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002361
2362 if (addr_list[i] == I2C_CLIENT_END) {
2363 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2364 return NULL;
2365 }
2366
2367 info->addr = addr_list[i];
2368 return i2c_new_device(adap, info);
2369}
2370EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2371
Jean Delvared735b342011-03-20 14:50:52 +01002372struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002375
Jean Delvarecaada322008-01-27 18:14:49 +01002376 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002377 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002378 if (adapter && !try_module_get(adapter->owner))
2379 adapter = NULL;
2380
Jean Delvarecaada322008-01-27 18:14:49 +01002381 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002382 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
David Brownellc0564602007-05-01 23:26:31 +02002384EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386void i2c_put_adapter(struct i2c_adapter *adap)
2387{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002388 if (adap)
2389 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
David Brownellc0564602007-05-01 23:26:31 +02002391EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393/* The SMBus parts */
2394
David Brownell438d6c22006-12-10 21:21:31 +01002395#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002396static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002399
Farid Hammane7225acf2010-05-21 18:40:58 +02002400 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002401 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 data = data ^ POLY;
2403 data = data << 1;
2404 }
2405 return (u8)(data >> 8);
2406}
2407
Jean Delvare421ef472005-10-26 21:28:55 +02002408/* Incremental CRC8 over count bytes in the array pointed to by p */
2409static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 int i;
2412
Farid Hammane7225acf2010-05-21 18:40:58 +02002413 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002414 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return crc;
2416}
2417
Jean Delvare421ef472005-10-26 21:28:55 +02002418/* Assume a 7-bit address, which is reasonable for SMBus */
2419static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Jean Delvare421ef472005-10-26 21:28:55 +02002421 /* The address will be sent first */
2422 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2423 pec = i2c_smbus_pec(pec, &addr, 1);
2424
2425 /* The data buffer follows */
2426 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Jean Delvare421ef472005-10-26 21:28:55 +02002429/* Used for write only transactions */
2430static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Jean Delvare421ef472005-10-26 21:28:55 +02002432 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2433 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Jean Delvare421ef472005-10-26 21:28:55 +02002436/* Return <0 on CRC error
2437 If there was a write before this read (most cases) we need to take the
2438 partial CRC from the write part into account.
2439 Note that this function does modify the message (we need to decrease the
2440 message length to hide the CRC byte from the caller). */
2441static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jean Delvare421ef472005-10-26 21:28:55 +02002443 u8 rpec = msg->buf[--msg->len];
2444 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (rpec != cpec) {
2447 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2448 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002449 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
David Brownell438d6c22006-12-10 21:21:31 +01002451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
David Brownella1cdeda2008-07-14 22:38:24 +02002454/**
2455 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2456 * @client: Handle to slave device
2457 *
2458 * This executes the SMBus "receive byte" protocol, returning negative errno
2459 * else the byte received from the device.
2460 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002461s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002464 int status;
2465
2466 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2467 I2C_SMBUS_READ, 0,
2468 I2C_SMBUS_BYTE, &data);
2469 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
David Brownellc0564602007-05-01 23:26:31 +02002471EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Brownella1cdeda2008-07-14 22:38:24 +02002473/**
2474 * i2c_smbus_write_byte - SMBus "send byte" protocol
2475 * @client: Handle to slave device
2476 * @value: Byte to be sent
2477 *
2478 * This executes the SMBus "send byte" protocol, returning negative errno
2479 * else zero on success.
2480 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002481s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Farid Hammane7225acf2010-05-21 18:40:58 +02002483 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002484 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
David Brownellc0564602007-05-01 23:26:31 +02002486EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
David Brownella1cdeda2008-07-14 22:38:24 +02002488/**
2489 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2490 * @client: Handle to slave device
2491 * @command: Byte interpreted by slave
2492 *
2493 * This executes the SMBus "read byte" protocol, returning negative errno
2494 * else a data byte received from the device.
2495 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002496s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002499 int status;
2500
2501 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2502 I2C_SMBUS_READ, command,
2503 I2C_SMBUS_BYTE_DATA, &data);
2504 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
David Brownellc0564602007-05-01 23:26:31 +02002506EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
David Brownella1cdeda2008-07-14 22:38:24 +02002508/**
2509 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2510 * @client: Handle to slave device
2511 * @command: Byte interpreted by slave
2512 * @value: Byte being written
2513 *
2514 * This executes the SMBus "write byte" protocol, returning negative errno
2515 * else zero on success.
2516 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002517s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2518 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 union i2c_smbus_data data;
2521 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002522 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2523 I2C_SMBUS_WRITE, command,
2524 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
David Brownellc0564602007-05-01 23:26:31 +02002526EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
David Brownella1cdeda2008-07-14 22:38:24 +02002528/**
2529 * i2c_smbus_read_word_data - SMBus "read word" protocol
2530 * @client: Handle to slave device
2531 * @command: Byte interpreted by slave
2532 *
2533 * This executes the SMBus "read word" protocol, returning negative errno
2534 * else a 16-bit unsigned "word" received from the device.
2535 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002536s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002539 int status;
2540
2541 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2542 I2C_SMBUS_READ, command,
2543 I2C_SMBUS_WORD_DATA, &data);
2544 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
David Brownellc0564602007-05-01 23:26:31 +02002546EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
David Brownella1cdeda2008-07-14 22:38:24 +02002548/**
2549 * i2c_smbus_write_word_data - SMBus "write word" protocol
2550 * @client: Handle to slave device
2551 * @command: Byte interpreted by slave
2552 * @value: 16-bit "word" being written
2553 *
2554 * This executes the SMBus "write word" protocol, returning negative errno
2555 * else zero on success.
2556 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002557s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2558 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 union i2c_smbus_data data;
2561 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002562 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2563 I2C_SMBUS_WRITE, command,
2564 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
David Brownellc0564602007-05-01 23:26:31 +02002566EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
David Brownella64ec072007-10-13 23:56:31 +02002568/**
David Brownella1cdeda2008-07-14 22:38:24 +02002569 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002570 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002571 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002572 * @values: Byte array into which data will be read; big enough to hold
2573 * the data returned by the slave. SMBus allows at most 32 bytes.
2574 *
David Brownella1cdeda2008-07-14 22:38:24 +02002575 * This executes the SMBus "block read" protocol, returning negative errno
2576 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002577 *
2578 * Note that using this function requires that the client's adapter support
2579 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2580 * support this; its emulation through I2C messaging relies on a specific
2581 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2582 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002583s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002584 u8 *values)
2585{
2586 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002587 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002588
David Brownell24a5bb72008-07-14 22:38:23 +02002589 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2590 I2C_SMBUS_READ, command,
2591 I2C_SMBUS_BLOCK_DATA, &data);
2592 if (status)
2593 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002594
2595 memcpy(values, &data.block[1], data.block[0]);
2596 return data.block[0];
2597}
2598EXPORT_SYMBOL(i2c_smbus_read_block_data);
2599
David Brownella1cdeda2008-07-14 22:38:24 +02002600/**
2601 * i2c_smbus_write_block_data - SMBus "block write" protocol
2602 * @client: Handle to slave device
2603 * @command: Byte interpreted by slave
2604 * @length: Size of data block; SMBus allows at most 32 bytes
2605 * @values: Byte array which will be written.
2606 *
2607 * This executes the SMBus "block write" protocol, returning negative errno
2608 * else zero on success.
2609 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002610s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002611 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (length > I2C_SMBUS_BLOCK_MAX)
2616 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002618 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002619 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2620 I2C_SMBUS_WRITE, command,
2621 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
David Brownellc0564602007-05-01 23:26:31 +02002623EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002626s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002627 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002630 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002631
Jean Delvare4b2643d2007-07-12 14:12:29 +02002632 if (length > I2C_SMBUS_BLOCK_MAX)
2633 length = I2C_SMBUS_BLOCK_MAX;
2634 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2636 I2C_SMBUS_READ, command,
2637 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2638 if (status < 0)
2639 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002640
2641 memcpy(values, &data.block[1], data.block[0]);
2642 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
David Brownellc0564602007-05-01 23:26:31 +02002644EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Jean Delvare0cc43a12011-01-10 22:11:23 +01002646s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002647 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002648{
2649 union i2c_smbus_data data;
2650
2651 if (length > I2C_SMBUS_BLOCK_MAX)
2652 length = I2C_SMBUS_BLOCK_MAX;
2653 data.block[0] = length;
2654 memcpy(data.block + 1, values, length);
2655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2656 I2C_SMBUS_WRITE, command,
2657 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2658}
David Brownellc0564602007-05-01 23:26:31 +02002659EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002660
David Brownell438d6c22006-12-10 21:21:31 +01002661/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002663static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2664 unsigned short flags,
2665 char read_write, u8 command, int size,
2666 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 /* So we need to generate a series of msgs. In the case of writing, we
2669 need to use only one message; when reading, we need two. We initialize
2670 most things with sane defaults, to keep the code below somewhat
2671 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002672 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2673 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002674 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002676 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002677 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002678 struct i2c_msg msg[2] = {
2679 {
2680 .addr = addr,
2681 .flags = flags,
2682 .len = 1,
2683 .buf = msgbuf0,
2684 }, {
2685 .addr = addr,
2686 .flags = flags | I2C_M_RD,
2687 .len = 0,
2688 .buf = msgbuf1,
2689 },
2690 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002693 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 case I2C_SMBUS_QUICK:
2695 msg[0].len = 0;
2696 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002697 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2698 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 num = 1;
2700 break;
2701 case I2C_SMBUS_BYTE:
2702 if (read_write == I2C_SMBUS_READ) {
2703 /* Special case: only a read! */
2704 msg[0].flags = I2C_M_RD | flags;
2705 num = 1;
2706 }
2707 break;
2708 case I2C_SMBUS_BYTE_DATA:
2709 if (read_write == I2C_SMBUS_READ)
2710 msg[1].len = 1;
2711 else {
2712 msg[0].len = 2;
2713 msgbuf0[1] = data->byte;
2714 }
2715 break;
2716 case I2C_SMBUS_WORD_DATA:
2717 if (read_write == I2C_SMBUS_READ)
2718 msg[1].len = 2;
2719 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002720 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002722 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 break;
2725 case I2C_SMBUS_PROC_CALL:
2726 num = 2; /* Special case */
2727 read_write = I2C_SMBUS_READ;
2728 msg[0].len = 3;
2729 msg[1].len = 2;
2730 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002731 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 break;
2733 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002735 msg[1].flags |= I2C_M_RECV_LEN;
2736 msg[1].len = 1; /* block length will be added by
2737 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 } else {
2739 msg[0].len = data->block[0] + 2;
2740 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002741 dev_err(&adapter->dev,
2742 "Invalid block write size %d\n",
2743 data->block[0]);
2744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002746 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 msgbuf0[i] = data->block[i-1];
2748 }
2749 break;
2750 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002751 num = 2; /* Another special case */
2752 read_write = I2C_SMBUS_READ;
2753 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002754 dev_err(&adapter->dev,
2755 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002756 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002757 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002758 }
2759 msg[0].len = data->block[0] + 2;
2760 for (i = 1; i < msg[0].len; i++)
2761 msgbuf0[i] = data->block[i-1];
2762 msg[1].flags |= I2C_M_RECV_LEN;
2763 msg[1].len = 1; /* block length will be added by
2764 the underlying bus driver */
2765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 case I2C_SMBUS_I2C_BLOCK_DATA:
2767 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002768 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 } else {
2770 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002771 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002772 dev_err(&adapter->dev,
2773 "Invalid block write size %d\n",
2774 data->block[0]);
2775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777 for (i = 1; i <= data->block[0]; i++)
2778 msgbuf0[i] = data->block[i];
2779 }
2780 break;
2781 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002782 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2783 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Jean Delvare421ef472005-10-26 21:28:55 +02002786 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2787 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2788 if (i) {
2789 /* Compute PEC if first message is a write */
2790 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002791 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002792 i2c_smbus_add_pec(&msg[0]);
2793 else /* Write followed by read */
2794 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2795 }
2796 /* Ask for PEC if last message is a read */
2797 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002798 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002799 }
2800
David Brownell24a5bb72008-07-14 22:38:23 +02002801 status = i2c_transfer(adapter, msg, num);
2802 if (status < 0)
2803 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Jean Delvare421ef472005-10-26 21:28:55 +02002805 /* Check PEC if last message is a read */
2806 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002807 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2808 if (status < 0)
2809 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002810 }
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002813 switch (size) {
2814 case I2C_SMBUS_BYTE:
2815 data->byte = msgbuf0[0];
2816 break;
2817 case I2C_SMBUS_BYTE_DATA:
2818 data->byte = msgbuf1[0];
2819 break;
2820 case I2C_SMBUS_WORD_DATA:
2821 case I2C_SMBUS_PROC_CALL:
2822 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2823 break;
2824 case I2C_SMBUS_I2C_BLOCK_DATA:
2825 for (i = 0; i < data->block[0]; i++)
2826 data->block[i+1] = msgbuf1[i];
2827 break;
2828 case I2C_SMBUS_BLOCK_DATA:
2829 case I2C_SMBUS_BLOCK_PROC_CALL:
2830 for (i = 0; i < msgbuf1[0] + 1; i++)
2831 data->block[i] = msgbuf1[i];
2832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
2834 return 0;
2835}
2836
David Brownella1cdeda2008-07-14 22:38:24 +02002837/**
2838 * i2c_smbus_xfer - execute SMBus protocol operations
2839 * @adapter: Handle to I2C bus
2840 * @addr: Address of SMBus slave on that bus
2841 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2842 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2843 * @command: Byte interpreted by slave, for protocols which use such bytes
2844 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2845 * @data: Data to be read or written
2846 *
2847 * This executes an SMBus protocol operation, and returns a negative
2848 * errno code else zero on success.
2849 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002850s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002851 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002852 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002854 unsigned long orig_jiffies;
2855 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
David Howells8a325992014-03-06 13:36:06 +00002858 /* If enabled, the following two tracepoints are conditional on
2859 * read_write and protocol.
2860 */
2861 trace_smbus_write(adapter, addr, flags, read_write,
2862 command, protocol, data);
2863 trace_smbus_read(adapter, addr, flags, read_write,
2864 command, protocol);
2865
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002866 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002869 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002870
2871 /* Retry automatically on arbitration loss */
2872 orig_jiffies = jiffies;
2873 for (res = 0, try = 0; try <= adapter->retries; try++) {
2874 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2875 read_write, command,
2876 protocol, data);
2877 if (res != -EAGAIN)
2878 break;
2879 if (time_after(jiffies,
2880 orig_jiffies + adapter->timeout))
2881 break;
2882 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002883 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002885 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002886 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002887 /*
2888 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2889 * implement native support for the SMBus operation.
2890 */
2891 }
2892
David Howells8a325992014-03-06 13:36:06 +00002893 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2894 command, protocol, data);
2895
2896trace:
2897 /* If enabled, the reply tracepoint is conditional on read_write. */
2898 trace_smbus_reply(adapter, addr, flags, read_write,
2899 command, protocol, data);
2900 trace_smbus_result(adapter, addr, flags, read_write,
2901 command, protocol, res);
2902
2903 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2908MODULE_DESCRIPTION("I2C-Bus main module");
2909MODULE_LICENSE("GPL");