blob: 1672e6b127747d5adc266e5a5d13e68ad53b03b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/init.h>
38#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010039#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010040#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060041#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020043#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010044#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010045#include <linux/hardirq.h>
46#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020047#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010048#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020049#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000051#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020053#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare6629dcf2010-05-04 11:09:28 +020060/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020061 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000062 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010063static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_IDR(i2c_adapter_idr);
65
Jean Delvare4f8cf822009-09-18 22:45:46 +020066static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
70
71void i2c_transfer_trace_reg(void)
72{
73 static_key_slow_inc(&i2c_trace_msg);
74}
75
76void i2c_transfer_trace_unreg(void)
77{
78 static_key_slow_dec(&i2c_trace_msg);
79}
80
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020081#if defined(CONFIG_ACPI)
82struct acpi_i2c_handler_data {
83 struct acpi_connection_info info;
84 struct i2c_adapter *adapter;
85};
86
87struct gsb_buffer {
88 u8 status;
89 u8 len;
90 union {
91 u16 wdata;
92 u8 bdata;
93 u8 data[0];
94 };
95} __packed;
96
97static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
98{
99 struct i2c_board_info *info = data;
100
101 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
102 struct acpi_resource_i2c_serialbus *sb;
103
104 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1c2014-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 }
110 } else if (info->irq < 0) {
111 struct resource r;
112
113 if (acpi_dev_resource_interrupt(ares, 0, &r))
114 info->irq = r.start;
115 }
116
117 /* Tell the ACPI core to skip this resource */
118 return 1;
119}
120
121static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
122 void *data, void **return_value)
123{
124 struct i2c_adapter *adapter = data;
125 struct list_head resource_list;
126 struct i2c_board_info info;
127 struct acpi_device *adev;
128 int ret;
129
130 if (acpi_bus_get_device(handle, &adev))
131 return AE_OK;
132 if (acpi_bus_get_status(adev) || !adev->status.present)
133 return AE_OK;
134
135 memset(&info, 0, sizeof(info));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100136 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200137 info.irq = -1;
138
139 INIT_LIST_HEAD(&resource_list);
140 ret = acpi_dev_get_resources(adev, &resource_list,
141 acpi_i2c_add_resource, &info);
142 acpi_dev_free_resource_list(&resource_list);
143
144 if (ret < 0 || !info.addr)
145 return AE_OK;
146
147 adev->power.flags.ignore_parent = true;
148 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
149 if (!i2c_new_device(adapter, &info)) {
150 adev->power.flags.ignore_parent = false;
151 dev_err(&adapter->dev,
152 "failed to add I2C device %s from ACPI\n",
153 dev_name(&adev->dev));
154 }
155
156 return AE_OK;
157}
158
159/**
160 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
161 * @adap: pointer to adapter
162 *
163 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
164 * namespace. When a device is found it will be added to the Linux device
165 * model and bound to the corresponding ACPI handle.
166 */
167static void acpi_i2c_register_devices(struct i2c_adapter *adap)
168{
169 acpi_handle handle;
170 acpi_status status;
171
172 if (!adap->dev.parent)
173 return;
174
175 handle = ACPI_HANDLE(adap->dev.parent);
176 if (!handle)
177 return;
178
179 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
180 acpi_i2c_add_device, NULL,
181 adap, NULL);
182 if (ACPI_FAILURE(status))
183 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
184}
185
186#else /* CONFIG_ACPI */
187static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
188#endif /* CONFIG_ACPI */
189
190#ifdef CONFIG_ACPI_I2C_OPREGION
191static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
192 u8 cmd, u8 *data, u8 data_len)
193{
194
195 struct i2c_msg msgs[2];
196 int ret;
197 u8 *buffer;
198
199 buffer = kzalloc(data_len, GFP_KERNEL);
200 if (!buffer)
201 return AE_NO_MEMORY;
202
203 msgs[0].addr = client->addr;
204 msgs[0].flags = client->flags;
205 msgs[0].len = 1;
206 msgs[0].buf = &cmd;
207
208 msgs[1].addr = client->addr;
209 msgs[1].flags = client->flags | I2C_M_RD;
210 msgs[1].len = data_len;
211 msgs[1].buf = buffer;
212
213 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
214 if (ret < 0)
215 dev_err(&client->adapter->dev, "i2c read failed\n");
216 else
217 memcpy(data, buffer, data_len);
218
219 kfree(buffer);
220 return ret;
221}
222
223static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
224 u8 cmd, u8 *data, u8 data_len)
225{
226
227 struct i2c_msg msgs[1];
228 u8 *buffer;
229 int ret = AE_OK;
230
231 buffer = kzalloc(data_len + 1, GFP_KERNEL);
232 if (!buffer)
233 return AE_NO_MEMORY;
234
235 buffer[0] = cmd;
236 memcpy(buffer + 1, data, data_len);
237
238 msgs[0].addr = client->addr;
239 msgs[0].flags = client->flags;
240 msgs[0].len = data_len + 1;
241 msgs[0].buf = buffer;
242
243 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
244 if (ret < 0)
245 dev_err(&client->adapter->dev, "i2c write failed\n");
246
247 kfree(buffer);
248 return ret;
249}
250
251static acpi_status
252acpi_i2c_space_handler(u32 function, acpi_physical_address command,
253 u32 bits, u64 *value64,
254 void *handler_context, void *region_context)
255{
256 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
257 struct acpi_i2c_handler_data *data = handler_context;
258 struct acpi_connection_info *info = &data->info;
259 struct acpi_resource_i2c_serialbus *sb;
260 struct i2c_adapter *adapter = data->adapter;
261 struct i2c_client client;
262 struct acpi_resource *ares;
263 u32 accessor_type = function >> 16;
264 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100265 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200266 int status;
267
268 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
269 if (ACPI_FAILURE(ret))
270 return ret;
271
272 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
273 ret = AE_BAD_PARAMETER;
274 goto err;
275 }
276
277 sb = &ares->data.i2c_serial_bus;
278 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
279 ret = AE_BAD_PARAMETER;
280 goto err;
281 }
282
283 memset(&client, 0, sizeof(client));
284 client.adapter = adapter;
285 client.addr = sb->slave_address;
286 client.flags = 0;
287
288 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
289 client.flags |= I2C_CLIENT_TEN;
290
291 switch (accessor_type) {
292 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
293 if (action == ACPI_READ) {
294 status = i2c_smbus_read_byte(&client);
295 if (status >= 0) {
296 gsb->bdata = status;
297 status = 0;
298 }
299 } else {
300 status = i2c_smbus_write_byte(&client, gsb->bdata);
301 }
302 break;
303
304 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
305 if (action == ACPI_READ) {
306 status = i2c_smbus_read_byte_data(&client, command);
307 if (status >= 0) {
308 gsb->bdata = status;
309 status = 0;
310 }
311 } else {
312 status = i2c_smbus_write_byte_data(&client, command,
313 gsb->bdata);
314 }
315 break;
316
317 case ACPI_GSB_ACCESS_ATTRIB_WORD:
318 if (action == ACPI_READ) {
319 status = i2c_smbus_read_word_data(&client, command);
320 if (status >= 0) {
321 gsb->wdata = status;
322 status = 0;
323 }
324 } else {
325 status = i2c_smbus_write_word_data(&client, command,
326 gsb->wdata);
327 }
328 break;
329
330 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
331 if (action == ACPI_READ) {
332 status = i2c_smbus_read_block_data(&client, command,
333 gsb->data);
334 if (status >= 0) {
335 gsb->len = status;
336 status = 0;
337 }
338 } else {
339 status = i2c_smbus_write_block_data(&client, command,
340 gsb->len, gsb->data);
341 }
342 break;
343
344 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
345 if (action == ACPI_READ) {
346 status = acpi_gsb_i2c_read_bytes(&client, command,
347 gsb->data, info->access_length);
348 if (status > 0)
349 status = 0;
350 } else {
351 status = acpi_gsb_i2c_write_bytes(&client, command,
352 gsb->data, info->access_length);
353 }
354 break;
355
356 default:
357 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
358 ret = AE_BAD_PARAMETER;
359 goto err;
360 }
361
362 gsb->status = status;
363
364 err:
365 ACPI_FREE(ares);
366 return ret;
367}
368
369
370static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
371{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200372 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200373 struct acpi_i2c_handler_data *data;
374 acpi_status status;
375
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200376 if (!adapter->dev.parent)
377 return -ENODEV;
378
379 handle = ACPI_HANDLE(adapter->dev.parent);
380
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200381 if (!handle)
382 return -ENODEV;
383
384 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
385 GFP_KERNEL);
386 if (!data)
387 return -ENOMEM;
388
389 data->adapter = adapter;
390 status = acpi_bus_attach_private_data(handle, (void *)data);
391 if (ACPI_FAILURE(status)) {
392 kfree(data);
393 return -ENOMEM;
394 }
395
396 status = acpi_install_address_space_handler(handle,
397 ACPI_ADR_SPACE_GSBUS,
398 &acpi_i2c_space_handler,
399 NULL,
400 data);
401 if (ACPI_FAILURE(status)) {
402 dev_err(&adapter->dev, "Error installing i2c space handler\n");
403 acpi_bus_detach_private_data(handle);
404 kfree(data);
405 return -ENOMEM;
406 }
407
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800408 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200409 return 0;
410}
411
412static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
413{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200414 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415 struct acpi_i2c_handler_data *data;
416 acpi_status status;
417
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200418 if (!adapter->dev.parent)
419 return;
420
421 handle = ACPI_HANDLE(adapter->dev.parent);
422
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200423 if (!handle)
424 return;
425
426 acpi_remove_address_space_handler(handle,
427 ACPI_ADR_SPACE_GSBUS,
428 &acpi_i2c_space_handler);
429
430 status = acpi_bus_get_private_data(handle, (void **)&data);
431 if (ACPI_SUCCESS(status))
432 kfree(data);
433
434 acpi_bus_detach_private_data(handle);
435}
436#else /* CONFIG_ACPI_I2C_OPREGION */
437static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
438{ }
439
440static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
441{ return 0; }
442#endif /* CONFIG_ACPI_I2C_OPREGION */
443
David Brownellf37dd802007-02-13 22:09:00 +0100444/* ------------------------------------------------------------------------- */
445
Jean Delvared2653e92008-04-29 23:11:39 +0200446static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
447 const struct i2c_client *client)
448{
449 while (id->name[0]) {
450 if (strcmp(client->name, id->name) == 0)
451 return id;
452 id++;
453 }
454 return NULL;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static int i2c_device_match(struct device *dev, struct device_driver *drv)
458{
Jean Delvare51298d12009-09-18 22:45:45 +0200459 struct i2c_client *client = i2c_verify_client(dev);
460 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200461
Jean Delvare51298d12009-09-18 22:45:45 +0200462 if (!client)
463 return 0;
464
Grant Likely959e85f2010-06-08 07:48:19 -0600465 /* Attempt an OF style match */
466 if (of_driver_match_device(dev, drv))
467 return 1;
468
Mika Westerberg907ddf82012-11-23 12:23:40 +0100469 /* Then ACPI style match */
470 if (acpi_driver_match_device(dev, drv))
471 return 1;
472
Jean Delvare51298d12009-09-18 22:45:45 +0200473 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200474 /* match on an id table if there is one */
475 if (driver->id_table)
476 return i2c_match_id(driver->id_table, client) != NULL;
477
Jean Delvareeb8a7902008-05-18 20:49:41 +0200478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David Brownell7b4fbc52007-05-01 23:26:30 +0200481
482/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200483static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200484{
485 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800486 int rc;
487
488 rc = acpi_device_uevent_modalias(dev, env);
489 if (rc != -ENODEV)
490 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200491
Jean Delvareeb8a7902008-05-18 20:49:41 +0200492 if (add_uevent_var(env, "MODALIAS=%s%s",
493 I2C_MODULE_PREFIX, client->name))
494 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200495 dev_dbg(dev, "uevent\n");
496 return 0;
497}
498
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530499/* i2c bus recovery routines */
500static int get_scl_gpio_value(struct i2c_adapter *adap)
501{
502 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
503}
504
505static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
506{
507 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
508}
509
510static int get_sda_gpio_value(struct i2c_adapter *adap)
511{
512 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
513}
514
515static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
516{
517 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
518 struct device *dev = &adap->dev;
519 int ret = 0;
520
521 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
522 GPIOF_OUT_INIT_HIGH, "i2c-scl");
523 if (ret) {
524 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
525 return ret;
526 }
527
528 if (bri->get_sda) {
529 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
530 /* work without SDA polling */
531 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
532 bri->sda_gpio);
533 bri->get_sda = NULL;
534 }
535 }
536
537 return ret;
538}
539
540static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
541{
542 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
543
544 if (bri->get_sda)
545 gpio_free(bri->sda_gpio);
546
547 gpio_free(bri->scl_gpio);
548}
549
550/*
551 * We are generating clock pulses. ndelay() determines durating of clk pulses.
552 * We will generate clock with rate 100 KHz and so duration of both clock levels
553 * is: delay in ns = (10^6 / 100) / 2
554 */
555#define RECOVERY_NDELAY 5000
556#define RECOVERY_CLK_CNT 9
557
558static int i2c_generic_recovery(struct i2c_adapter *adap)
559{
560 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
561 int i = 0, val = 1, ret = 0;
562
563 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300564 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530565
566 /*
567 * By this time SCL is high, as we need to give 9 falling-rising edges
568 */
569 while (i++ < RECOVERY_CLK_CNT * 2) {
570 if (val) {
571 /* Break if SDA is high */
572 if (bri->get_sda && bri->get_sda(adap))
573 break;
574 /* SCL shouldn't be low here */
575 if (!bri->get_scl(adap)) {
576 dev_err(&adap->dev,
577 "SCL is stuck low, exit recovery\n");
578 ret = -EBUSY;
579 break;
580 }
581 }
582
583 val = !val;
584 bri->set_scl(adap, val);
585 ndelay(RECOVERY_NDELAY);
586 }
587
588 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300589 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530590
591 return ret;
592}
593
594int i2c_generic_scl_recovery(struct i2c_adapter *adap)
595{
596 adap->bus_recovery_info->set_scl(adap, 1);
597 return i2c_generic_recovery(adap);
598}
599
600int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
601{
602 int ret;
603
604 ret = i2c_get_gpios_for_recovery(adap);
605 if (ret)
606 return ret;
607
608 ret = i2c_generic_recovery(adap);
609 i2c_put_gpios_for_recovery(adap);
610
611 return ret;
612}
613
614int i2c_recover_bus(struct i2c_adapter *adap)
615{
616 if (!adap->bus_recovery_info)
617 return -EOPNOTSUPP;
618
619 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
620 return adap->bus_recovery_info->recover_bus(adap);
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int i2c_device_probe(struct device *dev)
624{
Jean Delvare51298d12009-09-18 22:45:45 +0200625 struct i2c_client *client = i2c_verify_client(dev);
626 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100627 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200628
Jean Delvare51298d12009-09-18 22:45:45 +0200629 if (!client)
630 return 0;
631
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200632 if (!client->irq && dev->of_node) {
633 int irq = of_irq_get(dev->of_node, 0);
634
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100635 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200636 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100637 if (irq < 0)
638 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200639
640 client->irq = irq;
641 }
642
Jean Delvare51298d12009-09-18 22:45:45 +0200643 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200644 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200645 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200646
Marc Pignatee354252008-08-28 08:33:22 +0200647 if (!device_can_wakeup(&client->dev))
648 device_init_wakeup(&client->dev,
649 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200650 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200651
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200652 status = of_clk_set_defaults(dev->of_node, false);
653 if (status < 0)
654 return status;
655
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200656 status = dev_pm_domain_attach(&client->dev, true);
657 if (status != -EPROBE_DEFER) {
658 status = driver->probe(client, i2c_match_id(driver->id_table,
659 client));
660 if (status)
661 dev_pm_domain_detach(&client->dev, true);
662 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100663
Hans Verkuil50c33042008-03-12 14:15:00 +0100664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static int i2c_device_remove(struct device *dev)
668{
Jean Delvare51298d12009-09-18 22:45:45 +0200669 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200670 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100671 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200672
Jean Delvare51298d12009-09-18 22:45:45 +0200673 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200674 return 0;
675
676 driver = to_i2c_driver(dev->driver);
677 if (driver->remove) {
678 dev_dbg(dev, "remove\n");
679 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200680 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100681
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200682 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
David Brownellf37dd802007-02-13 22:09:00 +0100686static void i2c_device_shutdown(struct device *dev)
687{
Jean Delvare51298d12009-09-18 22:45:45 +0200688 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100689 struct i2c_driver *driver;
690
Jean Delvare51298d12009-09-18 22:45:45 +0200691 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100692 return;
693 driver = to_i2c_driver(dev->driver);
694 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200695 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100696}
697
David Brownell9c1600e2007-05-01 23:26:31 +0200698static void i2c_client_dev_release(struct device *dev)
699{
700 kfree(to_i2c_client(dev));
701}
702
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100703static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200704show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200705{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200706 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
707 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200708}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100709static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200710
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100711static ssize_t
712show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200713{
714 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800715 int len;
716
717 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
718 if (len != -ENODEV)
719 return len;
720
Jean Delvareeb8a7902008-05-18 20:49:41 +0200721 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200722}
Jean Delvare51298d12009-09-18 22:45:45 +0200723static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
724
725static struct attribute *i2c_dev_attrs[] = {
726 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200727 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200728 &dev_attr_modalias.attr,
729 NULL
730};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100731ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100732
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200733struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100734 .name = "i2c",
735 .match = i2c_device_match,
736 .probe = i2c_device_probe,
737 .remove = i2c_device_remove,
738 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000739};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200740EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000741
Jean Delvare51298d12009-09-18 22:45:45 +0200742static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100743 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200744 .uevent = i2c_device_uevent,
745 .release = i2c_client_dev_release,
746};
747
David Brownell9b766b82008-01-27 18:14:51 +0100748
749/**
750 * i2c_verify_client - return parameter as i2c_client, or NULL
751 * @dev: device, probably from some driver model iterator
752 *
753 * When traversing the driver model tree, perhaps using driver model
754 * iterators like @device_for_each_child(), you can't assume very much
755 * about the nodes you find. Use this function to avoid oopses caused
756 * by wrongly treating some non-I2C device as an i2c_client.
757 */
758struct i2c_client *i2c_verify_client(struct device *dev)
759{
Jean Delvare51298d12009-09-18 22:45:45 +0200760 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100761 ? to_i2c_client(dev)
762 : NULL;
763}
764EXPORT_SYMBOL(i2c_verify_client);
765
766
Jean Delvare3a89db52010-06-03 11:33:52 +0200767/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300768 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200769static int i2c_check_client_addr_validity(const struct i2c_client *client)
770{
771 if (client->flags & I2C_CLIENT_TEN) {
772 /* 10-bit address, all values are valid */
773 if (client->addr > 0x3ff)
774 return -EINVAL;
775 } else {
776 /* 7-bit address, reject the general call address */
777 if (client->addr == 0x00 || client->addr > 0x7f)
778 return -EINVAL;
779 }
780 return 0;
781}
782
Jean Delvare656b8762010-06-03 11:33:53 +0200783/* And this is a strict address validity check, used when probing. If a
784 * device uses a reserved address, then it shouldn't be probed. 7-bit
785 * addressing is assumed, 10-bit address devices are rare and should be
786 * explicitly enumerated. */
787static int i2c_check_addr_validity(unsigned short addr)
788{
789 /*
790 * Reserved addresses per I2C specification:
791 * 0x00 General call address / START byte
792 * 0x01 CBUS address
793 * 0x02 Reserved for different bus format
794 * 0x03 Reserved for future purposes
795 * 0x04-0x07 Hs-mode master code
796 * 0x78-0x7b 10-bit slave addressing
797 * 0x7c-0x7f Reserved for future purposes
798 */
799 if (addr < 0x08 || addr > 0x77)
800 return -EINVAL;
801 return 0;
802}
803
Jean Delvare3b5f7942010-06-03 11:33:55 +0200804static int __i2c_check_addr_busy(struct device *dev, void *addrp)
805{
806 struct i2c_client *client = i2c_verify_client(dev);
807 int addr = *(int *)addrp;
808
809 if (client && client->addr == addr)
810 return -EBUSY;
811 return 0;
812}
813
Michael Lawnick08263742010-08-11 18:21:02 +0200814/* walk up mux tree */
815static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
816{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200817 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200818 int result;
819
820 result = device_for_each_child(&adapter->dev, &addr,
821 __i2c_check_addr_busy);
822
Jean Delvare97cc4d42010-10-24 18:16:57 +0200823 if (!result && parent)
824 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200825
826 return result;
827}
828
829/* recurse down mux tree */
830static int i2c_check_mux_children(struct device *dev, void *addrp)
831{
832 int result;
833
834 if (dev->type == &i2c_adapter_type)
835 result = device_for_each_child(dev, addrp,
836 i2c_check_mux_children);
837 else
838 result = __i2c_check_addr_busy(dev, addrp);
839
840 return result;
841}
842
Jean Delvare3b5f7942010-06-03 11:33:55 +0200843static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
844{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200845 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200846 int result = 0;
847
Jean Delvare97cc4d42010-10-24 18:16:57 +0200848 if (parent)
849 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200850
851 if (!result)
852 result = device_for_each_child(&adapter->dev, &addr,
853 i2c_check_mux_children);
854
855 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200856}
857
David Brownell9c1600e2007-05-01 23:26:31 +0200858/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200859 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
860 * @adapter: Target I2C bus segment
861 */
862void i2c_lock_adapter(struct i2c_adapter *adapter)
863{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200864 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
865
866 if (parent)
867 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200868 else
869 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200870}
871EXPORT_SYMBOL_GPL(i2c_lock_adapter);
872
873/**
874 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
875 * @adapter: Target I2C bus segment
876 */
877static int i2c_trylock_adapter(struct i2c_adapter *adapter)
878{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200879 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
880
881 if (parent)
882 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200883 else
884 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200885}
886
887/**
888 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
889 * @adapter: Target I2C bus segment
890 */
891void i2c_unlock_adapter(struct i2c_adapter *adapter)
892{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200893 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
894
895 if (parent)
896 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200897 else
898 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200899}
900EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
901
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200902static void i2c_dev_set_name(struct i2c_adapter *adap,
903 struct i2c_client *client)
904{
905 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
906
907 if (adev) {
908 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
909 return;
910 }
911
912 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
913 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
914 client->addr | ((client->flags & I2C_CLIENT_TEN)
915 ? 0xa000 : 0));
916}
917
Jean Delvarefe61e072010-08-11 18:20:58 +0200918/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200919 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200920 * @adap: the adapter managing the device
921 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200922 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200923 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200924 * Create an i2c device. Binding is handled through driver model
925 * probe()/remove() methods. A driver may be bound to this device when we
926 * return from this function, or any later moment (e.g. maybe hotplugging will
927 * load the driver module). This call is not appropriate for use by mainboard
928 * initialization logic, which usually runs during an arch_initcall() long
929 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200930 *
931 * This returns the new i2c client, which may be saved for later use with
932 * i2c_unregister_device(); or NULL to indicate an error.
933 */
934struct i2c_client *
935i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
936{
937 struct i2c_client *client;
938 int status;
939
940 client = kzalloc(sizeof *client, GFP_KERNEL);
941 if (!client)
942 return NULL;
943
944 client->adapter = adap;
945
946 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200947
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200948 if (info->archdata)
949 client->dev.archdata = *info->archdata;
950
Marc Pignatee354252008-08-28 08:33:22 +0200951 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200952 client->addr = info->addr;
953 client->irq = info->irq;
954
David Brownell9c1600e2007-05-01 23:26:31 +0200955 strlcpy(client->name, info->type, sizeof(client->name));
956
Jean Delvare3a89db52010-06-03 11:33:52 +0200957 /* Check for address validity */
958 status = i2c_check_client_addr_validity(client);
959 if (status) {
960 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
961 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
962 goto out_err_silent;
963 }
964
Jean Delvaref8a227e2009-06-19 16:58:18 +0200965 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200966 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200967 if (status)
968 goto out_err;
969
970 client->dev.parent = &client->adapter->dev;
971 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200972 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700973 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100974 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200975
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200976 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200977 status = device_register(&client->dev);
978 if (status)
979 goto out_err;
980
Jean Delvaref8a227e2009-06-19 16:58:18 +0200981 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
982 client->name, dev_name(&client->dev));
983
David Brownell9c1600e2007-05-01 23:26:31 +0200984 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200985
986out_err:
987 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
988 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200989out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200990 kfree(client);
991 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200992}
993EXPORT_SYMBOL_GPL(i2c_new_device);
994
995
996/**
997 * i2c_unregister_device - reverse effect of i2c_new_device()
998 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200999 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001000 */
1001void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001002{
David Brownella1d9e6e2007-05-01 23:26:30 +02001003 device_unregister(&client->dev);
1004}
David Brownell9c1600e2007-05-01 23:26:31 +02001005EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001006
1007
Jean Delvare60b129d2008-05-11 20:37:06 +02001008static const struct i2c_device_id dummy_id[] = {
1009 { "dummy", 0 },
1010 { },
1011};
1012
Jean Delvared2653e92008-04-29 23:11:39 +02001013static int dummy_probe(struct i2c_client *client,
1014 const struct i2c_device_id *id)
1015{
1016 return 0;
1017}
1018
1019static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001020{
1021 return 0;
1022}
1023
1024static struct i2c_driver dummy_driver = {
1025 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001026 .probe = dummy_probe,
1027 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001028 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001029};
1030
1031/**
1032 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1033 * @adapter: the adapter managing the device
1034 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001035 * Context: can sleep
1036 *
1037 * This returns an I2C client bound to the "dummy" driver, intended for use
1038 * with devices that consume multiple addresses. Examples of such chips
1039 * include various EEPROMS (like 24c04 and 24c08 models).
1040 *
1041 * These dummy devices have two main uses. First, most I2C and SMBus calls
1042 * except i2c_transfer() need a client handle; the dummy will be that handle.
1043 * And second, this prevents the specified address from being bound to a
1044 * different driver.
1045 *
1046 * This returns the new i2c client, which should be saved for later use with
1047 * i2c_unregister_device(); or NULL to indicate an error.
1048 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001049struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001050{
1051 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001052 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001053 };
1054
David Brownelle9f13732008-01-27 18:14:52 +01001055 return i2c_new_device(adapter, &info);
1056}
1057EXPORT_SYMBOL_GPL(i2c_new_dummy);
1058
David Brownellf37dd802007-02-13 22:09:00 +01001059/* ------------------------------------------------------------------------- */
1060
David Brownell16ffadf2007-05-01 23:26:28 +02001061/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1062
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001063static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
David Brownellef2c83212007-05-01 23:26:28 +02001065 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 complete(&adap->dev_released);
1067}
1068
Jean Delvare99cd8e22009-06-19 16:58:20 +02001069/*
Jean Delvare390946b2012-09-10 10:14:02 +02001070 * This function is only needed for mutex_lock_nested, so it is never
1071 * called unless locking correctness checking is enabled. Thus we
1072 * make it inline to avoid a compiler warning. That's what gcc ends up
1073 * doing anyway.
1074 */
1075static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1076{
1077 unsigned int depth = 0;
1078
1079 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1080 depth++;
1081
1082 return depth;
1083}
1084
1085/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001086 * Let users instantiate I2C devices through sysfs. This can be used when
1087 * platform initialization code doesn't contain the proper data for
1088 * whatever reason. Also useful for drivers that do device detection and
1089 * detection fails, either because the device uses an unexpected address,
1090 * or this is a compatible device with different ID register values.
1091 *
1092 * Parameter checking may look overzealous, but we really don't want
1093 * the user to provide incorrect parameters.
1094 */
1095static ssize_t
1096i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1097 const char *buf, size_t count)
1098{
1099 struct i2c_adapter *adap = to_i2c_adapter(dev);
1100 struct i2c_board_info info;
1101 struct i2c_client *client;
1102 char *blank, end;
1103 int res;
1104
Jean Delvare99cd8e22009-06-19 16:58:20 +02001105 memset(&info, 0, sizeof(struct i2c_board_info));
1106
1107 blank = strchr(buf, ' ');
1108 if (!blank) {
1109 dev_err(dev, "%s: Missing parameters\n", "new_device");
1110 return -EINVAL;
1111 }
1112 if (blank - buf > I2C_NAME_SIZE - 1) {
1113 dev_err(dev, "%s: Invalid device name\n", "new_device");
1114 return -EINVAL;
1115 }
1116 memcpy(info.type, buf, blank - buf);
1117
1118 /* Parse remaining parameters, reject extra parameters */
1119 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1120 if (res < 1) {
1121 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1122 return -EINVAL;
1123 }
1124 if (res > 1 && end != '\n') {
1125 dev_err(dev, "%s: Extra parameters\n", "new_device");
1126 return -EINVAL;
1127 }
1128
Jean Delvare99cd8e22009-06-19 16:58:20 +02001129 client = i2c_new_device(adap, &info);
1130 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001131 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001132
1133 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001134 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001135 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001136 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001137 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1138 info.type, info.addr);
1139
1140 return count;
1141}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001142static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001143
1144/*
1145 * And of course let the users delete the devices they instantiated, if
1146 * they got it wrong. This interface can only be used to delete devices
1147 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1148 * don't delete devices to which some kernel code still has references.
1149 *
1150 * Parameter checking may look overzealous, but we really don't want
1151 * the user to delete the wrong device.
1152 */
1153static ssize_t
1154i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1155 const char *buf, size_t count)
1156{
1157 struct i2c_adapter *adap = to_i2c_adapter(dev);
1158 struct i2c_client *client, *next;
1159 unsigned short addr;
1160 char end;
1161 int res;
1162
1163 /* Parse parameters, reject extra parameters */
1164 res = sscanf(buf, "%hi%c", &addr, &end);
1165 if (res < 1) {
1166 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1167 return -EINVAL;
1168 }
1169 if (res > 1 && end != '\n') {
1170 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1171 return -EINVAL;
1172 }
1173
1174 /* Make sure the device was added through sysfs */
1175 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001176 mutex_lock_nested(&adap->userspace_clients_lock,
1177 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001178 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1179 detected) {
1180 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001181 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1182 "delete_device", client->name, client->addr);
1183
1184 list_del(&client->detected);
1185 i2c_unregister_device(client);
1186 res = count;
1187 break;
1188 }
1189 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001190 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001191
1192 if (res < 0)
1193 dev_err(dev, "%s: Can't find device in list\n",
1194 "delete_device");
1195 return res;
1196}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001197static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1198 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001199
1200static struct attribute *i2c_adapter_attrs[] = {
1201 &dev_attr_name.attr,
1202 &dev_attr_new_device.attr,
1203 &dev_attr_delete_device.attr,
1204 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001205};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001206ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001207
Michael Lawnick08263742010-08-11 18:21:02 +02001208struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001209 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001210 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001211};
Michael Lawnick08263742010-08-11 18:21:02 +02001212EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Stephen Warren643dd092012-04-17 12:43:33 -06001214/**
1215 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1216 * @dev: device, probably from some driver model iterator
1217 *
1218 * When traversing the driver model tree, perhaps using driver model
1219 * iterators like @device_for_each_child(), you can't assume very much
1220 * about the nodes you find. Use this function to avoid oopses caused
1221 * by wrongly treating some non-I2C device as an i2c_adapter.
1222 */
1223struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1224{
1225 return (dev->type == &i2c_adapter_type)
1226 ? to_i2c_adapter(dev)
1227 : NULL;
1228}
1229EXPORT_SYMBOL(i2c_verify_adapter);
1230
Jean Delvare2bb50952009-09-18 22:45:46 +02001231#ifdef CONFIG_I2C_COMPAT
1232static struct class_compat *i2c_adapter_compat_class;
1233#endif
1234
David Brownell9c1600e2007-05-01 23:26:31 +02001235static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1236{
1237 struct i2c_devinfo *devinfo;
1238
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001239 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001240 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1241 if (devinfo->busnum == adapter->nr
1242 && !i2c_new_device(adapter,
1243 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001244 dev_err(&adapter->dev,
1245 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001246 devinfo->board_info.addr);
1247 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001248 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001249}
1250
Wolfram Sang687b81d2013-07-11 12:56:15 +01001251/* OF support code */
1252
1253#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001254static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1255 struct device_node *node)
1256{
1257 struct i2c_client *result;
1258 struct i2c_board_info info = {};
1259 struct dev_archdata dev_ad = {};
1260 const __be32 *addr;
1261 int len;
1262
1263 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1264
1265 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1266 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1267 node->full_name);
1268 return ERR_PTR(-EINVAL);
1269 }
1270
1271 addr = of_get_property(node, "reg", &len);
1272 if (!addr || (len < sizeof(int))) {
1273 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1274 node->full_name);
1275 return ERR_PTR(-EINVAL);
1276 }
1277
1278 info.addr = be32_to_cpup(addr);
1279 if (info.addr > (1 << 10) - 1) {
1280 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1281 info.addr, node->full_name);
1282 return ERR_PTR(-EINVAL);
1283 }
1284
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001285 info.of_node = of_node_get(node);
1286 info.archdata = &dev_ad;
1287
1288 if (of_get_property(node, "wakeup-source", NULL))
1289 info.flags |= I2C_CLIENT_WAKE;
1290
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001291 result = i2c_new_device(adap, &info);
1292 if (result == NULL) {
1293 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1294 node->full_name);
1295 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001296 return ERR_PTR(-EINVAL);
1297 }
1298 return result;
1299}
1300
Wolfram Sang687b81d2013-07-11 12:56:15 +01001301static void of_i2c_register_devices(struct i2c_adapter *adap)
1302{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001303 struct device_node *node;
1304
1305 /* Only register child devices if the adapter has a node pointer set */
1306 if (!adap->dev.of_node)
1307 return;
1308
1309 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1310
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001311 for_each_available_child_of_node(adap->dev.of_node, node)
1312 of_i2c_register_device(adap, node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001313}
1314
1315static int of_dev_node_match(struct device *dev, void *data)
1316{
1317 return dev->of_node == data;
1318}
1319
1320/* must call put_device() when done with returned i2c_client device */
1321struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1322{
1323 struct device *dev;
1324
1325 dev = bus_find_device(&i2c_bus_type, NULL, node,
1326 of_dev_node_match);
1327 if (!dev)
1328 return NULL;
1329
1330 return i2c_verify_client(dev);
1331}
1332EXPORT_SYMBOL(of_find_i2c_device_by_node);
1333
1334/* must call put_device() when done with returned i2c_adapter device */
1335struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1336{
1337 struct device *dev;
1338
1339 dev = bus_find_device(&i2c_bus_type, NULL, node,
1340 of_dev_node_match);
1341 if (!dev)
1342 return NULL;
1343
1344 return i2c_verify_adapter(dev);
1345}
1346EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1347#else
1348static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1349#endif /* CONFIG_OF */
1350
Jean Delvare69b00892009-12-06 17:06:27 +01001351static int i2c_do_add_adapter(struct i2c_driver *driver,
1352 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001353{
Jean Delvare4735c982008-07-14 22:38:36 +02001354 /* Detect supported devices on that bus, and instantiate them */
1355 i2c_detect(adap, driver);
1356
1357 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001358 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001359 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1360 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001361 dev_warn(&adap->dev, "Please use another way to instantiate "
1362 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001363 /* We ignore the return code; if it fails, too bad */
1364 driver->attach_adapter(adap);
1365 }
1366 return 0;
1367}
1368
Jean Delvare69b00892009-12-06 17:06:27 +01001369static int __process_new_adapter(struct device_driver *d, void *data)
1370{
1371 return i2c_do_add_adapter(to_i2c_driver(d), data);
1372}
1373
David Brownell6e13e642007-05-01 23:26:31 +02001374static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Jean Delvared6703282010-08-11 18:20:59 +02001376 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
David Brownell1d0b19c2008-10-14 17:30:05 +02001378 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001379 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1380 res = -EAGAIN;
1381 goto out_list;
1382 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001383
Jean Delvare2236baa2010-11-15 22:40:38 +01001384 /* Sanity checks */
1385 if (unlikely(adap->name[0] == '\0')) {
1386 pr_err("i2c-core: Attempt to register an adapter with "
1387 "no name!\n");
1388 return -EINVAL;
1389 }
1390 if (unlikely(!adap->algo)) {
1391 pr_err("i2c-core: Attempt to register adapter '%s' with "
1392 "no algo!\n", adap->name);
1393 return -EINVAL;
1394 }
1395
Mika Kuoppala194684e2009-12-06 17:06:22 +01001396 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001397 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001398 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Jean Delvare8fcfef62009-03-28 21:34:43 +01001400 /* Set default timeout to 1 second if not already set */
1401 if (adap->timeout == 0)
1402 adap->timeout = HZ;
1403
Kay Sievers27d9c182009-01-07 14:29:16 +01001404 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001405 adap->dev.bus = &i2c_bus_type;
1406 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001407 res = device_register(&adap->dev);
1408 if (res)
1409 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001411 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1412
Jean Delvare2bb50952009-09-18 22:45:46 +02001413#ifdef CONFIG_I2C_COMPAT
1414 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1415 adap->dev.parent);
1416 if (res)
1417 dev_warn(&adap->dev,
1418 "Failed to create compatibility class link\n");
1419#endif
1420
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301421 /* bus recovery specific initialization */
1422 if (adap->bus_recovery_info) {
1423 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1424
1425 if (!bri->recover_bus) {
1426 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1427 adap->bus_recovery_info = NULL;
1428 goto exit_recovery;
1429 }
1430
1431 /* Generic GPIO recovery */
1432 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1433 if (!gpio_is_valid(bri->scl_gpio)) {
1434 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1435 adap->bus_recovery_info = NULL;
1436 goto exit_recovery;
1437 }
1438
1439 if (gpio_is_valid(bri->sda_gpio))
1440 bri->get_sda = get_sda_gpio_value;
1441 else
1442 bri->get_sda = NULL;
1443
1444 bri->get_scl = get_scl_gpio_value;
1445 bri->set_scl = set_scl_gpio_value;
1446 } else if (!bri->set_scl || !bri->get_scl) {
1447 /* Generic SCL recovery */
1448 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1449 adap->bus_recovery_info = NULL;
1450 }
1451 }
1452
1453exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001454 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001455 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001456 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001457 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001458
David Brownell6e13e642007-05-01 23:26:31 +02001459 if (adap->nr < __i2c_first_dynamic_bus_num)
1460 i2c_scan_static_board_info(adap);
1461
Jean Delvare4735c982008-07-14 22:38:36 +02001462 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001463 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001464 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001465 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001466
1467 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001468
Jean Delvareb119c6c2006-08-15 18:26:30 +02001469out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001470 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001471 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001472 mutex_unlock(&core_lock);
1473 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
David Brownell6e13e642007-05-01 23:26:31 +02001476/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001477 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1478 * @adap: the adapter to register (with adap->nr initialized)
1479 * Context: can sleep
1480 *
1481 * See i2c_add_numbered_adapter() for details.
1482 */
1483static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1484{
1485 int id;
1486
1487 mutex_lock(&core_lock);
1488 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1489 GFP_KERNEL);
1490 mutex_unlock(&core_lock);
1491 if (id < 0)
1492 return id == -ENOSPC ? -EBUSY : id;
1493
1494 return i2c_register_adapter(adap);
1495}
1496
1497/**
David Brownell6e13e642007-05-01 23:26:31 +02001498 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1499 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001500 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001501 *
1502 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001503 * doesn't matter or when its bus number is specified by an dt alias.
1504 * Examples of bases when the bus number doesn't matter: I2C adapters
1505 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001506 *
1507 * When this returns zero, a new bus number was allocated and stored
1508 * in adap->nr, and the specified adapter became available for clients.
1509 * Otherwise, a negative errno value is returned.
1510 */
1511int i2c_add_adapter(struct i2c_adapter *adapter)
1512{
Doug Andersonee5c2742013-03-01 08:57:31 -08001513 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001514 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001515
Doug Andersonee5c2742013-03-01 08:57:31 -08001516 if (dev->of_node) {
1517 id = of_alias_get_id(dev->of_node, "i2c");
1518 if (id >= 0) {
1519 adapter->nr = id;
1520 return __i2c_add_numbered_adapter(adapter);
1521 }
1522 }
1523
Jean Delvarecaada322008-01-27 18:14:49 +01001524 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001525 id = idr_alloc(&i2c_adapter_idr, adapter,
1526 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001527 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001528 if (id < 0)
1529 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001530
1531 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001532
David Brownell6e13e642007-05-01 23:26:31 +02001533 return i2c_register_adapter(adapter);
1534}
1535EXPORT_SYMBOL(i2c_add_adapter);
1536
1537/**
1538 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1539 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001540 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001541 *
1542 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001543 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1544 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001545 * is used to properly configure I2C devices.
1546 *
Grant Likely488bf312011-07-25 17:49:43 +02001547 * If the requested bus number is set to -1, then this function will behave
1548 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1549 *
David Brownell6e13e642007-05-01 23:26:31 +02001550 * If no devices have pre-been declared for this bus, then be sure to
1551 * register the adapter before any dynamically allocated ones. Otherwise
1552 * the required bus ID may not be available.
1553 *
1554 * When this returns zero, the specified adapter became available for
1555 * clients using the bus number provided in adap->nr. Also, the table
1556 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1557 * and the appropriate driver model device nodes are created. Otherwise, a
1558 * negative errno value is returned.
1559 */
1560int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1561{
Grant Likely488bf312011-07-25 17:49:43 +02001562 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1563 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001564
Doug Andersonee5c2742013-03-01 08:57:31 -08001565 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001566}
1567EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1568
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001569static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001570 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001571{
Jean Delvare4735c982008-07-14 22:38:36 +02001572 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001573
Jean Delvareacec2112009-03-28 21:34:40 +01001574 /* Remove the devices we created ourselves as the result of hardware
1575 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001576 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1577 if (client->adapter == adapter) {
1578 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1579 client->name, client->addr);
1580 list_del(&client->detected);
1581 i2c_unregister_device(client);
1582 }
1583 }
Jean Delvare026526f2008-01-27 18:14:49 +01001584}
1585
Jean Delvaree549c2b2009-06-19 16:58:19 +02001586static int __unregister_client(struct device *dev, void *dummy)
1587{
1588 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001589 if (client && strcmp(client->name, "dummy"))
1590 i2c_unregister_device(client);
1591 return 0;
1592}
1593
1594static int __unregister_dummy(struct device *dev, void *dummy)
1595{
1596 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001597 if (client)
1598 i2c_unregister_device(client);
1599 return 0;
1600}
1601
Jean Delvare69b00892009-12-06 17:06:27 +01001602static int __process_removed_adapter(struct device_driver *d, void *data)
1603{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001604 i2c_do_del_adapter(to_i2c_driver(d), data);
1605 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001606}
1607
David Brownelld64f73b2007-07-12 14:12:28 +02001608/**
1609 * i2c_del_adapter - unregister I2C adapter
1610 * @adap: the adapter being unregistered
1611 * Context: can sleep
1612 *
1613 * This unregisters an I2C adapter which was previously registered
1614 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1615 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001616void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001618 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001619 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001622 mutex_lock(&core_lock);
1623 found = idr_find(&i2c_adapter_idr, adap->nr);
1624 mutex_unlock(&core_lock);
1625 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001626 pr_debug("i2c-core: attempting to delete unregistered "
1627 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001628 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
Lan Tianyu5d98e612014-05-20 20:59:23 +08001631 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001632 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001633 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001634 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001635 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001636 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001638 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001639 mutex_lock_nested(&adap->userspace_clients_lock,
1640 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001641 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1642 detected) {
1643 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1644 client->addr);
1645 list_del(&client->detected);
1646 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001647 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001648 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001649
Jean Delvaree549c2b2009-06-19 16:58:19 +02001650 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001651 * check the returned value. This is a two-pass process, because
1652 * we can't remove the dummy devices during the first pass: they
1653 * could have been instantiated by real devices wishing to clean
1654 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001655 device_for_each_child(&adap->dev, NULL, __unregister_client);
1656 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jean Delvare2bb50952009-09-18 22:45:46 +02001658#ifdef CONFIG_I2C_COMPAT
1659 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1660 adap->dev.parent);
1661#endif
1662
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001663 /* device name is gone after device_unregister */
1664 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1665
Wolfram Sang26680ee2015-01-29 22:45:09 +01001666 /* wait until all references to the device are gone
1667 *
1668 * FIXME: This is old code and should ideally be replaced by an
1669 * alternative which results in decoupling the lifetime of the struct
1670 * device from the i2c_adapter, like spi or netdev do. Any solution
1671 * should be throughly tested with DEBUG_KOBJECT_RELEASE enabled!
1672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
David Brownell6e13e642007-05-01 23:26:31 +02001677 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001678 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001680 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001682 /* Clear the device structure in case this adapter is ever going to be
1683 added again */
1684 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
David Brownellc0564602007-05-01 23:26:31 +02001686EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
David Brownell7b4fbc52007-05-01 23:26:30 +02001688/* ------------------------------------------------------------------------- */
1689
Jean Delvare7ae31482011-03-20 14:50:52 +01001690int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1691{
1692 int res;
1693
1694 mutex_lock(&core_lock);
1695 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1696 mutex_unlock(&core_lock);
1697
1698 return res;
1699}
1700EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1701
Jean Delvare69b00892009-12-06 17:06:27 +01001702static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001703{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001704 if (dev->type != &i2c_adapter_type)
1705 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001706 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001707}
1708
David Brownell7b4fbc52007-05-01 23:26:30 +02001709/*
1710 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001711 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
1713
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001714int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001716 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
David Brownell1d0b19c2008-10-14 17:30:05 +02001718 /* Can't register until after driver model init */
1719 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1720 return -EAGAIN;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001723 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jean Delvare729d6dd2009-06-19 16:58:18 +02001726 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001727 * will have called probe() for all matching-but-unbound devices.
1728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 res = driver_register(&driver->driver);
1730 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001731 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001732
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001733 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Jean Delvare4735c982008-07-14 22:38:36 +02001735 INIT_LIST_HEAD(&driver->clients);
1736 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001737 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001738
Jean Delvare7eebcb72006-02-05 23:28:21 +01001739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001741EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jean Delvare69b00892009-12-06 17:06:27 +01001743static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001744{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001745 if (dev->type == &i2c_adapter_type)
1746 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1747 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001748}
1749
David Brownella1d9e6e2007-05-01 23:26:30 +02001750/**
1751 * i2c_del_driver - unregister I2C driver
1752 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001753 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001754 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001755void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Jean Delvare7ae31482011-03-20 14:50:52 +01001757 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001760 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
David Brownellc0564602007-05-01 23:26:31 +02001762EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
David Brownell7b4fbc52007-05-01 23:26:30 +02001764/* ------------------------------------------------------------------------- */
1765
Jean Delvaree48d3312008-01-27 18:14:48 +01001766/**
1767 * i2c_use_client - increments the reference count of the i2c client structure
1768 * @client: the client being referenced
1769 *
1770 * Each live reference to a client should be refcounted. The driver model does
1771 * that automatically as part of driver binding, so that most drivers don't
1772 * need to do this explicitly: they hold a reference until they're unbound
1773 * from the device.
1774 *
1775 * A pointer to the client with the incremented reference counter is returned.
1776 */
1777struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
David Brownell6ea438e2008-07-14 22:38:24 +02001779 if (client && get_device(&client->dev))
1780 return client;
1781 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
David Brownellc0564602007-05-01 23:26:31 +02001783EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Jean Delvaree48d3312008-01-27 18:14:48 +01001785/**
1786 * i2c_release_client - release a use of the i2c client structure
1787 * @client: the client being no longer referenced
1788 *
1789 * Must be called when a user of a client is finished with it.
1790 */
1791void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
David Brownell6ea438e2008-07-14 22:38:24 +02001793 if (client)
1794 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
David Brownellc0564602007-05-01 23:26:31 +02001796EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David Brownell9b766b82008-01-27 18:14:51 +01001798struct i2c_cmd_arg {
1799 unsigned cmd;
1800 void *arg;
1801};
1802
1803static int i2c_cmd(struct device *dev, void *_arg)
1804{
1805 struct i2c_client *client = i2c_verify_client(dev);
1806 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001807 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001808
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001809 if (!client || !client->dev.driver)
1810 return 0;
1811
1812 driver = to_i2c_driver(client->dev.driver);
1813 if (driver->command)
1814 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001815 return 0;
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1819{
David Brownell9b766b82008-01-27 18:14:51 +01001820 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
David Brownell9b766b82008-01-27 18:14:51 +01001822 cmd_arg.cmd = cmd;
1823 cmd_arg.arg = arg;
1824 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
David Brownellc0564602007-05-01 23:26:31 +02001826EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001828#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1829static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1830 void *arg)
1831{
1832 struct of_reconfig_data *rd = arg;
1833 struct i2c_adapter *adap;
1834 struct i2c_client *client;
1835
1836 switch (of_reconfig_get_state_change(action, rd)) {
1837 case OF_RECONFIG_CHANGE_ADD:
1838 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1839 if (adap == NULL)
1840 return NOTIFY_OK; /* not for us */
1841
1842 client = of_i2c_register_device(adap, rd->dn);
1843 put_device(&adap->dev);
1844
1845 if (IS_ERR(client)) {
1846 pr_err("%s: failed to create for '%s'\n",
1847 __func__, rd->dn->full_name);
1848 return notifier_from_errno(PTR_ERR(client));
1849 }
1850 break;
1851 case OF_RECONFIG_CHANGE_REMOVE:
1852 /* find our device by node */
1853 client = of_find_i2c_device_by_node(rd->dn);
1854 if (client == NULL)
1855 return NOTIFY_OK; /* no? not meant for us */
1856
1857 /* unregister takes one ref away */
1858 i2c_unregister_device(client);
1859
1860 /* and put the reference of the find */
1861 put_device(&client->dev);
1862 break;
1863 }
1864
1865 return NOTIFY_OK;
1866}
1867static struct notifier_block i2c_of_notifier = {
1868 .notifier_call = of_i2c_notify,
1869};
1870#else
1871extern struct notifier_block i2c_of_notifier;
1872#endif /* CONFIG_OF_DYNAMIC */
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874static int __init i2c_init(void)
1875{
1876 int retval;
1877
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001878 retval = of_alias_get_highest_id("i2c");
1879
1880 down_write(&__i2c_board_lock);
1881 if (retval >= __i2c_first_dynamic_bus_num)
1882 __i2c_first_dynamic_bus_num = retval + 1;
1883 up_write(&__i2c_board_lock);
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 retval = bus_register(&i2c_bus_type);
1886 if (retval)
1887 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001888#ifdef CONFIG_I2C_COMPAT
1889 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1890 if (!i2c_adapter_compat_class) {
1891 retval = -ENOMEM;
1892 goto bus_err;
1893 }
1894#endif
David Brownelle9f13732008-01-27 18:14:52 +01001895 retval = i2c_add_driver(&dummy_driver);
1896 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001897 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001898
1899 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1900 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1901
David Brownelle9f13732008-01-27 18:14:52 +01001902 return 0;
1903
Jean Delvare2bb50952009-09-18 22:45:46 +02001904class_err:
1905#ifdef CONFIG_I2C_COMPAT
1906 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001907bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001908#endif
David Brownelle9f13732008-01-27 18:14:52 +01001909 bus_unregister(&i2c_bus_type);
1910 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913static void __exit i2c_exit(void)
1914{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001915 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1916 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001917 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001918#ifdef CONFIG_I2C_COMPAT
1919 class_compat_unregister(i2c_adapter_compat_class);
1920#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001922 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
David Brownella10f9e72008-10-14 17:30:06 +02001925/* We must initialize early, because some subsystems register i2c drivers
1926 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1927 */
1928postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929module_exit(i2c_exit);
1930
1931/* ----------------------------------------------------
1932 * the functional interface to the i2c busses.
1933 * ----------------------------------------------------
1934 */
1935
Wolfram Sangb7f62582015-01-05 23:45:59 +01001936/* Check if val is exceeding the quirk IFF quirk is non 0 */
1937#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1938
1939static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1940{
1941 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1942 err_msg, msg->addr, msg->len,
1943 msg->flags & I2C_M_RD ? "read" : "write");
1944 return -EOPNOTSUPP;
1945}
1946
1947static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1948{
1949 const struct i2c_adapter_quirks *q = adap->quirks;
1950 int max_num = q->max_num_msgs, i;
1951 bool do_len_check = true;
1952
1953 if (q->flags & I2C_AQ_COMB) {
1954 max_num = 2;
1955
1956 /* special checks for combined messages */
1957 if (num == 2) {
1958 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1959 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1960
1961 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1962 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1963
1964 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1965 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1966
1967 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1968 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1969
1970 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1971 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1972
1973 do_len_check = false;
1974 }
1975 }
1976
1977 if (i2c_quirk_exceeded(num, max_num))
1978 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1979
1980 for (i = 0; i < num; i++) {
1981 u16 len = msgs[i].len;
1982
1983 if (msgs[i].flags & I2C_M_RD) {
1984 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1985 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1986 } else {
1987 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1988 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1989 }
1990 }
1991
1992 return 0;
1993}
1994
David Brownella1cdeda2008-07-14 22:38:24 +02001995/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001996 * __i2c_transfer - unlocked flavor of i2c_transfer
1997 * @adap: Handle to I2C bus
1998 * @msgs: One or more messages to execute before STOP is issued to
1999 * terminate the operation; each message begins with a START.
2000 * @num: Number of messages to be executed.
2001 *
2002 * Returns negative errno, else the number of messages executed.
2003 *
2004 * Adapter lock must be held when calling this function. No debug logging
2005 * takes place. adap->algo->master_xfer existence isn't checked.
2006 */
2007int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2008{
2009 unsigned long orig_jiffies;
2010 int ret, try;
2011
Wolfram Sangb7f62582015-01-05 23:45:59 +01002012 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2013 return -EOPNOTSUPP;
2014
David Howellsd9a83d62014-03-06 13:35:59 +00002015 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2016 * enabled. This is an efficient way of keeping the for-loop from
2017 * being executed when not needed.
2018 */
2019 if (static_key_false(&i2c_trace_msg)) {
2020 int i;
2021 for (i = 0; i < num; i++)
2022 if (msgs[i].flags & I2C_M_RD)
2023 trace_i2c_read(adap, &msgs[i], i);
2024 else
2025 trace_i2c_write(adap, &msgs[i], i);
2026 }
2027
Jean Delvareb37d2a32012-06-29 07:47:19 -03002028 /* Retry automatically on arbitration loss */
2029 orig_jiffies = jiffies;
2030 for (ret = 0, try = 0; try <= adap->retries; try++) {
2031 ret = adap->algo->master_xfer(adap, msgs, num);
2032 if (ret != -EAGAIN)
2033 break;
2034 if (time_after(jiffies, orig_jiffies + adap->timeout))
2035 break;
2036 }
2037
David Howellsd9a83d62014-03-06 13:35:59 +00002038 if (static_key_false(&i2c_trace_msg)) {
2039 int i;
2040 for (i = 0; i < ret; i++)
2041 if (msgs[i].flags & I2C_M_RD)
2042 trace_i2c_reply(adap, &msgs[i], i);
2043 trace_i2c_result(adap, i, ret);
2044 }
2045
Jean Delvareb37d2a32012-06-29 07:47:19 -03002046 return ret;
2047}
2048EXPORT_SYMBOL(__i2c_transfer);
2049
2050/**
David Brownella1cdeda2008-07-14 22:38:24 +02002051 * i2c_transfer - execute a single or combined I2C message
2052 * @adap: Handle to I2C bus
2053 * @msgs: One or more messages to execute before STOP is issued to
2054 * terminate the operation; each message begins with a START.
2055 * @num: Number of messages to be executed.
2056 *
2057 * Returns negative errno, else the number of messages executed.
2058 *
2059 * Note that there is no requirement that each message be sent to
2060 * the same slave address, although that is the most common model.
2061 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002062int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002064 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
David Brownella1cdeda2008-07-14 22:38:24 +02002066 /* REVISIT the fault reporting model here is weak:
2067 *
2068 * - When we get an error after receiving N bytes from a slave,
2069 * there is no way to report "N".
2070 *
2071 * - When we get a NAK after transmitting N bytes to a slave,
2072 * there is no way to report "N" ... or to let the master
2073 * continue executing the rest of this combined message, if
2074 * that's the appropriate response.
2075 *
2076 * - When for example "num" is two and we successfully complete
2077 * the first message but get an error part way through the
2078 * second, it's unclear whether that should be reported as
2079 * one (discarding status on the second message) or errno
2080 * (discarding status on the first one).
2081 */
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (adap->algo->master_xfer) {
2084#ifdef DEBUG
2085 for (ret = 0; ret < num; ret++) {
2086 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002087 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2088 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2089 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091#endif
2092
Mike Rapoportcea443a2008-01-27 18:14:50 +01002093 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002094 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002095 if (!ret)
2096 /* I2C activity is ongoing. */
2097 return -EAGAIN;
2098 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002099 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002100 }
2101
Jean Delvareb37d2a32012-06-29 07:47:19 -03002102 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002103 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 return ret;
2106 } else {
2107 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002108 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110}
David Brownellc0564602007-05-01 23:26:31 +02002111EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
David Brownella1cdeda2008-07-14 22:38:24 +02002113/**
2114 * i2c_master_send - issue a single I2C message in master transmit mode
2115 * @client: Handle to slave device
2116 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002117 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002118 *
2119 * Returns negative errno, or else the number of bytes written.
2120 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002121int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002124 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct i2c_msg msg;
2126
Jean Delvare815f55f2005-05-07 22:58:46 +02002127 msg.addr = client->addr;
2128 msg.flags = client->flags & I2C_M_TEN;
2129 msg.len = count;
2130 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002131
Jean Delvare815f55f2005-05-07 22:58:46 +02002132 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002134 /*
2135 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2136 * transmitted, else error code.
2137 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002138 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
David Brownellc0564602007-05-01 23:26:31 +02002140EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
David Brownella1cdeda2008-07-14 22:38:24 +02002142/**
2143 * i2c_master_recv - issue a single I2C message in master receive mode
2144 * @client: Handle to slave device
2145 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002146 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002147 *
2148 * Returns negative errno, or else the number of bytes read.
2149 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002150int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Farid Hammane7225acf2010-05-21 18:40:58 +02002152 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct i2c_msg msg;
2154 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jean Delvare815f55f2005-05-07 22:58:46 +02002156 msg.addr = client->addr;
2157 msg.flags = client->flags & I2C_M_TEN;
2158 msg.flags |= I2C_M_RD;
2159 msg.len = count;
2160 msg.buf = buf;
2161
2162 ret = i2c_transfer(adap, &msg, 1);
2163
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002164 /*
2165 * If everything went ok (i.e. 1 msg received), return #bytes received,
2166 * else error code.
2167 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002168 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
David Brownellc0564602007-05-01 23:26:31 +02002170EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172/* ----------------------------------------------------
2173 * the i2c address scanning function
2174 * Will not work for 10-bit addresses!
2175 * ----------------------------------------------------
2176 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002177
Jean Delvare63e4e802010-06-03 11:33:51 +02002178/*
2179 * Legacy default probe function, mostly relevant for SMBus. The default
2180 * probe method is a quick write, but it is known to corrupt the 24RF08
2181 * EEPROMs due to a state machine bug, and could also irreversibly
2182 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2183 * we use a short byte read instead. Also, some bus drivers don't implement
2184 * quick write, so we fallback to a byte read in that case too.
2185 * On x86, there is another special case for FSC hardware monitoring chips,
2186 * which want regular byte reads (address 0x73.) Fortunately, these are the
2187 * only known chips using this I2C address on PC hardware.
2188 * Returns 1 if probe succeeded, 0 if not.
2189 */
2190static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2191{
2192 int err;
2193 union i2c_smbus_data dummy;
2194
2195#ifdef CONFIG_X86
2196 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2197 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2198 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2199 I2C_SMBUS_BYTE_DATA, &dummy);
2200 else
2201#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002202 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2203 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002204 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2205 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002206 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2207 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2208 I2C_SMBUS_BYTE, &dummy);
2209 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002210 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2211 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002212 err = -EOPNOTSUPP;
2213 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002214
2215 return err >= 0;
2216}
2217
Jean Delvareccfbbd02009-12-06 17:06:25 +01002218static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002219 struct i2c_driver *driver)
2220{
2221 struct i2c_board_info info;
2222 struct i2c_adapter *adapter = temp_client->adapter;
2223 int addr = temp_client->addr;
2224 int err;
2225
2226 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002227 err = i2c_check_addr_validity(addr);
2228 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002229 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2230 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002231 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002232 }
2233
2234 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002235 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002236 return 0;
2237
Jean Delvareccfbbd02009-12-06 17:06:25 +01002238 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002239 if (!i2c_default_probe(adapter, addr))
2240 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002241
2242 /* Finally call the custom detection function */
2243 memset(&info, 0, sizeof(struct i2c_board_info));
2244 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002245 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002246 if (err) {
2247 /* -ENODEV is returned if the detection fails. We catch it
2248 here as this isn't an error. */
2249 return err == -ENODEV ? 0 : err;
2250 }
2251
2252 /* Consistency check */
2253 if (info.type[0] == '\0') {
2254 dev_err(&adapter->dev, "%s detection function provided "
2255 "no name for 0x%x\n", driver->driver.name,
2256 addr);
2257 } else {
2258 struct i2c_client *client;
2259
2260 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002261 if (adapter->class & I2C_CLASS_DEPRECATED)
2262 dev_warn(&adapter->dev,
2263 "This adapter will soon drop class based instantiation of devices. "
2264 "Please make sure client 0x%02x gets instantiated by other means. "
2265 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2266 info.addr);
2267
Jean Delvare4735c982008-07-14 22:38:36 +02002268 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2269 info.type, info.addr);
2270 client = i2c_new_device(adapter, &info);
2271 if (client)
2272 list_add_tail(&client->detected, &driver->clients);
2273 else
2274 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2275 info.type, info.addr);
2276 }
2277 return 0;
2278}
2279
2280static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2281{
Jean Delvarec3813d62009-12-14 21:17:25 +01002282 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002283 struct i2c_client *temp_client;
2284 int i, err = 0;
2285 int adap_id = i2c_adapter_id(adapter);
2286
Jean Delvarec3813d62009-12-14 21:17:25 +01002287 address_list = driver->address_list;
2288 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002289 return 0;
2290
Wolfram Sang45552272014-07-10 13:46:21 +02002291 /* Warn that the adapter lost class based instantiation */
2292 if (adapter->class == I2C_CLASS_DEPRECATED) {
2293 dev_dbg(&adapter->dev,
2294 "This adapter dropped support for I2C classes and "
2295 "won't auto-detect %s devices anymore. If you need it, check "
2296 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2297 driver->driver.name);
2298 return 0;
2299 }
2300
Jean Delvare51b54ba2010-10-24 18:16:58 +02002301 /* Stop here if the classes do not match */
2302 if (!(adapter->class & driver->class))
2303 return 0;
2304
Jean Delvare4735c982008-07-14 22:38:36 +02002305 /* Set up a temporary client to help detect callback */
2306 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2307 if (!temp_client)
2308 return -ENOMEM;
2309 temp_client->adapter = adapter;
2310
Jean Delvarec3813d62009-12-14 21:17:25 +01002311 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002312 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002313 "addr 0x%02x\n", adap_id, address_list[i]);
2314 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002315 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002316 if (unlikely(err))
2317 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002318 }
2319
Jean Delvare4735c982008-07-14 22:38:36 +02002320 kfree(temp_client);
2321 return err;
2322}
2323
Jean Delvared44f19d2010-08-11 18:20:57 +02002324int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2325{
2326 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2327 I2C_SMBUS_QUICK, NULL) >= 0;
2328}
2329EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2330
Jean Delvare12b5053a2007-05-01 23:26:31 +02002331struct i2c_client *
2332i2c_new_probed_device(struct i2c_adapter *adap,
2333 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002334 unsigned short const *addr_list,
2335 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002336{
2337 int i;
2338
Jean Delvare8031d792010-08-11 18:21:00 +02002339 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002340 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002341
Jean Delvare12b5053a2007-05-01 23:26:31 +02002342 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2343 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002344 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002345 dev_warn(&adap->dev, "Invalid 7-bit address "
2346 "0x%02x\n", addr_list[i]);
2347 continue;
2348 }
2349
2350 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002351 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002352 dev_dbg(&adap->dev, "Address 0x%02x already in "
2353 "use, not probing\n", addr_list[i]);
2354 continue;
2355 }
2356
Jean Delvare63e4e802010-06-03 11:33:51 +02002357 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002358 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002359 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002360 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002361
2362 if (addr_list[i] == I2C_CLIENT_END) {
2363 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2364 return NULL;
2365 }
2366
2367 info->addr = addr_list[i];
2368 return i2c_new_device(adap, info);
2369}
2370EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2371
Jean Delvared735b342011-03-20 14:50:52 +01002372struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002375
Jean Delvarecaada322008-01-27 18:14:49 +01002376 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002377 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002378 if (adapter && !try_module_get(adapter->owner))
2379 adapter = NULL;
2380
Jean Delvarecaada322008-01-27 18:14:49 +01002381 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002382 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
David Brownellc0564602007-05-01 23:26:31 +02002384EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386void i2c_put_adapter(struct i2c_adapter *adap)
2387{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002388 if (adap)
2389 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
David Brownellc0564602007-05-01 23:26:31 +02002391EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393/* The SMBus parts */
2394
David Brownell438d6c22006-12-10 21:21:31 +01002395#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002396static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
2398 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002399
Farid Hammane7225acf2010-05-21 18:40:58 +02002400 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002401 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 data = data ^ POLY;
2403 data = data << 1;
2404 }
2405 return (u8)(data >> 8);
2406}
2407
Jean Delvare421ef472005-10-26 21:28:55 +02002408/* Incremental CRC8 over count bytes in the array pointed to by p */
2409static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 int i;
2412
Farid Hammane7225acf2010-05-21 18:40:58 +02002413 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002414 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return crc;
2416}
2417
Jean Delvare421ef472005-10-26 21:28:55 +02002418/* Assume a 7-bit address, which is reasonable for SMBus */
2419static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Jean Delvare421ef472005-10-26 21:28:55 +02002421 /* The address will be sent first */
2422 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2423 pec = i2c_smbus_pec(pec, &addr, 1);
2424
2425 /* The data buffer follows */
2426 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Jean Delvare421ef472005-10-26 21:28:55 +02002429/* Used for write only transactions */
2430static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Jean Delvare421ef472005-10-26 21:28:55 +02002432 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2433 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
2435
Jean Delvare421ef472005-10-26 21:28:55 +02002436/* Return <0 on CRC error
2437 If there was a write before this read (most cases) we need to take the
2438 partial CRC from the write part into account.
2439 Note that this function does modify the message (we need to decrease the
2440 message length to hide the CRC byte from the caller). */
2441static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jean Delvare421ef472005-10-26 21:28:55 +02002443 u8 rpec = msg->buf[--msg->len];
2444 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (rpec != cpec) {
2447 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2448 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002449 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
David Brownell438d6c22006-12-10 21:21:31 +01002451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
David Brownella1cdeda2008-07-14 22:38:24 +02002454/**
2455 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2456 * @client: Handle to slave device
2457 *
2458 * This executes the SMBus "receive byte" protocol, returning negative errno
2459 * else the byte received from the device.
2460 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002461s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002464 int status;
2465
2466 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2467 I2C_SMBUS_READ, 0,
2468 I2C_SMBUS_BYTE, &data);
2469 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
David Brownellc0564602007-05-01 23:26:31 +02002471EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Brownella1cdeda2008-07-14 22:38:24 +02002473/**
2474 * i2c_smbus_write_byte - SMBus "send byte" protocol
2475 * @client: Handle to slave device
2476 * @value: Byte to be sent
2477 *
2478 * This executes the SMBus "send byte" protocol, returning negative errno
2479 * else zero on success.
2480 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002481s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
Farid Hammane7225acf2010-05-21 18:40:58 +02002483 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002484 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
David Brownellc0564602007-05-01 23:26:31 +02002486EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
David Brownella1cdeda2008-07-14 22:38:24 +02002488/**
2489 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2490 * @client: Handle to slave device
2491 * @command: Byte interpreted by slave
2492 *
2493 * This executes the SMBus "read byte" protocol, returning negative errno
2494 * else a data byte received from the device.
2495 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002496s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002499 int status;
2500
2501 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2502 I2C_SMBUS_READ, command,
2503 I2C_SMBUS_BYTE_DATA, &data);
2504 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
David Brownellc0564602007-05-01 23:26:31 +02002506EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
David Brownella1cdeda2008-07-14 22:38:24 +02002508/**
2509 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2510 * @client: Handle to slave device
2511 * @command: Byte interpreted by slave
2512 * @value: Byte being written
2513 *
2514 * This executes the SMBus "write byte" protocol, returning negative errno
2515 * else zero on success.
2516 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002517s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2518 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 union i2c_smbus_data data;
2521 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002522 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2523 I2C_SMBUS_WRITE, command,
2524 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
David Brownellc0564602007-05-01 23:26:31 +02002526EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
David Brownella1cdeda2008-07-14 22:38:24 +02002528/**
2529 * i2c_smbus_read_word_data - SMBus "read word" protocol
2530 * @client: Handle to slave device
2531 * @command: Byte interpreted by slave
2532 *
2533 * This executes the SMBus "read word" protocol, returning negative errno
2534 * else a 16-bit unsigned "word" received from the device.
2535 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002536s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002539 int status;
2540
2541 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2542 I2C_SMBUS_READ, command,
2543 I2C_SMBUS_WORD_DATA, &data);
2544 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
David Brownellc0564602007-05-01 23:26:31 +02002546EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
David Brownella1cdeda2008-07-14 22:38:24 +02002548/**
2549 * i2c_smbus_write_word_data - SMBus "write word" protocol
2550 * @client: Handle to slave device
2551 * @command: Byte interpreted by slave
2552 * @value: 16-bit "word" being written
2553 *
2554 * This executes the SMBus "write word" protocol, returning negative errno
2555 * else zero on success.
2556 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002557s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2558 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
2560 union i2c_smbus_data data;
2561 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002562 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2563 I2C_SMBUS_WRITE, command,
2564 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
David Brownellc0564602007-05-01 23:26:31 +02002566EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
David Brownella64ec072007-10-13 23:56:31 +02002568/**
David Brownella1cdeda2008-07-14 22:38:24 +02002569 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002570 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002571 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002572 * @values: Byte array into which data will be read; big enough to hold
2573 * the data returned by the slave. SMBus allows at most 32 bytes.
2574 *
David Brownella1cdeda2008-07-14 22:38:24 +02002575 * This executes the SMBus "block read" protocol, returning negative errno
2576 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002577 *
2578 * Note that using this function requires that the client's adapter support
2579 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2580 * support this; its emulation through I2C messaging relies on a specific
2581 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2582 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002583s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002584 u8 *values)
2585{
2586 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002587 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002588
David Brownell24a5bb72008-07-14 22:38:23 +02002589 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2590 I2C_SMBUS_READ, command,
2591 I2C_SMBUS_BLOCK_DATA, &data);
2592 if (status)
2593 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002594
2595 memcpy(values, &data.block[1], data.block[0]);
2596 return data.block[0];
2597}
2598EXPORT_SYMBOL(i2c_smbus_read_block_data);
2599
David Brownella1cdeda2008-07-14 22:38:24 +02002600/**
2601 * i2c_smbus_write_block_data - SMBus "block write" protocol
2602 * @client: Handle to slave device
2603 * @command: Byte interpreted by slave
2604 * @length: Size of data block; SMBus allows at most 32 bytes
2605 * @values: Byte array which will be written.
2606 *
2607 * This executes the SMBus "block write" protocol, returning negative errno
2608 * else zero on success.
2609 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002610s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002611 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (length > I2C_SMBUS_BLOCK_MAX)
2616 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002618 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002619 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2620 I2C_SMBUS_WRITE, command,
2621 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
David Brownellc0564602007-05-01 23:26:31 +02002623EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002626s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002627 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002630 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002631
Jean Delvare4b2643d2007-07-12 14:12:29 +02002632 if (length > I2C_SMBUS_BLOCK_MAX)
2633 length = I2C_SMBUS_BLOCK_MAX;
2634 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2636 I2C_SMBUS_READ, command,
2637 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2638 if (status < 0)
2639 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002640
2641 memcpy(values, &data.block[1], data.block[0]);
2642 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
David Brownellc0564602007-05-01 23:26:31 +02002644EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Jean Delvare0cc43a12011-01-10 22:11:23 +01002646s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002647 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002648{
2649 union i2c_smbus_data data;
2650
2651 if (length > I2C_SMBUS_BLOCK_MAX)
2652 length = I2C_SMBUS_BLOCK_MAX;
2653 data.block[0] = length;
2654 memcpy(data.block + 1, values, length);
2655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2656 I2C_SMBUS_WRITE, command,
2657 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2658}
David Brownellc0564602007-05-01 23:26:31 +02002659EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002660
David Brownell438d6c22006-12-10 21:21:31 +01002661/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002663static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2664 unsigned short flags,
2665 char read_write, u8 command, int size,
2666 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 /* So we need to generate a series of msgs. In the case of writing, we
2669 need to use only one message; when reading, we need two. We initialize
2670 most things with sane defaults, to keep the code below somewhat
2671 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002672 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2673 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002674 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002676 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002677 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002678 struct i2c_msg msg[2] = {
2679 {
2680 .addr = addr,
2681 .flags = flags,
2682 .len = 1,
2683 .buf = msgbuf0,
2684 }, {
2685 .addr = addr,
2686 .flags = flags | I2C_M_RD,
2687 .len = 0,
2688 .buf = msgbuf1,
2689 },
2690 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002693 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 case I2C_SMBUS_QUICK:
2695 msg[0].len = 0;
2696 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002697 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2698 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 num = 1;
2700 break;
2701 case I2C_SMBUS_BYTE:
2702 if (read_write == I2C_SMBUS_READ) {
2703 /* Special case: only a read! */
2704 msg[0].flags = I2C_M_RD | flags;
2705 num = 1;
2706 }
2707 break;
2708 case I2C_SMBUS_BYTE_DATA:
2709 if (read_write == I2C_SMBUS_READ)
2710 msg[1].len = 1;
2711 else {
2712 msg[0].len = 2;
2713 msgbuf0[1] = data->byte;
2714 }
2715 break;
2716 case I2C_SMBUS_WORD_DATA:
2717 if (read_write == I2C_SMBUS_READ)
2718 msg[1].len = 2;
2719 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002720 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002722 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 break;
2725 case I2C_SMBUS_PROC_CALL:
2726 num = 2; /* Special case */
2727 read_write = I2C_SMBUS_READ;
2728 msg[0].len = 3;
2729 msg[1].len = 2;
2730 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002731 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 break;
2733 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002735 msg[1].flags |= I2C_M_RECV_LEN;
2736 msg[1].len = 1; /* block length will be added by
2737 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 } else {
2739 msg[0].len = data->block[0] + 2;
2740 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002741 dev_err(&adapter->dev,
2742 "Invalid block write size %d\n",
2743 data->block[0]);
2744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002746 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 msgbuf0[i] = data->block[i-1];
2748 }
2749 break;
2750 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002751 num = 2; /* Another special case */
2752 read_write = I2C_SMBUS_READ;
2753 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002754 dev_err(&adapter->dev,
2755 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002756 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002757 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002758 }
2759 msg[0].len = data->block[0] + 2;
2760 for (i = 1; i < msg[0].len; i++)
2761 msgbuf0[i] = data->block[i-1];
2762 msg[1].flags |= I2C_M_RECV_LEN;
2763 msg[1].len = 1; /* block length will be added by
2764 the underlying bus driver */
2765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 case I2C_SMBUS_I2C_BLOCK_DATA:
2767 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002768 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 } else {
2770 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002771 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002772 dev_err(&adapter->dev,
2773 "Invalid block write size %d\n",
2774 data->block[0]);
2775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777 for (i = 1; i <= data->block[0]; i++)
2778 msgbuf0[i] = data->block[i];
2779 }
2780 break;
2781 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002782 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2783 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Jean Delvare421ef472005-10-26 21:28:55 +02002786 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2787 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2788 if (i) {
2789 /* Compute PEC if first message is a write */
2790 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002791 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002792 i2c_smbus_add_pec(&msg[0]);
2793 else /* Write followed by read */
2794 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2795 }
2796 /* Ask for PEC if last message is a read */
2797 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002798 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002799 }
2800
David Brownell24a5bb72008-07-14 22:38:23 +02002801 status = i2c_transfer(adapter, msg, num);
2802 if (status < 0)
2803 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Jean Delvare421ef472005-10-26 21:28:55 +02002805 /* Check PEC if last message is a read */
2806 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002807 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2808 if (status < 0)
2809 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002810 }
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002813 switch (size) {
2814 case I2C_SMBUS_BYTE:
2815 data->byte = msgbuf0[0];
2816 break;
2817 case I2C_SMBUS_BYTE_DATA:
2818 data->byte = msgbuf1[0];
2819 break;
2820 case I2C_SMBUS_WORD_DATA:
2821 case I2C_SMBUS_PROC_CALL:
2822 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2823 break;
2824 case I2C_SMBUS_I2C_BLOCK_DATA:
2825 for (i = 0; i < data->block[0]; i++)
2826 data->block[i+1] = msgbuf1[i];
2827 break;
2828 case I2C_SMBUS_BLOCK_DATA:
2829 case I2C_SMBUS_BLOCK_PROC_CALL:
2830 for (i = 0; i < msgbuf1[0] + 1; i++)
2831 data->block[i] = msgbuf1[i];
2832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
2834 return 0;
2835}
2836
David Brownella1cdeda2008-07-14 22:38:24 +02002837/**
2838 * i2c_smbus_xfer - execute SMBus protocol operations
2839 * @adapter: Handle to I2C bus
2840 * @addr: Address of SMBus slave on that bus
2841 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2842 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2843 * @command: Byte interpreted by slave, for protocols which use such bytes
2844 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2845 * @data: Data to be read or written
2846 *
2847 * This executes an SMBus protocol operation, and returns a negative
2848 * errno code else zero on success.
2849 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002850s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002851 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002852 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002854 unsigned long orig_jiffies;
2855 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
David Howells8a325992014-03-06 13:36:06 +00002858 /* If enabled, the following two tracepoints are conditional on
2859 * read_write and protocol.
2860 */
2861 trace_smbus_write(adapter, addr, flags, read_write,
2862 command, protocol, data);
2863 trace_smbus_read(adapter, addr, flags, read_write,
2864 command, protocol);
2865
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002866 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002869 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002870
2871 /* Retry automatically on arbitration loss */
2872 orig_jiffies = jiffies;
2873 for (res = 0, try = 0; try <= adapter->retries; try++) {
2874 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2875 read_write, command,
2876 protocol, data);
2877 if (res != -EAGAIN)
2878 break;
2879 if (time_after(jiffies,
2880 orig_jiffies + adapter->timeout))
2881 break;
2882 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002883 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002885 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002886 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002887 /*
2888 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2889 * implement native support for the SMBus operation.
2890 */
2891 }
2892
David Howells8a325992014-03-06 13:36:06 +00002893 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2894 command, protocol, data);
2895
2896trace:
2897 /* If enabled, the reply tracepoint is conditional on read_write. */
2898 trace_smbus_reply(adapter, addr, flags, read_write,
2899 command, protocol, data);
2900 trace_smbus_result(adapter, addr, flags, read_write,
2901 command, protocol, res);
2902
2903 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Jean Delvared5fd1202015-01-26 20:59:31 +01002907#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002908int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2909{
2910 int ret;
2911
2912 if (!client || !slave_cb)
2913 return -EINVAL;
2914
2915 if (!(client->flags & I2C_CLIENT_TEN)) {
2916 /* Enforce stricter address checking */
2917 ret = i2c_check_addr_validity(client->addr);
2918 if (ret)
2919 return ret;
2920 }
2921
2922 if (!client->adapter->algo->reg_slave)
2923 return -EOPNOTSUPP;
2924
2925 client->slave_cb = slave_cb;
2926
2927 i2c_lock_adapter(client->adapter);
2928 ret = client->adapter->algo->reg_slave(client);
2929 i2c_unlock_adapter(client->adapter);
2930
2931 if (ret)
2932 client->slave_cb = NULL;
2933
2934 return ret;
2935}
2936EXPORT_SYMBOL_GPL(i2c_slave_register);
2937
2938int i2c_slave_unregister(struct i2c_client *client)
2939{
2940 int ret;
2941
2942 if (!client->adapter->algo->unreg_slave)
2943 return -EOPNOTSUPP;
2944
2945 i2c_lock_adapter(client->adapter);
2946 ret = client->adapter->algo->unreg_slave(client);
2947 i2c_unlock_adapter(client->adapter);
2948
2949 if (ret == 0)
2950 client->slave_cb = NULL;
2951
2952 return ret;
2953}
2954EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01002955#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2958MODULE_DESCRIPTION("I2C-Bus main module");
2959MODULE_LICENSE("GPL");