blob: c21b3de70234b9ecf155969a753692f1bec3738d [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 Delvare7c81c60f2014-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 Rapoportcea443a82008-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;
Mika Westerberg393cc1ce2014-12-29 15:48:48 +0200105 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200106 info->addr = sb->slave_address;
107 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
108 info->flags |= I2C_CLIENT_TEN;
109 }
Mika Westerbergdab472e2015-05-06 13:29:07 +0300110 } else if (!info->irq) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200111 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));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100136 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200137
138 INIT_LIST_HEAD(&resource_list);
139 ret = acpi_dev_get_resources(adev, &resource_list,
140 acpi_i2c_add_resource, &info);
141 acpi_dev_free_resource_list(&resource_list);
142
143 if (ret < 0 || !info.addr)
144 return AE_OK;
145
146 adev->power.flags.ignore_parent = true;
147 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
148 if (!i2c_new_device(adapter, &info)) {
149 adev->power.flags.ignore_parent = false;
150 dev_err(&adapter->dev,
151 "failed to add I2C device %s from ACPI\n",
152 dev_name(&adev->dev));
153 }
154
155 return AE_OK;
156}
157
158/**
159 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
160 * @adap: pointer to adapter
161 *
162 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
163 * namespace. When a device is found it will be added to the Linux device
164 * model and bound to the corresponding ACPI handle.
165 */
166static void acpi_i2c_register_devices(struct i2c_adapter *adap)
167{
168 acpi_handle handle;
169 acpi_status status;
170
171 if (!adap->dev.parent)
172 return;
173
174 handle = ACPI_HANDLE(adap->dev.parent);
175 if (!handle)
176 return;
177
178 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
179 acpi_i2c_add_device, NULL,
180 adap, NULL);
181 if (ACPI_FAILURE(status))
182 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
183}
184
185#else /* CONFIG_ACPI */
186static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
187#endif /* CONFIG_ACPI */
188
189#ifdef CONFIG_ACPI_I2C_OPREGION
190static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
191 u8 cmd, u8 *data, u8 data_len)
192{
193
194 struct i2c_msg msgs[2];
195 int ret;
196 u8 *buffer;
197
198 buffer = kzalloc(data_len, GFP_KERNEL);
199 if (!buffer)
200 return AE_NO_MEMORY;
201
202 msgs[0].addr = client->addr;
203 msgs[0].flags = client->flags;
204 msgs[0].len = 1;
205 msgs[0].buf = &cmd;
206
207 msgs[1].addr = client->addr;
208 msgs[1].flags = client->flags | I2C_M_RD;
209 msgs[1].len = data_len;
210 msgs[1].buf = buffer;
211
212 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
213 if (ret < 0)
214 dev_err(&client->adapter->dev, "i2c read failed\n");
215 else
216 memcpy(data, buffer, data_len);
217
218 kfree(buffer);
219 return ret;
220}
221
222static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
223 u8 cmd, u8 *data, u8 data_len)
224{
225
226 struct i2c_msg msgs[1];
227 u8 *buffer;
228 int ret = AE_OK;
229
230 buffer = kzalloc(data_len + 1, GFP_KERNEL);
231 if (!buffer)
232 return AE_NO_MEMORY;
233
234 buffer[0] = cmd;
235 memcpy(buffer + 1, data, data_len);
236
237 msgs[0].addr = client->addr;
238 msgs[0].flags = client->flags;
239 msgs[0].len = data_len + 1;
240 msgs[0].buf = buffer;
241
242 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
243 if (ret < 0)
244 dev_err(&client->adapter->dev, "i2c write failed\n");
245
246 kfree(buffer);
247 return ret;
248}
249
250static acpi_status
251acpi_i2c_space_handler(u32 function, acpi_physical_address command,
252 u32 bits, u64 *value64,
253 void *handler_context, void *region_context)
254{
255 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
256 struct acpi_i2c_handler_data *data = handler_context;
257 struct acpi_connection_info *info = &data->info;
258 struct acpi_resource_i2c_serialbus *sb;
259 struct i2c_adapter *adapter = data->adapter;
260 struct i2c_client client;
261 struct acpi_resource *ares;
262 u32 accessor_type = function >> 16;
263 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100264 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 int status;
266
267 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
268 if (ACPI_FAILURE(ret))
269 return ret;
270
271 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
272 ret = AE_BAD_PARAMETER;
273 goto err;
274 }
275
276 sb = &ares->data.i2c_serial_bus;
277 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
278 ret = AE_BAD_PARAMETER;
279 goto err;
280 }
281
282 memset(&client, 0, sizeof(client));
283 client.adapter = adapter;
284 client.addr = sb->slave_address;
285 client.flags = 0;
286
287 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
288 client.flags |= I2C_CLIENT_TEN;
289
290 switch (accessor_type) {
291 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
292 if (action == ACPI_READ) {
293 status = i2c_smbus_read_byte(&client);
294 if (status >= 0) {
295 gsb->bdata = status;
296 status = 0;
297 }
298 } else {
299 status = i2c_smbus_write_byte(&client, gsb->bdata);
300 }
301 break;
302
303 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
304 if (action == ACPI_READ) {
305 status = i2c_smbus_read_byte_data(&client, command);
306 if (status >= 0) {
307 gsb->bdata = status;
308 status = 0;
309 }
310 } else {
311 status = i2c_smbus_write_byte_data(&client, command,
312 gsb->bdata);
313 }
314 break;
315
316 case ACPI_GSB_ACCESS_ATTRIB_WORD:
317 if (action == ACPI_READ) {
318 status = i2c_smbus_read_word_data(&client, command);
319 if (status >= 0) {
320 gsb->wdata = status;
321 status = 0;
322 }
323 } else {
324 status = i2c_smbus_write_word_data(&client, command,
325 gsb->wdata);
326 }
327 break;
328
329 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
330 if (action == ACPI_READ) {
331 status = i2c_smbus_read_block_data(&client, command,
332 gsb->data);
333 if (status >= 0) {
334 gsb->len = status;
335 status = 0;
336 }
337 } else {
338 status = i2c_smbus_write_block_data(&client, command,
339 gsb->len, gsb->data);
340 }
341 break;
342
343 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
344 if (action == ACPI_READ) {
345 status = acpi_gsb_i2c_read_bytes(&client, command,
346 gsb->data, info->access_length);
347 if (status > 0)
348 status = 0;
349 } else {
350 status = acpi_gsb_i2c_write_bytes(&client, command,
351 gsb->data, info->access_length);
352 }
353 break;
354
355 default:
356 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
357 ret = AE_BAD_PARAMETER;
358 goto err;
359 }
360
361 gsb->status = status;
362
363 err:
364 ACPI_FREE(ares);
365 return ret;
366}
367
368
369static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
370{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200371 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200372 struct acpi_i2c_handler_data *data;
373 acpi_status status;
374
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200375 if (!adapter->dev.parent)
376 return -ENODEV;
377
378 handle = ACPI_HANDLE(adapter->dev.parent);
379
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200380 if (!handle)
381 return -ENODEV;
382
383 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
384 GFP_KERNEL);
385 if (!data)
386 return -ENOMEM;
387
388 data->adapter = adapter;
389 status = acpi_bus_attach_private_data(handle, (void *)data);
390 if (ACPI_FAILURE(status)) {
391 kfree(data);
392 return -ENOMEM;
393 }
394
395 status = acpi_install_address_space_handler(handle,
396 ACPI_ADR_SPACE_GSBUS,
397 &acpi_i2c_space_handler,
398 NULL,
399 data);
400 if (ACPI_FAILURE(status)) {
401 dev_err(&adapter->dev, "Error installing i2c space handler\n");
402 acpi_bus_detach_private_data(handle);
403 kfree(data);
404 return -ENOMEM;
405 }
406
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800407 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200408 return 0;
409}
410
411static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
412{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200413 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200414 struct acpi_i2c_handler_data *data;
415 acpi_status status;
416
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200417 if (!adapter->dev.parent)
418 return;
419
420 handle = ACPI_HANDLE(adapter->dev.parent);
421
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200422 if (!handle)
423 return;
424
425 acpi_remove_address_space_handler(handle,
426 ACPI_ADR_SPACE_GSBUS,
427 &acpi_i2c_space_handler);
428
429 status = acpi_bus_get_private_data(handle, (void **)&data);
430 if (ACPI_SUCCESS(status))
431 kfree(data);
432
433 acpi_bus_detach_private_data(handle);
434}
435#else /* CONFIG_ACPI_I2C_OPREGION */
436static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
437{ }
438
439static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
440{ return 0; }
441#endif /* CONFIG_ACPI_I2C_OPREGION */
442
David Brownellf37dd802007-02-13 22:09:00 +0100443/* ------------------------------------------------------------------------- */
444
Jean Delvared2653e92008-04-29 23:11:39 +0200445static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
446 const struct i2c_client *client)
447{
448 while (id->name[0]) {
449 if (strcmp(client->name, id->name) == 0)
450 return id;
451 id++;
452 }
453 return NULL;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456static int i2c_device_match(struct device *dev, struct device_driver *drv)
457{
Jean Delvare51298d12009-09-18 22:45:45 +0200458 struct i2c_client *client = i2c_verify_client(dev);
459 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200460
Jean Delvare51298d12009-09-18 22:45:45 +0200461 if (!client)
462 return 0;
463
Grant Likely959e85f2010-06-08 07:48:19 -0600464 /* Attempt an OF style match */
465 if (of_driver_match_device(dev, drv))
466 return 1;
467
Mika Westerberg907ddf82012-11-23 12:23:40 +0100468 /* Then ACPI style match */
469 if (acpi_driver_match_device(dev, drv))
470 return 1;
471
Jean Delvare51298d12009-09-18 22:45:45 +0200472 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200473 /* match on an id table if there is one */
474 if (driver->id_table)
475 return i2c_match_id(driver->id_table, client) != NULL;
476
Jean Delvareeb8a7902008-05-18 20:49:41 +0200477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
David Brownell7b4fbc52007-05-01 23:26:30 +0200480
481/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200482static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200483{
484 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800485 int rc;
486
487 rc = acpi_device_uevent_modalias(dev, env);
488 if (rc != -ENODEV)
489 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200490
Jean Delvareeb8a7902008-05-18 20:49:41 +0200491 if (add_uevent_var(env, "MODALIAS=%s%s",
492 I2C_MODULE_PREFIX, client->name))
493 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200494 dev_dbg(dev, "uevent\n");
495 return 0;
496}
497
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530498/* i2c bus recovery routines */
499static int get_scl_gpio_value(struct i2c_adapter *adap)
500{
501 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
502}
503
504static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
505{
506 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
507}
508
509static int get_sda_gpio_value(struct i2c_adapter *adap)
510{
511 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
512}
513
514static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
515{
516 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
517 struct device *dev = &adap->dev;
518 int ret = 0;
519
520 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
521 GPIOF_OUT_INIT_HIGH, "i2c-scl");
522 if (ret) {
523 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
524 return ret;
525 }
526
527 if (bri->get_sda) {
528 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
529 /* work without SDA polling */
530 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
531 bri->sda_gpio);
532 bri->get_sda = NULL;
533 }
534 }
535
536 return ret;
537}
538
539static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
540{
541 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
542
543 if (bri->get_sda)
544 gpio_free(bri->sda_gpio);
545
546 gpio_free(bri->scl_gpio);
547}
548
549/*
550 * We are generating clock pulses. ndelay() determines durating of clk pulses.
551 * We will generate clock with rate 100 KHz and so duration of both clock levels
552 * is: delay in ns = (10^6 / 100) / 2
553 */
554#define RECOVERY_NDELAY 5000
555#define RECOVERY_CLK_CNT 9
556
557static int i2c_generic_recovery(struct i2c_adapter *adap)
558{
559 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
560 int i = 0, val = 1, ret = 0;
561
562 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300563 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530564
565 /*
566 * By this time SCL is high, as we need to give 9 falling-rising edges
567 */
568 while (i++ < RECOVERY_CLK_CNT * 2) {
569 if (val) {
570 /* Break if SDA is high */
571 if (bri->get_sda && bri->get_sda(adap))
572 break;
573 /* SCL shouldn't be low here */
574 if (!bri->get_scl(adap)) {
575 dev_err(&adap->dev,
576 "SCL is stuck low, exit recovery\n");
577 ret = -EBUSY;
578 break;
579 }
580 }
581
582 val = !val;
583 bri->set_scl(adap, val);
584 ndelay(RECOVERY_NDELAY);
585 }
586
587 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300588 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530589
590 return ret;
591}
592
593int i2c_generic_scl_recovery(struct i2c_adapter *adap)
594{
595 adap->bus_recovery_info->set_scl(adap, 1);
596 return i2c_generic_recovery(adap);
597}
Mark Brownc1c21f42015-04-15 19:18:39 +0100598EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530599
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}
Mark Brownc1c21f42015-04-15 19:18:39 +0100613EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530614
615int i2c_recover_bus(struct i2c_adapter *adap)
616{
617 if (!adap->bus_recovery_info)
618 return -EOPNOTSUPP;
619
620 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
621 return adap->bus_recovery_info->recover_bus(adap);
622}
Mark Brownc1c21f42015-04-15 19:18:39 +0100623EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static int i2c_device_probe(struct device *dev)
626{
Jean Delvare51298d12009-09-18 22:45:45 +0200627 struct i2c_client *client = i2c_verify_client(dev);
628 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100629 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200630
Jean Delvare51298d12009-09-18 22:45:45 +0200631 if (!client)
632 return 0;
633
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200634 if (!client->irq && dev->of_node) {
635 int irq = of_irq_get(dev->of_node, 0);
636
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100637 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200638 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100639 if (irq < 0)
640 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200641
642 client->irq = irq;
643 }
644
Jean Delvare51298d12009-09-18 22:45:45 +0200645 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200646 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200647 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200648
Marc Pignatee354252008-08-28 08:33:22 +0200649 if (!device_can_wakeup(&client->dev))
650 device_init_wakeup(&client->dev,
651 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200652 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200653
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200654 status = of_clk_set_defaults(dev->of_node, false);
655 if (status < 0)
656 return status;
657
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200658 status = dev_pm_domain_attach(&client->dev, true);
659 if (status != -EPROBE_DEFER) {
660 status = driver->probe(client, i2c_match_id(driver->id_table,
661 client));
662 if (status)
663 dev_pm_domain_detach(&client->dev, true);
664 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100665
Hans Verkuil50c33042008-03-12 14:15:00 +0100666 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669static int i2c_device_remove(struct device *dev)
670{
Jean Delvare51298d12009-09-18 22:45:45 +0200671 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200672 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100673 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200674
Jean Delvare51298d12009-09-18 22:45:45 +0200675 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200676 return 0;
677
678 driver = to_i2c_driver(dev->driver);
679 if (driver->remove) {
680 dev_dbg(dev, "remove\n");
681 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200682 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100683
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200684 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200685 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
David Brownellf37dd802007-02-13 22:09:00 +0100688static void i2c_device_shutdown(struct device *dev)
689{
Jean Delvare51298d12009-09-18 22:45:45 +0200690 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100691 struct i2c_driver *driver;
692
Jean Delvare51298d12009-09-18 22:45:45 +0200693 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100694 return;
695 driver = to_i2c_driver(dev->driver);
696 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200697 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100698}
699
David Brownell9c1600e2007-05-01 23:26:31 +0200700static void i2c_client_dev_release(struct device *dev)
701{
702 kfree(to_i2c_client(dev));
703}
704
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100705static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200706show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200707{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200708 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
709 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200710}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100711static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200712
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100713static ssize_t
714show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200715{
716 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800717 int len;
718
719 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
720 if (len != -ENODEV)
721 return len;
722
Jean Delvareeb8a7902008-05-18 20:49:41 +0200723 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200724}
Jean Delvare51298d12009-09-18 22:45:45 +0200725static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
726
727static struct attribute *i2c_dev_attrs[] = {
728 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200729 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200730 &dev_attr_modalias.attr,
731 NULL
732};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100733ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100734
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200735struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100736 .name = "i2c",
737 .match = i2c_device_match,
738 .probe = i2c_device_probe,
739 .remove = i2c_device_remove,
740 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000741};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200742EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000743
Jean Delvare51298d12009-09-18 22:45:45 +0200744static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100745 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200746 .uevent = i2c_device_uevent,
747 .release = i2c_client_dev_release,
748};
749
David Brownell9b766b82008-01-27 18:14:51 +0100750
751/**
752 * i2c_verify_client - return parameter as i2c_client, or NULL
753 * @dev: device, probably from some driver model iterator
754 *
755 * When traversing the driver model tree, perhaps using driver model
756 * iterators like @device_for_each_child(), you can't assume very much
757 * about the nodes you find. Use this function to avoid oopses caused
758 * by wrongly treating some non-I2C device as an i2c_client.
759 */
760struct i2c_client *i2c_verify_client(struct device *dev)
761{
Jean Delvare51298d12009-09-18 22:45:45 +0200762 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100763 ? to_i2c_client(dev)
764 : NULL;
765}
766EXPORT_SYMBOL(i2c_verify_client);
767
768
Jean Delvare3a89db52010-06-03 11:33:52 +0200769/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300770 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200771static int i2c_check_client_addr_validity(const struct i2c_client *client)
772{
773 if (client->flags & I2C_CLIENT_TEN) {
774 /* 10-bit address, all values are valid */
775 if (client->addr > 0x3ff)
776 return -EINVAL;
777 } else {
778 /* 7-bit address, reject the general call address */
779 if (client->addr == 0x00 || client->addr > 0x7f)
780 return -EINVAL;
781 }
782 return 0;
783}
784
Jean Delvare656b8762010-06-03 11:33:53 +0200785/* And this is a strict address validity check, used when probing. If a
786 * device uses a reserved address, then it shouldn't be probed. 7-bit
787 * addressing is assumed, 10-bit address devices are rare and should be
788 * explicitly enumerated. */
789static int i2c_check_addr_validity(unsigned short addr)
790{
791 /*
792 * Reserved addresses per I2C specification:
793 * 0x00 General call address / START byte
794 * 0x01 CBUS address
795 * 0x02 Reserved for different bus format
796 * 0x03 Reserved for future purposes
797 * 0x04-0x07 Hs-mode master code
798 * 0x78-0x7b 10-bit slave addressing
799 * 0x7c-0x7f Reserved for future purposes
800 */
801 if (addr < 0x08 || addr > 0x77)
802 return -EINVAL;
803 return 0;
804}
805
Jean Delvare3b5f7942010-06-03 11:33:55 +0200806static int __i2c_check_addr_busy(struct device *dev, void *addrp)
807{
808 struct i2c_client *client = i2c_verify_client(dev);
809 int addr = *(int *)addrp;
810
811 if (client && client->addr == addr)
812 return -EBUSY;
813 return 0;
814}
815
Michael Lawnick08263742010-08-11 18:21:02 +0200816/* walk up mux tree */
817static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
818{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200819 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200820 int result;
821
822 result = device_for_each_child(&adapter->dev, &addr,
823 __i2c_check_addr_busy);
824
Jean Delvare97cc4d42010-10-24 18:16:57 +0200825 if (!result && parent)
826 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200827
828 return result;
829}
830
831/* recurse down mux tree */
832static int i2c_check_mux_children(struct device *dev, void *addrp)
833{
834 int result;
835
836 if (dev->type == &i2c_adapter_type)
837 result = device_for_each_child(dev, addrp,
838 i2c_check_mux_children);
839 else
840 result = __i2c_check_addr_busy(dev, addrp);
841
842 return result;
843}
844
Jean Delvare3b5f7942010-06-03 11:33:55 +0200845static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
846{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200847 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200848 int result = 0;
849
Jean Delvare97cc4d42010-10-24 18:16:57 +0200850 if (parent)
851 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200852
853 if (!result)
854 result = device_for_each_child(&adapter->dev, &addr,
855 i2c_check_mux_children);
856
857 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200858}
859
David Brownell9c1600e2007-05-01 23:26:31 +0200860/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200861 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
862 * @adapter: Target I2C bus segment
863 */
864void i2c_lock_adapter(struct i2c_adapter *adapter)
865{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200866 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
867
868 if (parent)
869 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200870 else
871 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200872}
873EXPORT_SYMBOL_GPL(i2c_lock_adapter);
874
875/**
876 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
877 * @adapter: Target I2C bus segment
878 */
879static int i2c_trylock_adapter(struct i2c_adapter *adapter)
880{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200881 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
882
883 if (parent)
884 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200885 else
886 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200887}
888
889/**
890 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
891 * @adapter: Target I2C bus segment
892 */
893void i2c_unlock_adapter(struct i2c_adapter *adapter)
894{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200895 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
896
897 if (parent)
898 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200899 else
900 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200901}
902EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
903
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200904static void i2c_dev_set_name(struct i2c_adapter *adap,
905 struct i2c_client *client)
906{
907 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
908
909 if (adev) {
910 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
911 return;
912 }
913
914 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
915 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
916 client->addr | ((client->flags & I2C_CLIENT_TEN)
917 ? 0xa000 : 0));
918}
919
Jean Delvarefe61e072010-08-11 18:20:58 +0200920/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200921 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200922 * @adap: the adapter managing the device
923 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200924 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200925 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200926 * Create an i2c device. Binding is handled through driver model
927 * probe()/remove() methods. A driver may be bound to this device when we
928 * return from this function, or any later moment (e.g. maybe hotplugging will
929 * load the driver module). This call is not appropriate for use by mainboard
930 * initialization logic, which usually runs during an arch_initcall() long
931 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200932 *
933 * This returns the new i2c client, which may be saved for later use with
934 * i2c_unregister_device(); or NULL to indicate an error.
935 */
936struct i2c_client *
937i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
938{
939 struct i2c_client *client;
940 int status;
941
942 client = kzalloc(sizeof *client, GFP_KERNEL);
943 if (!client)
944 return NULL;
945
946 client->adapter = adap;
947
948 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200949
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200950 if (info->archdata)
951 client->dev.archdata = *info->archdata;
952
Marc Pignatee354252008-08-28 08:33:22 +0200953 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200954 client->addr = info->addr;
955 client->irq = info->irq;
956
David Brownell9c1600e2007-05-01 23:26:31 +0200957 strlcpy(client->name, info->type, sizeof(client->name));
958
Jean Delvare3a89db52010-06-03 11:33:52 +0200959 /* Check for address validity */
960 status = i2c_check_client_addr_validity(client);
961 if (status) {
962 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
963 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
964 goto out_err_silent;
965 }
966
Jean Delvaref8a227e2009-06-19 16:58:18 +0200967 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200968 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200969 if (status)
970 goto out_err;
971
972 client->dev.parent = &client->adapter->dev;
973 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200974 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700975 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100976 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200977
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200978 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200979 status = device_register(&client->dev);
980 if (status)
981 goto out_err;
982
Jean Delvaref8a227e2009-06-19 16:58:18 +0200983 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
984 client->name, dev_name(&client->dev));
985
David Brownell9c1600e2007-05-01 23:26:31 +0200986 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200987
988out_err:
989 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
990 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200991out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200992 kfree(client);
993 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200994}
995EXPORT_SYMBOL_GPL(i2c_new_device);
996
997
998/**
999 * i2c_unregister_device - reverse effect of i2c_new_device()
1000 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001001 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001002 */
1003void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001004{
David Brownella1d9e6e2007-05-01 23:26:30 +02001005 device_unregister(&client->dev);
1006}
David Brownell9c1600e2007-05-01 23:26:31 +02001007EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001008
1009
Jean Delvare60b129d2008-05-11 20:37:06 +02001010static const struct i2c_device_id dummy_id[] = {
1011 { "dummy", 0 },
1012 { },
1013};
1014
Jean Delvared2653e92008-04-29 23:11:39 +02001015static int dummy_probe(struct i2c_client *client,
1016 const struct i2c_device_id *id)
1017{
1018 return 0;
1019}
1020
1021static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001022{
1023 return 0;
1024}
1025
1026static struct i2c_driver dummy_driver = {
1027 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001028 .probe = dummy_probe,
1029 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001030 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001031};
1032
1033/**
1034 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1035 * @adapter: the adapter managing the device
1036 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001037 * Context: can sleep
1038 *
1039 * This returns an I2C client bound to the "dummy" driver, intended for use
1040 * with devices that consume multiple addresses. Examples of such chips
1041 * include various EEPROMS (like 24c04 and 24c08 models).
1042 *
1043 * These dummy devices have two main uses. First, most I2C and SMBus calls
1044 * except i2c_transfer() need a client handle; the dummy will be that handle.
1045 * And second, this prevents the specified address from being bound to a
1046 * different driver.
1047 *
1048 * This returns the new i2c client, which should be saved for later use with
1049 * i2c_unregister_device(); or NULL to indicate an error.
1050 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001051struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001052{
1053 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001054 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001055 };
1056
David Brownelle9f13732008-01-27 18:14:52 +01001057 return i2c_new_device(adapter, &info);
1058}
1059EXPORT_SYMBOL_GPL(i2c_new_dummy);
1060
David Brownellf37dd802007-02-13 22:09:00 +01001061/* ------------------------------------------------------------------------- */
1062
David Brownell16ffadf2007-05-01 23:26:28 +02001063/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1064
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001065static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
David Brownellef2c83212007-05-01 23:26:28 +02001067 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 complete(&adap->dev_released);
1069}
1070
Jean Delvare99cd8e22009-06-19 16:58:20 +02001071/*
Jean Delvare390946b2012-09-10 10:14:02 +02001072 * This function is only needed for mutex_lock_nested, so it is never
1073 * called unless locking correctness checking is enabled. Thus we
1074 * make it inline to avoid a compiler warning. That's what gcc ends up
1075 * doing anyway.
1076 */
1077static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1078{
1079 unsigned int depth = 0;
1080
1081 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1082 depth++;
1083
1084 return depth;
1085}
1086
1087/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001088 * Let users instantiate I2C devices through sysfs. This can be used when
1089 * platform initialization code doesn't contain the proper data for
1090 * whatever reason. Also useful for drivers that do device detection and
1091 * detection fails, either because the device uses an unexpected address,
1092 * or this is a compatible device with different ID register values.
1093 *
1094 * Parameter checking may look overzealous, but we really don't want
1095 * the user to provide incorrect parameters.
1096 */
1097static ssize_t
1098i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1099 const char *buf, size_t count)
1100{
1101 struct i2c_adapter *adap = to_i2c_adapter(dev);
1102 struct i2c_board_info info;
1103 struct i2c_client *client;
1104 char *blank, end;
1105 int res;
1106
Jean Delvare99cd8e22009-06-19 16:58:20 +02001107 memset(&info, 0, sizeof(struct i2c_board_info));
1108
1109 blank = strchr(buf, ' ');
1110 if (!blank) {
1111 dev_err(dev, "%s: Missing parameters\n", "new_device");
1112 return -EINVAL;
1113 }
1114 if (blank - buf > I2C_NAME_SIZE - 1) {
1115 dev_err(dev, "%s: Invalid device name\n", "new_device");
1116 return -EINVAL;
1117 }
1118 memcpy(info.type, buf, blank - buf);
1119
1120 /* Parse remaining parameters, reject extra parameters */
1121 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1122 if (res < 1) {
1123 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1124 return -EINVAL;
1125 }
1126 if (res > 1 && end != '\n') {
1127 dev_err(dev, "%s: Extra parameters\n", "new_device");
1128 return -EINVAL;
1129 }
1130
Jean Delvare99cd8e22009-06-19 16:58:20 +02001131 client = i2c_new_device(adap, &info);
1132 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001133 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001134
1135 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001136 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001137 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001138 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001139 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1140 info.type, info.addr);
1141
1142 return count;
1143}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001144static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001145
1146/*
1147 * And of course let the users delete the devices they instantiated, if
1148 * they got it wrong. This interface can only be used to delete devices
1149 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1150 * don't delete devices to which some kernel code still has references.
1151 *
1152 * Parameter checking may look overzealous, but we really don't want
1153 * the user to delete the wrong device.
1154 */
1155static ssize_t
1156i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1157 const char *buf, size_t count)
1158{
1159 struct i2c_adapter *adap = to_i2c_adapter(dev);
1160 struct i2c_client *client, *next;
1161 unsigned short addr;
1162 char end;
1163 int res;
1164
1165 /* Parse parameters, reject extra parameters */
1166 res = sscanf(buf, "%hi%c", &addr, &end);
1167 if (res < 1) {
1168 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1169 return -EINVAL;
1170 }
1171 if (res > 1 && end != '\n') {
1172 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1173 return -EINVAL;
1174 }
1175
1176 /* Make sure the device was added through sysfs */
1177 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001178 mutex_lock_nested(&adap->userspace_clients_lock,
1179 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001180 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1181 detected) {
1182 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001183 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1184 "delete_device", client->name, client->addr);
1185
1186 list_del(&client->detected);
1187 i2c_unregister_device(client);
1188 res = count;
1189 break;
1190 }
1191 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001192 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001193
1194 if (res < 0)
1195 dev_err(dev, "%s: Can't find device in list\n",
1196 "delete_device");
1197 return res;
1198}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001199static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1200 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001201
1202static struct attribute *i2c_adapter_attrs[] = {
1203 &dev_attr_name.attr,
1204 &dev_attr_new_device.attr,
1205 &dev_attr_delete_device.attr,
1206 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001207};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001208ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001209
Michael Lawnick08263742010-08-11 18:21:02 +02001210struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001211 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001212 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001213};
Michael Lawnick08263742010-08-11 18:21:02 +02001214EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Stephen Warren643dd092012-04-17 12:43:33 -06001216/**
1217 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1218 * @dev: device, probably from some driver model iterator
1219 *
1220 * When traversing the driver model tree, perhaps using driver model
1221 * iterators like @device_for_each_child(), you can't assume very much
1222 * about the nodes you find. Use this function to avoid oopses caused
1223 * by wrongly treating some non-I2C device as an i2c_adapter.
1224 */
1225struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1226{
1227 return (dev->type == &i2c_adapter_type)
1228 ? to_i2c_adapter(dev)
1229 : NULL;
1230}
1231EXPORT_SYMBOL(i2c_verify_adapter);
1232
Jean Delvare2bb50952009-09-18 22:45:46 +02001233#ifdef CONFIG_I2C_COMPAT
1234static struct class_compat *i2c_adapter_compat_class;
1235#endif
1236
David Brownell9c1600e2007-05-01 23:26:31 +02001237static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1238{
1239 struct i2c_devinfo *devinfo;
1240
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001241 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001242 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1243 if (devinfo->busnum == adapter->nr
1244 && !i2c_new_device(adapter,
1245 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001246 dev_err(&adapter->dev,
1247 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001248 devinfo->board_info.addr);
1249 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001250 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001251}
1252
Wolfram Sang687b81d2013-07-11 12:56:15 +01001253/* OF support code */
1254
1255#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001256static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1257 struct device_node *node)
1258{
1259 struct i2c_client *result;
1260 struct i2c_board_info info = {};
1261 struct dev_archdata dev_ad = {};
1262 const __be32 *addr;
1263 int len;
1264
1265 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1266
1267 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1268 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1269 node->full_name);
1270 return ERR_PTR(-EINVAL);
1271 }
1272
1273 addr = of_get_property(node, "reg", &len);
1274 if (!addr || (len < sizeof(int))) {
1275 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1276 node->full_name);
1277 return ERR_PTR(-EINVAL);
1278 }
1279
1280 info.addr = be32_to_cpup(addr);
1281 if (info.addr > (1 << 10) - 1) {
1282 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1283 info.addr, node->full_name);
1284 return ERR_PTR(-EINVAL);
1285 }
1286
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001287 info.of_node = of_node_get(node);
1288 info.archdata = &dev_ad;
1289
1290 if (of_get_property(node, "wakeup-source", NULL))
1291 info.flags |= I2C_CLIENT_WAKE;
1292
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001293 result = i2c_new_device(adap, &info);
1294 if (result == NULL) {
1295 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1296 node->full_name);
1297 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001298 return ERR_PTR(-EINVAL);
1299 }
1300 return result;
1301}
1302
Wolfram Sang687b81d2013-07-11 12:56:15 +01001303static void of_i2c_register_devices(struct i2c_adapter *adap)
1304{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001305 struct device_node *node;
1306
1307 /* Only register child devices if the adapter has a node pointer set */
1308 if (!adap->dev.of_node)
1309 return;
1310
1311 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1312
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001313 for_each_available_child_of_node(adap->dev.of_node, node)
1314 of_i2c_register_device(adap, node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001315}
1316
1317static int of_dev_node_match(struct device *dev, void *data)
1318{
1319 return dev->of_node == data;
1320}
1321
1322/* must call put_device() when done with returned i2c_client device */
1323struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1324{
1325 struct device *dev;
1326
1327 dev = bus_find_device(&i2c_bus_type, NULL, node,
1328 of_dev_node_match);
1329 if (!dev)
1330 return NULL;
1331
1332 return i2c_verify_client(dev);
1333}
1334EXPORT_SYMBOL(of_find_i2c_device_by_node);
1335
1336/* must call put_device() when done with returned i2c_adapter device */
1337struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1338{
1339 struct device *dev;
1340
1341 dev = bus_find_device(&i2c_bus_type, NULL, node,
1342 of_dev_node_match);
1343 if (!dev)
1344 return NULL;
1345
1346 return i2c_verify_adapter(dev);
1347}
1348EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1349#else
1350static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1351#endif /* CONFIG_OF */
1352
Jean Delvare69b00892009-12-06 17:06:27 +01001353static int i2c_do_add_adapter(struct i2c_driver *driver,
1354 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001355{
Jean Delvare4735c982008-07-14 22:38:36 +02001356 /* Detect supported devices on that bus, and instantiate them */
1357 i2c_detect(adap, driver);
1358
1359 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001360 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001361 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1362 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001363 dev_warn(&adap->dev, "Please use another way to instantiate "
1364 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001365 /* We ignore the return code; if it fails, too bad */
1366 driver->attach_adapter(adap);
1367 }
1368 return 0;
1369}
1370
Jean Delvare69b00892009-12-06 17:06:27 +01001371static int __process_new_adapter(struct device_driver *d, void *data)
1372{
1373 return i2c_do_add_adapter(to_i2c_driver(d), data);
1374}
1375
David Brownell6e13e642007-05-01 23:26:31 +02001376static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Jean Delvared6703282010-08-11 18:20:59 +02001378 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
David Brownell1d0b19c2008-10-14 17:30:05 +02001380 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001381 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1382 res = -EAGAIN;
1383 goto out_list;
1384 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001385
Jean Delvare2236baa72010-11-15 22:40:38 +01001386 /* Sanity checks */
1387 if (unlikely(adap->name[0] == '\0')) {
1388 pr_err("i2c-core: Attempt to register an adapter with "
1389 "no name!\n");
1390 return -EINVAL;
1391 }
1392 if (unlikely(!adap->algo)) {
1393 pr_err("i2c-core: Attempt to register adapter '%s' with "
1394 "no algo!\n", adap->name);
1395 return -EINVAL;
1396 }
1397
Mika Kuoppala194684e2009-12-06 17:06:22 +01001398 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001399 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001400 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jean Delvare8fcfef62009-03-28 21:34:43 +01001402 /* Set default timeout to 1 second if not already set */
1403 if (adap->timeout == 0)
1404 adap->timeout = HZ;
1405
Kay Sievers27d9c182009-01-07 14:29:16 +01001406 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001407 adap->dev.bus = &i2c_bus_type;
1408 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001409 res = device_register(&adap->dev);
1410 if (res)
1411 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001413 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1414
Charles Keepax6ada5c12015-04-16 13:05:19 +01001415 pm_runtime_no_callbacks(&adap->dev);
1416
Jean Delvare2bb50952009-09-18 22:45:46 +02001417#ifdef CONFIG_I2C_COMPAT
1418 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1419 adap->dev.parent);
1420 if (res)
1421 dev_warn(&adap->dev,
1422 "Failed to create compatibility class link\n");
1423#endif
1424
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301425 /* bus recovery specific initialization */
1426 if (adap->bus_recovery_info) {
1427 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1428
1429 if (!bri->recover_bus) {
1430 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1431 adap->bus_recovery_info = NULL;
1432 goto exit_recovery;
1433 }
1434
1435 /* Generic GPIO recovery */
1436 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1437 if (!gpio_is_valid(bri->scl_gpio)) {
1438 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1439 adap->bus_recovery_info = NULL;
1440 goto exit_recovery;
1441 }
1442
1443 if (gpio_is_valid(bri->sda_gpio))
1444 bri->get_sda = get_sda_gpio_value;
1445 else
1446 bri->get_sda = NULL;
1447
1448 bri->get_scl = get_scl_gpio_value;
1449 bri->set_scl = set_scl_gpio_value;
1450 } else if (!bri->set_scl || !bri->get_scl) {
1451 /* Generic SCL recovery */
1452 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1453 adap->bus_recovery_info = NULL;
1454 }
1455 }
1456
1457exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001458 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001459 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001460 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001461 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001462
David Brownell6e13e642007-05-01 23:26:31 +02001463 if (adap->nr < __i2c_first_dynamic_bus_num)
1464 i2c_scan_static_board_info(adap);
1465
Jean Delvare4735c982008-07-14 22:38:36 +02001466 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001467 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001468 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001469 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001470
1471 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001472
Jean Delvareb119c6c2006-08-15 18:26:30 +02001473out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001474 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001475 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001476 mutex_unlock(&core_lock);
1477 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
David Brownell6e13e642007-05-01 23:26:31 +02001480/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001481 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1482 * @adap: the adapter to register (with adap->nr initialized)
1483 * Context: can sleep
1484 *
1485 * See i2c_add_numbered_adapter() for details.
1486 */
1487static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1488{
1489 int id;
1490
1491 mutex_lock(&core_lock);
1492 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1493 GFP_KERNEL);
1494 mutex_unlock(&core_lock);
1495 if (id < 0)
1496 return id == -ENOSPC ? -EBUSY : id;
1497
1498 return i2c_register_adapter(adap);
1499}
1500
1501/**
David Brownell6e13e642007-05-01 23:26:31 +02001502 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1503 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001504 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001505 *
1506 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001507 * doesn't matter or when its bus number is specified by an dt alias.
1508 * Examples of bases when the bus number doesn't matter: I2C adapters
1509 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001510 *
1511 * When this returns zero, a new bus number was allocated and stored
1512 * in adap->nr, and the specified adapter became available for clients.
1513 * Otherwise, a negative errno value is returned.
1514 */
1515int i2c_add_adapter(struct i2c_adapter *adapter)
1516{
Doug Andersonee5c2742013-03-01 08:57:31 -08001517 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001518 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001519
Doug Andersonee5c2742013-03-01 08:57:31 -08001520 if (dev->of_node) {
1521 id = of_alias_get_id(dev->of_node, "i2c");
1522 if (id >= 0) {
1523 adapter->nr = id;
1524 return __i2c_add_numbered_adapter(adapter);
1525 }
1526 }
1527
Jean Delvarecaada322008-01-27 18:14:49 +01001528 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001529 id = idr_alloc(&i2c_adapter_idr, adapter,
1530 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001531 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001532 if (id < 0)
1533 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001534
1535 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001536
David Brownell6e13e642007-05-01 23:26:31 +02001537 return i2c_register_adapter(adapter);
1538}
1539EXPORT_SYMBOL(i2c_add_adapter);
1540
1541/**
1542 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1543 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001544 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001545 *
1546 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001547 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1548 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001549 * is used to properly configure I2C devices.
1550 *
Grant Likely488bf312011-07-25 17:49:43 +02001551 * If the requested bus number is set to -1, then this function will behave
1552 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1553 *
David Brownell6e13e642007-05-01 23:26:31 +02001554 * If no devices have pre-been declared for this bus, then be sure to
1555 * register the adapter before any dynamically allocated ones. Otherwise
1556 * the required bus ID may not be available.
1557 *
1558 * When this returns zero, the specified adapter became available for
1559 * clients using the bus number provided in adap->nr. Also, the table
1560 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1561 * and the appropriate driver model device nodes are created. Otherwise, a
1562 * negative errno value is returned.
1563 */
1564int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1565{
Grant Likely488bf312011-07-25 17:49:43 +02001566 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1567 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001568
Doug Andersonee5c2742013-03-01 08:57:31 -08001569 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001570}
1571EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1572
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001573static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001574 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001575{
Jean Delvare4735c982008-07-14 22:38:36 +02001576 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001577
Jean Delvareacec2112009-03-28 21:34:40 +01001578 /* Remove the devices we created ourselves as the result of hardware
1579 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001580 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1581 if (client->adapter == adapter) {
1582 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1583 client->name, client->addr);
1584 list_del(&client->detected);
1585 i2c_unregister_device(client);
1586 }
1587 }
Jean Delvare026526f2008-01-27 18:14:49 +01001588}
1589
Jean Delvaree549c2b2009-06-19 16:58:19 +02001590static int __unregister_client(struct device *dev, void *dummy)
1591{
1592 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001593 if (client && strcmp(client->name, "dummy"))
1594 i2c_unregister_device(client);
1595 return 0;
1596}
1597
1598static int __unregister_dummy(struct device *dev, void *dummy)
1599{
1600 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001601 if (client)
1602 i2c_unregister_device(client);
1603 return 0;
1604}
1605
Jean Delvare69b00892009-12-06 17:06:27 +01001606static int __process_removed_adapter(struct device_driver *d, void *data)
1607{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001608 i2c_do_del_adapter(to_i2c_driver(d), data);
1609 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001610}
1611
David Brownelld64f73b2007-07-12 14:12:28 +02001612/**
1613 * i2c_del_adapter - unregister I2C adapter
1614 * @adap: the adapter being unregistered
1615 * Context: can sleep
1616 *
1617 * This unregisters an I2C adapter which was previously registered
1618 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1619 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001620void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001622 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001623 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001626 mutex_lock(&core_lock);
1627 found = idr_find(&i2c_adapter_idr, adap->nr);
1628 mutex_unlock(&core_lock);
1629 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001630 pr_debug("i2c-core: attempting to delete unregistered "
1631 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634
Lan Tianyu5d98e612014-05-20 20:59:23 +08001635 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001636 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001637 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001638 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001639 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001640 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001642 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001643 mutex_lock_nested(&adap->userspace_clients_lock,
1644 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001645 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1646 detected) {
1647 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1648 client->addr);
1649 list_del(&client->detected);
1650 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001651 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001652 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001653
Jean Delvaree549c2b2009-06-19 16:58:19 +02001654 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001655 * check the returned value. This is a two-pass process, because
1656 * we can't remove the dummy devices during the first pass: they
1657 * could have been instantiated by real devices wishing to clean
1658 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001659 device_for_each_child(&adap->dev, NULL, __unregister_client);
1660 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jean Delvare2bb50952009-09-18 22:45:46 +02001662#ifdef CONFIG_I2C_COMPAT
1663 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1664 adap->dev.parent);
1665#endif
1666
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001667 /* device name is gone after device_unregister */
1668 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1669
Wolfram Sang26680ee2015-01-29 22:45:09 +01001670 /* wait until all references to the device are gone
1671 *
1672 * FIXME: This is old code and should ideally be replaced by an
1673 * alternative which results in decoupling the lifetime of the struct
1674 * device from the i2c_adapter, like spi or netdev do. Any solution
1675 * should be throughly tested with DEBUG_KOBJECT_RELEASE enabled!
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
David Brownell6e13e642007-05-01 23:26:31 +02001681 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001682 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001684 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001686 /* Clear the device structure in case this adapter is ever going to be
1687 added again */
1688 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
David Brownellc0564602007-05-01 23:26:31 +02001690EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
David Brownell7b4fbc52007-05-01 23:26:30 +02001692/* ------------------------------------------------------------------------- */
1693
Jean Delvare7ae31482011-03-20 14:50:52 +01001694int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1695{
1696 int res;
1697
1698 mutex_lock(&core_lock);
1699 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1700 mutex_unlock(&core_lock);
1701
1702 return res;
1703}
1704EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1705
Jean Delvare69b00892009-12-06 17:06:27 +01001706static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001707{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001708 if (dev->type != &i2c_adapter_type)
1709 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001710 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001711}
1712
David Brownell7b4fbc52007-05-01 23:26:30 +02001713/*
1714 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001715 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
1717
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001718int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001720 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
David Brownell1d0b19c2008-10-14 17:30:05 +02001722 /* Can't register until after driver model init */
1723 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1724 return -EAGAIN;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001727 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Jean Delvare729d6dd2009-06-19 16:58:18 +02001730 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001731 * will have called probe() for all matching-but-unbound devices.
1732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 res = driver_register(&driver->driver);
1734 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001735 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001736
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001737 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Jean Delvare4735c982008-07-14 22:38:36 +02001739 INIT_LIST_HEAD(&driver->clients);
1740 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001741 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001742
Jean Delvare7eebcb72006-02-05 23:28:21 +01001743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001745EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jean Delvare69b00892009-12-06 17:06:27 +01001747static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001748{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001749 if (dev->type == &i2c_adapter_type)
1750 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1751 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001752}
1753
David Brownella1d9e6e2007-05-01 23:26:30 +02001754/**
1755 * i2c_del_driver - unregister I2C driver
1756 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001757 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001758 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001759void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Jean Delvare7ae31482011-03-20 14:50:52 +01001761 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001764 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
David Brownellc0564602007-05-01 23:26:31 +02001766EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
David Brownell7b4fbc52007-05-01 23:26:30 +02001768/* ------------------------------------------------------------------------- */
1769
Jean Delvaree48d3312008-01-27 18:14:48 +01001770/**
1771 * i2c_use_client - increments the reference count of the i2c client structure
1772 * @client: the client being referenced
1773 *
1774 * Each live reference to a client should be refcounted. The driver model does
1775 * that automatically as part of driver binding, so that most drivers don't
1776 * need to do this explicitly: they hold a reference until they're unbound
1777 * from the device.
1778 *
1779 * A pointer to the client with the incremented reference counter is returned.
1780 */
1781struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
David Brownell6ea438e2008-07-14 22:38:24 +02001783 if (client && get_device(&client->dev))
1784 return client;
1785 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
David Brownellc0564602007-05-01 23:26:31 +02001787EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Jean Delvaree48d3312008-01-27 18:14:48 +01001789/**
1790 * i2c_release_client - release a use of the i2c client structure
1791 * @client: the client being no longer referenced
1792 *
1793 * Must be called when a user of a client is finished with it.
1794 */
1795void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
David Brownell6ea438e2008-07-14 22:38:24 +02001797 if (client)
1798 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
David Brownellc0564602007-05-01 23:26:31 +02001800EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
David Brownell9b766b82008-01-27 18:14:51 +01001802struct i2c_cmd_arg {
1803 unsigned cmd;
1804 void *arg;
1805};
1806
1807static int i2c_cmd(struct device *dev, void *_arg)
1808{
1809 struct i2c_client *client = i2c_verify_client(dev);
1810 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001811 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001812
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001813 if (!client || !client->dev.driver)
1814 return 0;
1815
1816 driver = to_i2c_driver(client->dev.driver);
1817 if (driver->command)
1818 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001819 return 0;
1820}
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1823{
David Brownell9b766b82008-01-27 18:14:51 +01001824 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
David Brownell9b766b82008-01-27 18:14:51 +01001826 cmd_arg.cmd = cmd;
1827 cmd_arg.arg = arg;
1828 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
David Brownellc0564602007-05-01 23:26:31 +02001830EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001832#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1833static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1834 void *arg)
1835{
1836 struct of_reconfig_data *rd = arg;
1837 struct i2c_adapter *adap;
1838 struct i2c_client *client;
1839
1840 switch (of_reconfig_get_state_change(action, rd)) {
1841 case OF_RECONFIG_CHANGE_ADD:
1842 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1843 if (adap == NULL)
1844 return NOTIFY_OK; /* not for us */
1845
1846 client = of_i2c_register_device(adap, rd->dn);
1847 put_device(&adap->dev);
1848
1849 if (IS_ERR(client)) {
1850 pr_err("%s: failed to create for '%s'\n",
1851 __func__, rd->dn->full_name);
1852 return notifier_from_errno(PTR_ERR(client));
1853 }
1854 break;
1855 case OF_RECONFIG_CHANGE_REMOVE:
1856 /* find our device by node */
1857 client = of_find_i2c_device_by_node(rd->dn);
1858 if (client == NULL)
1859 return NOTIFY_OK; /* no? not meant for us */
1860
1861 /* unregister takes one ref away */
1862 i2c_unregister_device(client);
1863
1864 /* and put the reference of the find */
1865 put_device(&client->dev);
1866 break;
1867 }
1868
1869 return NOTIFY_OK;
1870}
1871static struct notifier_block i2c_of_notifier = {
1872 .notifier_call = of_i2c_notify,
1873};
1874#else
1875extern struct notifier_block i2c_of_notifier;
1876#endif /* CONFIG_OF_DYNAMIC */
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878static int __init i2c_init(void)
1879{
1880 int retval;
1881
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001882 retval = of_alias_get_highest_id("i2c");
1883
1884 down_write(&__i2c_board_lock);
1885 if (retval >= __i2c_first_dynamic_bus_num)
1886 __i2c_first_dynamic_bus_num = retval + 1;
1887 up_write(&__i2c_board_lock);
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 retval = bus_register(&i2c_bus_type);
1890 if (retval)
1891 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001892#ifdef CONFIG_I2C_COMPAT
1893 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1894 if (!i2c_adapter_compat_class) {
1895 retval = -ENOMEM;
1896 goto bus_err;
1897 }
1898#endif
David Brownelle9f13732008-01-27 18:14:52 +01001899 retval = i2c_add_driver(&dummy_driver);
1900 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001901 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001902
1903 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1904 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1905
David Brownelle9f13732008-01-27 18:14:52 +01001906 return 0;
1907
Jean Delvare2bb50952009-09-18 22:45:46 +02001908class_err:
1909#ifdef CONFIG_I2C_COMPAT
1910 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001911bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001912#endif
David Brownelle9f13732008-01-27 18:14:52 +01001913 bus_unregister(&i2c_bus_type);
1914 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917static void __exit i2c_exit(void)
1918{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001919 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1920 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001921 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001922#ifdef CONFIG_I2C_COMPAT
1923 class_compat_unregister(i2c_adapter_compat_class);
1924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001926 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
David Brownella10f9e72008-10-14 17:30:06 +02001929/* We must initialize early, because some subsystems register i2c drivers
1930 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1931 */
1932postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933module_exit(i2c_exit);
1934
1935/* ----------------------------------------------------
1936 * the functional interface to the i2c busses.
1937 * ----------------------------------------------------
1938 */
1939
Wolfram Sangb7f62582015-01-05 23:45:59 +01001940/* Check if val is exceeding the quirk IFF quirk is non 0 */
1941#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1942
1943static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1944{
1945 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1946 err_msg, msg->addr, msg->len,
1947 msg->flags & I2C_M_RD ? "read" : "write");
1948 return -EOPNOTSUPP;
1949}
1950
1951static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1952{
1953 const struct i2c_adapter_quirks *q = adap->quirks;
1954 int max_num = q->max_num_msgs, i;
1955 bool do_len_check = true;
1956
1957 if (q->flags & I2C_AQ_COMB) {
1958 max_num = 2;
1959
1960 /* special checks for combined messages */
1961 if (num == 2) {
1962 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1963 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1964
1965 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1966 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1967
1968 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1969 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1970
1971 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1972 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1973
1974 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1975 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1976
1977 do_len_check = false;
1978 }
1979 }
1980
1981 if (i2c_quirk_exceeded(num, max_num))
1982 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1983
1984 for (i = 0; i < num; i++) {
1985 u16 len = msgs[i].len;
1986
1987 if (msgs[i].flags & I2C_M_RD) {
1988 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1989 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1990 } else {
1991 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1992 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1993 }
1994 }
1995
1996 return 0;
1997}
1998
David Brownella1cdeda2008-07-14 22:38:24 +02001999/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002000 * __i2c_transfer - unlocked flavor of i2c_transfer
2001 * @adap: Handle to I2C bus
2002 * @msgs: One or more messages to execute before STOP is issued to
2003 * terminate the operation; each message begins with a START.
2004 * @num: Number of messages to be executed.
2005 *
2006 * Returns negative errno, else the number of messages executed.
2007 *
2008 * Adapter lock must be held when calling this function. No debug logging
2009 * takes place. adap->algo->master_xfer existence isn't checked.
2010 */
2011int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2012{
2013 unsigned long orig_jiffies;
2014 int ret, try;
2015
Wolfram Sangb7f62582015-01-05 23:45:59 +01002016 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2017 return -EOPNOTSUPP;
2018
David Howellsd9a83d62014-03-06 13:35:59 +00002019 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2020 * enabled. This is an efficient way of keeping the for-loop from
2021 * being executed when not needed.
2022 */
2023 if (static_key_false(&i2c_trace_msg)) {
2024 int i;
2025 for (i = 0; i < num; i++)
2026 if (msgs[i].flags & I2C_M_RD)
2027 trace_i2c_read(adap, &msgs[i], i);
2028 else
2029 trace_i2c_write(adap, &msgs[i], i);
2030 }
2031
Jean Delvareb37d2a32012-06-29 07:47:19 -03002032 /* Retry automatically on arbitration loss */
2033 orig_jiffies = jiffies;
2034 for (ret = 0, try = 0; try <= adap->retries; try++) {
2035 ret = adap->algo->master_xfer(adap, msgs, num);
2036 if (ret != -EAGAIN)
2037 break;
2038 if (time_after(jiffies, orig_jiffies + adap->timeout))
2039 break;
2040 }
2041
David Howellsd9a83d62014-03-06 13:35:59 +00002042 if (static_key_false(&i2c_trace_msg)) {
2043 int i;
2044 for (i = 0; i < ret; i++)
2045 if (msgs[i].flags & I2C_M_RD)
2046 trace_i2c_reply(adap, &msgs[i], i);
2047 trace_i2c_result(adap, i, ret);
2048 }
2049
Jean Delvareb37d2a32012-06-29 07:47:19 -03002050 return ret;
2051}
2052EXPORT_SYMBOL(__i2c_transfer);
2053
2054/**
David Brownella1cdeda2008-07-14 22:38:24 +02002055 * i2c_transfer - execute a single or combined I2C message
2056 * @adap: Handle to I2C bus
2057 * @msgs: One or more messages to execute before STOP is issued to
2058 * terminate the operation; each message begins with a START.
2059 * @num: Number of messages to be executed.
2060 *
2061 * Returns negative errno, else the number of messages executed.
2062 *
2063 * Note that there is no requirement that each message be sent to
2064 * the same slave address, although that is the most common model.
2065 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002066int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002068 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
David Brownella1cdeda2008-07-14 22:38:24 +02002070 /* REVISIT the fault reporting model here is weak:
2071 *
2072 * - When we get an error after receiving N bytes from a slave,
2073 * there is no way to report "N".
2074 *
2075 * - When we get a NAK after transmitting N bytes to a slave,
2076 * there is no way to report "N" ... or to let the master
2077 * continue executing the rest of this combined message, if
2078 * that's the appropriate response.
2079 *
2080 * - When for example "num" is two and we successfully complete
2081 * the first message but get an error part way through the
2082 * second, it's unclear whether that should be reported as
2083 * one (discarding status on the second message) or errno
2084 * (discarding status on the first one).
2085 */
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (adap->algo->master_xfer) {
2088#ifdef DEBUG
2089 for (ret = 0; ret < num; ret++) {
2090 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002091 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2092 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2093 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095#endif
2096
Mike Rapoportcea443a82008-01-27 18:14:50 +01002097 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002098 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002099 if (!ret)
2100 /* I2C activity is ongoing. */
2101 return -EAGAIN;
2102 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002103 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002104 }
2105
Jean Delvareb37d2a32012-06-29 07:47:19 -03002106 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002107 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 return ret;
2110 } else {
2111 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002112 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114}
David Brownellc0564602007-05-01 23:26:31 +02002115EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
David Brownella1cdeda2008-07-14 22:38:24 +02002117/**
2118 * i2c_master_send - issue a single I2C message in master transmit mode
2119 * @client: Handle to slave device
2120 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002121 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002122 *
2123 * Returns negative errno, or else the number of bytes written.
2124 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002125int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002128 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct i2c_msg msg;
2130
Jean Delvare815f55f2005-05-07 22:58:46 +02002131 msg.addr = client->addr;
2132 msg.flags = client->flags & I2C_M_TEN;
2133 msg.len = count;
2134 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002135
Jean Delvare815f55f2005-05-07 22:58:46 +02002136 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002138 /*
2139 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2140 * transmitted, else error code.
2141 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002142 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
David Brownellc0564602007-05-01 23:26:31 +02002144EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Brownella1cdeda2008-07-14 22:38:24 +02002146/**
2147 * i2c_master_recv - issue a single I2C message in master receive mode
2148 * @client: Handle to slave device
2149 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002150 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002151 *
2152 * Returns negative errno, or else the number of bytes read.
2153 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002154int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Farid Hammane7225acf2010-05-21 18:40:58 +02002156 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct i2c_msg msg;
2158 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Jean Delvare815f55f2005-05-07 22:58:46 +02002160 msg.addr = client->addr;
2161 msg.flags = client->flags & I2C_M_TEN;
2162 msg.flags |= I2C_M_RD;
2163 msg.len = count;
2164 msg.buf = buf;
2165
2166 ret = i2c_transfer(adap, &msg, 1);
2167
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002168 /*
2169 * If everything went ok (i.e. 1 msg received), return #bytes received,
2170 * else error code.
2171 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002172 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
David Brownellc0564602007-05-01 23:26:31 +02002174EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/* ----------------------------------------------------
2177 * the i2c address scanning function
2178 * Will not work for 10-bit addresses!
2179 * ----------------------------------------------------
2180 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002181
Jean Delvare63e4e802010-06-03 11:33:51 +02002182/*
2183 * Legacy default probe function, mostly relevant for SMBus. The default
2184 * probe method is a quick write, but it is known to corrupt the 24RF08
2185 * EEPROMs due to a state machine bug, and could also irreversibly
2186 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2187 * we use a short byte read instead. Also, some bus drivers don't implement
2188 * quick write, so we fallback to a byte read in that case too.
2189 * On x86, there is another special case for FSC hardware monitoring chips,
2190 * which want regular byte reads (address 0x73.) Fortunately, these are the
2191 * only known chips using this I2C address on PC hardware.
2192 * Returns 1 if probe succeeded, 0 if not.
2193 */
2194static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2195{
2196 int err;
2197 union i2c_smbus_data dummy;
2198
2199#ifdef CONFIG_X86
2200 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2201 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2202 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2203 I2C_SMBUS_BYTE_DATA, &dummy);
2204 else
2205#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002206 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2207 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002208 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2209 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002210 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2211 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2212 I2C_SMBUS_BYTE, &dummy);
2213 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002214 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2215 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002216 err = -EOPNOTSUPP;
2217 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002218
2219 return err >= 0;
2220}
2221
Jean Delvareccfbbd02009-12-06 17:06:25 +01002222static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002223 struct i2c_driver *driver)
2224{
2225 struct i2c_board_info info;
2226 struct i2c_adapter *adapter = temp_client->adapter;
2227 int addr = temp_client->addr;
2228 int err;
2229
2230 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002231 err = i2c_check_addr_validity(addr);
2232 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002233 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2234 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002235 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002236 }
2237
2238 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002239 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002240 return 0;
2241
Jean Delvareccfbbd02009-12-06 17:06:25 +01002242 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002243 if (!i2c_default_probe(adapter, addr))
2244 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002245
2246 /* Finally call the custom detection function */
2247 memset(&info, 0, sizeof(struct i2c_board_info));
2248 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002249 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002250 if (err) {
2251 /* -ENODEV is returned if the detection fails. We catch it
2252 here as this isn't an error. */
2253 return err == -ENODEV ? 0 : err;
2254 }
2255
2256 /* Consistency check */
2257 if (info.type[0] == '\0') {
2258 dev_err(&adapter->dev, "%s detection function provided "
2259 "no name for 0x%x\n", driver->driver.name,
2260 addr);
2261 } else {
2262 struct i2c_client *client;
2263
2264 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002265 if (adapter->class & I2C_CLASS_DEPRECATED)
2266 dev_warn(&adapter->dev,
2267 "This adapter will soon drop class based instantiation of devices. "
2268 "Please make sure client 0x%02x gets instantiated by other means. "
2269 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2270 info.addr);
2271
Jean Delvare4735c982008-07-14 22:38:36 +02002272 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2273 info.type, info.addr);
2274 client = i2c_new_device(adapter, &info);
2275 if (client)
2276 list_add_tail(&client->detected, &driver->clients);
2277 else
2278 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2279 info.type, info.addr);
2280 }
2281 return 0;
2282}
2283
2284static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2285{
Jean Delvarec3813d62009-12-14 21:17:25 +01002286 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002287 struct i2c_client *temp_client;
2288 int i, err = 0;
2289 int adap_id = i2c_adapter_id(adapter);
2290
Jean Delvarec3813d62009-12-14 21:17:25 +01002291 address_list = driver->address_list;
2292 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002293 return 0;
2294
Wolfram Sang45552272014-07-10 13:46:21 +02002295 /* Warn that the adapter lost class based instantiation */
2296 if (adapter->class == I2C_CLASS_DEPRECATED) {
2297 dev_dbg(&adapter->dev,
2298 "This adapter dropped support for I2C classes and "
2299 "won't auto-detect %s devices anymore. If you need it, check "
2300 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2301 driver->driver.name);
2302 return 0;
2303 }
2304
Jean Delvare51b54ba2010-10-24 18:16:58 +02002305 /* Stop here if the classes do not match */
2306 if (!(adapter->class & driver->class))
2307 return 0;
2308
Jean Delvare4735c982008-07-14 22:38:36 +02002309 /* Set up a temporary client to help detect callback */
2310 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2311 if (!temp_client)
2312 return -ENOMEM;
2313 temp_client->adapter = adapter;
2314
Jean Delvarec3813d62009-12-14 21:17:25 +01002315 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002316 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002317 "addr 0x%02x\n", adap_id, address_list[i]);
2318 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002319 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002320 if (unlikely(err))
2321 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002322 }
2323
Jean Delvare4735c982008-07-14 22:38:36 +02002324 kfree(temp_client);
2325 return err;
2326}
2327
Jean Delvared44f19d2010-08-11 18:20:57 +02002328int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2329{
2330 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2331 I2C_SMBUS_QUICK, NULL) >= 0;
2332}
2333EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2334
Jean Delvare12b5053a2007-05-01 23:26:31 +02002335struct i2c_client *
2336i2c_new_probed_device(struct i2c_adapter *adap,
2337 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002338 unsigned short const *addr_list,
2339 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002340{
2341 int i;
2342
Jean Delvare8031d792010-08-11 18:21:00 +02002343 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002344 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002345
Jean Delvare12b5053a2007-05-01 23:26:31 +02002346 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2347 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002348 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002349 dev_warn(&adap->dev, "Invalid 7-bit address "
2350 "0x%02x\n", addr_list[i]);
2351 continue;
2352 }
2353
2354 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002355 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002356 dev_dbg(&adap->dev, "Address 0x%02x already in "
2357 "use, not probing\n", addr_list[i]);
2358 continue;
2359 }
2360
Jean Delvare63e4e802010-06-03 11:33:51 +02002361 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002362 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002363 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002364 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002365
2366 if (addr_list[i] == I2C_CLIENT_END) {
2367 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2368 return NULL;
2369 }
2370
2371 info->addr = addr_list[i];
2372 return i2c_new_device(adap, info);
2373}
2374EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2375
Jean Delvared735b342011-03-20 14:50:52 +01002376struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002379
Jean Delvarecaada322008-01-27 18:14:49 +01002380 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002381 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002382 if (adapter && !try_module_get(adapter->owner))
2383 adapter = NULL;
2384
Jean Delvarecaada322008-01-27 18:14:49 +01002385 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002386 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
David Brownellc0564602007-05-01 23:26:31 +02002388EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390void i2c_put_adapter(struct i2c_adapter *adap)
2391{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002392 if (adap)
2393 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
David Brownellc0564602007-05-01 23:26:31 +02002395EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397/* The SMBus parts */
2398
David Brownell438d6c22006-12-10 21:21:31 +01002399#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002400static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002403
Farid Hammane7225acf2010-05-21 18:40:58 +02002404 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002405 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 data = data ^ POLY;
2407 data = data << 1;
2408 }
2409 return (u8)(data >> 8);
2410}
2411
Jean Delvare421ef472005-10-26 21:28:55 +02002412/* Incremental CRC8 over count bytes in the array pointed to by p */
2413static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 int i;
2416
Farid Hammane7225acf2010-05-21 18:40:58 +02002417 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002418 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return crc;
2420}
2421
Jean Delvare421ef472005-10-26 21:28:55 +02002422/* Assume a 7-bit address, which is reasonable for SMBus */
2423static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Jean Delvare421ef472005-10-26 21:28:55 +02002425 /* The address will be sent first */
2426 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2427 pec = i2c_smbus_pec(pec, &addr, 1);
2428
2429 /* The data buffer follows */
2430 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Jean Delvare421ef472005-10-26 21:28:55 +02002433/* Used for write only transactions */
2434static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Jean Delvare421ef472005-10-26 21:28:55 +02002436 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2437 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Jean Delvare421ef472005-10-26 21:28:55 +02002440/* Return <0 on CRC error
2441 If there was a write before this read (most cases) we need to take the
2442 partial CRC from the write part into account.
2443 Note that this function does modify the message (we need to decrease the
2444 message length to hide the CRC byte from the caller). */
2445static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Jean Delvare421ef472005-10-26 21:28:55 +02002447 u8 rpec = msg->buf[--msg->len];
2448 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (rpec != cpec) {
2451 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2452 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002453 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
David Brownell438d6c22006-12-10 21:21:31 +01002455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
David Brownella1cdeda2008-07-14 22:38:24 +02002458/**
2459 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2460 * @client: Handle to slave device
2461 *
2462 * This executes the SMBus "receive byte" protocol, returning negative errno
2463 * else the byte received from the device.
2464 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002465s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002468 int status;
2469
2470 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2471 I2C_SMBUS_READ, 0,
2472 I2C_SMBUS_BYTE, &data);
2473 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
David Brownellc0564602007-05-01 23:26:31 +02002475EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
David Brownella1cdeda2008-07-14 22:38:24 +02002477/**
2478 * i2c_smbus_write_byte - SMBus "send byte" protocol
2479 * @client: Handle to slave device
2480 * @value: Byte to be sent
2481 *
2482 * This executes the SMBus "send byte" protocol, returning negative errno
2483 * else zero on success.
2484 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002485s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Farid Hammane7225acf2010-05-21 18:40:58 +02002487 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002488 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
David Brownellc0564602007-05-01 23:26:31 +02002490EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
David Brownella1cdeda2008-07-14 22:38:24 +02002492/**
2493 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2494 * @client: Handle to slave device
2495 * @command: Byte interpreted by slave
2496 *
2497 * This executes the SMBus "read byte" protocol, returning negative errno
2498 * else a data byte received from the device.
2499 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002500s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002503 int status;
2504
2505 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2506 I2C_SMBUS_READ, command,
2507 I2C_SMBUS_BYTE_DATA, &data);
2508 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
David Brownellc0564602007-05-01 23:26:31 +02002510EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
David Brownella1cdeda2008-07-14 22:38:24 +02002512/**
2513 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2514 * @client: Handle to slave device
2515 * @command: Byte interpreted by slave
2516 * @value: Byte being written
2517 *
2518 * This executes the SMBus "write byte" protocol, returning negative errno
2519 * else zero on success.
2520 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002521s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2522 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 union i2c_smbus_data data;
2525 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002526 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2527 I2C_SMBUS_WRITE, command,
2528 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
David Brownellc0564602007-05-01 23:26:31 +02002530EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
David Brownella1cdeda2008-07-14 22:38:24 +02002532/**
2533 * i2c_smbus_read_word_data - SMBus "read word" protocol
2534 * @client: Handle to slave device
2535 * @command: Byte interpreted by slave
2536 *
2537 * This executes the SMBus "read word" protocol, returning negative errno
2538 * else a 16-bit unsigned "word" received from the device.
2539 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002540s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002543 int status;
2544
2545 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2546 I2C_SMBUS_READ, command,
2547 I2C_SMBUS_WORD_DATA, &data);
2548 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
David Brownellc0564602007-05-01 23:26:31 +02002550EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
David Brownella1cdeda2008-07-14 22:38:24 +02002552/**
2553 * i2c_smbus_write_word_data - SMBus "write word" protocol
2554 * @client: Handle to slave device
2555 * @command: Byte interpreted by slave
2556 * @value: 16-bit "word" being written
2557 *
2558 * This executes the SMBus "write word" protocol, returning negative errno
2559 * else zero on success.
2560 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002561s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2562 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 union i2c_smbus_data data;
2565 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002566 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2567 I2C_SMBUS_WRITE, command,
2568 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
David Brownellc0564602007-05-01 23:26:31 +02002570EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
David Brownella64ec072007-10-13 23:56:31 +02002572/**
David Brownella1cdeda2008-07-14 22:38:24 +02002573 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002574 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002575 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002576 * @values: Byte array into which data will be read; big enough to hold
2577 * the data returned by the slave. SMBus allows at most 32 bytes.
2578 *
David Brownella1cdeda2008-07-14 22:38:24 +02002579 * This executes the SMBus "block read" protocol, returning negative errno
2580 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002581 *
2582 * Note that using this function requires that the client's adapter support
2583 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2584 * support this; its emulation through I2C messaging relies on a specific
2585 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2586 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002587s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002588 u8 *values)
2589{
2590 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002591 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002592
David Brownell24a5bb72008-07-14 22:38:23 +02002593 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2594 I2C_SMBUS_READ, command,
2595 I2C_SMBUS_BLOCK_DATA, &data);
2596 if (status)
2597 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002598
2599 memcpy(values, &data.block[1], data.block[0]);
2600 return data.block[0];
2601}
2602EXPORT_SYMBOL(i2c_smbus_read_block_data);
2603
David Brownella1cdeda2008-07-14 22:38:24 +02002604/**
2605 * i2c_smbus_write_block_data - SMBus "block write" protocol
2606 * @client: Handle to slave device
2607 * @command: Byte interpreted by slave
2608 * @length: Size of data block; SMBus allows at most 32 bytes
2609 * @values: Byte array which will be written.
2610 *
2611 * This executes the SMBus "block write" protocol, returning negative errno
2612 * else zero on success.
2613 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002614s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002615 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (length > I2C_SMBUS_BLOCK_MAX)
2620 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002622 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002623 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2624 I2C_SMBUS_WRITE, command,
2625 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
David Brownellc0564602007-05-01 23:26:31 +02002627EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002630s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002631 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002634 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002635
Jean Delvare4b2643d2007-07-12 14:12:29 +02002636 if (length > I2C_SMBUS_BLOCK_MAX)
2637 length = I2C_SMBUS_BLOCK_MAX;
2638 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002639 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2640 I2C_SMBUS_READ, command,
2641 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2642 if (status < 0)
2643 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002644
2645 memcpy(values, &data.block[1], data.block[0]);
2646 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
David Brownellc0564602007-05-01 23:26:31 +02002648EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Jean Delvare0cc43a12011-01-10 22:11:23 +01002650s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002651 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002652{
2653 union i2c_smbus_data data;
2654
2655 if (length > I2C_SMBUS_BLOCK_MAX)
2656 length = I2C_SMBUS_BLOCK_MAX;
2657 data.block[0] = length;
2658 memcpy(data.block + 1, values, length);
2659 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2660 I2C_SMBUS_WRITE, command,
2661 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2662}
David Brownellc0564602007-05-01 23:26:31 +02002663EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002664
David Brownell438d6c22006-12-10 21:21:31 +01002665/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002667static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2668 unsigned short flags,
2669 char read_write, u8 command, int size,
2670 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 /* So we need to generate a series of msgs. In the case of writing, we
2673 need to use only one message; when reading, we need two. We initialize
2674 most things with sane defaults, to keep the code below somewhat
2675 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002676 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2677 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002678 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002680 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002681 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002682 struct i2c_msg msg[2] = {
2683 {
2684 .addr = addr,
2685 .flags = flags,
2686 .len = 1,
2687 .buf = msgbuf0,
2688 }, {
2689 .addr = addr,
2690 .flags = flags | I2C_M_RD,
2691 .len = 0,
2692 .buf = msgbuf1,
2693 },
2694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002697 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 case I2C_SMBUS_QUICK:
2699 msg[0].len = 0;
2700 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002701 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2702 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 num = 1;
2704 break;
2705 case I2C_SMBUS_BYTE:
2706 if (read_write == I2C_SMBUS_READ) {
2707 /* Special case: only a read! */
2708 msg[0].flags = I2C_M_RD | flags;
2709 num = 1;
2710 }
2711 break;
2712 case I2C_SMBUS_BYTE_DATA:
2713 if (read_write == I2C_SMBUS_READ)
2714 msg[1].len = 1;
2715 else {
2716 msg[0].len = 2;
2717 msgbuf0[1] = data->byte;
2718 }
2719 break;
2720 case I2C_SMBUS_WORD_DATA:
2721 if (read_write == I2C_SMBUS_READ)
2722 msg[1].len = 2;
2723 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002724 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002726 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
2728 break;
2729 case I2C_SMBUS_PROC_CALL:
2730 num = 2; /* Special case */
2731 read_write = I2C_SMBUS_READ;
2732 msg[0].len = 3;
2733 msg[1].len = 2;
2734 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002735 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 break;
2737 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002739 msg[1].flags |= I2C_M_RECV_LEN;
2740 msg[1].len = 1; /* block length will be added by
2741 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 } else {
2743 msg[0].len = data->block[0] + 2;
2744 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002745 dev_err(&adapter->dev,
2746 "Invalid block write size %d\n",
2747 data->block[0]);
2748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002750 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 msgbuf0[i] = data->block[i-1];
2752 }
2753 break;
2754 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002755 num = 2; /* Another special case */
2756 read_write = I2C_SMBUS_READ;
2757 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002758 dev_err(&adapter->dev,
2759 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002760 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002761 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002762 }
2763 msg[0].len = data->block[0] + 2;
2764 for (i = 1; i < msg[0].len; i++)
2765 msgbuf0[i] = data->block[i-1];
2766 msg[1].flags |= I2C_M_RECV_LEN;
2767 msg[1].len = 1; /* block length will be added by
2768 the underlying bus driver */
2769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 case I2C_SMBUS_I2C_BLOCK_DATA:
2771 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002772 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 } else {
2774 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002775 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002776 dev_err(&adapter->dev,
2777 "Invalid block write size %d\n",
2778 data->block[0]);
2779 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781 for (i = 1; i <= data->block[0]; i++)
2782 msgbuf0[i] = data->block[i];
2783 }
2784 break;
2785 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002786 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2787 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789
Jean Delvare421ef472005-10-26 21:28:55 +02002790 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2791 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2792 if (i) {
2793 /* Compute PEC if first message is a write */
2794 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002795 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002796 i2c_smbus_add_pec(&msg[0]);
2797 else /* Write followed by read */
2798 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2799 }
2800 /* Ask for PEC if last message is a read */
2801 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002802 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002803 }
2804
David Brownell24a5bb72008-07-14 22:38:23 +02002805 status = i2c_transfer(adapter, msg, num);
2806 if (status < 0)
2807 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Jean Delvare421ef472005-10-26 21:28:55 +02002809 /* Check PEC if last message is a read */
2810 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002811 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2812 if (status < 0)
2813 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002814 }
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002817 switch (size) {
2818 case I2C_SMBUS_BYTE:
2819 data->byte = msgbuf0[0];
2820 break;
2821 case I2C_SMBUS_BYTE_DATA:
2822 data->byte = msgbuf1[0];
2823 break;
2824 case I2C_SMBUS_WORD_DATA:
2825 case I2C_SMBUS_PROC_CALL:
2826 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2827 break;
2828 case I2C_SMBUS_I2C_BLOCK_DATA:
2829 for (i = 0; i < data->block[0]; i++)
2830 data->block[i+1] = msgbuf1[i];
2831 break;
2832 case I2C_SMBUS_BLOCK_DATA:
2833 case I2C_SMBUS_BLOCK_PROC_CALL:
2834 for (i = 0; i < msgbuf1[0] + 1; i++)
2835 data->block[i] = msgbuf1[i];
2836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 return 0;
2839}
2840
David Brownella1cdeda2008-07-14 22:38:24 +02002841/**
2842 * i2c_smbus_xfer - execute SMBus protocol operations
2843 * @adapter: Handle to I2C bus
2844 * @addr: Address of SMBus slave on that bus
2845 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2846 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2847 * @command: Byte interpreted by slave, for protocols which use such bytes
2848 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2849 * @data: Data to be read or written
2850 *
2851 * This executes an SMBus protocol operation, and returns a negative
2852 * errno code else zero on success.
2853 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002854s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002855 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002856 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002858 unsigned long orig_jiffies;
2859 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
David Howells8a325992014-03-06 13:36:06 +00002862 /* If enabled, the following two tracepoints are conditional on
2863 * read_write and protocol.
2864 */
2865 trace_smbus_write(adapter, addr, flags, read_write,
2866 command, protocol, data);
2867 trace_smbus_read(adapter, addr, flags, read_write,
2868 command, protocol);
2869
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002870 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002873 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002874
2875 /* Retry automatically on arbitration loss */
2876 orig_jiffies = jiffies;
2877 for (res = 0, try = 0; try <= adapter->retries; try++) {
2878 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2879 read_write, command,
2880 protocol, data);
2881 if (res != -EAGAIN)
2882 break;
2883 if (time_after(jiffies,
2884 orig_jiffies + adapter->timeout))
2885 break;
2886 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002887 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002889 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002890 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002891 /*
2892 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2893 * implement native support for the SMBus operation.
2894 */
2895 }
2896
David Howells8a325992014-03-06 13:36:06 +00002897 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2898 command, protocol, data);
2899
2900trace:
2901 /* If enabled, the reply tracepoint is conditional on read_write. */
2902 trace_smbus_reply(adapter, addr, flags, read_write,
2903 command, protocol, data);
2904 trace_smbus_result(adapter, addr, flags, read_write,
2905 command, protocol, res);
2906
2907 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Jean Delvared5fd1202015-01-26 20:59:31 +01002911#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002912int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2913{
2914 int ret;
2915
2916 if (!client || !slave_cb)
2917 return -EINVAL;
2918
2919 if (!(client->flags & I2C_CLIENT_TEN)) {
2920 /* Enforce stricter address checking */
2921 ret = i2c_check_addr_validity(client->addr);
2922 if (ret)
2923 return ret;
2924 }
2925
2926 if (!client->adapter->algo->reg_slave)
2927 return -EOPNOTSUPP;
2928
2929 client->slave_cb = slave_cb;
2930
2931 i2c_lock_adapter(client->adapter);
2932 ret = client->adapter->algo->reg_slave(client);
2933 i2c_unlock_adapter(client->adapter);
2934
2935 if (ret)
2936 client->slave_cb = NULL;
2937
2938 return ret;
2939}
2940EXPORT_SYMBOL_GPL(i2c_slave_register);
2941
2942int i2c_slave_unregister(struct i2c_client *client)
2943{
2944 int ret;
2945
2946 if (!client->adapter->algo->unreg_slave)
2947 return -EOPNOTSUPP;
2948
2949 i2c_lock_adapter(client->adapter);
2950 ret = client->adapter->algo->unreg_slave(client);
2951 i2c_unlock_adapter(client->adapter);
2952
2953 if (ret == 0)
2954 client->slave_cb = NULL;
2955
2956 return ret;
2957}
2958EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01002959#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2962MODULE_DESCRIPTION("I2C-Bus main module");
2963MODULE_LICENSE("GPL");