blob: e9eae57a2b50f77e3d25c4d9fcfa003728464740 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/init.h>
38#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010039#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010040#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060041#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020043#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010044#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010045#include <linux/hardirq.h>
46#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020047#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010048#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020049#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000051#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020053#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare6629dcf2010-05-04 11:09:28 +020060/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020061 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000062 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010063static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_IDR(i2c_adapter_idr);
65
Jean Delvare4f8cf822009-09-18 22:45:46 +020066static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
70
71void i2c_transfer_trace_reg(void)
72{
73 static_key_slow_inc(&i2c_trace_msg);
74}
75
76void i2c_transfer_trace_unreg(void)
77{
78 static_key_slow_dec(&i2c_trace_msg);
79}
80
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020081#if defined(CONFIG_ACPI)
82struct acpi_i2c_handler_data {
83 struct acpi_connection_info info;
84 struct i2c_adapter *adapter;
85};
86
87struct gsb_buffer {
88 u8 status;
89 u8 len;
90 union {
91 u16 wdata;
92 u8 bdata;
93 u8 data[0];
94 };
95} __packed;
96
97static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
98{
99 struct i2c_board_info *info = data;
100
101 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
102 struct acpi_resource_i2c_serialbus *sb;
103
104 sb = &ares->data.i2c_serial_bus;
105 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
106 info->addr = sb->slave_address;
107 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
108 info->flags |= I2C_CLIENT_TEN;
109 }
110 } else if (info->irq < 0) {
111 struct resource r;
112
113 if (acpi_dev_resource_interrupt(ares, 0, &r))
114 info->irq = r.start;
115 }
116
117 /* Tell the ACPI core to skip this resource */
118 return 1;
119}
120
121static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
122 void *data, void **return_value)
123{
124 struct i2c_adapter *adapter = data;
125 struct list_head resource_list;
126 struct i2c_board_info info;
127 struct acpi_device *adev;
128 int ret;
129
130 if (acpi_bus_get_device(handle, &adev))
131 return AE_OK;
132 if (acpi_bus_get_status(adev) || !adev->status.present)
133 return AE_OK;
134
135 memset(&info, 0, sizeof(info));
136 info.acpi_node.companion = adev;
137 info.irq = -1;
138
139 INIT_LIST_HEAD(&resource_list);
140 ret = acpi_dev_get_resources(adev, &resource_list,
141 acpi_i2c_add_resource, &info);
142 acpi_dev_free_resource_list(&resource_list);
143
144 if (ret < 0 || !info.addr)
145 return AE_OK;
146
147 adev->power.flags.ignore_parent = true;
148 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
149 if (!i2c_new_device(adapter, &info)) {
150 adev->power.flags.ignore_parent = false;
151 dev_err(&adapter->dev,
152 "failed to add I2C device %s from ACPI\n",
153 dev_name(&adev->dev));
154 }
155
156 return AE_OK;
157}
158
159/**
160 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
161 * @adap: pointer to adapter
162 *
163 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
164 * namespace. When a device is found it will be added to the Linux device
165 * model and bound to the corresponding ACPI handle.
166 */
167static void acpi_i2c_register_devices(struct i2c_adapter *adap)
168{
169 acpi_handle handle;
170 acpi_status status;
171
172 if (!adap->dev.parent)
173 return;
174
175 handle = ACPI_HANDLE(adap->dev.parent);
176 if (!handle)
177 return;
178
179 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
180 acpi_i2c_add_device, NULL,
181 adap, NULL);
182 if (ACPI_FAILURE(status))
183 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
184}
185
186#else /* CONFIG_ACPI */
187static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
188#endif /* CONFIG_ACPI */
189
190#ifdef CONFIG_ACPI_I2C_OPREGION
191static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
192 u8 cmd, u8 *data, u8 data_len)
193{
194
195 struct i2c_msg msgs[2];
196 int ret;
197 u8 *buffer;
198
199 buffer = kzalloc(data_len, GFP_KERNEL);
200 if (!buffer)
201 return AE_NO_MEMORY;
202
203 msgs[0].addr = client->addr;
204 msgs[0].flags = client->flags;
205 msgs[0].len = 1;
206 msgs[0].buf = &cmd;
207
208 msgs[1].addr = client->addr;
209 msgs[1].flags = client->flags | I2C_M_RD;
210 msgs[1].len = data_len;
211 msgs[1].buf = buffer;
212
213 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
214 if (ret < 0)
215 dev_err(&client->adapter->dev, "i2c read failed\n");
216 else
217 memcpy(data, buffer, data_len);
218
219 kfree(buffer);
220 return ret;
221}
222
223static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
224 u8 cmd, u8 *data, u8 data_len)
225{
226
227 struct i2c_msg msgs[1];
228 u8 *buffer;
229 int ret = AE_OK;
230
231 buffer = kzalloc(data_len + 1, GFP_KERNEL);
232 if (!buffer)
233 return AE_NO_MEMORY;
234
235 buffer[0] = cmd;
236 memcpy(buffer + 1, data, data_len);
237
238 msgs[0].addr = client->addr;
239 msgs[0].flags = client->flags;
240 msgs[0].len = data_len + 1;
241 msgs[0].buf = buffer;
242
243 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
244 if (ret < 0)
245 dev_err(&client->adapter->dev, "i2c write failed\n");
246
247 kfree(buffer);
248 return ret;
249}
250
251static acpi_status
252acpi_i2c_space_handler(u32 function, acpi_physical_address command,
253 u32 bits, u64 *value64,
254 void *handler_context, void *region_context)
255{
256 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
257 struct acpi_i2c_handler_data *data = handler_context;
258 struct acpi_connection_info *info = &data->info;
259 struct acpi_resource_i2c_serialbus *sb;
260 struct i2c_adapter *adapter = data->adapter;
261 struct i2c_client client;
262 struct acpi_resource *ares;
263 u32 accessor_type = function >> 16;
264 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100265 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200266 int status;
267
268 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
269 if (ACPI_FAILURE(ret))
270 return ret;
271
272 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
273 ret = AE_BAD_PARAMETER;
274 goto err;
275 }
276
277 sb = &ares->data.i2c_serial_bus;
278 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
279 ret = AE_BAD_PARAMETER;
280 goto err;
281 }
282
283 memset(&client, 0, sizeof(client));
284 client.adapter = adapter;
285 client.addr = sb->slave_address;
286 client.flags = 0;
287
288 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
289 client.flags |= I2C_CLIENT_TEN;
290
291 switch (accessor_type) {
292 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
293 if (action == ACPI_READ) {
294 status = i2c_smbus_read_byte(&client);
295 if (status >= 0) {
296 gsb->bdata = status;
297 status = 0;
298 }
299 } else {
300 status = i2c_smbus_write_byte(&client, gsb->bdata);
301 }
302 break;
303
304 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
305 if (action == ACPI_READ) {
306 status = i2c_smbus_read_byte_data(&client, command);
307 if (status >= 0) {
308 gsb->bdata = status;
309 status = 0;
310 }
311 } else {
312 status = i2c_smbus_write_byte_data(&client, command,
313 gsb->bdata);
314 }
315 break;
316
317 case ACPI_GSB_ACCESS_ATTRIB_WORD:
318 if (action == ACPI_READ) {
319 status = i2c_smbus_read_word_data(&client, command);
320 if (status >= 0) {
321 gsb->wdata = status;
322 status = 0;
323 }
324 } else {
325 status = i2c_smbus_write_word_data(&client, command,
326 gsb->wdata);
327 }
328 break;
329
330 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
331 if (action == ACPI_READ) {
332 status = i2c_smbus_read_block_data(&client, command,
333 gsb->data);
334 if (status >= 0) {
335 gsb->len = status;
336 status = 0;
337 }
338 } else {
339 status = i2c_smbus_write_block_data(&client, command,
340 gsb->len, gsb->data);
341 }
342 break;
343
344 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
345 if (action == ACPI_READ) {
346 status = acpi_gsb_i2c_read_bytes(&client, command,
347 gsb->data, info->access_length);
348 if (status > 0)
349 status = 0;
350 } else {
351 status = acpi_gsb_i2c_write_bytes(&client, command,
352 gsb->data, info->access_length);
353 }
354 break;
355
356 default:
357 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
358 ret = AE_BAD_PARAMETER;
359 goto err;
360 }
361
362 gsb->status = status;
363
364 err:
365 ACPI_FREE(ares);
366 return ret;
367}
368
369
370static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
371{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200372 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200373 struct acpi_i2c_handler_data *data;
374 acpi_status status;
375
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200376 if (!adapter->dev.parent)
377 return -ENODEV;
378
379 handle = ACPI_HANDLE(adapter->dev.parent);
380
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200381 if (!handle)
382 return -ENODEV;
383
384 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
385 GFP_KERNEL);
386 if (!data)
387 return -ENOMEM;
388
389 data->adapter = adapter;
390 status = acpi_bus_attach_private_data(handle, (void *)data);
391 if (ACPI_FAILURE(status)) {
392 kfree(data);
393 return -ENOMEM;
394 }
395
396 status = acpi_install_address_space_handler(handle,
397 ACPI_ADR_SPACE_GSBUS,
398 &acpi_i2c_space_handler,
399 NULL,
400 data);
401 if (ACPI_FAILURE(status)) {
402 dev_err(&adapter->dev, "Error installing i2c space handler\n");
403 acpi_bus_detach_private_data(handle);
404 kfree(data);
405 return -ENOMEM;
406 }
407
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800408 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200409 return 0;
410}
411
412static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
413{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200414 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415 struct acpi_i2c_handler_data *data;
416 acpi_status status;
417
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200418 if (!adapter->dev.parent)
419 return;
420
421 handle = ACPI_HANDLE(adapter->dev.parent);
422
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200423 if (!handle)
424 return;
425
426 acpi_remove_address_space_handler(handle,
427 ACPI_ADR_SPACE_GSBUS,
428 &acpi_i2c_space_handler);
429
430 status = acpi_bus_get_private_data(handle, (void **)&data);
431 if (ACPI_SUCCESS(status))
432 kfree(data);
433
434 acpi_bus_detach_private_data(handle);
435}
436#else /* CONFIG_ACPI_I2C_OPREGION */
437static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
438{ }
439
440static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
441{ return 0; }
442#endif /* CONFIG_ACPI_I2C_OPREGION */
443
David Brownellf37dd802007-02-13 22:09:00 +0100444/* ------------------------------------------------------------------------- */
445
Jean Delvared2653e92008-04-29 23:11:39 +0200446static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
447 const struct i2c_client *client)
448{
449 while (id->name[0]) {
450 if (strcmp(client->name, id->name) == 0)
451 return id;
452 id++;
453 }
454 return NULL;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static int i2c_device_match(struct device *dev, struct device_driver *drv)
458{
Jean Delvare51298d12009-09-18 22:45:45 +0200459 struct i2c_client *client = i2c_verify_client(dev);
460 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200461
Jean Delvare51298d12009-09-18 22:45:45 +0200462 if (!client)
463 return 0;
464
Grant Likely959e85f2010-06-08 07:48:19 -0600465 /* Attempt an OF style match */
466 if (of_driver_match_device(dev, drv))
467 return 1;
468
Mika Westerberg907ddf82012-11-23 12:23:40 +0100469 /* Then ACPI style match */
470 if (acpi_driver_match_device(dev, drv))
471 return 1;
472
Jean Delvare51298d12009-09-18 22:45:45 +0200473 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200474 /* match on an id table if there is one */
475 if (driver->id_table)
476 return i2c_match_id(driver->id_table, client) != NULL;
477
Jean Delvareeb8a7902008-05-18 20:49:41 +0200478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David Brownell7b4fbc52007-05-01 23:26:30 +0200481
482/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200483static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200484{
485 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800486 int rc;
487
488 rc = acpi_device_uevent_modalias(dev, env);
489 if (rc != -ENODEV)
490 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200491
Jean Delvareeb8a7902008-05-18 20:49:41 +0200492 if (add_uevent_var(env, "MODALIAS=%s%s",
493 I2C_MODULE_PREFIX, client->name))
494 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200495 dev_dbg(dev, "uevent\n");
496 return 0;
497}
498
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530499/* i2c bus recovery routines */
500static int get_scl_gpio_value(struct i2c_adapter *adap)
501{
502 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
503}
504
505static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
506{
507 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
508}
509
510static int get_sda_gpio_value(struct i2c_adapter *adap)
511{
512 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
513}
514
515static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
516{
517 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
518 struct device *dev = &adap->dev;
519 int ret = 0;
520
521 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
522 GPIOF_OUT_INIT_HIGH, "i2c-scl");
523 if (ret) {
524 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
525 return ret;
526 }
527
528 if (bri->get_sda) {
529 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
530 /* work without SDA polling */
531 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
532 bri->sda_gpio);
533 bri->get_sda = NULL;
534 }
535 }
536
537 return ret;
538}
539
540static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
541{
542 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
543
544 if (bri->get_sda)
545 gpio_free(bri->sda_gpio);
546
547 gpio_free(bri->scl_gpio);
548}
549
550/*
551 * We are generating clock pulses. ndelay() determines durating of clk pulses.
552 * We will generate clock with rate 100 KHz and so duration of both clock levels
553 * is: delay in ns = (10^6 / 100) / 2
554 */
555#define RECOVERY_NDELAY 5000
556#define RECOVERY_CLK_CNT 9
557
558static int i2c_generic_recovery(struct i2c_adapter *adap)
559{
560 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
561 int i = 0, val = 1, ret = 0;
562
563 if (bri->prepare_recovery)
564 bri->prepare_recovery(bri);
565
566 /*
567 * By this time SCL is high, as we need to give 9 falling-rising edges
568 */
569 while (i++ < RECOVERY_CLK_CNT * 2) {
570 if (val) {
571 /* Break if SDA is high */
572 if (bri->get_sda && bri->get_sda(adap))
573 break;
574 /* SCL shouldn't be low here */
575 if (!bri->get_scl(adap)) {
576 dev_err(&adap->dev,
577 "SCL is stuck low, exit recovery\n");
578 ret = -EBUSY;
579 break;
580 }
581 }
582
583 val = !val;
584 bri->set_scl(adap, val);
585 ndelay(RECOVERY_NDELAY);
586 }
587
588 if (bri->unprepare_recovery)
589 bri->unprepare_recovery(bri);
590
591 return ret;
592}
593
594int i2c_generic_scl_recovery(struct i2c_adapter *adap)
595{
596 adap->bus_recovery_info->set_scl(adap, 1);
597 return i2c_generic_recovery(adap);
598}
599
600int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
601{
602 int ret;
603
604 ret = i2c_get_gpios_for_recovery(adap);
605 if (ret)
606 return ret;
607
608 ret = i2c_generic_recovery(adap);
609 i2c_put_gpios_for_recovery(adap);
610
611 return ret;
612}
613
614int i2c_recover_bus(struct i2c_adapter *adap)
615{
616 if (!adap->bus_recovery_info)
617 return -EOPNOTSUPP;
618
619 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
620 return adap->bus_recovery_info->recover_bus(adap);
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int i2c_device_probe(struct device *dev)
624{
Jean Delvare51298d12009-09-18 22:45:45 +0200625 struct i2c_client *client = i2c_verify_client(dev);
626 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100627 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200628
Jean Delvare51298d12009-09-18 22:45:45 +0200629 if (!client)
630 return 0;
631
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200632 if (!client->irq && dev->of_node) {
633 int irq = of_irq_get(dev->of_node, 0);
634
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100635 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200636 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100637 if (irq < 0)
638 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200639
640 client->irq = irq;
641 }
642
Jean Delvare51298d12009-09-18 22:45:45 +0200643 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200644 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200645 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200646
Marc Pignatee354252008-08-28 08:33:22 +0200647 if (!device_can_wakeup(&client->dev))
648 device_init_wakeup(&client->dev,
649 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200650 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200651
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200652 status = of_clk_set_defaults(dev->of_node, false);
653 if (status < 0)
654 return status;
655
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200656 status = dev_pm_domain_attach(&client->dev, true);
657 if (status != -EPROBE_DEFER) {
658 status = driver->probe(client, i2c_match_id(driver->id_table,
659 client));
660 if (status)
661 dev_pm_domain_detach(&client->dev, true);
662 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100663
Hans Verkuil50c33042008-03-12 14:15:00 +0100664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static int i2c_device_remove(struct device *dev)
668{
Jean Delvare51298d12009-09-18 22:45:45 +0200669 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200670 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100671 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200672
Jean Delvare51298d12009-09-18 22:45:45 +0200673 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200674 return 0;
675
676 driver = to_i2c_driver(dev->driver);
677 if (driver->remove) {
678 dev_dbg(dev, "remove\n");
679 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200680 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100681
Laurent Pincharte4df3a02014-10-30 15:59:37 +0200682 if (dev->of_node)
683 irq_dispose_mapping(client->irq);
684
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200685 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200686 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
David Brownellf37dd802007-02-13 22:09:00 +0100689static void i2c_device_shutdown(struct device *dev)
690{
Jean Delvare51298d12009-09-18 22:45:45 +0200691 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100692 struct i2c_driver *driver;
693
Jean Delvare51298d12009-09-18 22:45:45 +0200694 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100695 return;
696 driver = to_i2c_driver(dev->driver);
697 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200698 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100699}
700
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200701#ifdef CONFIG_PM_SLEEP
702static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100703{
Jean Delvare51298d12009-09-18 22:45:45 +0200704 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100705 struct i2c_driver *driver;
706
Jean Delvare51298d12009-09-18 22:45:45 +0200707 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100708 return 0;
709 driver = to_i2c_driver(dev->driver);
710 if (!driver->suspend)
711 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200712 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100713}
714
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200715static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100716{
Jean Delvare51298d12009-09-18 22:45:45 +0200717 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100718 struct i2c_driver *driver;
719
Jean Delvare51298d12009-09-18 22:45:45 +0200720 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100721 return 0;
722 driver = to_i2c_driver(dev->driver);
723 if (!driver->resume)
724 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200725 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100726}
727
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200728static int i2c_device_pm_suspend(struct device *dev)
729{
730 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
731
Mark Brownd529de22011-01-14 22:03:49 +0100732 if (pm)
733 return pm_generic_suspend(dev);
734 else
735 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200736}
737
738static int i2c_device_pm_resume(struct device *dev)
739{
740 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200741
742 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100743 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200744 else
Mark Brownd529de22011-01-14 22:03:49 +0100745 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200746}
747
748static int i2c_device_pm_freeze(struct device *dev)
749{
750 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
751
Mark Brownd529de22011-01-14 22:03:49 +0100752 if (pm)
753 return pm_generic_freeze(dev);
754 else
755 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200756}
757
758static int i2c_device_pm_thaw(struct device *dev)
759{
760 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
761
Mark Brownd529de22011-01-14 22:03:49 +0100762 if (pm)
763 return pm_generic_thaw(dev);
764 else
765 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200766}
767
768static int i2c_device_pm_poweroff(struct device *dev)
769{
770 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
771
Mark Brownd529de22011-01-14 22:03:49 +0100772 if (pm)
773 return pm_generic_poweroff(dev);
774 else
775 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200776}
777
778static int i2c_device_pm_restore(struct device *dev)
779{
780 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200781
782 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100783 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200784 else
Mark Brownd529de22011-01-14 22:03:49 +0100785 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200786}
787#else /* !CONFIG_PM_SLEEP */
788#define i2c_device_pm_suspend NULL
789#define i2c_device_pm_resume NULL
790#define i2c_device_pm_freeze NULL
791#define i2c_device_pm_thaw NULL
792#define i2c_device_pm_poweroff NULL
793#define i2c_device_pm_restore NULL
794#endif /* !CONFIG_PM_SLEEP */
795
David Brownell9c1600e2007-05-01 23:26:31 +0200796static void i2c_client_dev_release(struct device *dev)
797{
798 kfree(to_i2c_client(dev));
799}
800
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100801static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200802show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200803{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200804 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
805 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200806}
807
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100808static ssize_t
809show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200810{
811 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800812 int len;
813
814 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
815 if (len != -ENODEV)
816 return len;
817
Jean Delvareeb8a7902008-05-18 20:49:41 +0200818 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200819}
820
Jean Delvare4f8cf822009-09-18 22:45:46 +0200821static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200822static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
823
824static struct attribute *i2c_dev_attrs[] = {
825 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200826 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200827 &dev_attr_modalias.attr,
828 NULL
829};
830
831static struct attribute_group i2c_dev_attr_group = {
832 .attrs = i2c_dev_attrs,
833};
834
835static const struct attribute_group *i2c_dev_attr_groups[] = {
836 &i2c_dev_attr_group,
837 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200838};
839
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100840static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100841 .suspend = i2c_device_pm_suspend,
842 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200843 .freeze = i2c_device_pm_freeze,
844 .thaw = i2c_device_pm_thaw,
845 .poweroff = i2c_device_pm_poweroff,
846 .restore = i2c_device_pm_restore,
847 SET_RUNTIME_PM_OPS(
848 pm_generic_runtime_suspend,
849 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200850 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200851 )
sonic zhang54067ee2009-12-14 21:17:30 +0100852};
853
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200854struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100855 .name = "i2c",
856 .match = i2c_device_match,
857 .probe = i2c_device_probe,
858 .remove = i2c_device_remove,
859 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100860 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000861};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200862EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000863
Jean Delvare51298d12009-09-18 22:45:45 +0200864static struct device_type i2c_client_type = {
865 .groups = i2c_dev_attr_groups,
866 .uevent = i2c_device_uevent,
867 .release = i2c_client_dev_release,
868};
869
David Brownell9b766b82008-01-27 18:14:51 +0100870
871/**
872 * i2c_verify_client - return parameter as i2c_client, or NULL
873 * @dev: device, probably from some driver model iterator
874 *
875 * When traversing the driver model tree, perhaps using driver model
876 * iterators like @device_for_each_child(), you can't assume very much
877 * about the nodes you find. Use this function to avoid oopses caused
878 * by wrongly treating some non-I2C device as an i2c_client.
879 */
880struct i2c_client *i2c_verify_client(struct device *dev)
881{
Jean Delvare51298d12009-09-18 22:45:45 +0200882 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100883 ? to_i2c_client(dev)
884 : NULL;
885}
886EXPORT_SYMBOL(i2c_verify_client);
887
888
Jean Delvare3a89db52010-06-03 11:33:52 +0200889/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300890 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200891static int i2c_check_client_addr_validity(const struct i2c_client *client)
892{
893 if (client->flags & I2C_CLIENT_TEN) {
894 /* 10-bit address, all values are valid */
895 if (client->addr > 0x3ff)
896 return -EINVAL;
897 } else {
898 /* 7-bit address, reject the general call address */
899 if (client->addr == 0x00 || client->addr > 0x7f)
900 return -EINVAL;
901 }
902 return 0;
903}
904
Jean Delvare656b8762010-06-03 11:33:53 +0200905/* And this is a strict address validity check, used when probing. If a
906 * device uses a reserved address, then it shouldn't be probed. 7-bit
907 * addressing is assumed, 10-bit address devices are rare and should be
908 * explicitly enumerated. */
909static int i2c_check_addr_validity(unsigned short addr)
910{
911 /*
912 * Reserved addresses per I2C specification:
913 * 0x00 General call address / START byte
914 * 0x01 CBUS address
915 * 0x02 Reserved for different bus format
916 * 0x03 Reserved for future purposes
917 * 0x04-0x07 Hs-mode master code
918 * 0x78-0x7b 10-bit slave addressing
919 * 0x7c-0x7f Reserved for future purposes
920 */
921 if (addr < 0x08 || addr > 0x77)
922 return -EINVAL;
923 return 0;
924}
925
Jean Delvare3b5f7942010-06-03 11:33:55 +0200926static int __i2c_check_addr_busy(struct device *dev, void *addrp)
927{
928 struct i2c_client *client = i2c_verify_client(dev);
929 int addr = *(int *)addrp;
930
931 if (client && client->addr == addr)
932 return -EBUSY;
933 return 0;
934}
935
Michael Lawnick08263742010-08-11 18:21:02 +0200936/* walk up mux tree */
937static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
938{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200939 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200940 int result;
941
942 result = device_for_each_child(&adapter->dev, &addr,
943 __i2c_check_addr_busy);
944
Jean Delvare97cc4d42010-10-24 18:16:57 +0200945 if (!result && parent)
946 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200947
948 return result;
949}
950
951/* recurse down mux tree */
952static int i2c_check_mux_children(struct device *dev, void *addrp)
953{
954 int result;
955
956 if (dev->type == &i2c_adapter_type)
957 result = device_for_each_child(dev, addrp,
958 i2c_check_mux_children);
959 else
960 result = __i2c_check_addr_busy(dev, addrp);
961
962 return result;
963}
964
Jean Delvare3b5f7942010-06-03 11:33:55 +0200965static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
966{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200967 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200968 int result = 0;
969
Jean Delvare97cc4d42010-10-24 18:16:57 +0200970 if (parent)
971 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200972
973 if (!result)
974 result = device_for_each_child(&adapter->dev, &addr,
975 i2c_check_mux_children);
976
977 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200978}
979
David Brownell9c1600e2007-05-01 23:26:31 +0200980/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200981 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
982 * @adapter: Target I2C bus segment
983 */
984void i2c_lock_adapter(struct i2c_adapter *adapter)
985{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200986 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
987
988 if (parent)
989 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200990 else
991 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200992}
993EXPORT_SYMBOL_GPL(i2c_lock_adapter);
994
995/**
996 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
997 * @adapter: Target I2C bus segment
998 */
999static int i2c_trylock_adapter(struct i2c_adapter *adapter)
1000{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001001 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1002
1003 if (parent)
1004 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001005 else
1006 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001007}
1008
1009/**
1010 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
1011 * @adapter: Target I2C bus segment
1012 */
1013void i2c_unlock_adapter(struct i2c_adapter *adapter)
1014{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001015 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1016
1017 if (parent)
1018 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001019 else
1020 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001021}
1022EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1023
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001024static void i2c_dev_set_name(struct i2c_adapter *adap,
1025 struct i2c_client *client)
1026{
1027 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1028
1029 if (adev) {
1030 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1031 return;
1032 }
1033
1034 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1035 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1036 client->addr | ((client->flags & I2C_CLIENT_TEN)
1037 ? 0xa000 : 0));
1038}
1039
Jean Delvarefe61e072010-08-11 18:20:58 +02001040/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001041 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001042 * @adap: the adapter managing the device
1043 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001044 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001045 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001046 * Create an i2c device. Binding is handled through driver model
1047 * probe()/remove() methods. A driver may be bound to this device when we
1048 * return from this function, or any later moment (e.g. maybe hotplugging will
1049 * load the driver module). This call is not appropriate for use by mainboard
1050 * initialization logic, which usually runs during an arch_initcall() long
1051 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001052 *
1053 * This returns the new i2c client, which may be saved for later use with
1054 * i2c_unregister_device(); or NULL to indicate an error.
1055 */
1056struct i2c_client *
1057i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1058{
1059 struct i2c_client *client;
1060 int status;
1061
1062 client = kzalloc(sizeof *client, GFP_KERNEL);
1063 if (!client)
1064 return NULL;
1065
1066 client->adapter = adap;
1067
1068 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001069
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001070 if (info->archdata)
1071 client->dev.archdata = *info->archdata;
1072
Marc Pignatee354252008-08-28 08:33:22 +02001073 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001074 client->addr = info->addr;
1075 client->irq = info->irq;
1076
David Brownell9c1600e2007-05-01 23:26:31 +02001077 strlcpy(client->name, info->type, sizeof(client->name));
1078
Jean Delvare3a89db52010-06-03 11:33:52 +02001079 /* Check for address validity */
1080 status = i2c_check_client_addr_validity(client);
1081 if (status) {
1082 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1083 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1084 goto out_err_silent;
1085 }
1086
Jean Delvaref8a227e2009-06-19 16:58:18 +02001087 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001088 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001089 if (status)
1090 goto out_err;
1091
1092 client->dev.parent = &client->adapter->dev;
1093 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001094 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001095 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001096 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001097
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001098 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001099 status = device_register(&client->dev);
1100 if (status)
1101 goto out_err;
1102
Jean Delvaref8a227e2009-06-19 16:58:18 +02001103 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1104 client->name, dev_name(&client->dev));
1105
David Brownell9c1600e2007-05-01 23:26:31 +02001106 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001107
1108out_err:
1109 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1110 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001111out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001112 kfree(client);
1113 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001114}
1115EXPORT_SYMBOL_GPL(i2c_new_device);
1116
1117
1118/**
1119 * i2c_unregister_device - reverse effect of i2c_new_device()
1120 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001121 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001122 */
1123void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001124{
David Brownella1d9e6e2007-05-01 23:26:30 +02001125 device_unregister(&client->dev);
1126}
David Brownell9c1600e2007-05-01 23:26:31 +02001127EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001128
1129
Jean Delvare60b129d2008-05-11 20:37:06 +02001130static const struct i2c_device_id dummy_id[] = {
1131 { "dummy", 0 },
1132 { },
1133};
1134
Jean Delvared2653e92008-04-29 23:11:39 +02001135static int dummy_probe(struct i2c_client *client,
1136 const struct i2c_device_id *id)
1137{
1138 return 0;
1139}
1140
1141static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001142{
1143 return 0;
1144}
1145
1146static struct i2c_driver dummy_driver = {
1147 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001148 .probe = dummy_probe,
1149 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001150 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001151};
1152
1153/**
1154 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1155 * @adapter: the adapter managing the device
1156 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001157 * Context: can sleep
1158 *
1159 * This returns an I2C client bound to the "dummy" driver, intended for use
1160 * with devices that consume multiple addresses. Examples of such chips
1161 * include various EEPROMS (like 24c04 and 24c08 models).
1162 *
1163 * These dummy devices have two main uses. First, most I2C and SMBus calls
1164 * except i2c_transfer() need a client handle; the dummy will be that handle.
1165 * And second, this prevents the specified address from being bound to a
1166 * different driver.
1167 *
1168 * This returns the new i2c client, which should be saved for later use with
1169 * i2c_unregister_device(); or NULL to indicate an error.
1170 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001171struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001172{
1173 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001174 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001175 };
1176
David Brownelle9f13732008-01-27 18:14:52 +01001177 return i2c_new_device(adapter, &info);
1178}
1179EXPORT_SYMBOL_GPL(i2c_new_dummy);
1180
David Brownellf37dd802007-02-13 22:09:00 +01001181/* ------------------------------------------------------------------------- */
1182
David Brownell16ffadf2007-05-01 23:26:28 +02001183/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1184
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001185static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
David Brownellef2c83212007-05-01 23:26:28 +02001187 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 complete(&adap->dev_released);
1189}
1190
Jean Delvare99cd8e22009-06-19 16:58:20 +02001191/*
Jean Delvare390946b2012-09-10 10:14:02 +02001192 * This function is only needed for mutex_lock_nested, so it is never
1193 * called unless locking correctness checking is enabled. Thus we
1194 * make it inline to avoid a compiler warning. That's what gcc ends up
1195 * doing anyway.
1196 */
1197static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1198{
1199 unsigned int depth = 0;
1200
1201 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1202 depth++;
1203
1204 return depth;
1205}
1206
1207/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001208 * Let users instantiate I2C devices through sysfs. This can be used when
1209 * platform initialization code doesn't contain the proper data for
1210 * whatever reason. Also useful for drivers that do device detection and
1211 * detection fails, either because the device uses an unexpected address,
1212 * or this is a compatible device with different ID register values.
1213 *
1214 * Parameter checking may look overzealous, but we really don't want
1215 * the user to provide incorrect parameters.
1216 */
1217static ssize_t
1218i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1219 const char *buf, size_t count)
1220{
1221 struct i2c_adapter *adap = to_i2c_adapter(dev);
1222 struct i2c_board_info info;
1223 struct i2c_client *client;
1224 char *blank, end;
1225 int res;
1226
Jean Delvare99cd8e22009-06-19 16:58:20 +02001227 memset(&info, 0, sizeof(struct i2c_board_info));
1228
1229 blank = strchr(buf, ' ');
1230 if (!blank) {
1231 dev_err(dev, "%s: Missing parameters\n", "new_device");
1232 return -EINVAL;
1233 }
1234 if (blank - buf > I2C_NAME_SIZE - 1) {
1235 dev_err(dev, "%s: Invalid device name\n", "new_device");
1236 return -EINVAL;
1237 }
1238 memcpy(info.type, buf, blank - buf);
1239
1240 /* Parse remaining parameters, reject extra parameters */
1241 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1242 if (res < 1) {
1243 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1244 return -EINVAL;
1245 }
1246 if (res > 1 && end != '\n') {
1247 dev_err(dev, "%s: Extra parameters\n", "new_device");
1248 return -EINVAL;
1249 }
1250
Jean Delvare99cd8e22009-06-19 16:58:20 +02001251 client = i2c_new_device(adap, &info);
1252 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001253 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001254
1255 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001256 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001257 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001258 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001259 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1260 info.type, info.addr);
1261
1262 return count;
1263}
1264
1265/*
1266 * And of course let the users delete the devices they instantiated, if
1267 * they got it wrong. This interface can only be used to delete devices
1268 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1269 * don't delete devices to which some kernel code still has references.
1270 *
1271 * Parameter checking may look overzealous, but we really don't want
1272 * the user to delete the wrong device.
1273 */
1274static ssize_t
1275i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1276 const char *buf, size_t count)
1277{
1278 struct i2c_adapter *adap = to_i2c_adapter(dev);
1279 struct i2c_client *client, *next;
1280 unsigned short addr;
1281 char end;
1282 int res;
1283
1284 /* Parse parameters, reject extra parameters */
1285 res = sscanf(buf, "%hi%c", &addr, &end);
1286 if (res < 1) {
1287 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1288 return -EINVAL;
1289 }
1290 if (res > 1 && end != '\n') {
1291 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1292 return -EINVAL;
1293 }
1294
1295 /* Make sure the device was added through sysfs */
1296 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001297 mutex_lock_nested(&adap->userspace_clients_lock,
1298 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001299 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1300 detected) {
1301 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001302 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1303 "delete_device", client->name, client->addr);
1304
1305 list_del(&client->detected);
1306 i2c_unregister_device(client);
1307 res = count;
1308 break;
1309 }
1310 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001311 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001312
1313 if (res < 0)
1314 dev_err(dev, "%s: Can't find device in list\n",
1315 "delete_device");
1316 return res;
1317}
1318
Jean Delvare4f8cf822009-09-18 22:45:46 +02001319static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001320static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1321 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001322
1323static struct attribute *i2c_adapter_attrs[] = {
1324 &dev_attr_name.attr,
1325 &dev_attr_new_device.attr,
1326 &dev_attr_delete_device.attr,
1327 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001328};
1329
Jean Delvare4f8cf822009-09-18 22:45:46 +02001330static struct attribute_group i2c_adapter_attr_group = {
1331 .attrs = i2c_adapter_attrs,
1332};
1333
1334static const struct attribute_group *i2c_adapter_attr_groups[] = {
1335 &i2c_adapter_attr_group,
1336 NULL
1337};
1338
Michael Lawnick08263742010-08-11 18:21:02 +02001339struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001340 .groups = i2c_adapter_attr_groups,
1341 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001342};
Michael Lawnick08263742010-08-11 18:21:02 +02001343EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Stephen Warren643dd092012-04-17 12:43:33 -06001345/**
1346 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1347 * @dev: device, probably from some driver model iterator
1348 *
1349 * When traversing the driver model tree, perhaps using driver model
1350 * iterators like @device_for_each_child(), you can't assume very much
1351 * about the nodes you find. Use this function to avoid oopses caused
1352 * by wrongly treating some non-I2C device as an i2c_adapter.
1353 */
1354struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1355{
1356 return (dev->type == &i2c_adapter_type)
1357 ? to_i2c_adapter(dev)
1358 : NULL;
1359}
1360EXPORT_SYMBOL(i2c_verify_adapter);
1361
Jean Delvare2bb50952009-09-18 22:45:46 +02001362#ifdef CONFIG_I2C_COMPAT
1363static struct class_compat *i2c_adapter_compat_class;
1364#endif
1365
David Brownell9c1600e2007-05-01 23:26:31 +02001366static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1367{
1368 struct i2c_devinfo *devinfo;
1369
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001370 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001371 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1372 if (devinfo->busnum == adapter->nr
1373 && !i2c_new_device(adapter,
1374 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001375 dev_err(&adapter->dev,
1376 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001377 devinfo->board_info.addr);
1378 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001379 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001380}
1381
Wolfram Sang687b81d2013-07-11 12:56:15 +01001382/* OF support code */
1383
1384#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001385static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1386 struct device_node *node)
1387{
1388 struct i2c_client *result;
1389 struct i2c_board_info info = {};
1390 struct dev_archdata dev_ad = {};
1391 const __be32 *addr;
1392 int len;
1393
1394 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1395
1396 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1397 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1398 node->full_name);
1399 return ERR_PTR(-EINVAL);
1400 }
1401
1402 addr = of_get_property(node, "reg", &len);
1403 if (!addr || (len < sizeof(int))) {
1404 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1405 node->full_name);
1406 return ERR_PTR(-EINVAL);
1407 }
1408
1409 info.addr = be32_to_cpup(addr);
1410 if (info.addr > (1 << 10) - 1) {
1411 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1412 info.addr, node->full_name);
1413 return ERR_PTR(-EINVAL);
1414 }
1415
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001416 info.of_node = of_node_get(node);
1417 info.archdata = &dev_ad;
1418
1419 if (of_get_property(node, "wakeup-source", NULL))
1420 info.flags |= I2C_CLIENT_WAKE;
1421
1422 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1423
1424 result = i2c_new_device(adap, &info);
1425 if (result == NULL) {
1426 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1427 node->full_name);
1428 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001429 return ERR_PTR(-EINVAL);
1430 }
1431 return result;
1432}
1433
Wolfram Sang687b81d2013-07-11 12:56:15 +01001434static void of_i2c_register_devices(struct i2c_adapter *adap)
1435{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001436 struct device_node *node;
1437
1438 /* Only register child devices if the adapter has a node pointer set */
1439 if (!adap->dev.of_node)
1440 return;
1441
1442 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1443
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001444 for_each_available_child_of_node(adap->dev.of_node, node)
1445 of_i2c_register_device(adap, node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001446}
1447
1448static int of_dev_node_match(struct device *dev, void *data)
1449{
1450 return dev->of_node == data;
1451}
1452
1453/* must call put_device() when done with returned i2c_client device */
1454struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1455{
1456 struct device *dev;
1457
1458 dev = bus_find_device(&i2c_bus_type, NULL, node,
1459 of_dev_node_match);
1460 if (!dev)
1461 return NULL;
1462
1463 return i2c_verify_client(dev);
1464}
1465EXPORT_SYMBOL(of_find_i2c_device_by_node);
1466
1467/* must call put_device() when done with returned i2c_adapter device */
1468struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1469{
1470 struct device *dev;
1471
1472 dev = bus_find_device(&i2c_bus_type, NULL, node,
1473 of_dev_node_match);
1474 if (!dev)
1475 return NULL;
1476
1477 return i2c_verify_adapter(dev);
1478}
1479EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1480#else
1481static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1482#endif /* CONFIG_OF */
1483
Jean Delvare69b00892009-12-06 17:06:27 +01001484static int i2c_do_add_adapter(struct i2c_driver *driver,
1485 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001486{
Jean Delvare4735c982008-07-14 22:38:36 +02001487 /* Detect supported devices on that bus, and instantiate them */
1488 i2c_detect(adap, driver);
1489
1490 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001491 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001492 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1493 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001494 dev_warn(&adap->dev, "Please use another way to instantiate "
1495 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001496 /* We ignore the return code; if it fails, too bad */
1497 driver->attach_adapter(adap);
1498 }
1499 return 0;
1500}
1501
Jean Delvare69b00892009-12-06 17:06:27 +01001502static int __process_new_adapter(struct device_driver *d, void *data)
1503{
1504 return i2c_do_add_adapter(to_i2c_driver(d), data);
1505}
1506
David Brownell6e13e642007-05-01 23:26:31 +02001507static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Jean Delvared6703282010-08-11 18:20:59 +02001509 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
David Brownell1d0b19c2008-10-14 17:30:05 +02001511 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001512 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1513 res = -EAGAIN;
1514 goto out_list;
1515 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001516
Jean Delvare2236baa2010-11-15 22:40:38 +01001517 /* Sanity checks */
1518 if (unlikely(adap->name[0] == '\0')) {
1519 pr_err("i2c-core: Attempt to register an adapter with "
1520 "no name!\n");
1521 return -EINVAL;
1522 }
1523 if (unlikely(!adap->algo)) {
1524 pr_err("i2c-core: Attempt to register adapter '%s' with "
1525 "no algo!\n", adap->name);
1526 return -EINVAL;
1527 }
1528
Mika Kuoppala194684e2009-12-06 17:06:22 +01001529 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001530 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001531 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Jean Delvare8fcfef62009-03-28 21:34:43 +01001533 /* Set default timeout to 1 second if not already set */
1534 if (adap->timeout == 0)
1535 adap->timeout = HZ;
1536
Kay Sievers27d9c182009-01-07 14:29:16 +01001537 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001538 adap->dev.bus = &i2c_bus_type;
1539 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001540 res = device_register(&adap->dev);
1541 if (res)
1542 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001544 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1545
Jean Delvare2bb50952009-09-18 22:45:46 +02001546#ifdef CONFIG_I2C_COMPAT
1547 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1548 adap->dev.parent);
1549 if (res)
1550 dev_warn(&adap->dev,
1551 "Failed to create compatibility class link\n");
1552#endif
1553
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301554 /* bus recovery specific initialization */
1555 if (adap->bus_recovery_info) {
1556 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1557
1558 if (!bri->recover_bus) {
1559 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1560 adap->bus_recovery_info = NULL;
1561 goto exit_recovery;
1562 }
1563
1564 /* Generic GPIO recovery */
1565 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1566 if (!gpio_is_valid(bri->scl_gpio)) {
1567 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1568 adap->bus_recovery_info = NULL;
1569 goto exit_recovery;
1570 }
1571
1572 if (gpio_is_valid(bri->sda_gpio))
1573 bri->get_sda = get_sda_gpio_value;
1574 else
1575 bri->get_sda = NULL;
1576
1577 bri->get_scl = get_scl_gpio_value;
1578 bri->set_scl = set_scl_gpio_value;
1579 } else if (!bri->set_scl || !bri->get_scl) {
1580 /* Generic SCL recovery */
1581 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1582 adap->bus_recovery_info = NULL;
1583 }
1584 }
1585
1586exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001587 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001588 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001589 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001590 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001591
David Brownell6e13e642007-05-01 23:26:31 +02001592 if (adap->nr < __i2c_first_dynamic_bus_num)
1593 i2c_scan_static_board_info(adap);
1594
Jean Delvare4735c982008-07-14 22:38:36 +02001595 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001596 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001597 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001598 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001599
1600 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001601
Jean Delvareb119c6c2006-08-15 18:26:30 +02001602out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001603 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001604 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001605 mutex_unlock(&core_lock);
1606 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
David Brownell6e13e642007-05-01 23:26:31 +02001609/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001610 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1611 * @adap: the adapter to register (with adap->nr initialized)
1612 * Context: can sleep
1613 *
1614 * See i2c_add_numbered_adapter() for details.
1615 */
1616static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1617{
1618 int id;
1619
1620 mutex_lock(&core_lock);
1621 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1622 GFP_KERNEL);
1623 mutex_unlock(&core_lock);
1624 if (id < 0)
1625 return id == -ENOSPC ? -EBUSY : id;
1626
1627 return i2c_register_adapter(adap);
1628}
1629
1630/**
David Brownell6e13e642007-05-01 23:26:31 +02001631 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1632 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001633 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001634 *
1635 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001636 * doesn't matter or when its bus number is specified by an dt alias.
1637 * Examples of bases when the bus number doesn't matter: I2C adapters
1638 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001639 *
1640 * When this returns zero, a new bus number was allocated and stored
1641 * in adap->nr, and the specified adapter became available for clients.
1642 * Otherwise, a negative errno value is returned.
1643 */
1644int i2c_add_adapter(struct i2c_adapter *adapter)
1645{
Doug Andersonee5c2742013-03-01 08:57:31 -08001646 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001647 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001648
Doug Andersonee5c2742013-03-01 08:57:31 -08001649 if (dev->of_node) {
1650 id = of_alias_get_id(dev->of_node, "i2c");
1651 if (id >= 0) {
1652 adapter->nr = id;
1653 return __i2c_add_numbered_adapter(adapter);
1654 }
1655 }
1656
Jean Delvarecaada322008-01-27 18:14:49 +01001657 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001658 id = idr_alloc(&i2c_adapter_idr, adapter,
1659 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001660 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001661 if (id < 0)
1662 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001663
1664 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001665
David Brownell6e13e642007-05-01 23:26:31 +02001666 return i2c_register_adapter(adapter);
1667}
1668EXPORT_SYMBOL(i2c_add_adapter);
1669
1670/**
1671 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1672 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001673 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001674 *
1675 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001676 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1677 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001678 * is used to properly configure I2C devices.
1679 *
Grant Likely488bf312011-07-25 17:49:43 +02001680 * If the requested bus number is set to -1, then this function will behave
1681 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1682 *
David Brownell6e13e642007-05-01 23:26:31 +02001683 * If no devices have pre-been declared for this bus, then be sure to
1684 * register the adapter before any dynamically allocated ones. Otherwise
1685 * the required bus ID may not be available.
1686 *
1687 * When this returns zero, the specified adapter became available for
1688 * clients using the bus number provided in adap->nr. Also, the table
1689 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1690 * and the appropriate driver model device nodes are created. Otherwise, a
1691 * negative errno value is returned.
1692 */
1693int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1694{
Grant Likely488bf312011-07-25 17:49:43 +02001695 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1696 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001697
Doug Andersonee5c2742013-03-01 08:57:31 -08001698 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001699}
1700EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1701
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001702static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001703 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001704{
Jean Delvare4735c982008-07-14 22:38:36 +02001705 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001706
Jean Delvareacec2112009-03-28 21:34:40 +01001707 /* Remove the devices we created ourselves as the result of hardware
1708 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001709 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1710 if (client->adapter == adapter) {
1711 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1712 client->name, client->addr);
1713 list_del(&client->detected);
1714 i2c_unregister_device(client);
1715 }
1716 }
Jean Delvare026526f2008-01-27 18:14:49 +01001717}
1718
Jean Delvaree549c2b2009-06-19 16:58:19 +02001719static int __unregister_client(struct device *dev, void *dummy)
1720{
1721 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001722 if (client && strcmp(client->name, "dummy"))
1723 i2c_unregister_device(client);
1724 return 0;
1725}
1726
1727static int __unregister_dummy(struct device *dev, void *dummy)
1728{
1729 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001730 if (client)
1731 i2c_unregister_device(client);
1732 return 0;
1733}
1734
Jean Delvare69b00892009-12-06 17:06:27 +01001735static int __process_removed_adapter(struct device_driver *d, void *data)
1736{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001737 i2c_do_del_adapter(to_i2c_driver(d), data);
1738 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001739}
1740
David Brownelld64f73b2007-07-12 14:12:28 +02001741/**
1742 * i2c_del_adapter - unregister I2C adapter
1743 * @adap: the adapter being unregistered
1744 * Context: can sleep
1745 *
1746 * This unregisters an I2C adapter which was previously registered
1747 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1748 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001749void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001751 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001752 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001755 mutex_lock(&core_lock);
1756 found = idr_find(&i2c_adapter_idr, adap->nr);
1757 mutex_unlock(&core_lock);
1758 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001759 pr_debug("i2c-core: attempting to delete unregistered "
1760 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001761 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763
Lan Tianyu5d98e612014-05-20 20:59:23 +08001764 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001765 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001766 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001767 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001768 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001769 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001771 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001772 mutex_lock_nested(&adap->userspace_clients_lock,
1773 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001774 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1775 detected) {
1776 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1777 client->addr);
1778 list_del(&client->detected);
1779 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001780 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001781 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001782
Jean Delvaree549c2b2009-06-19 16:58:19 +02001783 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001784 * check the returned value. This is a two-pass process, because
1785 * we can't remove the dummy devices during the first pass: they
1786 * could have been instantiated by real devices wishing to clean
1787 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001788 device_for_each_child(&adap->dev, NULL, __unregister_client);
1789 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jean Delvare2bb50952009-09-18 22:45:46 +02001791#ifdef CONFIG_I2C_COMPAT
1792 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1793 adap->dev.parent);
1794#endif
1795
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001796 /* device name is gone after device_unregister */
1797 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* clean up the sysfs representation */
1800 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /* wait for sysfs to drop all references */
1804 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Brownell6e13e642007-05-01 23:26:31 +02001806 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001807 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001809 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001811 /* Clear the device structure in case this adapter is ever going to be
1812 added again */
1813 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
David Brownellc0564602007-05-01 23:26:31 +02001815EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
David Brownell7b4fbc52007-05-01 23:26:30 +02001817/* ------------------------------------------------------------------------- */
1818
Jean Delvare7ae31482011-03-20 14:50:52 +01001819int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1820{
1821 int res;
1822
1823 mutex_lock(&core_lock);
1824 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1825 mutex_unlock(&core_lock);
1826
1827 return res;
1828}
1829EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1830
Jean Delvare69b00892009-12-06 17:06:27 +01001831static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001832{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001833 if (dev->type != &i2c_adapter_type)
1834 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001835 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001836}
1837
David Brownell7b4fbc52007-05-01 23:26:30 +02001838/*
1839 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001840 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
1842
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001843int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001845 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
David Brownell1d0b19c2008-10-14 17:30:05 +02001847 /* Can't register until after driver model init */
1848 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1849 return -EAGAIN;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001852 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jean Delvare729d6dd2009-06-19 16:58:18 +02001855 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001856 * will have called probe() for all matching-but-unbound devices.
1857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 res = driver_register(&driver->driver);
1859 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001860 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001861
Mark Brownf4e8db32011-01-14 22:03:50 +01001862 /* Drivers should switch to dev_pm_ops instead. */
1863 if (driver->suspend)
1864 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1865 driver->driver.name);
1866 if (driver->resume)
1867 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1868 driver->driver.name);
1869
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001870 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Jean Delvare4735c982008-07-14 22:38:36 +02001872 INIT_LIST_HEAD(&driver->clients);
1873 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001874 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001875
Jean Delvare7eebcb72006-02-05 23:28:21 +01001876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001878EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Jean Delvare69b00892009-12-06 17:06:27 +01001880static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001881{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001882 if (dev->type == &i2c_adapter_type)
1883 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1884 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001885}
1886
David Brownella1d9e6e2007-05-01 23:26:30 +02001887/**
1888 * i2c_del_driver - unregister I2C driver
1889 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001890 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001891 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001892void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Jean Delvare7ae31482011-03-20 14:50:52 +01001894 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001897 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
David Brownellc0564602007-05-01 23:26:31 +02001899EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
David Brownell7b4fbc52007-05-01 23:26:30 +02001901/* ------------------------------------------------------------------------- */
1902
Jean Delvaree48d3312008-01-27 18:14:48 +01001903/**
1904 * i2c_use_client - increments the reference count of the i2c client structure
1905 * @client: the client being referenced
1906 *
1907 * Each live reference to a client should be refcounted. The driver model does
1908 * that automatically as part of driver binding, so that most drivers don't
1909 * need to do this explicitly: they hold a reference until they're unbound
1910 * from the device.
1911 *
1912 * A pointer to the client with the incremented reference counter is returned.
1913 */
1914struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
David Brownell6ea438e2008-07-14 22:38:24 +02001916 if (client && get_device(&client->dev))
1917 return client;
1918 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
David Brownellc0564602007-05-01 23:26:31 +02001920EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jean Delvaree48d3312008-01-27 18:14:48 +01001922/**
1923 * i2c_release_client - release a use of the i2c client structure
1924 * @client: the client being no longer referenced
1925 *
1926 * Must be called when a user of a client is finished with it.
1927 */
1928void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
David Brownell6ea438e2008-07-14 22:38:24 +02001930 if (client)
1931 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
David Brownellc0564602007-05-01 23:26:31 +02001933EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
David Brownell9b766b82008-01-27 18:14:51 +01001935struct i2c_cmd_arg {
1936 unsigned cmd;
1937 void *arg;
1938};
1939
1940static int i2c_cmd(struct device *dev, void *_arg)
1941{
1942 struct i2c_client *client = i2c_verify_client(dev);
1943 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001944 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001945
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001946 if (!client || !client->dev.driver)
1947 return 0;
1948
1949 driver = to_i2c_driver(client->dev.driver);
1950 if (driver->command)
1951 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001952 return 0;
1953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1956{
David Brownell9b766b82008-01-27 18:14:51 +01001957 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
David Brownell9b766b82008-01-27 18:14:51 +01001959 cmd_arg.cmd = cmd;
1960 cmd_arg.arg = arg;
1961 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
David Brownellc0564602007-05-01 23:26:31 +02001963EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001965#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1966static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1967 void *arg)
1968{
1969 struct of_reconfig_data *rd = arg;
1970 struct i2c_adapter *adap;
1971 struct i2c_client *client;
1972
1973 switch (of_reconfig_get_state_change(action, rd)) {
1974 case OF_RECONFIG_CHANGE_ADD:
1975 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1976 if (adap == NULL)
1977 return NOTIFY_OK; /* not for us */
1978
1979 client = of_i2c_register_device(adap, rd->dn);
1980 put_device(&adap->dev);
1981
1982 if (IS_ERR(client)) {
1983 pr_err("%s: failed to create for '%s'\n",
1984 __func__, rd->dn->full_name);
1985 return notifier_from_errno(PTR_ERR(client));
1986 }
1987 break;
1988 case OF_RECONFIG_CHANGE_REMOVE:
1989 /* find our device by node */
1990 client = of_find_i2c_device_by_node(rd->dn);
1991 if (client == NULL)
1992 return NOTIFY_OK; /* no? not meant for us */
1993
1994 /* unregister takes one ref away */
1995 i2c_unregister_device(client);
1996
1997 /* and put the reference of the find */
1998 put_device(&client->dev);
1999 break;
2000 }
2001
2002 return NOTIFY_OK;
2003}
2004static struct notifier_block i2c_of_notifier = {
2005 .notifier_call = of_i2c_notify,
2006};
2007#else
2008extern struct notifier_block i2c_of_notifier;
2009#endif /* CONFIG_OF_DYNAMIC */
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011static int __init i2c_init(void)
2012{
2013 int retval;
2014
2015 retval = bus_register(&i2c_bus_type);
2016 if (retval)
2017 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02002018#ifdef CONFIG_I2C_COMPAT
2019 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2020 if (!i2c_adapter_compat_class) {
2021 retval = -ENOMEM;
2022 goto bus_err;
2023 }
2024#endif
David Brownelle9f13732008-01-27 18:14:52 +01002025 retval = i2c_add_driver(&dummy_driver);
2026 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002027 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002028
2029 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2030 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
2031
David Brownelle9f13732008-01-27 18:14:52 +01002032 return 0;
2033
Jean Delvare2bb50952009-09-18 22:45:46 +02002034class_err:
2035#ifdef CONFIG_I2C_COMPAT
2036 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002037bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002038#endif
David Brownelle9f13732008-01-27 18:14:52 +01002039 bus_unregister(&i2c_bus_type);
2040 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043static void __exit i2c_exit(void)
2044{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002045 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2046 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002047 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002048#ifdef CONFIG_I2C_COMPAT
2049 class_compat_unregister(i2c_adapter_compat_class);
2050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002052 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
David Brownella10f9e72008-10-14 17:30:06 +02002055/* We must initialize early, because some subsystems register i2c drivers
2056 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2057 */
2058postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059module_exit(i2c_exit);
2060
2061/* ----------------------------------------------------
2062 * the functional interface to the i2c busses.
2063 * ----------------------------------------------------
2064 */
2065
David Brownella1cdeda2008-07-14 22:38:24 +02002066/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002067 * __i2c_transfer - unlocked flavor of i2c_transfer
2068 * @adap: Handle to I2C bus
2069 * @msgs: One or more messages to execute before STOP is issued to
2070 * terminate the operation; each message begins with a START.
2071 * @num: Number of messages to be executed.
2072 *
2073 * Returns negative errno, else the number of messages executed.
2074 *
2075 * Adapter lock must be held when calling this function. No debug logging
2076 * takes place. adap->algo->master_xfer existence isn't checked.
2077 */
2078int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2079{
2080 unsigned long orig_jiffies;
2081 int ret, try;
2082
David Howellsd9a83d62014-03-06 13:35:59 +00002083 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2084 * enabled. This is an efficient way of keeping the for-loop from
2085 * being executed when not needed.
2086 */
2087 if (static_key_false(&i2c_trace_msg)) {
2088 int i;
2089 for (i = 0; i < num; i++)
2090 if (msgs[i].flags & I2C_M_RD)
2091 trace_i2c_read(adap, &msgs[i], i);
2092 else
2093 trace_i2c_write(adap, &msgs[i], i);
2094 }
2095
Jean Delvareb37d2a32012-06-29 07:47:19 -03002096 /* Retry automatically on arbitration loss */
2097 orig_jiffies = jiffies;
2098 for (ret = 0, try = 0; try <= adap->retries; try++) {
2099 ret = adap->algo->master_xfer(adap, msgs, num);
2100 if (ret != -EAGAIN)
2101 break;
2102 if (time_after(jiffies, orig_jiffies + adap->timeout))
2103 break;
2104 }
2105
David Howellsd9a83d62014-03-06 13:35:59 +00002106 if (static_key_false(&i2c_trace_msg)) {
2107 int i;
2108 for (i = 0; i < ret; i++)
2109 if (msgs[i].flags & I2C_M_RD)
2110 trace_i2c_reply(adap, &msgs[i], i);
2111 trace_i2c_result(adap, i, ret);
2112 }
2113
Jean Delvareb37d2a32012-06-29 07:47:19 -03002114 return ret;
2115}
2116EXPORT_SYMBOL(__i2c_transfer);
2117
2118/**
David Brownella1cdeda2008-07-14 22:38:24 +02002119 * i2c_transfer - execute a single or combined I2C message
2120 * @adap: Handle to I2C bus
2121 * @msgs: One or more messages to execute before STOP is issued to
2122 * terminate the operation; each message begins with a START.
2123 * @num: Number of messages to be executed.
2124 *
2125 * Returns negative errno, else the number of messages executed.
2126 *
2127 * Note that there is no requirement that each message be sent to
2128 * the same slave address, although that is the most common model.
2129 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002130int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002132 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Brownella1cdeda2008-07-14 22:38:24 +02002134 /* REVISIT the fault reporting model here is weak:
2135 *
2136 * - When we get an error after receiving N bytes from a slave,
2137 * there is no way to report "N".
2138 *
2139 * - When we get a NAK after transmitting N bytes to a slave,
2140 * there is no way to report "N" ... or to let the master
2141 * continue executing the rest of this combined message, if
2142 * that's the appropriate response.
2143 *
2144 * - When for example "num" is two and we successfully complete
2145 * the first message but get an error part way through the
2146 * second, it's unclear whether that should be reported as
2147 * one (discarding status on the second message) or errno
2148 * (discarding status on the first one).
2149 */
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (adap->algo->master_xfer) {
2152#ifdef DEBUG
2153 for (ret = 0; ret < num; ret++) {
2154 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002155 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2156 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2157 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159#endif
2160
Mike Rapoportcea443a2008-01-27 18:14:50 +01002161 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002162 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002163 if (!ret)
2164 /* I2C activity is ongoing. */
2165 return -EAGAIN;
2166 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002167 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002168 }
2169
Jean Delvareb37d2a32012-06-29 07:47:19 -03002170 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002171 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 return ret;
2174 } else {
2175 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002176 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178}
David Brownellc0564602007-05-01 23:26:31 +02002179EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
David Brownella1cdeda2008-07-14 22:38:24 +02002181/**
2182 * i2c_master_send - issue a single I2C message in master transmit mode
2183 * @client: Handle to slave device
2184 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002185 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002186 *
2187 * Returns negative errno, or else the number of bytes written.
2188 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002189int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002192 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct i2c_msg msg;
2194
Jean Delvare815f55f2005-05-07 22:58:46 +02002195 msg.addr = client->addr;
2196 msg.flags = client->flags & I2C_M_TEN;
2197 msg.len = count;
2198 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002199
Jean Delvare815f55f2005-05-07 22:58:46 +02002200 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002202 /*
2203 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2204 * transmitted, else error code.
2205 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002206 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
David Brownellc0564602007-05-01 23:26:31 +02002208EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
David Brownella1cdeda2008-07-14 22:38:24 +02002210/**
2211 * i2c_master_recv - issue a single I2C message in master receive mode
2212 * @client: Handle to slave device
2213 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002214 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002215 *
2216 * Returns negative errno, or else the number of bytes read.
2217 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002218int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Farid Hammane7225acf2010-05-21 18:40:58 +02002220 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct i2c_msg msg;
2222 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Jean Delvare815f55f2005-05-07 22:58:46 +02002224 msg.addr = client->addr;
2225 msg.flags = client->flags & I2C_M_TEN;
2226 msg.flags |= I2C_M_RD;
2227 msg.len = count;
2228 msg.buf = buf;
2229
2230 ret = i2c_transfer(adap, &msg, 1);
2231
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002232 /*
2233 * If everything went ok (i.e. 1 msg received), return #bytes received,
2234 * else error code.
2235 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002236 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
David Brownellc0564602007-05-01 23:26:31 +02002238EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/* ----------------------------------------------------
2241 * the i2c address scanning function
2242 * Will not work for 10-bit addresses!
2243 * ----------------------------------------------------
2244 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002245
Jean Delvare63e4e802010-06-03 11:33:51 +02002246/*
2247 * Legacy default probe function, mostly relevant for SMBus. The default
2248 * probe method is a quick write, but it is known to corrupt the 24RF08
2249 * EEPROMs due to a state machine bug, and could also irreversibly
2250 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2251 * we use a short byte read instead. Also, some bus drivers don't implement
2252 * quick write, so we fallback to a byte read in that case too.
2253 * On x86, there is another special case for FSC hardware monitoring chips,
2254 * which want regular byte reads (address 0x73.) Fortunately, these are the
2255 * only known chips using this I2C address on PC hardware.
2256 * Returns 1 if probe succeeded, 0 if not.
2257 */
2258static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2259{
2260 int err;
2261 union i2c_smbus_data dummy;
2262
2263#ifdef CONFIG_X86
2264 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2265 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2266 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2267 I2C_SMBUS_BYTE_DATA, &dummy);
2268 else
2269#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002270 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2271 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002272 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2273 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002274 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2275 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2276 I2C_SMBUS_BYTE, &dummy);
2277 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002278 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2279 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002280 err = -EOPNOTSUPP;
2281 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002282
2283 return err >= 0;
2284}
2285
Jean Delvareccfbbd02009-12-06 17:06:25 +01002286static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002287 struct i2c_driver *driver)
2288{
2289 struct i2c_board_info info;
2290 struct i2c_adapter *adapter = temp_client->adapter;
2291 int addr = temp_client->addr;
2292 int err;
2293
2294 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002295 err = i2c_check_addr_validity(addr);
2296 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002297 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2298 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002299 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002300 }
2301
2302 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002303 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002304 return 0;
2305
Jean Delvareccfbbd02009-12-06 17:06:25 +01002306 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002307 if (!i2c_default_probe(adapter, addr))
2308 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002309
2310 /* Finally call the custom detection function */
2311 memset(&info, 0, sizeof(struct i2c_board_info));
2312 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002313 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002314 if (err) {
2315 /* -ENODEV is returned if the detection fails. We catch it
2316 here as this isn't an error. */
2317 return err == -ENODEV ? 0 : err;
2318 }
2319
2320 /* Consistency check */
2321 if (info.type[0] == '\0') {
2322 dev_err(&adapter->dev, "%s detection function provided "
2323 "no name for 0x%x\n", driver->driver.name,
2324 addr);
2325 } else {
2326 struct i2c_client *client;
2327
2328 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002329 if (adapter->class & I2C_CLASS_DEPRECATED)
2330 dev_warn(&adapter->dev,
2331 "This adapter will soon drop class based instantiation of devices. "
2332 "Please make sure client 0x%02x gets instantiated by other means. "
2333 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2334 info.addr);
2335
Jean Delvare4735c982008-07-14 22:38:36 +02002336 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2337 info.type, info.addr);
2338 client = i2c_new_device(adapter, &info);
2339 if (client)
2340 list_add_tail(&client->detected, &driver->clients);
2341 else
2342 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2343 info.type, info.addr);
2344 }
2345 return 0;
2346}
2347
2348static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2349{
Jean Delvarec3813d62009-12-14 21:17:25 +01002350 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002351 struct i2c_client *temp_client;
2352 int i, err = 0;
2353 int adap_id = i2c_adapter_id(adapter);
2354
Jean Delvarec3813d62009-12-14 21:17:25 +01002355 address_list = driver->address_list;
2356 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002357 return 0;
2358
Wolfram Sang45552272014-07-10 13:46:21 +02002359 /* Warn that the adapter lost class based instantiation */
2360 if (adapter->class == I2C_CLASS_DEPRECATED) {
2361 dev_dbg(&adapter->dev,
2362 "This adapter dropped support for I2C classes and "
2363 "won't auto-detect %s devices anymore. If you need it, check "
2364 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2365 driver->driver.name);
2366 return 0;
2367 }
2368
Jean Delvare51b54ba2010-10-24 18:16:58 +02002369 /* Stop here if the classes do not match */
2370 if (!(adapter->class & driver->class))
2371 return 0;
2372
Jean Delvare4735c982008-07-14 22:38:36 +02002373 /* Set up a temporary client to help detect callback */
2374 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2375 if (!temp_client)
2376 return -ENOMEM;
2377 temp_client->adapter = adapter;
2378
Jean Delvarec3813d62009-12-14 21:17:25 +01002379 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002380 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002381 "addr 0x%02x\n", adap_id, address_list[i]);
2382 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002383 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002384 if (unlikely(err))
2385 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002386 }
2387
Jean Delvare4735c982008-07-14 22:38:36 +02002388 kfree(temp_client);
2389 return err;
2390}
2391
Jean Delvared44f19d2010-08-11 18:20:57 +02002392int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2393{
2394 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2395 I2C_SMBUS_QUICK, NULL) >= 0;
2396}
2397EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2398
Jean Delvare12b5053a2007-05-01 23:26:31 +02002399struct i2c_client *
2400i2c_new_probed_device(struct i2c_adapter *adap,
2401 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002402 unsigned short const *addr_list,
2403 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002404{
2405 int i;
2406
Jean Delvare8031d792010-08-11 18:21:00 +02002407 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002408 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002409
Jean Delvare12b5053a2007-05-01 23:26:31 +02002410 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2411 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002412 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002413 dev_warn(&adap->dev, "Invalid 7-bit address "
2414 "0x%02x\n", addr_list[i]);
2415 continue;
2416 }
2417
2418 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002419 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002420 dev_dbg(&adap->dev, "Address 0x%02x already in "
2421 "use, not probing\n", addr_list[i]);
2422 continue;
2423 }
2424
Jean Delvare63e4e802010-06-03 11:33:51 +02002425 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002426 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002427 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002428 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002429
2430 if (addr_list[i] == I2C_CLIENT_END) {
2431 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2432 return NULL;
2433 }
2434
2435 info->addr = addr_list[i];
2436 return i2c_new_device(adap, info);
2437}
2438EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2439
Jean Delvared735b342011-03-20 14:50:52 +01002440struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002443
Jean Delvarecaada322008-01-27 18:14:49 +01002444 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002445 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002446 if (adapter && !try_module_get(adapter->owner))
2447 adapter = NULL;
2448
Jean Delvarecaada322008-01-27 18:14:49 +01002449 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002450 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
David Brownellc0564602007-05-01 23:26:31 +02002452EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454void i2c_put_adapter(struct i2c_adapter *adap)
2455{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002456 if (adap)
2457 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
David Brownellc0564602007-05-01 23:26:31 +02002459EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461/* The SMBus parts */
2462
David Brownell438d6c22006-12-10 21:21:31 +01002463#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002464static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002467
Farid Hammane7225acf2010-05-21 18:40:58 +02002468 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002469 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 data = data ^ POLY;
2471 data = data << 1;
2472 }
2473 return (u8)(data >> 8);
2474}
2475
Jean Delvare421ef472005-10-26 21:28:55 +02002476/* Incremental CRC8 over count bytes in the array pointed to by p */
2477static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 int i;
2480
Farid Hammane7225acf2010-05-21 18:40:58 +02002481 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002482 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return crc;
2484}
2485
Jean Delvare421ef472005-10-26 21:28:55 +02002486/* Assume a 7-bit address, which is reasonable for SMBus */
2487static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Jean Delvare421ef472005-10-26 21:28:55 +02002489 /* The address will be sent first */
2490 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2491 pec = i2c_smbus_pec(pec, &addr, 1);
2492
2493 /* The data buffer follows */
2494 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Jean Delvare421ef472005-10-26 21:28:55 +02002497/* Used for write only transactions */
2498static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Jean Delvare421ef472005-10-26 21:28:55 +02002500 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2501 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Jean Delvare421ef472005-10-26 21:28:55 +02002504/* Return <0 on CRC error
2505 If there was a write before this read (most cases) we need to take the
2506 partial CRC from the write part into account.
2507 Note that this function does modify the message (we need to decrease the
2508 message length to hide the CRC byte from the caller). */
2509static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Jean Delvare421ef472005-10-26 21:28:55 +02002511 u8 rpec = msg->buf[--msg->len];
2512 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (rpec != cpec) {
2515 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2516 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002517 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
David Brownell438d6c22006-12-10 21:21:31 +01002519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
David Brownella1cdeda2008-07-14 22:38:24 +02002522/**
2523 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2524 * @client: Handle to slave device
2525 *
2526 * This executes the SMBus "receive byte" protocol, returning negative errno
2527 * else the byte received from the device.
2528 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002529s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002532 int status;
2533
2534 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2535 I2C_SMBUS_READ, 0,
2536 I2C_SMBUS_BYTE, &data);
2537 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
David Brownellc0564602007-05-01 23:26:31 +02002539EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
David Brownella1cdeda2008-07-14 22:38:24 +02002541/**
2542 * i2c_smbus_write_byte - SMBus "send byte" protocol
2543 * @client: Handle to slave device
2544 * @value: Byte to be sent
2545 *
2546 * This executes the SMBus "send byte" protocol, returning negative errno
2547 * else zero on success.
2548 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002549s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Farid Hammane7225acf2010-05-21 18:40:58 +02002551 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002552 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
David Brownellc0564602007-05-01 23:26:31 +02002554EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
David Brownella1cdeda2008-07-14 22:38:24 +02002556/**
2557 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2558 * @client: Handle to slave device
2559 * @command: Byte interpreted by slave
2560 *
2561 * This executes the SMBus "read byte" protocol, returning negative errno
2562 * else a data byte received from the device.
2563 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002564s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002567 int status;
2568
2569 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2570 I2C_SMBUS_READ, command,
2571 I2C_SMBUS_BYTE_DATA, &data);
2572 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
David Brownellc0564602007-05-01 23:26:31 +02002574EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David Brownella1cdeda2008-07-14 22:38:24 +02002576/**
2577 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2578 * @client: Handle to slave device
2579 * @command: Byte interpreted by slave
2580 * @value: Byte being written
2581 *
2582 * This executes the SMBus "write byte" protocol, returning negative errno
2583 * else zero on success.
2584 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002585s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2586 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 union i2c_smbus_data data;
2589 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002590 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2591 I2C_SMBUS_WRITE, command,
2592 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
David Brownellc0564602007-05-01 23:26:31 +02002594EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
David Brownella1cdeda2008-07-14 22:38:24 +02002596/**
2597 * i2c_smbus_read_word_data - SMBus "read word" protocol
2598 * @client: Handle to slave device
2599 * @command: Byte interpreted by slave
2600 *
2601 * This executes the SMBus "read word" protocol, returning negative errno
2602 * else a 16-bit unsigned "word" received from the device.
2603 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002604s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
2606 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002607 int status;
2608
2609 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2610 I2C_SMBUS_READ, command,
2611 I2C_SMBUS_WORD_DATA, &data);
2612 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
David Brownellc0564602007-05-01 23:26:31 +02002614EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
David Brownella1cdeda2008-07-14 22:38:24 +02002616/**
2617 * i2c_smbus_write_word_data - SMBus "write word" protocol
2618 * @client: Handle to slave device
2619 * @command: Byte interpreted by slave
2620 * @value: 16-bit "word" being written
2621 *
2622 * This executes the SMBus "write word" protocol, returning negative errno
2623 * else zero on success.
2624 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002625s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2626 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
2628 union i2c_smbus_data data;
2629 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002630 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2631 I2C_SMBUS_WRITE, command,
2632 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
David Brownellc0564602007-05-01 23:26:31 +02002634EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
David Brownella64ec072007-10-13 23:56:31 +02002636/**
David Brownella1cdeda2008-07-14 22:38:24 +02002637 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002638 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002639 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002640 * @values: Byte array into which data will be read; big enough to hold
2641 * the data returned by the slave. SMBus allows at most 32 bytes.
2642 *
David Brownella1cdeda2008-07-14 22:38:24 +02002643 * This executes the SMBus "block read" protocol, returning negative errno
2644 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002645 *
2646 * Note that using this function requires that the client's adapter support
2647 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2648 * support this; its emulation through I2C messaging relies on a specific
2649 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2650 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002651s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002652 u8 *values)
2653{
2654 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002655 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002656
David Brownell24a5bb72008-07-14 22:38:23 +02002657 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2658 I2C_SMBUS_READ, command,
2659 I2C_SMBUS_BLOCK_DATA, &data);
2660 if (status)
2661 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002662
2663 memcpy(values, &data.block[1], data.block[0]);
2664 return data.block[0];
2665}
2666EXPORT_SYMBOL(i2c_smbus_read_block_data);
2667
David Brownella1cdeda2008-07-14 22:38:24 +02002668/**
2669 * i2c_smbus_write_block_data - SMBus "block write" protocol
2670 * @client: Handle to slave device
2671 * @command: Byte interpreted by slave
2672 * @length: Size of data block; SMBus allows at most 32 bytes
2673 * @values: Byte array which will be written.
2674 *
2675 * This executes the SMBus "block write" protocol, returning negative errno
2676 * else zero on success.
2677 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002678s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002679 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (length > I2C_SMBUS_BLOCK_MAX)
2684 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002686 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002687 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2688 I2C_SMBUS_WRITE, command,
2689 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
David Brownellc0564602007-05-01 23:26:31 +02002691EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002694s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002695 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002698 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002699
Jean Delvare4b2643d2007-07-12 14:12:29 +02002700 if (length > I2C_SMBUS_BLOCK_MAX)
2701 length = I2C_SMBUS_BLOCK_MAX;
2702 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002703 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2704 I2C_SMBUS_READ, command,
2705 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2706 if (status < 0)
2707 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002708
2709 memcpy(values, &data.block[1], data.block[0]);
2710 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
David Brownellc0564602007-05-01 23:26:31 +02002712EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Jean Delvare0cc43a12011-01-10 22:11:23 +01002714s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002715 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002716{
2717 union i2c_smbus_data data;
2718
2719 if (length > I2C_SMBUS_BLOCK_MAX)
2720 length = I2C_SMBUS_BLOCK_MAX;
2721 data.block[0] = length;
2722 memcpy(data.block + 1, values, length);
2723 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2724 I2C_SMBUS_WRITE, command,
2725 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2726}
David Brownellc0564602007-05-01 23:26:31 +02002727EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002728
David Brownell438d6c22006-12-10 21:21:31 +01002729/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002731static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2732 unsigned short flags,
2733 char read_write, u8 command, int size,
2734 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 /* So we need to generate a series of msgs. In the case of writing, we
2737 need to use only one message; when reading, we need two. We initialize
2738 most things with sane defaults, to keep the code below somewhat
2739 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002740 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2741 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002742 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002744 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002745 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002746 struct i2c_msg msg[2] = {
2747 {
2748 .addr = addr,
2749 .flags = flags,
2750 .len = 1,
2751 .buf = msgbuf0,
2752 }, {
2753 .addr = addr,
2754 .flags = flags | I2C_M_RD,
2755 .len = 0,
2756 .buf = msgbuf1,
2757 },
2758 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002761 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 case I2C_SMBUS_QUICK:
2763 msg[0].len = 0;
2764 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002765 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2766 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 num = 1;
2768 break;
2769 case I2C_SMBUS_BYTE:
2770 if (read_write == I2C_SMBUS_READ) {
2771 /* Special case: only a read! */
2772 msg[0].flags = I2C_M_RD | flags;
2773 num = 1;
2774 }
2775 break;
2776 case I2C_SMBUS_BYTE_DATA:
2777 if (read_write == I2C_SMBUS_READ)
2778 msg[1].len = 1;
2779 else {
2780 msg[0].len = 2;
2781 msgbuf0[1] = data->byte;
2782 }
2783 break;
2784 case I2C_SMBUS_WORD_DATA:
2785 if (read_write == I2C_SMBUS_READ)
2786 msg[1].len = 2;
2787 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002788 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002790 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792 break;
2793 case I2C_SMBUS_PROC_CALL:
2794 num = 2; /* Special case */
2795 read_write = I2C_SMBUS_READ;
2796 msg[0].len = 3;
2797 msg[1].len = 2;
2798 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002799 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 break;
2801 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002803 msg[1].flags |= I2C_M_RECV_LEN;
2804 msg[1].len = 1; /* block length will be added by
2805 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 } else {
2807 msg[0].len = data->block[0] + 2;
2808 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002809 dev_err(&adapter->dev,
2810 "Invalid block write size %d\n",
2811 data->block[0]);
2812 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002814 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 msgbuf0[i] = data->block[i-1];
2816 }
2817 break;
2818 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002819 num = 2; /* Another special case */
2820 read_write = I2C_SMBUS_READ;
2821 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002822 dev_err(&adapter->dev,
2823 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002824 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002825 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002826 }
2827 msg[0].len = data->block[0] + 2;
2828 for (i = 1; i < msg[0].len; i++)
2829 msgbuf0[i] = data->block[i-1];
2830 msg[1].flags |= I2C_M_RECV_LEN;
2831 msg[1].len = 1; /* block length will be added by
2832 the underlying bus driver */
2833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 case I2C_SMBUS_I2C_BLOCK_DATA:
2835 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002836 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 } else {
2838 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002839 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002840 dev_err(&adapter->dev,
2841 "Invalid block write size %d\n",
2842 data->block[0]);
2843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845 for (i = 1; i <= data->block[0]; i++)
2846 msgbuf0[i] = data->block[i];
2847 }
2848 break;
2849 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002850 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2851 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853
Jean Delvare421ef472005-10-26 21:28:55 +02002854 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2855 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2856 if (i) {
2857 /* Compute PEC if first message is a write */
2858 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002859 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002860 i2c_smbus_add_pec(&msg[0]);
2861 else /* Write followed by read */
2862 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2863 }
2864 /* Ask for PEC if last message is a read */
2865 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002866 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002867 }
2868
David Brownell24a5bb72008-07-14 22:38:23 +02002869 status = i2c_transfer(adapter, msg, num);
2870 if (status < 0)
2871 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Jean Delvare421ef472005-10-26 21:28:55 +02002873 /* Check PEC if last message is a read */
2874 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002875 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2876 if (status < 0)
2877 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002878 }
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002881 switch (size) {
2882 case I2C_SMBUS_BYTE:
2883 data->byte = msgbuf0[0];
2884 break;
2885 case I2C_SMBUS_BYTE_DATA:
2886 data->byte = msgbuf1[0];
2887 break;
2888 case I2C_SMBUS_WORD_DATA:
2889 case I2C_SMBUS_PROC_CALL:
2890 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2891 break;
2892 case I2C_SMBUS_I2C_BLOCK_DATA:
2893 for (i = 0; i < data->block[0]; i++)
2894 data->block[i+1] = msgbuf1[i];
2895 break;
2896 case I2C_SMBUS_BLOCK_DATA:
2897 case I2C_SMBUS_BLOCK_PROC_CALL:
2898 for (i = 0; i < msgbuf1[0] + 1; i++)
2899 data->block[i] = msgbuf1[i];
2900 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 return 0;
2903}
2904
David Brownella1cdeda2008-07-14 22:38:24 +02002905/**
2906 * i2c_smbus_xfer - execute SMBus protocol operations
2907 * @adapter: Handle to I2C bus
2908 * @addr: Address of SMBus slave on that bus
2909 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2910 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2911 * @command: Byte interpreted by slave, for protocols which use such bytes
2912 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2913 * @data: Data to be read or written
2914 *
2915 * This executes an SMBus protocol operation, and returns a negative
2916 * errno code else zero on success.
2917 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002918s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002919 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002920 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002922 unsigned long orig_jiffies;
2923 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
David Howells8a325992014-03-06 13:36:06 +00002926 /* If enabled, the following two tracepoints are conditional on
2927 * read_write and protocol.
2928 */
2929 trace_smbus_write(adapter, addr, flags, read_write,
2930 command, protocol, data);
2931 trace_smbus_read(adapter, addr, flags, read_write,
2932 command, protocol);
2933
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002934 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002937 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002938
2939 /* Retry automatically on arbitration loss */
2940 orig_jiffies = jiffies;
2941 for (res = 0, try = 0; try <= adapter->retries; try++) {
2942 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2943 read_write, command,
2944 protocol, data);
2945 if (res != -EAGAIN)
2946 break;
2947 if (time_after(jiffies,
2948 orig_jiffies + adapter->timeout))
2949 break;
2950 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002951 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002953 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002954 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002955 /*
2956 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2957 * implement native support for the SMBus operation.
2958 */
2959 }
2960
David Howells8a325992014-03-06 13:36:06 +00002961 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2962 command, protocol, data);
2963
2964trace:
2965 /* If enabled, the reply tracepoint is conditional on read_write. */
2966 trace_smbus_reply(adapter, addr, flags, read_write,
2967 command, protocol, data);
2968 trace_smbus_result(adapter, addr, flags, read_write,
2969 command, protocol, res);
2970
2971 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jean Delvared5fd1202015-01-26 20:59:31 +01002975#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002976int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2977{
2978 int ret;
2979
2980 if (!client || !slave_cb)
2981 return -EINVAL;
2982
2983 if (!(client->flags & I2C_CLIENT_TEN)) {
2984 /* Enforce stricter address checking */
2985 ret = i2c_check_addr_validity(client->addr);
2986 if (ret)
2987 return ret;
2988 }
2989
2990 if (!client->adapter->algo->reg_slave)
2991 return -EOPNOTSUPP;
2992
2993 client->slave_cb = slave_cb;
2994
2995 i2c_lock_adapter(client->adapter);
2996 ret = client->adapter->algo->reg_slave(client);
2997 i2c_unlock_adapter(client->adapter);
2998
2999 if (ret)
3000 client->slave_cb = NULL;
3001
3002 return ret;
3003}
3004EXPORT_SYMBOL_GPL(i2c_slave_register);
3005
3006int i2c_slave_unregister(struct i2c_client *client)
3007{
3008 int ret;
3009
3010 if (!client->adapter->algo->unreg_slave)
3011 return -EOPNOTSUPP;
3012
3013 i2c_lock_adapter(client->adapter);
3014 ret = client->adapter->algo->unreg_slave(client);
3015 i2c_unlock_adapter(client->adapter);
3016
3017 if (ret == 0)
3018 client->slave_cb = NULL;
3019
3020 return ret;
3021}
3022EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003023#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3026MODULE_DESCRIPTION("I2C-Bus main module");
3027MODULE_LICENSE("GPL");