blob: 98f6c75b1d18e4d685d381d6c1b43056b3c99452 [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
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020030#include <dt-bindings/i2c/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053033#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053035#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/init.h>
39#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010040#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010041#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060042#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010043#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020044#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020050#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010051#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000052#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020054#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Brownell9c1600e2007-05-01 23:26:31 +020056#include "i2c-core.h"
57
David Howellsd9a83d62014-03-06 13:35:59 +000058#define CREATE_TRACE_POINTS
59#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Wolfram Sangda899f52015-05-18 21:09:12 +020061#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
62#define I2C_ADDR_OFFSET_SLAVE 0x1000
63
Jean Delvare6629dcf2010-05-04 11:09:28 +020064/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020065 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000066 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010067static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_IDR(i2c_adapter_idr);
69
Jean Delvare4f8cf822009-09-18 22:45:46 +020070static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020071static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010072
David Howellsd9a83d62014-03-06 13:35:59 +000073static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
74
75void i2c_transfer_trace_reg(void)
76{
77 static_key_slow_inc(&i2c_trace_msg);
78}
79
80void i2c_transfer_trace_unreg(void)
81{
82 static_key_slow_dec(&i2c_trace_msg);
83}
84
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020085#if defined(CONFIG_ACPI)
86struct acpi_i2c_handler_data {
87 struct acpi_connection_info info;
88 struct i2c_adapter *adapter;
89};
90
91struct gsb_buffer {
92 u8 status;
93 u8 len;
94 union {
95 u16 wdata;
96 u8 bdata;
97 u8 data[0];
98 };
99} __packed;
100
101static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
102{
103 struct i2c_board_info *info = data;
104
105 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
106 struct acpi_resource_i2c_serialbus *sb;
107
108 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1c2014-12-29 15:48:48 +0200109 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200110 info->addr = sb->slave_address;
111 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
112 info->flags |= I2C_CLIENT_TEN;
113 }
Mika Westerbergdab472e2015-05-06 13:29:07 +0300114 } else if (!info->irq) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200115 struct resource r;
116
117 if (acpi_dev_resource_interrupt(ares, 0, &r))
118 info->irq = r.start;
119 }
120
121 /* Tell the ACPI core to skip this resource */
122 return 1;
123}
124
125static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
126 void *data, void **return_value)
127{
128 struct i2c_adapter *adapter = data;
129 struct list_head resource_list;
130 struct i2c_board_info info;
131 struct acpi_device *adev;
132 int ret;
133
134 if (acpi_bus_get_device(handle, &adev))
135 return AE_OK;
136 if (acpi_bus_get_status(adev) || !adev->status.present)
137 return AE_OK;
138
139 memset(&info, 0, sizeof(info));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100140 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200141
142 INIT_LIST_HEAD(&resource_list);
143 ret = acpi_dev_get_resources(adev, &resource_list,
144 acpi_i2c_add_resource, &info);
145 acpi_dev_free_resource_list(&resource_list);
146
147 if (ret < 0 || !info.addr)
148 return AE_OK;
149
150 adev->power.flags.ignore_parent = true;
151 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
152 if (!i2c_new_device(adapter, &info)) {
153 adev->power.flags.ignore_parent = false;
154 dev_err(&adapter->dev,
155 "failed to add I2C device %s from ACPI\n",
156 dev_name(&adev->dev));
157 }
158
159 return AE_OK;
160}
161
162/**
163 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
164 * @adap: pointer to adapter
165 *
166 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
167 * namespace. When a device is found it will be added to the Linux device
168 * model and bound to the corresponding ACPI handle.
169 */
170static void acpi_i2c_register_devices(struct i2c_adapter *adap)
171{
172 acpi_handle handle;
173 acpi_status status;
174
175 if (!adap->dev.parent)
176 return;
177
178 handle = ACPI_HANDLE(adap->dev.parent);
179 if (!handle)
180 return;
181
182 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
183 acpi_i2c_add_device, NULL,
184 adap, NULL);
185 if (ACPI_FAILURE(status))
186 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
187}
188
189#else /* CONFIG_ACPI */
190static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
191#endif /* CONFIG_ACPI */
192
193#ifdef CONFIG_ACPI_I2C_OPREGION
194static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
195 u8 cmd, u8 *data, u8 data_len)
196{
197
198 struct i2c_msg msgs[2];
199 int ret;
200 u8 *buffer;
201
202 buffer = kzalloc(data_len, GFP_KERNEL);
203 if (!buffer)
204 return AE_NO_MEMORY;
205
206 msgs[0].addr = client->addr;
207 msgs[0].flags = client->flags;
208 msgs[0].len = 1;
209 msgs[0].buf = &cmd;
210
211 msgs[1].addr = client->addr;
212 msgs[1].flags = client->flags | I2C_M_RD;
213 msgs[1].len = data_len;
214 msgs[1].buf = buffer;
215
216 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
217 if (ret < 0)
218 dev_err(&client->adapter->dev, "i2c read failed\n");
219 else
220 memcpy(data, buffer, data_len);
221
222 kfree(buffer);
223 return ret;
224}
225
226static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
227 u8 cmd, u8 *data, u8 data_len)
228{
229
230 struct i2c_msg msgs[1];
231 u8 *buffer;
232 int ret = AE_OK;
233
234 buffer = kzalloc(data_len + 1, GFP_KERNEL);
235 if (!buffer)
236 return AE_NO_MEMORY;
237
238 buffer[0] = cmd;
239 memcpy(buffer + 1, data, data_len);
240
241 msgs[0].addr = client->addr;
242 msgs[0].flags = client->flags;
243 msgs[0].len = data_len + 1;
244 msgs[0].buf = buffer;
245
246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
247 if (ret < 0)
248 dev_err(&client->adapter->dev, "i2c write failed\n");
249
250 kfree(buffer);
251 return ret;
252}
253
254static acpi_status
255acpi_i2c_space_handler(u32 function, acpi_physical_address command,
256 u32 bits, u64 *value64,
257 void *handler_context, void *region_context)
258{
259 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
260 struct acpi_i2c_handler_data *data = handler_context;
261 struct acpi_connection_info *info = &data->info;
262 struct acpi_resource_i2c_serialbus *sb;
263 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300264 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 struct acpi_resource *ares;
266 u32 accessor_type = function >> 16;
267 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100268 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200269 int status;
270
271 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
272 if (ACPI_FAILURE(ret))
273 return ret;
274
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300275 client = kzalloc(sizeof(*client), GFP_KERNEL);
276 if (!client) {
277 ret = AE_NO_MEMORY;
278 goto err;
279 }
280
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200281 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
282 ret = AE_BAD_PARAMETER;
283 goto err;
284 }
285
286 sb = &ares->data.i2c_serial_bus;
287 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
288 ret = AE_BAD_PARAMETER;
289 goto err;
290 }
291
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300292 client->adapter = adapter;
293 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200294
295 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300296 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200297
298 switch (accessor_type) {
299 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
300 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300301 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200302 if (status >= 0) {
303 gsb->bdata = status;
304 status = 0;
305 }
306 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300307 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200308 }
309 break;
310
311 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
312 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300313 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200314 if (status >= 0) {
315 gsb->bdata = status;
316 status = 0;
317 }
318 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300319 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200320 gsb->bdata);
321 }
322 break;
323
324 case ACPI_GSB_ACCESS_ATTRIB_WORD:
325 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300326 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200327 if (status >= 0) {
328 gsb->wdata = status;
329 status = 0;
330 }
331 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300332 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200333 gsb->wdata);
334 }
335 break;
336
337 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
338 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300339 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200340 gsb->data);
341 if (status >= 0) {
342 gsb->len = status;
343 status = 0;
344 }
345 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300346 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200347 gsb->len, gsb->data);
348 }
349 break;
350
351 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
352 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300353 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200354 gsb->data, info->access_length);
355 if (status > 0)
356 status = 0;
357 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300358 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200359 gsb->data, info->access_length);
360 }
361 break;
362
363 default:
364 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
365 ret = AE_BAD_PARAMETER;
366 goto err;
367 }
368
369 gsb->status = status;
370
371 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300372 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200373 ACPI_FREE(ares);
374 return ret;
375}
376
377
378static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
379{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200380 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200381 struct acpi_i2c_handler_data *data;
382 acpi_status status;
383
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200384 if (!adapter->dev.parent)
385 return -ENODEV;
386
387 handle = ACPI_HANDLE(adapter->dev.parent);
388
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200389 if (!handle)
390 return -ENODEV;
391
392 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
393 GFP_KERNEL);
394 if (!data)
395 return -ENOMEM;
396
397 data->adapter = adapter;
398 status = acpi_bus_attach_private_data(handle, (void *)data);
399 if (ACPI_FAILURE(status)) {
400 kfree(data);
401 return -ENOMEM;
402 }
403
404 status = acpi_install_address_space_handler(handle,
405 ACPI_ADR_SPACE_GSBUS,
406 &acpi_i2c_space_handler,
407 NULL,
408 data);
409 if (ACPI_FAILURE(status)) {
410 dev_err(&adapter->dev, "Error installing i2c space handler\n");
411 acpi_bus_detach_private_data(handle);
412 kfree(data);
413 return -ENOMEM;
414 }
415
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800416 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200417 return 0;
418}
419
420static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
421{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200422 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200423 struct acpi_i2c_handler_data *data;
424 acpi_status status;
425
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200426 if (!adapter->dev.parent)
427 return;
428
429 handle = ACPI_HANDLE(adapter->dev.parent);
430
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200431 if (!handle)
432 return;
433
434 acpi_remove_address_space_handler(handle,
435 ACPI_ADR_SPACE_GSBUS,
436 &acpi_i2c_space_handler);
437
438 status = acpi_bus_get_private_data(handle, (void **)&data);
439 if (ACPI_SUCCESS(status))
440 kfree(data);
441
442 acpi_bus_detach_private_data(handle);
443}
444#else /* CONFIG_ACPI_I2C_OPREGION */
445static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
446{ }
447
448static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
449{ return 0; }
450#endif /* CONFIG_ACPI_I2C_OPREGION */
451
David Brownellf37dd802007-02-13 22:09:00 +0100452/* ------------------------------------------------------------------------- */
453
Jean Delvared2653e92008-04-29 23:11:39 +0200454static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
455 const struct i2c_client *client)
456{
457 while (id->name[0]) {
458 if (strcmp(client->name, id->name) == 0)
459 return id;
460 id++;
461 }
462 return NULL;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static int i2c_device_match(struct device *dev, struct device_driver *drv)
466{
Jean Delvare51298d12009-09-18 22:45:45 +0200467 struct i2c_client *client = i2c_verify_client(dev);
468 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200469
Jean Delvare51298d12009-09-18 22:45:45 +0200470 if (!client)
471 return 0;
472
Grant Likely959e85f2010-06-08 07:48:19 -0600473 /* Attempt an OF style match */
474 if (of_driver_match_device(dev, drv))
475 return 1;
476
Mika Westerberg907ddf82012-11-23 12:23:40 +0100477 /* Then ACPI style match */
478 if (acpi_driver_match_device(dev, drv))
479 return 1;
480
Jean Delvare51298d12009-09-18 22:45:45 +0200481 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200482 /* match on an id table if there is one */
483 if (driver->id_table)
484 return i2c_match_id(driver->id_table, client) != NULL;
485
Jean Delvareeb8a7902008-05-18 20:49:41 +0200486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
David Brownell7b4fbc52007-05-01 23:26:30 +0200489
490/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200491static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200492{
493 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800494 int rc;
495
496 rc = acpi_device_uevent_modalias(dev, env);
497 if (rc != -ENODEV)
498 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200499
Jean Delvareeb8a7902008-05-18 20:49:41 +0200500 if (add_uevent_var(env, "MODALIAS=%s%s",
501 I2C_MODULE_PREFIX, client->name))
502 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200503 dev_dbg(dev, "uevent\n");
504 return 0;
505}
506
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530507/* i2c bus recovery routines */
508static int get_scl_gpio_value(struct i2c_adapter *adap)
509{
510 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
511}
512
513static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
514{
515 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
516}
517
518static int get_sda_gpio_value(struct i2c_adapter *adap)
519{
520 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
521}
522
523static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
524{
525 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
526 struct device *dev = &adap->dev;
527 int ret = 0;
528
529 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
530 GPIOF_OUT_INIT_HIGH, "i2c-scl");
531 if (ret) {
532 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
533 return ret;
534 }
535
536 if (bri->get_sda) {
537 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
538 /* work without SDA polling */
539 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
540 bri->sda_gpio);
541 bri->get_sda = NULL;
542 }
543 }
544
545 return ret;
546}
547
548static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
549{
550 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
551
552 if (bri->get_sda)
553 gpio_free(bri->sda_gpio);
554
555 gpio_free(bri->scl_gpio);
556}
557
558/*
559 * We are generating clock pulses. ndelay() determines durating of clk pulses.
560 * We will generate clock with rate 100 KHz and so duration of both clock levels
561 * is: delay in ns = (10^6 / 100) / 2
562 */
563#define RECOVERY_NDELAY 5000
564#define RECOVERY_CLK_CNT 9
565
566static int i2c_generic_recovery(struct i2c_adapter *adap)
567{
568 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
569 int i = 0, val = 1, ret = 0;
570
571 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300572 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530573
Jan Luebbe8b062602015-07-08 16:35:06 +0200574 bri->set_scl(adap, val);
575 ndelay(RECOVERY_NDELAY);
576
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530577 /*
578 * By this time SCL is high, as we need to give 9 falling-rising edges
579 */
580 while (i++ < RECOVERY_CLK_CNT * 2) {
581 if (val) {
582 /* Break if SDA is high */
583 if (bri->get_sda && bri->get_sda(adap))
584 break;
585 /* SCL shouldn't be low here */
586 if (!bri->get_scl(adap)) {
587 dev_err(&adap->dev,
588 "SCL is stuck low, exit recovery\n");
589 ret = -EBUSY;
590 break;
591 }
592 }
593
594 val = !val;
595 bri->set_scl(adap, val);
596 ndelay(RECOVERY_NDELAY);
597 }
598
599 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300600 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530601
602 return ret;
603}
604
605int i2c_generic_scl_recovery(struct i2c_adapter *adap)
606{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530607 return i2c_generic_recovery(adap);
608}
Mark Brownc1c21f42015-04-15 19:18:39 +0100609EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530610
611int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
612{
613 int ret;
614
615 ret = i2c_get_gpios_for_recovery(adap);
616 if (ret)
617 return ret;
618
619 ret = i2c_generic_recovery(adap);
620 i2c_put_gpios_for_recovery(adap);
621
622 return ret;
623}
Mark Brownc1c21f42015-04-15 19:18:39 +0100624EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530625
626int i2c_recover_bus(struct i2c_adapter *adap)
627{
628 if (!adap->bus_recovery_info)
629 return -EOPNOTSUPP;
630
631 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
632 return adap->bus_recovery_info->recover_bus(adap);
633}
Mark Brownc1c21f42015-04-15 19:18:39 +0100634EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static int i2c_device_probe(struct device *dev)
637{
Jean Delvare51298d12009-09-18 22:45:45 +0200638 struct i2c_client *client = i2c_verify_client(dev);
639 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100640 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200641
Jean Delvare51298d12009-09-18 22:45:45 +0200642 if (!client)
643 return 0;
644
Mika Westerberg845c8772015-05-06 13:29:08 +0300645 if (!client->irq) {
646 int irq = -ENOENT;
647
648 if (dev->of_node)
649 irq = of_irq_get(dev->of_node, 0);
650 else if (ACPI_COMPANION(dev))
651 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200652
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100653 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200654 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100655 if (irq < 0)
656 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200657
658 client->irq = irq;
659 }
660
Jean Delvare51298d12009-09-18 22:45:45 +0200661 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200662 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200663 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200664
Marc Pignatee354252008-08-28 08:33:22 +0200665 if (!device_can_wakeup(&client->dev))
666 device_init_wakeup(&client->dev,
667 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200668 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200669
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200670 status = of_clk_set_defaults(dev->of_node, false);
671 if (status < 0)
672 return status;
673
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200674 status = dev_pm_domain_attach(&client->dev, true);
675 if (status != -EPROBE_DEFER) {
676 status = driver->probe(client, i2c_match_id(driver->id_table,
677 client));
678 if (status)
679 dev_pm_domain_detach(&client->dev, true);
680 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100681
Hans Verkuil50c33042008-03-12 14:15:00 +0100682 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685static int i2c_device_remove(struct device *dev)
686{
Jean Delvare51298d12009-09-18 22:45:45 +0200687 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200688 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100689 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200690
Jean Delvare51298d12009-09-18 22:45:45 +0200691 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200692 return 0;
693
694 driver = to_i2c_driver(dev->driver);
695 if (driver->remove) {
696 dev_dbg(dev, "remove\n");
697 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200698 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100699
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200700 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
David Brownellf37dd802007-02-13 22:09:00 +0100704static void i2c_device_shutdown(struct device *dev)
705{
Jean Delvare51298d12009-09-18 22:45:45 +0200706 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100707 struct i2c_driver *driver;
708
Jean Delvare51298d12009-09-18 22:45:45 +0200709 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100710 return;
711 driver = to_i2c_driver(dev->driver);
712 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200713 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100714}
715
David Brownell9c1600e2007-05-01 23:26:31 +0200716static void i2c_client_dev_release(struct device *dev)
717{
718 kfree(to_i2c_client(dev));
719}
720
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100721static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200722show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200723{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200724 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
725 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200726}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100727static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200728
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100729static ssize_t
730show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200731{
732 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800733 int len;
734
735 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
736 if (len != -ENODEV)
737 return len;
738
Jean Delvareeb8a7902008-05-18 20:49:41 +0200739 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200740}
Jean Delvare51298d12009-09-18 22:45:45 +0200741static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
742
743static struct attribute *i2c_dev_attrs[] = {
744 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200745 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200746 &dev_attr_modalias.attr,
747 NULL
748};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100749ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100750
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200751struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100752 .name = "i2c",
753 .match = i2c_device_match,
754 .probe = i2c_device_probe,
755 .remove = i2c_device_remove,
756 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000757};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200758EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000759
Jean Delvare51298d12009-09-18 22:45:45 +0200760static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100761 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200762 .uevent = i2c_device_uevent,
763 .release = i2c_client_dev_release,
764};
765
David Brownell9b766b82008-01-27 18:14:51 +0100766
767/**
768 * i2c_verify_client - return parameter as i2c_client, or NULL
769 * @dev: device, probably from some driver model iterator
770 *
771 * When traversing the driver model tree, perhaps using driver model
772 * iterators like @device_for_each_child(), you can't assume very much
773 * about the nodes you find. Use this function to avoid oopses caused
774 * by wrongly treating some non-I2C device as an i2c_client.
775 */
776struct i2c_client *i2c_verify_client(struct device *dev)
777{
Jean Delvare51298d12009-09-18 22:45:45 +0200778 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100779 ? to_i2c_client(dev)
780 : NULL;
781}
782EXPORT_SYMBOL(i2c_verify_client);
783
784
Wolfram Sangda899f52015-05-18 21:09:12 +0200785/* Return a unique address which takes the flags of the client into account */
786static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
787{
788 unsigned short addr = client->addr;
789
790 /* For some client flags, add an arbitrary offset to avoid collisions */
791 if (client->flags & I2C_CLIENT_TEN)
792 addr |= I2C_ADDR_OFFSET_TEN_BIT;
793
794 if (client->flags & I2C_CLIENT_SLAVE)
795 addr |= I2C_ADDR_OFFSET_SLAVE;
796
797 return addr;
798}
799
Jean Delvare3a89db52010-06-03 11:33:52 +0200800/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200802static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200803{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200804 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200805 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200806 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200807 return -EINVAL;
808 } else {
809 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200810 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200811 return -EINVAL;
812 }
813 return 0;
814}
815
Jean Delvare656b8762010-06-03 11:33:53 +0200816/* And this is a strict address validity check, used when probing. If a
817 * device uses a reserved address, then it shouldn't be probed. 7-bit
818 * addressing is assumed, 10-bit address devices are rare and should be
819 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +0200820static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200821{
822 /*
823 * Reserved addresses per I2C specification:
824 * 0x00 General call address / START byte
825 * 0x01 CBUS address
826 * 0x02 Reserved for different bus format
827 * 0x03 Reserved for future purposes
828 * 0x04-0x07 Hs-mode master code
829 * 0x78-0x7b 10-bit slave addressing
830 * 0x7c-0x7f Reserved for future purposes
831 */
832 if (addr < 0x08 || addr > 0x77)
833 return -EINVAL;
834 return 0;
835}
836
Jean Delvare3b5f7942010-06-03 11:33:55 +0200837static int __i2c_check_addr_busy(struct device *dev, void *addrp)
838{
839 struct i2c_client *client = i2c_verify_client(dev);
840 int addr = *(int *)addrp;
841
Wolfram Sang9bccc702015-07-17 14:48:56 +0200842 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200843 return -EBUSY;
844 return 0;
845}
846
Michael Lawnick08263742010-08-11 18:21:02 +0200847/* walk up mux tree */
848static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
849{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200850 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200851 int result;
852
853 result = device_for_each_child(&adapter->dev, &addr,
854 __i2c_check_addr_busy);
855
Jean Delvare97cc4d42010-10-24 18:16:57 +0200856 if (!result && parent)
857 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200858
859 return result;
860}
861
862/* recurse down mux tree */
863static int i2c_check_mux_children(struct device *dev, void *addrp)
864{
865 int result;
866
867 if (dev->type == &i2c_adapter_type)
868 result = device_for_each_child(dev, addrp,
869 i2c_check_mux_children);
870 else
871 result = __i2c_check_addr_busy(dev, addrp);
872
873 return result;
874}
875
Jean Delvare3b5f7942010-06-03 11:33:55 +0200876static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
877{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200878 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200879 int result = 0;
880
Jean Delvare97cc4d42010-10-24 18:16:57 +0200881 if (parent)
882 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200883
884 if (!result)
885 result = device_for_each_child(&adapter->dev, &addr,
886 i2c_check_mux_children);
887
888 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200889}
890
David Brownell9c1600e2007-05-01 23:26:31 +0200891/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200892 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
893 * @adapter: Target I2C bus segment
894 */
895void i2c_lock_adapter(struct i2c_adapter *adapter)
896{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200897 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
898
899 if (parent)
900 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200901 else
902 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200903}
904EXPORT_SYMBOL_GPL(i2c_lock_adapter);
905
906/**
907 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
908 * @adapter: Target I2C bus segment
909 */
910static int i2c_trylock_adapter(struct i2c_adapter *adapter)
911{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200912 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
913
914 if (parent)
915 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200916 else
917 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200918}
919
920/**
921 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
922 * @adapter: Target I2C bus segment
923 */
924void i2c_unlock_adapter(struct i2c_adapter *adapter)
925{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200926 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
927
928 if (parent)
929 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200930 else
931 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200932}
933EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
934
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200935static void i2c_dev_set_name(struct i2c_adapter *adap,
936 struct i2c_client *client)
937{
938 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
939
940 if (adev) {
941 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
942 return;
943 }
944
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200945 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200946 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200947}
948
Jean Delvarefe61e072010-08-11 18:20:58 +0200949/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200950 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200951 * @adap: the adapter managing the device
952 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200953 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200954 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200955 * Create an i2c device. Binding is handled through driver model
956 * probe()/remove() methods. A driver may be bound to this device when we
957 * return from this function, or any later moment (e.g. maybe hotplugging will
958 * load the driver module). This call is not appropriate for use by mainboard
959 * initialization logic, which usually runs during an arch_initcall() long
960 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200961 *
962 * This returns the new i2c client, which may be saved for later use with
963 * i2c_unregister_device(); or NULL to indicate an error.
964 */
965struct i2c_client *
966i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
967{
968 struct i2c_client *client;
969 int status;
970
971 client = kzalloc(sizeof *client, GFP_KERNEL);
972 if (!client)
973 return NULL;
974
975 client->adapter = adap;
976
977 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200978
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200979 if (info->archdata)
980 client->dev.archdata = *info->archdata;
981
Marc Pignatee354252008-08-28 08:33:22 +0200982 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200983 client->addr = info->addr;
984 client->irq = info->irq;
985
David Brownell9c1600e2007-05-01 23:26:31 +0200986 strlcpy(client->name, info->type, sizeof(client->name));
987
Wolfram Sangc4019b72015-07-17 12:50:06 +0200988 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200989 if (status) {
990 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
991 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
992 goto out_err_silent;
993 }
994
Jean Delvaref8a227e2009-06-19 16:58:18 +0200995 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200996 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200997 if (status)
998 goto out_err;
999
1000 client->dev.parent = &client->adapter->dev;
1001 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001002 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001003 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001004 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001005
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001006 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001007 status = device_register(&client->dev);
1008 if (status)
1009 goto out_err;
1010
Jean Delvaref8a227e2009-06-19 16:58:18 +02001011 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1012 client->name, dev_name(&client->dev));
1013
David Brownell9c1600e2007-05-01 23:26:31 +02001014 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001015
1016out_err:
1017 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1018 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001019out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001020 kfree(client);
1021 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001022}
1023EXPORT_SYMBOL_GPL(i2c_new_device);
1024
1025
1026/**
1027 * i2c_unregister_device - reverse effect of i2c_new_device()
1028 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001029 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001030 */
1031void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001032{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001033 if (client->dev.of_node)
1034 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +02001035 device_unregister(&client->dev);
1036}
David Brownell9c1600e2007-05-01 23:26:31 +02001037EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001038
1039
Jean Delvare60b129d2008-05-11 20:37:06 +02001040static const struct i2c_device_id dummy_id[] = {
1041 { "dummy", 0 },
1042 { },
1043};
1044
Jean Delvared2653e92008-04-29 23:11:39 +02001045static int dummy_probe(struct i2c_client *client,
1046 const struct i2c_device_id *id)
1047{
1048 return 0;
1049}
1050
1051static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001052{
1053 return 0;
1054}
1055
1056static struct i2c_driver dummy_driver = {
1057 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001058 .probe = dummy_probe,
1059 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001060 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001061};
1062
1063/**
1064 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1065 * @adapter: the adapter managing the device
1066 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001067 * Context: can sleep
1068 *
1069 * This returns an I2C client bound to the "dummy" driver, intended for use
1070 * with devices that consume multiple addresses. Examples of such chips
1071 * include various EEPROMS (like 24c04 and 24c08 models).
1072 *
1073 * These dummy devices have two main uses. First, most I2C and SMBus calls
1074 * except i2c_transfer() need a client handle; the dummy will be that handle.
1075 * And second, this prevents the specified address from being bound to a
1076 * different driver.
1077 *
1078 * This returns the new i2c client, which should be saved for later use with
1079 * i2c_unregister_device(); or NULL to indicate an error.
1080 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001081struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001082{
1083 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001084 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001085 };
1086
David Brownelle9f13732008-01-27 18:14:52 +01001087 return i2c_new_device(adapter, &info);
1088}
1089EXPORT_SYMBOL_GPL(i2c_new_dummy);
1090
David Brownellf37dd802007-02-13 22:09:00 +01001091/* ------------------------------------------------------------------------- */
1092
David Brownell16ffadf2007-05-01 23:26:28 +02001093/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1094
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001095static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
David Brownellef2c83212007-05-01 23:26:28 +02001097 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 complete(&adap->dev_released);
1099}
1100
Jean Delvare99cd8e22009-06-19 16:58:20 +02001101/*
Jean Delvare390946b2012-09-10 10:14:02 +02001102 * This function is only needed for mutex_lock_nested, so it is never
1103 * called unless locking correctness checking is enabled. Thus we
1104 * make it inline to avoid a compiler warning. That's what gcc ends up
1105 * doing anyway.
1106 */
1107static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1108{
1109 unsigned int depth = 0;
1110
1111 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1112 depth++;
1113
1114 return depth;
1115}
1116
1117/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001118 * Let users instantiate I2C devices through sysfs. This can be used when
1119 * platform initialization code doesn't contain the proper data for
1120 * whatever reason. Also useful for drivers that do device detection and
1121 * detection fails, either because the device uses an unexpected address,
1122 * or this is a compatible device with different ID register values.
1123 *
1124 * Parameter checking may look overzealous, but we really don't want
1125 * the user to provide incorrect parameters.
1126 */
1127static ssize_t
1128i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1129 const char *buf, size_t count)
1130{
1131 struct i2c_adapter *adap = to_i2c_adapter(dev);
1132 struct i2c_board_info info;
1133 struct i2c_client *client;
1134 char *blank, end;
1135 int res;
1136
Jean Delvare99cd8e22009-06-19 16:58:20 +02001137 memset(&info, 0, sizeof(struct i2c_board_info));
1138
1139 blank = strchr(buf, ' ');
1140 if (!blank) {
1141 dev_err(dev, "%s: Missing parameters\n", "new_device");
1142 return -EINVAL;
1143 }
1144 if (blank - buf > I2C_NAME_SIZE - 1) {
1145 dev_err(dev, "%s: Invalid device name\n", "new_device");
1146 return -EINVAL;
1147 }
1148 memcpy(info.type, buf, blank - buf);
1149
1150 /* Parse remaining parameters, reject extra parameters */
1151 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1152 if (res < 1) {
1153 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1154 return -EINVAL;
1155 }
1156 if (res > 1 && end != '\n') {
1157 dev_err(dev, "%s: Extra parameters\n", "new_device");
1158 return -EINVAL;
1159 }
1160
Wolfram Sangcfa03272015-07-27 14:03:38 +02001161 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1162 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1163 info.flags |= I2C_CLIENT_TEN;
1164 }
1165
1166 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1167 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1168 info.flags |= I2C_CLIENT_SLAVE;
1169 }
1170
Jean Delvare99cd8e22009-06-19 16:58:20 +02001171 client = i2c_new_device(adap, &info);
1172 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001173 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001174
1175 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001176 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001177 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001178 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001179 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1180 info.type, info.addr);
1181
1182 return count;
1183}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001184static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001185
1186/*
1187 * And of course let the users delete the devices they instantiated, if
1188 * they got it wrong. This interface can only be used to delete devices
1189 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1190 * don't delete devices to which some kernel code still has references.
1191 *
1192 * Parameter checking may look overzealous, but we really don't want
1193 * the user to delete the wrong device.
1194 */
1195static ssize_t
1196i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1197 const char *buf, size_t count)
1198{
1199 struct i2c_adapter *adap = to_i2c_adapter(dev);
1200 struct i2c_client *client, *next;
1201 unsigned short addr;
1202 char end;
1203 int res;
1204
1205 /* Parse parameters, reject extra parameters */
1206 res = sscanf(buf, "%hi%c", &addr, &end);
1207 if (res < 1) {
1208 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1209 return -EINVAL;
1210 }
1211 if (res > 1 && end != '\n') {
1212 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1213 return -EINVAL;
1214 }
1215
1216 /* Make sure the device was added through sysfs */
1217 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001218 mutex_lock_nested(&adap->userspace_clients_lock,
1219 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001220 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1221 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001222 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001223 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1224 "delete_device", client->name, client->addr);
1225
1226 list_del(&client->detected);
1227 i2c_unregister_device(client);
1228 res = count;
1229 break;
1230 }
1231 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001232 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001233
1234 if (res < 0)
1235 dev_err(dev, "%s: Can't find device in list\n",
1236 "delete_device");
1237 return res;
1238}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001239static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1240 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001241
1242static struct attribute *i2c_adapter_attrs[] = {
1243 &dev_attr_name.attr,
1244 &dev_attr_new_device.attr,
1245 &dev_attr_delete_device.attr,
1246 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001247};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001248ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001249
Michael Lawnick08263742010-08-11 18:21:02 +02001250struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001251 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001252 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001253};
Michael Lawnick08263742010-08-11 18:21:02 +02001254EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Stephen Warren643dd092012-04-17 12:43:33 -06001256/**
1257 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1258 * @dev: device, probably from some driver model iterator
1259 *
1260 * When traversing the driver model tree, perhaps using driver model
1261 * iterators like @device_for_each_child(), you can't assume very much
1262 * about the nodes you find. Use this function to avoid oopses caused
1263 * by wrongly treating some non-I2C device as an i2c_adapter.
1264 */
1265struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1266{
1267 return (dev->type == &i2c_adapter_type)
1268 ? to_i2c_adapter(dev)
1269 : NULL;
1270}
1271EXPORT_SYMBOL(i2c_verify_adapter);
1272
Jean Delvare2bb50952009-09-18 22:45:46 +02001273#ifdef CONFIG_I2C_COMPAT
1274static struct class_compat *i2c_adapter_compat_class;
1275#endif
1276
David Brownell9c1600e2007-05-01 23:26:31 +02001277static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1278{
1279 struct i2c_devinfo *devinfo;
1280
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001281 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001282 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1283 if (devinfo->busnum == adapter->nr
1284 && !i2c_new_device(adapter,
1285 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001286 dev_err(&adapter->dev,
1287 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001288 devinfo->board_info.addr);
1289 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001290 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001291}
1292
Wolfram Sang687b81d2013-07-11 12:56:15 +01001293/* OF support code */
1294
1295#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001296static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1297 struct device_node *node)
1298{
1299 struct i2c_client *result;
1300 struct i2c_board_info info = {};
1301 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001302 const __be32 *addr_be;
1303 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001304 int len;
1305
1306 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1307
1308 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1309 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1310 node->full_name);
1311 return ERR_PTR(-EINVAL);
1312 }
1313
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001314 addr_be = of_get_property(node, "reg", &len);
1315 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001316 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1317 node->full_name);
1318 return ERR_PTR(-EINVAL);
1319 }
1320
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001321 addr = be32_to_cpup(addr_be);
1322 if (addr & I2C_TEN_BIT_ADDRESS) {
1323 addr &= ~I2C_TEN_BIT_ADDRESS;
1324 info.flags |= I2C_CLIENT_TEN;
1325 }
1326
1327 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1328 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1329 info.flags |= I2C_CLIENT_SLAVE;
1330 }
1331
1332 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001333 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1334 info.addr, node->full_name);
1335 return ERR_PTR(-EINVAL);
1336 }
1337
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001338 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001339 info.of_node = of_node_get(node);
1340 info.archdata = &dev_ad;
1341
1342 if (of_get_property(node, "wakeup-source", NULL))
1343 info.flags |= I2C_CLIENT_WAKE;
1344
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001345 result = i2c_new_device(adap, &info);
1346 if (result == NULL) {
1347 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1348 node->full_name);
1349 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001350 return ERR_PTR(-EINVAL);
1351 }
1352 return result;
1353}
1354
Wolfram Sang687b81d2013-07-11 12:56:15 +01001355static void of_i2c_register_devices(struct i2c_adapter *adap)
1356{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001357 struct device_node *node;
1358
1359 /* Only register child devices if the adapter has a node pointer set */
1360 if (!adap->dev.of_node)
1361 return;
1362
1363 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1364
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001365 for_each_available_child_of_node(adap->dev.of_node, node) {
1366 if (of_node_test_and_set_flag(node, OF_POPULATED))
1367 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001368 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001369 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001370}
1371
1372static int of_dev_node_match(struct device *dev, void *data)
1373{
1374 return dev->of_node == data;
1375}
1376
1377/* must call put_device() when done with returned i2c_client device */
1378struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1379{
1380 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001381 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001382
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001383 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001384 if (!dev)
1385 return NULL;
1386
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001387 client = i2c_verify_client(dev);
1388 if (!client)
1389 put_device(dev);
1390
1391 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001392}
1393EXPORT_SYMBOL(of_find_i2c_device_by_node);
1394
1395/* must call put_device() when done with returned i2c_adapter device */
1396struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1397{
1398 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001399 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001400
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001401 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001402 if (!dev)
1403 return NULL;
1404
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001405 adapter = i2c_verify_adapter(dev);
1406 if (!adapter)
1407 put_device(dev);
1408
1409 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001410}
1411EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001412
1413/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1414struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1415{
1416 struct i2c_adapter *adapter;
1417
1418 adapter = of_find_i2c_adapter_by_node(node);
1419 if (!adapter)
1420 return NULL;
1421
1422 if (!try_module_get(adapter->owner)) {
1423 put_device(&adapter->dev);
1424 adapter = NULL;
1425 }
1426
1427 return adapter;
1428}
1429EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001430#else
1431static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1432#endif /* CONFIG_OF */
1433
Jean Delvare69b00892009-12-06 17:06:27 +01001434static int i2c_do_add_adapter(struct i2c_driver *driver,
1435 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001436{
Jean Delvare4735c982008-07-14 22:38:36 +02001437 /* Detect supported devices on that bus, and instantiate them */
1438 i2c_detect(adap, driver);
1439
1440 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001441 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001442 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1443 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001444 dev_warn(&adap->dev, "Please use another way to instantiate "
1445 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001446 /* We ignore the return code; if it fails, too bad */
1447 driver->attach_adapter(adap);
1448 }
1449 return 0;
1450}
1451
Jean Delvare69b00892009-12-06 17:06:27 +01001452static int __process_new_adapter(struct device_driver *d, void *data)
1453{
1454 return i2c_do_add_adapter(to_i2c_driver(d), data);
1455}
1456
David Brownell6e13e642007-05-01 23:26:31 +02001457static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Jean Delvared6703282010-08-11 18:20:59 +02001459 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
David Brownell1d0b19c2008-10-14 17:30:05 +02001461 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001462 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1463 res = -EAGAIN;
1464 goto out_list;
1465 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001466
Jean Delvare2236baa2010-11-15 22:40:38 +01001467 /* Sanity checks */
1468 if (unlikely(adap->name[0] == '\0')) {
1469 pr_err("i2c-core: Attempt to register an adapter with "
1470 "no name!\n");
1471 return -EINVAL;
1472 }
1473 if (unlikely(!adap->algo)) {
1474 pr_err("i2c-core: Attempt to register adapter '%s' with "
1475 "no algo!\n", adap->name);
1476 return -EINVAL;
1477 }
1478
Mika Kuoppala194684e2009-12-06 17:06:22 +01001479 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001480 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001481 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jean Delvare8fcfef62009-03-28 21:34:43 +01001483 /* Set default timeout to 1 second if not already set */
1484 if (adap->timeout == 0)
1485 adap->timeout = HZ;
1486
Kay Sievers27d9c182009-01-07 14:29:16 +01001487 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001488 adap->dev.bus = &i2c_bus_type;
1489 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001490 res = device_register(&adap->dev);
1491 if (res)
1492 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001494 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1495
Charles Keepax6ada5c12015-04-16 13:05:19 +01001496 pm_runtime_no_callbacks(&adap->dev);
1497
Jean Delvare2bb50952009-09-18 22:45:46 +02001498#ifdef CONFIG_I2C_COMPAT
1499 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1500 adap->dev.parent);
1501 if (res)
1502 dev_warn(&adap->dev,
1503 "Failed to create compatibility class link\n");
1504#endif
1505
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301506 /* bus recovery specific initialization */
1507 if (adap->bus_recovery_info) {
1508 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1509
1510 if (!bri->recover_bus) {
1511 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1512 adap->bus_recovery_info = NULL;
1513 goto exit_recovery;
1514 }
1515
1516 /* Generic GPIO recovery */
1517 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1518 if (!gpio_is_valid(bri->scl_gpio)) {
1519 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1520 adap->bus_recovery_info = NULL;
1521 goto exit_recovery;
1522 }
1523
1524 if (gpio_is_valid(bri->sda_gpio))
1525 bri->get_sda = get_sda_gpio_value;
1526 else
1527 bri->get_sda = NULL;
1528
1529 bri->get_scl = get_scl_gpio_value;
1530 bri->set_scl = set_scl_gpio_value;
1531 } else if (!bri->set_scl || !bri->get_scl) {
1532 /* Generic SCL recovery */
1533 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1534 adap->bus_recovery_info = NULL;
1535 }
1536 }
1537
1538exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001539 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001540 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001541 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001542 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001543
David Brownell6e13e642007-05-01 23:26:31 +02001544 if (adap->nr < __i2c_first_dynamic_bus_num)
1545 i2c_scan_static_board_info(adap);
1546
Jean Delvare4735c982008-07-14 22:38:36 +02001547 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001548 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001549 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001550 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001551
1552 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001553
Jean Delvareb119c6c2006-08-15 18:26:30 +02001554out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001555 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001556 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001557 mutex_unlock(&core_lock);
1558 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
David Brownell6e13e642007-05-01 23:26:31 +02001561/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001562 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1563 * @adap: the adapter to register (with adap->nr initialized)
1564 * Context: can sleep
1565 *
1566 * See i2c_add_numbered_adapter() for details.
1567 */
1568static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1569{
1570 int id;
1571
1572 mutex_lock(&core_lock);
1573 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1574 GFP_KERNEL);
1575 mutex_unlock(&core_lock);
1576 if (id < 0)
1577 return id == -ENOSPC ? -EBUSY : id;
1578
1579 return i2c_register_adapter(adap);
1580}
1581
1582/**
David Brownell6e13e642007-05-01 23:26:31 +02001583 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1584 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001585 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001586 *
1587 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001588 * doesn't matter or when its bus number is specified by an dt alias.
1589 * Examples of bases when the bus number doesn't matter: I2C adapters
1590 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001591 *
1592 * When this returns zero, a new bus number was allocated and stored
1593 * in adap->nr, and the specified adapter became available for clients.
1594 * Otherwise, a negative errno value is returned.
1595 */
1596int i2c_add_adapter(struct i2c_adapter *adapter)
1597{
Doug Andersonee5c2742013-03-01 08:57:31 -08001598 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001599 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001600
Doug Andersonee5c2742013-03-01 08:57:31 -08001601 if (dev->of_node) {
1602 id = of_alias_get_id(dev->of_node, "i2c");
1603 if (id >= 0) {
1604 adapter->nr = id;
1605 return __i2c_add_numbered_adapter(adapter);
1606 }
1607 }
1608
Jean Delvarecaada322008-01-27 18:14:49 +01001609 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001610 id = idr_alloc(&i2c_adapter_idr, adapter,
1611 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001612 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001613 if (id < 0)
1614 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001615
1616 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001617
David Brownell6e13e642007-05-01 23:26:31 +02001618 return i2c_register_adapter(adapter);
1619}
1620EXPORT_SYMBOL(i2c_add_adapter);
1621
1622/**
1623 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1624 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001625 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001626 *
1627 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001628 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1629 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001630 * is used to properly configure I2C devices.
1631 *
Grant Likely488bf312011-07-25 17:49:43 +02001632 * If the requested bus number is set to -1, then this function will behave
1633 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1634 *
David Brownell6e13e642007-05-01 23:26:31 +02001635 * If no devices have pre-been declared for this bus, then be sure to
1636 * register the adapter before any dynamically allocated ones. Otherwise
1637 * the required bus ID may not be available.
1638 *
1639 * When this returns zero, the specified adapter became available for
1640 * clients using the bus number provided in adap->nr. Also, the table
1641 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1642 * and the appropriate driver model device nodes are created. Otherwise, a
1643 * negative errno value is returned.
1644 */
1645int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1646{
Grant Likely488bf312011-07-25 17:49:43 +02001647 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1648 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001649
Doug Andersonee5c2742013-03-01 08:57:31 -08001650 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001651}
1652EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1653
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001654static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001655 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001656{
Jean Delvare4735c982008-07-14 22:38:36 +02001657 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001658
Jean Delvareacec2112009-03-28 21:34:40 +01001659 /* Remove the devices we created ourselves as the result of hardware
1660 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001661 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1662 if (client->adapter == adapter) {
1663 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1664 client->name, client->addr);
1665 list_del(&client->detected);
1666 i2c_unregister_device(client);
1667 }
1668 }
Jean Delvare026526f2008-01-27 18:14:49 +01001669}
1670
Jean Delvaree549c2b2009-06-19 16:58:19 +02001671static int __unregister_client(struct device *dev, void *dummy)
1672{
1673 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001674 if (client && strcmp(client->name, "dummy"))
1675 i2c_unregister_device(client);
1676 return 0;
1677}
1678
1679static int __unregister_dummy(struct device *dev, void *dummy)
1680{
1681 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001682 if (client)
1683 i2c_unregister_device(client);
1684 return 0;
1685}
1686
Jean Delvare69b00892009-12-06 17:06:27 +01001687static int __process_removed_adapter(struct device_driver *d, void *data)
1688{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001689 i2c_do_del_adapter(to_i2c_driver(d), data);
1690 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001691}
1692
David Brownelld64f73b2007-07-12 14:12:28 +02001693/**
1694 * i2c_del_adapter - unregister I2C adapter
1695 * @adap: the adapter being unregistered
1696 * Context: can sleep
1697 *
1698 * This unregisters an I2C adapter which was previously registered
1699 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1700 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001701void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001703 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001704 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001707 mutex_lock(&core_lock);
1708 found = idr_find(&i2c_adapter_idr, adap->nr);
1709 mutex_unlock(&core_lock);
1710 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001711 pr_debug("i2c-core: attempting to delete unregistered "
1712 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
Lan Tianyu5d98e612014-05-20 20:59:23 +08001716 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001717 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001718 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001719 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001720 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001721 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001723 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001724 mutex_lock_nested(&adap->userspace_clients_lock,
1725 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001726 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1727 detected) {
1728 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1729 client->addr);
1730 list_del(&client->detected);
1731 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001732 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001733 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001734
Jean Delvaree549c2b2009-06-19 16:58:19 +02001735 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001736 * check the returned value. This is a two-pass process, because
1737 * we can't remove the dummy devices during the first pass: they
1738 * could have been instantiated by real devices wishing to clean
1739 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001740 device_for_each_child(&adap->dev, NULL, __unregister_client);
1741 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jean Delvare2bb50952009-09-18 22:45:46 +02001743#ifdef CONFIG_I2C_COMPAT
1744 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1745 adap->dev.parent);
1746#endif
1747
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001748 /* device name is gone after device_unregister */
1749 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1750
Wolfram Sang26680ee2015-01-29 22:45:09 +01001751 /* wait until all references to the device are gone
1752 *
1753 * FIXME: This is old code and should ideally be replaced by an
1754 * alternative which results in decoupling the lifetime of the struct
1755 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301756 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
David Brownell6e13e642007-05-01 23:26:31 +02001762 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001763 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001765 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001767 /* Clear the device structure in case this adapter is ever going to be
1768 added again */
1769 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
David Brownellc0564602007-05-01 23:26:31 +02001771EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
David Brownell7b4fbc52007-05-01 23:26:30 +02001773/* ------------------------------------------------------------------------- */
1774
Jean Delvare7ae31482011-03-20 14:50:52 +01001775int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1776{
1777 int res;
1778
1779 mutex_lock(&core_lock);
1780 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1781 mutex_unlock(&core_lock);
1782
1783 return res;
1784}
1785EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1786
Jean Delvare69b00892009-12-06 17:06:27 +01001787static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001788{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001789 if (dev->type != &i2c_adapter_type)
1790 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001791 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001792}
1793
David Brownell7b4fbc52007-05-01 23:26:30 +02001794/*
1795 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001796 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
1798
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001799int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001801 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
David Brownell1d0b19c2008-10-14 17:30:05 +02001803 /* Can't register until after driver model init */
1804 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1805 return -EAGAIN;
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001808 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jean Delvare729d6dd2009-06-19 16:58:18 +02001811 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001812 * will have called probe() for all matching-but-unbound devices.
1813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 res = driver_register(&driver->driver);
1815 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001816 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001817
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001818 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Jean Delvare4735c982008-07-14 22:38:36 +02001820 INIT_LIST_HEAD(&driver->clients);
1821 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001822 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001823
Jean Delvare7eebcb72006-02-05 23:28:21 +01001824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001826EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jean Delvare69b00892009-12-06 17:06:27 +01001828static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001829{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001830 if (dev->type == &i2c_adapter_type)
1831 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1832 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001833}
1834
David Brownella1d9e6e2007-05-01 23:26:30 +02001835/**
1836 * i2c_del_driver - unregister I2C driver
1837 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001838 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001839 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001840void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Jean Delvare7ae31482011-03-20 14:50:52 +01001842 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001845 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
David Brownellc0564602007-05-01 23:26:31 +02001847EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
David Brownell7b4fbc52007-05-01 23:26:30 +02001849/* ------------------------------------------------------------------------- */
1850
Jean Delvaree48d3312008-01-27 18:14:48 +01001851/**
1852 * i2c_use_client - increments the reference count of the i2c client structure
1853 * @client: the client being referenced
1854 *
1855 * Each live reference to a client should be refcounted. The driver model does
1856 * that automatically as part of driver binding, so that most drivers don't
1857 * need to do this explicitly: they hold a reference until they're unbound
1858 * from the device.
1859 *
1860 * A pointer to the client with the incremented reference counter is returned.
1861 */
1862struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
David Brownell6ea438e2008-07-14 22:38:24 +02001864 if (client && get_device(&client->dev))
1865 return client;
1866 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
David Brownellc0564602007-05-01 23:26:31 +02001868EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Jean Delvaree48d3312008-01-27 18:14:48 +01001870/**
1871 * i2c_release_client - release a use of the i2c client structure
1872 * @client: the client being no longer referenced
1873 *
1874 * Must be called when a user of a client is finished with it.
1875 */
1876void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
David Brownell6ea438e2008-07-14 22:38:24 +02001878 if (client)
1879 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
David Brownellc0564602007-05-01 23:26:31 +02001881EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
David Brownell9b766b82008-01-27 18:14:51 +01001883struct i2c_cmd_arg {
1884 unsigned cmd;
1885 void *arg;
1886};
1887
1888static int i2c_cmd(struct device *dev, void *_arg)
1889{
1890 struct i2c_client *client = i2c_verify_client(dev);
1891 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001892 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001893
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001894 if (!client || !client->dev.driver)
1895 return 0;
1896
1897 driver = to_i2c_driver(client->dev.driver);
1898 if (driver->command)
1899 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001900 return 0;
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1904{
David Brownell9b766b82008-01-27 18:14:51 +01001905 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
David Brownell9b766b82008-01-27 18:14:51 +01001907 cmd_arg.cmd = cmd;
1908 cmd_arg.arg = arg;
1909 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
David Brownellc0564602007-05-01 23:26:31 +02001911EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001913#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1914static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1915 void *arg)
1916{
1917 struct of_reconfig_data *rd = arg;
1918 struct i2c_adapter *adap;
1919 struct i2c_client *client;
1920
1921 switch (of_reconfig_get_state_change(action, rd)) {
1922 case OF_RECONFIG_CHANGE_ADD:
1923 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1924 if (adap == NULL)
1925 return NOTIFY_OK; /* not for us */
1926
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001927 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1928 put_device(&adap->dev);
1929 return NOTIFY_OK;
1930 }
1931
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001932 client = of_i2c_register_device(adap, rd->dn);
1933 put_device(&adap->dev);
1934
1935 if (IS_ERR(client)) {
1936 pr_err("%s: failed to create for '%s'\n",
1937 __func__, rd->dn->full_name);
1938 return notifier_from_errno(PTR_ERR(client));
1939 }
1940 break;
1941 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001942 /* already depopulated? */
1943 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1944 return NOTIFY_OK;
1945
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001946 /* find our device by node */
1947 client = of_find_i2c_device_by_node(rd->dn);
1948 if (client == NULL)
1949 return NOTIFY_OK; /* no? not meant for us */
1950
1951 /* unregister takes one ref away */
1952 i2c_unregister_device(client);
1953
1954 /* and put the reference of the find */
1955 put_device(&client->dev);
1956 break;
1957 }
1958
1959 return NOTIFY_OK;
1960}
1961static struct notifier_block i2c_of_notifier = {
1962 .notifier_call = of_i2c_notify,
1963};
1964#else
1965extern struct notifier_block i2c_of_notifier;
1966#endif /* CONFIG_OF_DYNAMIC */
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static int __init i2c_init(void)
1969{
1970 int retval;
1971
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001972 retval = of_alias_get_highest_id("i2c");
1973
1974 down_write(&__i2c_board_lock);
1975 if (retval >= __i2c_first_dynamic_bus_num)
1976 __i2c_first_dynamic_bus_num = retval + 1;
1977 up_write(&__i2c_board_lock);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 retval = bus_register(&i2c_bus_type);
1980 if (retval)
1981 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001982#ifdef CONFIG_I2C_COMPAT
1983 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1984 if (!i2c_adapter_compat_class) {
1985 retval = -ENOMEM;
1986 goto bus_err;
1987 }
1988#endif
David Brownelle9f13732008-01-27 18:14:52 +01001989 retval = i2c_add_driver(&dummy_driver);
1990 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001991 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001992
1993 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1994 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1995
David Brownelle9f13732008-01-27 18:14:52 +01001996 return 0;
1997
Jean Delvare2bb50952009-09-18 22:45:46 +02001998class_err:
1999#ifdef CONFIG_I2C_COMPAT
2000 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002001bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002002#endif
David Brownelle9f13732008-01-27 18:14:52 +01002003 bus_unregister(&i2c_bus_type);
2004 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
2007static void __exit i2c_exit(void)
2008{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002009 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2010 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002011 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002012#ifdef CONFIG_I2C_COMPAT
2013 class_compat_unregister(i2c_adapter_compat_class);
2014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002016 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
David Brownella10f9e72008-10-14 17:30:06 +02002019/* We must initialize early, because some subsystems register i2c drivers
2020 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2021 */
2022postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023module_exit(i2c_exit);
2024
2025/* ----------------------------------------------------
2026 * the functional interface to the i2c busses.
2027 * ----------------------------------------------------
2028 */
2029
Wolfram Sangb7f62582015-01-05 23:45:59 +01002030/* Check if val is exceeding the quirk IFF quirk is non 0 */
2031#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2032
2033static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2034{
2035 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2036 err_msg, msg->addr, msg->len,
2037 msg->flags & I2C_M_RD ? "read" : "write");
2038 return -EOPNOTSUPP;
2039}
2040
2041static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2042{
2043 const struct i2c_adapter_quirks *q = adap->quirks;
2044 int max_num = q->max_num_msgs, i;
2045 bool do_len_check = true;
2046
2047 if (q->flags & I2C_AQ_COMB) {
2048 max_num = 2;
2049
2050 /* special checks for combined messages */
2051 if (num == 2) {
2052 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2053 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2054
2055 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2056 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2057
2058 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2059 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2060
2061 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2062 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2063
2064 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2065 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2066
2067 do_len_check = false;
2068 }
2069 }
2070
2071 if (i2c_quirk_exceeded(num, max_num))
2072 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2073
2074 for (i = 0; i < num; i++) {
2075 u16 len = msgs[i].len;
2076
2077 if (msgs[i].flags & I2C_M_RD) {
2078 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2079 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2080 } else {
2081 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2082 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2083 }
2084 }
2085
2086 return 0;
2087}
2088
David Brownella1cdeda2008-07-14 22:38:24 +02002089/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002090 * __i2c_transfer - unlocked flavor of i2c_transfer
2091 * @adap: Handle to I2C bus
2092 * @msgs: One or more messages to execute before STOP is issued to
2093 * terminate the operation; each message begins with a START.
2094 * @num: Number of messages to be executed.
2095 *
2096 * Returns negative errno, else the number of messages executed.
2097 *
2098 * Adapter lock must be held when calling this function. No debug logging
2099 * takes place. adap->algo->master_xfer existence isn't checked.
2100 */
2101int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2102{
2103 unsigned long orig_jiffies;
2104 int ret, try;
2105
Wolfram Sangb7f62582015-01-05 23:45:59 +01002106 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2107 return -EOPNOTSUPP;
2108
David Howellsd9a83d62014-03-06 13:35:59 +00002109 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2110 * enabled. This is an efficient way of keeping the for-loop from
2111 * being executed when not needed.
2112 */
2113 if (static_key_false(&i2c_trace_msg)) {
2114 int i;
2115 for (i = 0; i < num; i++)
2116 if (msgs[i].flags & I2C_M_RD)
2117 trace_i2c_read(adap, &msgs[i], i);
2118 else
2119 trace_i2c_write(adap, &msgs[i], i);
2120 }
2121
Jean Delvareb37d2a32012-06-29 07:47:19 -03002122 /* Retry automatically on arbitration loss */
2123 orig_jiffies = jiffies;
2124 for (ret = 0, try = 0; try <= adap->retries; try++) {
2125 ret = adap->algo->master_xfer(adap, msgs, num);
2126 if (ret != -EAGAIN)
2127 break;
2128 if (time_after(jiffies, orig_jiffies + adap->timeout))
2129 break;
2130 }
2131
David Howellsd9a83d62014-03-06 13:35:59 +00002132 if (static_key_false(&i2c_trace_msg)) {
2133 int i;
2134 for (i = 0; i < ret; i++)
2135 if (msgs[i].flags & I2C_M_RD)
2136 trace_i2c_reply(adap, &msgs[i], i);
2137 trace_i2c_result(adap, i, ret);
2138 }
2139
Jean Delvareb37d2a32012-06-29 07:47:19 -03002140 return ret;
2141}
2142EXPORT_SYMBOL(__i2c_transfer);
2143
2144/**
David Brownella1cdeda2008-07-14 22:38:24 +02002145 * i2c_transfer - execute a single or combined I2C message
2146 * @adap: Handle to I2C bus
2147 * @msgs: One or more messages to execute before STOP is issued to
2148 * terminate the operation; each message begins with a START.
2149 * @num: Number of messages to be executed.
2150 *
2151 * Returns negative errno, else the number of messages executed.
2152 *
2153 * Note that there is no requirement that each message be sent to
2154 * the same slave address, although that is the most common model.
2155 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002156int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002158 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
David Brownella1cdeda2008-07-14 22:38:24 +02002160 /* REVISIT the fault reporting model here is weak:
2161 *
2162 * - When we get an error after receiving N bytes from a slave,
2163 * there is no way to report "N".
2164 *
2165 * - When we get a NAK after transmitting N bytes to a slave,
2166 * there is no way to report "N" ... or to let the master
2167 * continue executing the rest of this combined message, if
2168 * that's the appropriate response.
2169 *
2170 * - When for example "num" is two and we successfully complete
2171 * the first message but get an error part way through the
2172 * second, it's unclear whether that should be reported as
2173 * one (discarding status on the second message) or errno
2174 * (discarding status on the first one).
2175 */
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (adap->algo->master_xfer) {
2178#ifdef DEBUG
2179 for (ret = 0; ret < num; ret++) {
2180 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002181 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2182 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2183 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185#endif
2186
Mike Rapoportcea443a2008-01-27 18:14:50 +01002187 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002188 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002189 if (!ret)
2190 /* I2C activity is ongoing. */
2191 return -EAGAIN;
2192 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002193 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002194 }
2195
Jean Delvareb37d2a32012-06-29 07:47:19 -03002196 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002197 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 return ret;
2200 } else {
2201 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002202 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204}
David Brownellc0564602007-05-01 23:26:31 +02002205EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
David Brownella1cdeda2008-07-14 22:38:24 +02002207/**
2208 * i2c_master_send - issue a single I2C message in master transmit mode
2209 * @client: Handle to slave device
2210 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002211 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002212 *
2213 * Returns negative errno, or else the number of bytes written.
2214 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002215int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002218 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 struct i2c_msg msg;
2220
Jean Delvare815f55f2005-05-07 22:58:46 +02002221 msg.addr = client->addr;
2222 msg.flags = client->flags & I2C_M_TEN;
2223 msg.len = count;
2224 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002225
Jean Delvare815f55f2005-05-07 22:58:46 +02002226 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002228 /*
2229 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2230 * transmitted, else error code.
2231 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002232 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
David Brownellc0564602007-05-01 23:26:31 +02002234EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
David Brownella1cdeda2008-07-14 22:38:24 +02002236/**
2237 * i2c_master_recv - issue a single I2C message in master receive mode
2238 * @client: Handle to slave device
2239 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002240 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002241 *
2242 * Returns negative errno, or else the number of bytes read.
2243 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002244int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Farid Hammane7225acf2010-05-21 18:40:58 +02002246 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 struct i2c_msg msg;
2248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jean Delvare815f55f2005-05-07 22:58:46 +02002250 msg.addr = client->addr;
2251 msg.flags = client->flags & I2C_M_TEN;
2252 msg.flags |= I2C_M_RD;
2253 msg.len = count;
2254 msg.buf = buf;
2255
2256 ret = i2c_transfer(adap, &msg, 1);
2257
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002258 /*
2259 * If everything went ok (i.e. 1 msg received), return #bytes received,
2260 * else error code.
2261 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002262 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
David Brownellc0564602007-05-01 23:26:31 +02002264EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266/* ----------------------------------------------------
2267 * the i2c address scanning function
2268 * Will not work for 10-bit addresses!
2269 * ----------------------------------------------------
2270 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002271
Jean Delvare63e4e802010-06-03 11:33:51 +02002272/*
2273 * Legacy default probe function, mostly relevant for SMBus. The default
2274 * probe method is a quick write, but it is known to corrupt the 24RF08
2275 * EEPROMs due to a state machine bug, and could also irreversibly
2276 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2277 * we use a short byte read instead. Also, some bus drivers don't implement
2278 * quick write, so we fallback to a byte read in that case too.
2279 * On x86, there is another special case for FSC hardware monitoring chips,
2280 * which want regular byte reads (address 0x73.) Fortunately, these are the
2281 * only known chips using this I2C address on PC hardware.
2282 * Returns 1 if probe succeeded, 0 if not.
2283 */
2284static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2285{
2286 int err;
2287 union i2c_smbus_data dummy;
2288
2289#ifdef CONFIG_X86
2290 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2291 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2292 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2293 I2C_SMBUS_BYTE_DATA, &dummy);
2294 else
2295#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002296 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2297 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002298 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2299 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002300 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2301 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2302 I2C_SMBUS_BYTE, &dummy);
2303 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002304 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2305 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002306 err = -EOPNOTSUPP;
2307 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002308
2309 return err >= 0;
2310}
2311
Jean Delvareccfbbd02009-12-06 17:06:25 +01002312static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002313 struct i2c_driver *driver)
2314{
2315 struct i2c_board_info info;
2316 struct i2c_adapter *adapter = temp_client->adapter;
2317 int addr = temp_client->addr;
2318 int err;
2319
2320 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002321 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002322 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002323 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2324 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002325 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002326 }
2327
Wolfram Sang9bccc702015-07-17 14:48:56 +02002328 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002329 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002330 return 0;
2331
Jean Delvareccfbbd02009-12-06 17:06:25 +01002332 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002333 if (!i2c_default_probe(adapter, addr))
2334 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002335
2336 /* Finally call the custom detection function */
2337 memset(&info, 0, sizeof(struct i2c_board_info));
2338 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002339 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002340 if (err) {
2341 /* -ENODEV is returned if the detection fails. We catch it
2342 here as this isn't an error. */
2343 return err == -ENODEV ? 0 : err;
2344 }
2345
2346 /* Consistency check */
2347 if (info.type[0] == '\0') {
2348 dev_err(&adapter->dev, "%s detection function provided "
2349 "no name for 0x%x\n", driver->driver.name,
2350 addr);
2351 } else {
2352 struct i2c_client *client;
2353
2354 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002355 if (adapter->class & I2C_CLASS_DEPRECATED)
2356 dev_warn(&adapter->dev,
2357 "This adapter will soon drop class based instantiation of devices. "
2358 "Please make sure client 0x%02x gets instantiated by other means. "
2359 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2360 info.addr);
2361
Jean Delvare4735c982008-07-14 22:38:36 +02002362 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2363 info.type, info.addr);
2364 client = i2c_new_device(adapter, &info);
2365 if (client)
2366 list_add_tail(&client->detected, &driver->clients);
2367 else
2368 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2369 info.type, info.addr);
2370 }
2371 return 0;
2372}
2373
2374static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2375{
Jean Delvarec3813d62009-12-14 21:17:25 +01002376 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002377 struct i2c_client *temp_client;
2378 int i, err = 0;
2379 int adap_id = i2c_adapter_id(adapter);
2380
Jean Delvarec3813d62009-12-14 21:17:25 +01002381 address_list = driver->address_list;
2382 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002383 return 0;
2384
Wolfram Sang45552272014-07-10 13:46:21 +02002385 /* Warn that the adapter lost class based instantiation */
2386 if (adapter->class == I2C_CLASS_DEPRECATED) {
2387 dev_dbg(&adapter->dev,
2388 "This adapter dropped support for I2C classes and "
2389 "won't auto-detect %s devices anymore. If you need it, check "
2390 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2391 driver->driver.name);
2392 return 0;
2393 }
2394
Jean Delvare51b54ba2010-10-24 18:16:58 +02002395 /* Stop here if the classes do not match */
2396 if (!(adapter->class & driver->class))
2397 return 0;
2398
Jean Delvare4735c982008-07-14 22:38:36 +02002399 /* Set up a temporary client to help detect callback */
2400 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2401 if (!temp_client)
2402 return -ENOMEM;
2403 temp_client->adapter = adapter;
2404
Jean Delvarec3813d62009-12-14 21:17:25 +01002405 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002406 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002407 "addr 0x%02x\n", adap_id, address_list[i]);
2408 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002409 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002410 if (unlikely(err))
2411 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002412 }
2413
Jean Delvare4735c982008-07-14 22:38:36 +02002414 kfree(temp_client);
2415 return err;
2416}
2417
Jean Delvared44f19d2010-08-11 18:20:57 +02002418int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2419{
2420 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2421 I2C_SMBUS_QUICK, NULL) >= 0;
2422}
2423EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2424
Jean Delvare12b5053a2007-05-01 23:26:31 +02002425struct i2c_client *
2426i2c_new_probed_device(struct i2c_adapter *adap,
2427 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002428 unsigned short const *addr_list,
2429 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002430{
2431 int i;
2432
Jean Delvare8031d792010-08-11 18:21:00 +02002433 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002434 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002435
Jean Delvare12b5053a2007-05-01 23:26:31 +02002436 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2437 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002438 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002439 dev_warn(&adap->dev, "Invalid 7-bit address "
2440 "0x%02x\n", addr_list[i]);
2441 continue;
2442 }
2443
Wolfram Sang9bccc702015-07-17 14:48:56 +02002444 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002445 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002446 dev_dbg(&adap->dev, "Address 0x%02x already in "
2447 "use, not probing\n", addr_list[i]);
2448 continue;
2449 }
2450
Jean Delvare63e4e802010-06-03 11:33:51 +02002451 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002452 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002453 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002454 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002455
2456 if (addr_list[i] == I2C_CLIENT_END) {
2457 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2458 return NULL;
2459 }
2460
2461 info->addr = addr_list[i];
2462 return i2c_new_device(adap, info);
2463}
2464EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2465
Jean Delvared735b342011-03-20 14:50:52 +01002466struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002469
Jean Delvarecaada322008-01-27 18:14:49 +01002470 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002471 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002472 if (!adapter)
2473 goto exit;
2474
2475 if (try_module_get(adapter->owner))
2476 get_device(&adapter->dev);
2477 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002478 adapter = NULL;
2479
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002480 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002481 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002482 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
David Brownellc0564602007-05-01 23:26:31 +02002484EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486void i2c_put_adapter(struct i2c_adapter *adap)
2487{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002488 if (!adap)
2489 return;
2490
2491 put_device(&adap->dev);
2492 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
David Brownellc0564602007-05-01 23:26:31 +02002494EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496/* The SMBus parts */
2497
David Brownell438d6c22006-12-10 21:21:31 +01002498#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002499static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002502
Farid Hammane7225acf2010-05-21 18:40:58 +02002503 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002504 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 data = data ^ POLY;
2506 data = data << 1;
2507 }
2508 return (u8)(data >> 8);
2509}
2510
Jean Delvare421ef472005-10-26 21:28:55 +02002511/* Incremental CRC8 over count bytes in the array pointed to by p */
2512static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 int i;
2515
Farid Hammane7225acf2010-05-21 18:40:58 +02002516 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002517 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return crc;
2519}
2520
Jean Delvare421ef472005-10-26 21:28:55 +02002521/* Assume a 7-bit address, which is reasonable for SMBus */
2522static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Jean Delvare421ef472005-10-26 21:28:55 +02002524 /* The address will be sent first */
2525 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2526 pec = i2c_smbus_pec(pec, &addr, 1);
2527
2528 /* The data buffer follows */
2529 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Jean Delvare421ef472005-10-26 21:28:55 +02002532/* Used for write only transactions */
2533static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Jean Delvare421ef472005-10-26 21:28:55 +02002535 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2536 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Jean Delvare421ef472005-10-26 21:28:55 +02002539/* Return <0 on CRC error
2540 If there was a write before this read (most cases) we need to take the
2541 partial CRC from the write part into account.
2542 Note that this function does modify the message (we need to decrease the
2543 message length to hide the CRC byte from the caller). */
2544static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Jean Delvare421ef472005-10-26 21:28:55 +02002546 u8 rpec = msg->buf[--msg->len];
2547 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (rpec != cpec) {
2550 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2551 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002552 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
David Brownell438d6c22006-12-10 21:21:31 +01002554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
David Brownella1cdeda2008-07-14 22:38:24 +02002557/**
2558 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2559 * @client: Handle to slave device
2560 *
2561 * This executes the SMBus "receive byte" protocol, returning negative errno
2562 * else the byte received from the device.
2563 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002564s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002567 int status;
2568
2569 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2570 I2C_SMBUS_READ, 0,
2571 I2C_SMBUS_BYTE, &data);
2572 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
David Brownellc0564602007-05-01 23:26:31 +02002574EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David Brownella1cdeda2008-07-14 22:38:24 +02002576/**
2577 * i2c_smbus_write_byte - SMBus "send byte" protocol
2578 * @client: Handle to slave device
2579 * @value: Byte to be sent
2580 *
2581 * This executes the SMBus "send byte" protocol, returning negative errno
2582 * else zero on success.
2583 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002584s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Farid Hammane7225acf2010-05-21 18:40:58 +02002586 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002587 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
David Brownellc0564602007-05-01 23:26:31 +02002589EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
David Brownella1cdeda2008-07-14 22:38:24 +02002591/**
2592 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2593 * @client: Handle to slave device
2594 * @command: Byte interpreted by slave
2595 *
2596 * This executes the SMBus "read byte" protocol, returning negative errno
2597 * else a data byte received from the device.
2598 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002599s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002602 int status;
2603
2604 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2605 I2C_SMBUS_READ, command,
2606 I2C_SMBUS_BYTE_DATA, &data);
2607 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
David Brownellc0564602007-05-01 23:26:31 +02002609EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
David Brownella1cdeda2008-07-14 22:38:24 +02002611/**
2612 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2613 * @client: Handle to slave device
2614 * @command: Byte interpreted by slave
2615 * @value: Byte being written
2616 *
2617 * This executes the SMBus "write byte" protocol, returning negative errno
2618 * else zero on success.
2619 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002620s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2621 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 union i2c_smbus_data data;
2624 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002625 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2626 I2C_SMBUS_WRITE, command,
2627 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
David Brownellc0564602007-05-01 23:26:31 +02002629EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
David Brownella1cdeda2008-07-14 22:38:24 +02002631/**
2632 * i2c_smbus_read_word_data - SMBus "read word" protocol
2633 * @client: Handle to slave device
2634 * @command: Byte interpreted by slave
2635 *
2636 * This executes the SMBus "read word" protocol, returning negative errno
2637 * else a 16-bit unsigned "word" received from the device.
2638 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002639s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002642 int status;
2643
2644 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2645 I2C_SMBUS_READ, command,
2646 I2C_SMBUS_WORD_DATA, &data);
2647 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
David Brownellc0564602007-05-01 23:26:31 +02002649EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
David Brownella1cdeda2008-07-14 22:38:24 +02002651/**
2652 * i2c_smbus_write_word_data - SMBus "write word" protocol
2653 * @client: Handle to slave device
2654 * @command: Byte interpreted by slave
2655 * @value: 16-bit "word" being written
2656 *
2657 * This executes the SMBus "write word" protocol, returning negative errno
2658 * else zero on success.
2659 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002660s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2661 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 union i2c_smbus_data data;
2664 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002665 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2666 I2C_SMBUS_WRITE, command,
2667 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
David Brownellc0564602007-05-01 23:26:31 +02002669EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
David Brownella64ec072007-10-13 23:56:31 +02002671/**
David Brownella1cdeda2008-07-14 22:38:24 +02002672 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002673 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002674 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002675 * @values: Byte array into which data will be read; big enough to hold
2676 * the data returned by the slave. SMBus allows at most 32 bytes.
2677 *
David Brownella1cdeda2008-07-14 22:38:24 +02002678 * This executes the SMBus "block read" protocol, returning negative errno
2679 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002680 *
2681 * Note that using this function requires that the client's adapter support
2682 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2683 * support this; its emulation through I2C messaging relies on a specific
2684 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2685 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002686s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002687 u8 *values)
2688{
2689 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002690 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002691
David Brownell24a5bb72008-07-14 22:38:23 +02002692 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2693 I2C_SMBUS_READ, command,
2694 I2C_SMBUS_BLOCK_DATA, &data);
2695 if (status)
2696 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002697
2698 memcpy(values, &data.block[1], data.block[0]);
2699 return data.block[0];
2700}
2701EXPORT_SYMBOL(i2c_smbus_read_block_data);
2702
David Brownella1cdeda2008-07-14 22:38:24 +02002703/**
2704 * i2c_smbus_write_block_data - SMBus "block write" protocol
2705 * @client: Handle to slave device
2706 * @command: Byte interpreted by slave
2707 * @length: Size of data block; SMBus allows at most 32 bytes
2708 * @values: Byte array which will be written.
2709 *
2710 * This executes the SMBus "block write" protocol, returning negative errno
2711 * else zero on success.
2712 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002713s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002714 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (length > I2C_SMBUS_BLOCK_MAX)
2719 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002721 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002722 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2723 I2C_SMBUS_WRITE, command,
2724 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
David Brownellc0564602007-05-01 23:26:31 +02002726EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002729s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002730 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002733 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002734
Jean Delvare4b2643d2007-07-12 14:12:29 +02002735 if (length > I2C_SMBUS_BLOCK_MAX)
2736 length = I2C_SMBUS_BLOCK_MAX;
2737 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002738 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2739 I2C_SMBUS_READ, command,
2740 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2741 if (status < 0)
2742 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002743
2744 memcpy(values, &data.block[1], data.block[0]);
2745 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
David Brownellc0564602007-05-01 23:26:31 +02002747EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Jean Delvare0cc43a12011-01-10 22:11:23 +01002749s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002750 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002751{
2752 union i2c_smbus_data data;
2753
2754 if (length > I2C_SMBUS_BLOCK_MAX)
2755 length = I2C_SMBUS_BLOCK_MAX;
2756 data.block[0] = length;
2757 memcpy(data.block + 1, values, length);
2758 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2759 I2C_SMBUS_WRITE, command,
2760 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2761}
David Brownellc0564602007-05-01 23:26:31 +02002762EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002763
David Brownell438d6c22006-12-10 21:21:31 +01002764/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002766static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2767 unsigned short flags,
2768 char read_write, u8 command, int size,
2769 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
2771 /* So we need to generate a series of msgs. In the case of writing, we
2772 need to use only one message; when reading, we need two. We initialize
2773 most things with sane defaults, to keep the code below somewhat
2774 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002775 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2776 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002777 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002779 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002780 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002781 struct i2c_msg msg[2] = {
2782 {
2783 .addr = addr,
2784 .flags = flags,
2785 .len = 1,
2786 .buf = msgbuf0,
2787 }, {
2788 .addr = addr,
2789 .flags = flags | I2C_M_RD,
2790 .len = 0,
2791 .buf = msgbuf1,
2792 },
2793 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002796 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 case I2C_SMBUS_QUICK:
2798 msg[0].len = 0;
2799 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002800 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2801 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 num = 1;
2803 break;
2804 case I2C_SMBUS_BYTE:
2805 if (read_write == I2C_SMBUS_READ) {
2806 /* Special case: only a read! */
2807 msg[0].flags = I2C_M_RD | flags;
2808 num = 1;
2809 }
2810 break;
2811 case I2C_SMBUS_BYTE_DATA:
2812 if (read_write == I2C_SMBUS_READ)
2813 msg[1].len = 1;
2814 else {
2815 msg[0].len = 2;
2816 msgbuf0[1] = data->byte;
2817 }
2818 break;
2819 case I2C_SMBUS_WORD_DATA:
2820 if (read_write == I2C_SMBUS_READ)
2821 msg[1].len = 2;
2822 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002823 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002825 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827 break;
2828 case I2C_SMBUS_PROC_CALL:
2829 num = 2; /* Special case */
2830 read_write = I2C_SMBUS_READ;
2831 msg[0].len = 3;
2832 msg[1].len = 2;
2833 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002834 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 break;
2836 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002838 msg[1].flags |= I2C_M_RECV_LEN;
2839 msg[1].len = 1; /* block length will be added by
2840 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 } else {
2842 msg[0].len = data->block[0] + 2;
2843 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002844 dev_err(&adapter->dev,
2845 "Invalid block write size %d\n",
2846 data->block[0]);
2847 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002849 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 msgbuf0[i] = data->block[i-1];
2851 }
2852 break;
2853 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002854 num = 2; /* Another special case */
2855 read_write = I2C_SMBUS_READ;
2856 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002857 dev_err(&adapter->dev,
2858 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002859 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002860 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002861 }
2862 msg[0].len = data->block[0] + 2;
2863 for (i = 1; i < msg[0].len; i++)
2864 msgbuf0[i] = data->block[i-1];
2865 msg[1].flags |= I2C_M_RECV_LEN;
2866 msg[1].len = 1; /* block length will be added by
2867 the underlying bus driver */
2868 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 case I2C_SMBUS_I2C_BLOCK_DATA:
2870 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002871 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 } else {
2873 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002874 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002875 dev_err(&adapter->dev,
2876 "Invalid block write size %d\n",
2877 data->block[0]);
2878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880 for (i = 1; i <= data->block[0]; i++)
2881 msgbuf0[i] = data->block[i];
2882 }
2883 break;
2884 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002885 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2886 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
Jean Delvare421ef472005-10-26 21:28:55 +02002889 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2890 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2891 if (i) {
2892 /* Compute PEC if first message is a write */
2893 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002894 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002895 i2c_smbus_add_pec(&msg[0]);
2896 else /* Write followed by read */
2897 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2898 }
2899 /* Ask for PEC if last message is a read */
2900 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002901 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002902 }
2903
David Brownell24a5bb72008-07-14 22:38:23 +02002904 status = i2c_transfer(adapter, msg, num);
2905 if (status < 0)
2906 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Jean Delvare421ef472005-10-26 21:28:55 +02002908 /* Check PEC if last message is a read */
2909 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002910 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2911 if (status < 0)
2912 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002913 }
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002916 switch (size) {
2917 case I2C_SMBUS_BYTE:
2918 data->byte = msgbuf0[0];
2919 break;
2920 case I2C_SMBUS_BYTE_DATA:
2921 data->byte = msgbuf1[0];
2922 break;
2923 case I2C_SMBUS_WORD_DATA:
2924 case I2C_SMBUS_PROC_CALL:
2925 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2926 break;
2927 case I2C_SMBUS_I2C_BLOCK_DATA:
2928 for (i = 0; i < data->block[0]; i++)
2929 data->block[i+1] = msgbuf1[i];
2930 break;
2931 case I2C_SMBUS_BLOCK_DATA:
2932 case I2C_SMBUS_BLOCK_PROC_CALL:
2933 for (i = 0; i < msgbuf1[0] + 1; i++)
2934 data->block[i] = msgbuf1[i];
2935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937 return 0;
2938}
2939
David Brownella1cdeda2008-07-14 22:38:24 +02002940/**
2941 * i2c_smbus_xfer - execute SMBus protocol operations
2942 * @adapter: Handle to I2C bus
2943 * @addr: Address of SMBus slave on that bus
2944 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2945 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2946 * @command: Byte interpreted by slave, for protocols which use such bytes
2947 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2948 * @data: Data to be read or written
2949 *
2950 * This executes an SMBus protocol operation, and returns a negative
2951 * errno code else zero on success.
2952 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002953s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002954 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002955 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002957 unsigned long orig_jiffies;
2958 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
David Howells8a325992014-03-06 13:36:06 +00002961 /* If enabled, the following two tracepoints are conditional on
2962 * read_write and protocol.
2963 */
2964 trace_smbus_write(adapter, addr, flags, read_write,
2965 command, protocol, data);
2966 trace_smbus_read(adapter, addr, flags, read_write,
2967 command, protocol);
2968
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002969 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002972 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002973
2974 /* Retry automatically on arbitration loss */
2975 orig_jiffies = jiffies;
2976 for (res = 0, try = 0; try <= adapter->retries; try++) {
2977 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2978 read_write, command,
2979 protocol, data);
2980 if (res != -EAGAIN)
2981 break;
2982 if (time_after(jiffies,
2983 orig_jiffies + adapter->timeout))
2984 break;
2985 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002986 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002988 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002989 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002990 /*
2991 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2992 * implement native support for the SMBus operation.
2993 */
2994 }
2995
David Howells8a325992014-03-06 13:36:06 +00002996 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2997 command, protocol, data);
2998
2999trace:
3000 /* If enabled, the reply tracepoint is conditional on read_write. */
3001 trace_smbus_reply(adapter, addr, flags, read_write,
3002 command, protocol, data);
3003 trace_smbus_result(adapter, addr, flags, read_write,
3004 command, protocol, res);
3005
3006 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Irina Tirdea01eef962015-08-12 17:31:33 +03003010/**
3011 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3012 * @client: Handle to slave device
3013 * @command: Byte interpreted by slave
3014 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3015 * @values: Byte array into which data will be read; big enough to hold
3016 * the data returned by the slave. SMBus allows at most
3017 * I2C_SMBUS_BLOCK_MAX bytes.
3018 *
3019 * This executes the SMBus "block read" protocol if supported by the adapter.
3020 * If block read is not supported, it emulates it using either word or byte
3021 * read protocols depending on availability.
3022 *
3023 * The addresses of the I2C slave device that are accessed with this function
3024 * must be mapped to a linear region, so that a block read will have the same
3025 * effect as a byte read. Before using this function you must double-check
3026 * if the I2C slave does support exchanging a block transfer with a byte
3027 * transfer.
3028 */
3029s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3030 u8 command, u8 length, u8 *values)
3031{
3032 u8 i = 0;
3033 int status;
3034
3035 if (length > I2C_SMBUS_BLOCK_MAX)
3036 length = I2C_SMBUS_BLOCK_MAX;
3037
3038 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3039 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3040
3041 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3042 return -EOPNOTSUPP;
3043
3044 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3045 while ((i + 2) <= length) {
3046 status = i2c_smbus_read_word_data(client, command + i);
3047 if (status < 0)
3048 return status;
3049 values[i] = status & 0xff;
3050 values[i + 1] = status >> 8;
3051 i += 2;
3052 }
3053 }
3054
3055 while (i < length) {
3056 status = i2c_smbus_read_byte_data(client, command + i);
3057 if (status < 0)
3058 return status;
3059 values[i] = status;
3060 i++;
3061 }
3062
3063 return i;
3064}
3065EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3066
Jean Delvared5fd1202015-01-26 20:59:31 +01003067#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003068int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3069{
3070 int ret;
3071
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003072 if (!client || !slave_cb) {
3073 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003074 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003075 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003076
Wolfram Sangc6909d62015-08-05 21:12:54 +02003077 if (!(client->flags & I2C_CLIENT_SLAVE))
3078 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3079 __func__);
3080
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003081 if (!(client->flags & I2C_CLIENT_TEN)) {
3082 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003083 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003084 if (ret) {
3085 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003086 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003087 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003088 }
3089
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003090 if (!client->adapter->algo->reg_slave) {
3091 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003092 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003093 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003094
3095 client->slave_cb = slave_cb;
3096
3097 i2c_lock_adapter(client->adapter);
3098 ret = client->adapter->algo->reg_slave(client);
3099 i2c_unlock_adapter(client->adapter);
3100
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003101 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003102 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003103 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3104 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003105
3106 return ret;
3107}
3108EXPORT_SYMBOL_GPL(i2c_slave_register);
3109
3110int i2c_slave_unregister(struct i2c_client *client)
3111{
3112 int ret;
3113
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003114 if (!client->adapter->algo->unreg_slave) {
3115 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003116 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003117 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003118
3119 i2c_lock_adapter(client->adapter);
3120 ret = client->adapter->algo->unreg_slave(client);
3121 i2c_unlock_adapter(client->adapter);
3122
3123 if (ret == 0)
3124 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003125 else
3126 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003127
3128 return ret;
3129}
3130EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003131#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3134MODULE_DESCRIPTION("I2C-Bus main module");
3135MODULE_LICENSE("GPL");