blob: 7b18f31bf6c6394062a810e8c3c3e916a16a66a6 [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
842 if (client && client->addr == addr)
843 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 */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200996 status = i2c_check_addr_busy(adap, client->addr);
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
Jean Delvare99cd8e22009-06-19 16:58:20 +02001161 client = i2c_new_device(adap, &info);
1162 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001163 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001164
1165 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001166 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001167 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001168 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001169 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1170 info.type, info.addr);
1171
1172 return count;
1173}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001174static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001175
1176/*
1177 * And of course let the users delete the devices they instantiated, if
1178 * they got it wrong. This interface can only be used to delete devices
1179 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1180 * don't delete devices to which some kernel code still has references.
1181 *
1182 * Parameter checking may look overzealous, but we really don't want
1183 * the user to delete the wrong device.
1184 */
1185static ssize_t
1186i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1187 const char *buf, size_t count)
1188{
1189 struct i2c_adapter *adap = to_i2c_adapter(dev);
1190 struct i2c_client *client, *next;
1191 unsigned short addr;
1192 char end;
1193 int res;
1194
1195 /* Parse parameters, reject extra parameters */
1196 res = sscanf(buf, "%hi%c", &addr, &end);
1197 if (res < 1) {
1198 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1199 return -EINVAL;
1200 }
1201 if (res > 1 && end != '\n') {
1202 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1203 return -EINVAL;
1204 }
1205
1206 /* Make sure the device was added through sysfs */
1207 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001208 mutex_lock_nested(&adap->userspace_clients_lock,
1209 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001210 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1211 detected) {
1212 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001213 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1214 "delete_device", client->name, client->addr);
1215
1216 list_del(&client->detected);
1217 i2c_unregister_device(client);
1218 res = count;
1219 break;
1220 }
1221 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001222 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001223
1224 if (res < 0)
1225 dev_err(dev, "%s: Can't find device in list\n",
1226 "delete_device");
1227 return res;
1228}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001229static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1230 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001231
1232static struct attribute *i2c_adapter_attrs[] = {
1233 &dev_attr_name.attr,
1234 &dev_attr_new_device.attr,
1235 &dev_attr_delete_device.attr,
1236 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001237};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001238ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001239
Michael Lawnick08263742010-08-11 18:21:02 +02001240struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001241 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001242 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001243};
Michael Lawnick08263742010-08-11 18:21:02 +02001244EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Stephen Warren643dd092012-04-17 12:43:33 -06001246/**
1247 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1248 * @dev: device, probably from some driver model iterator
1249 *
1250 * When traversing the driver model tree, perhaps using driver model
1251 * iterators like @device_for_each_child(), you can't assume very much
1252 * about the nodes you find. Use this function to avoid oopses caused
1253 * by wrongly treating some non-I2C device as an i2c_adapter.
1254 */
1255struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1256{
1257 return (dev->type == &i2c_adapter_type)
1258 ? to_i2c_adapter(dev)
1259 : NULL;
1260}
1261EXPORT_SYMBOL(i2c_verify_adapter);
1262
Jean Delvare2bb50952009-09-18 22:45:46 +02001263#ifdef CONFIG_I2C_COMPAT
1264static struct class_compat *i2c_adapter_compat_class;
1265#endif
1266
David Brownell9c1600e2007-05-01 23:26:31 +02001267static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1268{
1269 struct i2c_devinfo *devinfo;
1270
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001271 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001272 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1273 if (devinfo->busnum == adapter->nr
1274 && !i2c_new_device(adapter,
1275 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001276 dev_err(&adapter->dev,
1277 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001278 devinfo->board_info.addr);
1279 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001280 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001281}
1282
Wolfram Sang687b81d2013-07-11 12:56:15 +01001283/* OF support code */
1284
1285#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001286static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1287 struct device_node *node)
1288{
1289 struct i2c_client *result;
1290 struct i2c_board_info info = {};
1291 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001292 const __be32 *addr_be;
1293 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001294 int len;
1295
1296 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1297
1298 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1299 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1300 node->full_name);
1301 return ERR_PTR(-EINVAL);
1302 }
1303
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001304 addr_be = of_get_property(node, "reg", &len);
1305 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001306 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1307 node->full_name);
1308 return ERR_PTR(-EINVAL);
1309 }
1310
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001311 addr = be32_to_cpup(addr_be);
1312 if (addr & I2C_TEN_BIT_ADDRESS) {
1313 addr &= ~I2C_TEN_BIT_ADDRESS;
1314 info.flags |= I2C_CLIENT_TEN;
1315 }
1316
1317 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1318 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1319 info.flags |= I2C_CLIENT_SLAVE;
1320 }
1321
1322 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001323 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1324 info.addr, node->full_name);
1325 return ERR_PTR(-EINVAL);
1326 }
1327
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001328 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001329 info.of_node = of_node_get(node);
1330 info.archdata = &dev_ad;
1331
1332 if (of_get_property(node, "wakeup-source", NULL))
1333 info.flags |= I2C_CLIENT_WAKE;
1334
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001335 result = i2c_new_device(adap, &info);
1336 if (result == NULL) {
1337 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1338 node->full_name);
1339 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001340 return ERR_PTR(-EINVAL);
1341 }
1342 return result;
1343}
1344
Wolfram Sang687b81d2013-07-11 12:56:15 +01001345static void of_i2c_register_devices(struct i2c_adapter *adap)
1346{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001347 struct device_node *node;
1348
1349 /* Only register child devices if the adapter has a node pointer set */
1350 if (!adap->dev.of_node)
1351 return;
1352
1353 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1354
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001355 for_each_available_child_of_node(adap->dev.of_node, node) {
1356 if (of_node_test_and_set_flag(node, OF_POPULATED))
1357 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001358 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001359 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001360}
1361
1362static int of_dev_node_match(struct device *dev, void *data)
1363{
1364 return dev->of_node == data;
1365}
1366
1367/* must call put_device() when done with returned i2c_client device */
1368struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1369{
1370 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001371 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001372
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001373 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001374 if (!dev)
1375 return NULL;
1376
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001377 client = i2c_verify_client(dev);
1378 if (!client)
1379 put_device(dev);
1380
1381 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001382}
1383EXPORT_SYMBOL(of_find_i2c_device_by_node);
1384
1385/* must call put_device() when done with returned i2c_adapter device */
1386struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1387{
1388 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001389 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001390
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001391 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001392 if (!dev)
1393 return NULL;
1394
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001395 adapter = i2c_verify_adapter(dev);
1396 if (!adapter)
1397 put_device(dev);
1398
1399 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001400}
1401EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001402
1403/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1404struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1405{
1406 struct i2c_adapter *adapter;
1407
1408 adapter = of_find_i2c_adapter_by_node(node);
1409 if (!adapter)
1410 return NULL;
1411
1412 if (!try_module_get(adapter->owner)) {
1413 put_device(&adapter->dev);
1414 adapter = NULL;
1415 }
1416
1417 return adapter;
1418}
1419EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001420#else
1421static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1422#endif /* CONFIG_OF */
1423
Jean Delvare69b00892009-12-06 17:06:27 +01001424static int i2c_do_add_adapter(struct i2c_driver *driver,
1425 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001426{
Jean Delvare4735c982008-07-14 22:38:36 +02001427 /* Detect supported devices on that bus, and instantiate them */
1428 i2c_detect(adap, driver);
1429
1430 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001431 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001432 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1433 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001434 dev_warn(&adap->dev, "Please use another way to instantiate "
1435 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001436 /* We ignore the return code; if it fails, too bad */
1437 driver->attach_adapter(adap);
1438 }
1439 return 0;
1440}
1441
Jean Delvare69b00892009-12-06 17:06:27 +01001442static int __process_new_adapter(struct device_driver *d, void *data)
1443{
1444 return i2c_do_add_adapter(to_i2c_driver(d), data);
1445}
1446
David Brownell6e13e642007-05-01 23:26:31 +02001447static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Jean Delvared6703282010-08-11 18:20:59 +02001449 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
David Brownell1d0b19c2008-10-14 17:30:05 +02001451 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001452 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1453 res = -EAGAIN;
1454 goto out_list;
1455 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001456
Jean Delvare2236baa2010-11-15 22:40:38 +01001457 /* Sanity checks */
1458 if (unlikely(adap->name[0] == '\0')) {
1459 pr_err("i2c-core: Attempt to register an adapter with "
1460 "no name!\n");
1461 return -EINVAL;
1462 }
1463 if (unlikely(!adap->algo)) {
1464 pr_err("i2c-core: Attempt to register adapter '%s' with "
1465 "no algo!\n", adap->name);
1466 return -EINVAL;
1467 }
1468
Mika Kuoppala194684e2009-12-06 17:06:22 +01001469 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001470 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001471 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Jean Delvare8fcfef62009-03-28 21:34:43 +01001473 /* Set default timeout to 1 second if not already set */
1474 if (adap->timeout == 0)
1475 adap->timeout = HZ;
1476
Kay Sievers27d9c182009-01-07 14:29:16 +01001477 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001478 adap->dev.bus = &i2c_bus_type;
1479 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001480 res = device_register(&adap->dev);
1481 if (res)
1482 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001484 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1485
Charles Keepax6ada5c12015-04-16 13:05:19 +01001486 pm_runtime_no_callbacks(&adap->dev);
1487
Jean Delvare2bb50952009-09-18 22:45:46 +02001488#ifdef CONFIG_I2C_COMPAT
1489 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1490 adap->dev.parent);
1491 if (res)
1492 dev_warn(&adap->dev,
1493 "Failed to create compatibility class link\n");
1494#endif
1495
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301496 /* bus recovery specific initialization */
1497 if (adap->bus_recovery_info) {
1498 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1499
1500 if (!bri->recover_bus) {
1501 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1502 adap->bus_recovery_info = NULL;
1503 goto exit_recovery;
1504 }
1505
1506 /* Generic GPIO recovery */
1507 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1508 if (!gpio_is_valid(bri->scl_gpio)) {
1509 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1510 adap->bus_recovery_info = NULL;
1511 goto exit_recovery;
1512 }
1513
1514 if (gpio_is_valid(bri->sda_gpio))
1515 bri->get_sda = get_sda_gpio_value;
1516 else
1517 bri->get_sda = NULL;
1518
1519 bri->get_scl = get_scl_gpio_value;
1520 bri->set_scl = set_scl_gpio_value;
1521 } else if (!bri->set_scl || !bri->get_scl) {
1522 /* Generic SCL recovery */
1523 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1524 adap->bus_recovery_info = NULL;
1525 }
1526 }
1527
1528exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001529 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001530 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001531 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001532 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001533
David Brownell6e13e642007-05-01 23:26:31 +02001534 if (adap->nr < __i2c_first_dynamic_bus_num)
1535 i2c_scan_static_board_info(adap);
1536
Jean Delvare4735c982008-07-14 22:38:36 +02001537 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001538 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001539 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001540 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001541
1542 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001543
Jean Delvareb119c6c2006-08-15 18:26:30 +02001544out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001545 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001546 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001547 mutex_unlock(&core_lock);
1548 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
David Brownell6e13e642007-05-01 23:26:31 +02001551/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001552 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1553 * @adap: the adapter to register (with adap->nr initialized)
1554 * Context: can sleep
1555 *
1556 * See i2c_add_numbered_adapter() for details.
1557 */
1558static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1559{
1560 int id;
1561
1562 mutex_lock(&core_lock);
1563 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1564 GFP_KERNEL);
1565 mutex_unlock(&core_lock);
1566 if (id < 0)
1567 return id == -ENOSPC ? -EBUSY : id;
1568
1569 return i2c_register_adapter(adap);
1570}
1571
1572/**
David Brownell6e13e642007-05-01 23:26:31 +02001573 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1574 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001575 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001576 *
1577 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001578 * doesn't matter or when its bus number is specified by an dt alias.
1579 * Examples of bases when the bus number doesn't matter: I2C adapters
1580 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001581 *
1582 * When this returns zero, a new bus number was allocated and stored
1583 * in adap->nr, and the specified adapter became available for clients.
1584 * Otherwise, a negative errno value is returned.
1585 */
1586int i2c_add_adapter(struct i2c_adapter *adapter)
1587{
Doug Andersonee5c2742013-03-01 08:57:31 -08001588 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001589 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001590
Doug Andersonee5c2742013-03-01 08:57:31 -08001591 if (dev->of_node) {
1592 id = of_alias_get_id(dev->of_node, "i2c");
1593 if (id >= 0) {
1594 adapter->nr = id;
1595 return __i2c_add_numbered_adapter(adapter);
1596 }
1597 }
1598
Jean Delvarecaada322008-01-27 18:14:49 +01001599 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001600 id = idr_alloc(&i2c_adapter_idr, adapter,
1601 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001602 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001603 if (id < 0)
1604 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001605
1606 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001607
David Brownell6e13e642007-05-01 23:26:31 +02001608 return i2c_register_adapter(adapter);
1609}
1610EXPORT_SYMBOL(i2c_add_adapter);
1611
1612/**
1613 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1614 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001615 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001616 *
1617 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001618 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1619 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001620 * is used to properly configure I2C devices.
1621 *
Grant Likely488bf312011-07-25 17:49:43 +02001622 * If the requested bus number is set to -1, then this function will behave
1623 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1624 *
David Brownell6e13e642007-05-01 23:26:31 +02001625 * If no devices have pre-been declared for this bus, then be sure to
1626 * register the adapter before any dynamically allocated ones. Otherwise
1627 * the required bus ID may not be available.
1628 *
1629 * When this returns zero, the specified adapter became available for
1630 * clients using the bus number provided in adap->nr. Also, the table
1631 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1632 * and the appropriate driver model device nodes are created. Otherwise, a
1633 * negative errno value is returned.
1634 */
1635int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1636{
Grant Likely488bf312011-07-25 17:49:43 +02001637 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1638 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001639
Doug Andersonee5c2742013-03-01 08:57:31 -08001640 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001641}
1642EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1643
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001644static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001645 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001646{
Jean Delvare4735c982008-07-14 22:38:36 +02001647 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001648
Jean Delvareacec2112009-03-28 21:34:40 +01001649 /* Remove the devices we created ourselves as the result of hardware
1650 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001651 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1652 if (client->adapter == adapter) {
1653 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1654 client->name, client->addr);
1655 list_del(&client->detected);
1656 i2c_unregister_device(client);
1657 }
1658 }
Jean Delvare026526f2008-01-27 18:14:49 +01001659}
1660
Jean Delvaree549c2b2009-06-19 16:58:19 +02001661static int __unregister_client(struct device *dev, void *dummy)
1662{
1663 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001664 if (client && strcmp(client->name, "dummy"))
1665 i2c_unregister_device(client);
1666 return 0;
1667}
1668
1669static int __unregister_dummy(struct device *dev, void *dummy)
1670{
1671 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001672 if (client)
1673 i2c_unregister_device(client);
1674 return 0;
1675}
1676
Jean Delvare69b00892009-12-06 17:06:27 +01001677static int __process_removed_adapter(struct device_driver *d, void *data)
1678{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001679 i2c_do_del_adapter(to_i2c_driver(d), data);
1680 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001681}
1682
David Brownelld64f73b2007-07-12 14:12:28 +02001683/**
1684 * i2c_del_adapter - unregister I2C adapter
1685 * @adap: the adapter being unregistered
1686 * Context: can sleep
1687 *
1688 * This unregisters an I2C adapter which was previously registered
1689 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1690 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001691void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001693 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001694 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001697 mutex_lock(&core_lock);
1698 found = idr_find(&i2c_adapter_idr, adap->nr);
1699 mutex_unlock(&core_lock);
1700 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001701 pr_debug("i2c-core: attempting to delete unregistered "
1702 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
Lan Tianyu5d98e612014-05-20 20:59:23 +08001706 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001707 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001708 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001709 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001710 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001711 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001713 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001714 mutex_lock_nested(&adap->userspace_clients_lock,
1715 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001716 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1717 detected) {
1718 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1719 client->addr);
1720 list_del(&client->detected);
1721 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001722 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001723 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001724
Jean Delvaree549c2b2009-06-19 16:58:19 +02001725 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001726 * check the returned value. This is a two-pass process, because
1727 * we can't remove the dummy devices during the first pass: they
1728 * could have been instantiated by real devices wishing to clean
1729 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001730 device_for_each_child(&adap->dev, NULL, __unregister_client);
1731 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Jean Delvare2bb50952009-09-18 22:45:46 +02001733#ifdef CONFIG_I2C_COMPAT
1734 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1735 adap->dev.parent);
1736#endif
1737
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001738 /* device name is gone after device_unregister */
1739 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1740
Wolfram Sang26680ee2015-01-29 22:45:09 +01001741 /* wait until all references to the device are gone
1742 *
1743 * FIXME: This is old code and should ideally be replaced by an
1744 * alternative which results in decoupling the lifetime of the struct
1745 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301746 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
David Brownell6e13e642007-05-01 23:26:31 +02001752 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001753 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001755 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001757 /* Clear the device structure in case this adapter is ever going to be
1758 added again */
1759 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
David Brownellc0564602007-05-01 23:26:31 +02001761EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
David Brownell7b4fbc52007-05-01 23:26:30 +02001763/* ------------------------------------------------------------------------- */
1764
Jean Delvare7ae31482011-03-20 14:50:52 +01001765int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1766{
1767 int res;
1768
1769 mutex_lock(&core_lock);
1770 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1771 mutex_unlock(&core_lock);
1772
1773 return res;
1774}
1775EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1776
Jean Delvare69b00892009-12-06 17:06:27 +01001777static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001778{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001779 if (dev->type != &i2c_adapter_type)
1780 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001781 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001782}
1783
David Brownell7b4fbc52007-05-01 23:26:30 +02001784/*
1785 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001786 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
1788
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001789int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001791 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
David Brownell1d0b19c2008-10-14 17:30:05 +02001793 /* Can't register until after driver model init */
1794 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1795 return -EAGAIN;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001798 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jean Delvare729d6dd2009-06-19 16:58:18 +02001801 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001802 * will have called probe() for all matching-but-unbound devices.
1803 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 res = driver_register(&driver->driver);
1805 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001806 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001807
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001808 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Jean Delvare4735c982008-07-14 22:38:36 +02001810 INIT_LIST_HEAD(&driver->clients);
1811 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001812 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001813
Jean Delvare7eebcb72006-02-05 23:28:21 +01001814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001816EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jean Delvare69b00892009-12-06 17:06:27 +01001818static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001819{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001820 if (dev->type == &i2c_adapter_type)
1821 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1822 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001823}
1824
David Brownella1d9e6e2007-05-01 23:26:30 +02001825/**
1826 * i2c_del_driver - unregister I2C driver
1827 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001828 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001829 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001830void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Jean Delvare7ae31482011-03-20 14:50:52 +01001832 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001835 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
David Brownellc0564602007-05-01 23:26:31 +02001837EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David Brownell7b4fbc52007-05-01 23:26:30 +02001839/* ------------------------------------------------------------------------- */
1840
Jean Delvaree48d3312008-01-27 18:14:48 +01001841/**
1842 * i2c_use_client - increments the reference count of the i2c client structure
1843 * @client: the client being referenced
1844 *
1845 * Each live reference to a client should be refcounted. The driver model does
1846 * that automatically as part of driver binding, so that most drivers don't
1847 * need to do this explicitly: they hold a reference until they're unbound
1848 * from the device.
1849 *
1850 * A pointer to the client with the incremented reference counter is returned.
1851 */
1852struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
David Brownell6ea438e2008-07-14 22:38:24 +02001854 if (client && get_device(&client->dev))
1855 return client;
1856 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
David Brownellc0564602007-05-01 23:26:31 +02001858EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Jean Delvaree48d3312008-01-27 18:14:48 +01001860/**
1861 * i2c_release_client - release a use of the i2c client structure
1862 * @client: the client being no longer referenced
1863 *
1864 * Must be called when a user of a client is finished with it.
1865 */
1866void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
David Brownell6ea438e2008-07-14 22:38:24 +02001868 if (client)
1869 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
David Brownellc0564602007-05-01 23:26:31 +02001871EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
David Brownell9b766b82008-01-27 18:14:51 +01001873struct i2c_cmd_arg {
1874 unsigned cmd;
1875 void *arg;
1876};
1877
1878static int i2c_cmd(struct device *dev, void *_arg)
1879{
1880 struct i2c_client *client = i2c_verify_client(dev);
1881 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001882 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001883
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001884 if (!client || !client->dev.driver)
1885 return 0;
1886
1887 driver = to_i2c_driver(client->dev.driver);
1888 if (driver->command)
1889 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001890 return 0;
1891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1894{
David Brownell9b766b82008-01-27 18:14:51 +01001895 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
David Brownell9b766b82008-01-27 18:14:51 +01001897 cmd_arg.cmd = cmd;
1898 cmd_arg.arg = arg;
1899 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
David Brownellc0564602007-05-01 23:26:31 +02001901EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001903#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1904static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1905 void *arg)
1906{
1907 struct of_reconfig_data *rd = arg;
1908 struct i2c_adapter *adap;
1909 struct i2c_client *client;
1910
1911 switch (of_reconfig_get_state_change(action, rd)) {
1912 case OF_RECONFIG_CHANGE_ADD:
1913 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1914 if (adap == NULL)
1915 return NOTIFY_OK; /* not for us */
1916
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001917 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1918 put_device(&adap->dev);
1919 return NOTIFY_OK;
1920 }
1921
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001922 client = of_i2c_register_device(adap, rd->dn);
1923 put_device(&adap->dev);
1924
1925 if (IS_ERR(client)) {
1926 pr_err("%s: failed to create for '%s'\n",
1927 __func__, rd->dn->full_name);
1928 return notifier_from_errno(PTR_ERR(client));
1929 }
1930 break;
1931 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001932 /* already depopulated? */
1933 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1934 return NOTIFY_OK;
1935
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001936 /* find our device by node */
1937 client = of_find_i2c_device_by_node(rd->dn);
1938 if (client == NULL)
1939 return NOTIFY_OK; /* no? not meant for us */
1940
1941 /* unregister takes one ref away */
1942 i2c_unregister_device(client);
1943
1944 /* and put the reference of the find */
1945 put_device(&client->dev);
1946 break;
1947 }
1948
1949 return NOTIFY_OK;
1950}
1951static struct notifier_block i2c_of_notifier = {
1952 .notifier_call = of_i2c_notify,
1953};
1954#else
1955extern struct notifier_block i2c_of_notifier;
1956#endif /* CONFIG_OF_DYNAMIC */
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958static int __init i2c_init(void)
1959{
1960 int retval;
1961
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001962 retval = of_alias_get_highest_id("i2c");
1963
1964 down_write(&__i2c_board_lock);
1965 if (retval >= __i2c_first_dynamic_bus_num)
1966 __i2c_first_dynamic_bus_num = retval + 1;
1967 up_write(&__i2c_board_lock);
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 retval = bus_register(&i2c_bus_type);
1970 if (retval)
1971 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001972#ifdef CONFIG_I2C_COMPAT
1973 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1974 if (!i2c_adapter_compat_class) {
1975 retval = -ENOMEM;
1976 goto bus_err;
1977 }
1978#endif
David Brownelle9f13732008-01-27 18:14:52 +01001979 retval = i2c_add_driver(&dummy_driver);
1980 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001981 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001982
1983 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1984 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1985
David Brownelle9f13732008-01-27 18:14:52 +01001986 return 0;
1987
Jean Delvare2bb50952009-09-18 22:45:46 +02001988class_err:
1989#ifdef CONFIG_I2C_COMPAT
1990 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001991bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001992#endif
David Brownelle9f13732008-01-27 18:14:52 +01001993 bus_unregister(&i2c_bus_type);
1994 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997static void __exit i2c_exit(void)
1998{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001999 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2000 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002001 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002002#ifdef CONFIG_I2C_COMPAT
2003 class_compat_unregister(i2c_adapter_compat_class);
2004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002006 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
David Brownella10f9e72008-10-14 17:30:06 +02002009/* We must initialize early, because some subsystems register i2c drivers
2010 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2011 */
2012postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013module_exit(i2c_exit);
2014
2015/* ----------------------------------------------------
2016 * the functional interface to the i2c busses.
2017 * ----------------------------------------------------
2018 */
2019
Wolfram Sangb7f62582015-01-05 23:45:59 +01002020/* Check if val is exceeding the quirk IFF quirk is non 0 */
2021#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2022
2023static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2024{
2025 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2026 err_msg, msg->addr, msg->len,
2027 msg->flags & I2C_M_RD ? "read" : "write");
2028 return -EOPNOTSUPP;
2029}
2030
2031static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2032{
2033 const struct i2c_adapter_quirks *q = adap->quirks;
2034 int max_num = q->max_num_msgs, i;
2035 bool do_len_check = true;
2036
2037 if (q->flags & I2C_AQ_COMB) {
2038 max_num = 2;
2039
2040 /* special checks for combined messages */
2041 if (num == 2) {
2042 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2043 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2044
2045 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2046 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2047
2048 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2049 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2050
2051 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2052 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2053
2054 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2055 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2056
2057 do_len_check = false;
2058 }
2059 }
2060
2061 if (i2c_quirk_exceeded(num, max_num))
2062 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2063
2064 for (i = 0; i < num; i++) {
2065 u16 len = msgs[i].len;
2066
2067 if (msgs[i].flags & I2C_M_RD) {
2068 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2069 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2070 } else {
2071 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2072 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2073 }
2074 }
2075
2076 return 0;
2077}
2078
David Brownella1cdeda2008-07-14 22:38:24 +02002079/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002080 * __i2c_transfer - unlocked flavor of i2c_transfer
2081 * @adap: Handle to I2C bus
2082 * @msgs: One or more messages to execute before STOP is issued to
2083 * terminate the operation; each message begins with a START.
2084 * @num: Number of messages to be executed.
2085 *
2086 * Returns negative errno, else the number of messages executed.
2087 *
2088 * Adapter lock must be held when calling this function. No debug logging
2089 * takes place. adap->algo->master_xfer existence isn't checked.
2090 */
2091int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2092{
2093 unsigned long orig_jiffies;
2094 int ret, try;
2095
Wolfram Sangb7f62582015-01-05 23:45:59 +01002096 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2097 return -EOPNOTSUPP;
2098
David Howellsd9a83d62014-03-06 13:35:59 +00002099 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2100 * enabled. This is an efficient way of keeping the for-loop from
2101 * being executed when not needed.
2102 */
2103 if (static_key_false(&i2c_trace_msg)) {
2104 int i;
2105 for (i = 0; i < num; i++)
2106 if (msgs[i].flags & I2C_M_RD)
2107 trace_i2c_read(adap, &msgs[i], i);
2108 else
2109 trace_i2c_write(adap, &msgs[i], i);
2110 }
2111
Jean Delvareb37d2a32012-06-29 07:47:19 -03002112 /* Retry automatically on arbitration loss */
2113 orig_jiffies = jiffies;
2114 for (ret = 0, try = 0; try <= adap->retries; try++) {
2115 ret = adap->algo->master_xfer(adap, msgs, num);
2116 if (ret != -EAGAIN)
2117 break;
2118 if (time_after(jiffies, orig_jiffies + adap->timeout))
2119 break;
2120 }
2121
David Howellsd9a83d62014-03-06 13:35:59 +00002122 if (static_key_false(&i2c_trace_msg)) {
2123 int i;
2124 for (i = 0; i < ret; i++)
2125 if (msgs[i].flags & I2C_M_RD)
2126 trace_i2c_reply(adap, &msgs[i], i);
2127 trace_i2c_result(adap, i, ret);
2128 }
2129
Jean Delvareb37d2a32012-06-29 07:47:19 -03002130 return ret;
2131}
2132EXPORT_SYMBOL(__i2c_transfer);
2133
2134/**
David Brownella1cdeda2008-07-14 22:38:24 +02002135 * i2c_transfer - execute a single or combined I2C message
2136 * @adap: Handle to I2C bus
2137 * @msgs: One or more messages to execute before STOP is issued to
2138 * terminate the operation; each message begins with a START.
2139 * @num: Number of messages to be executed.
2140 *
2141 * Returns negative errno, else the number of messages executed.
2142 *
2143 * Note that there is no requirement that each message be sent to
2144 * the same slave address, although that is the most common model.
2145 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002146int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002148 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Brownella1cdeda2008-07-14 22:38:24 +02002150 /* REVISIT the fault reporting model here is weak:
2151 *
2152 * - When we get an error after receiving N bytes from a slave,
2153 * there is no way to report "N".
2154 *
2155 * - When we get a NAK after transmitting N bytes to a slave,
2156 * there is no way to report "N" ... or to let the master
2157 * continue executing the rest of this combined message, if
2158 * that's the appropriate response.
2159 *
2160 * - When for example "num" is two and we successfully complete
2161 * the first message but get an error part way through the
2162 * second, it's unclear whether that should be reported as
2163 * one (discarding status on the second message) or errno
2164 * (discarding status on the first one).
2165 */
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (adap->algo->master_xfer) {
2168#ifdef DEBUG
2169 for (ret = 0; ret < num; ret++) {
2170 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002171 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2172 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2173 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175#endif
2176
Mike Rapoportcea443a2008-01-27 18:14:50 +01002177 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002178 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002179 if (!ret)
2180 /* I2C activity is ongoing. */
2181 return -EAGAIN;
2182 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002183 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002184 }
2185
Jean Delvareb37d2a32012-06-29 07:47:19 -03002186 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002187 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 return ret;
2190 } else {
2191 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002192 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194}
David Brownellc0564602007-05-01 23:26:31 +02002195EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
David Brownella1cdeda2008-07-14 22:38:24 +02002197/**
2198 * i2c_master_send - issue a single I2C message in master transmit mode
2199 * @client: Handle to slave device
2200 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002201 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002202 *
2203 * Returns negative errno, or else the number of bytes written.
2204 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002205int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002208 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 struct i2c_msg msg;
2210
Jean Delvare815f55f2005-05-07 22:58:46 +02002211 msg.addr = client->addr;
2212 msg.flags = client->flags & I2C_M_TEN;
2213 msg.len = count;
2214 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002215
Jean Delvare815f55f2005-05-07 22:58:46 +02002216 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002218 /*
2219 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2220 * transmitted, else error code.
2221 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002222 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
David Brownellc0564602007-05-01 23:26:31 +02002224EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
David Brownella1cdeda2008-07-14 22:38:24 +02002226/**
2227 * i2c_master_recv - issue a single I2C message in master receive mode
2228 * @client: Handle to slave device
2229 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002230 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002231 *
2232 * Returns negative errno, or else the number of bytes read.
2233 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002234int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Farid Hammane7225acf2010-05-21 18:40:58 +02002236 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct i2c_msg msg;
2238 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Jean Delvare815f55f2005-05-07 22:58:46 +02002240 msg.addr = client->addr;
2241 msg.flags = client->flags & I2C_M_TEN;
2242 msg.flags |= I2C_M_RD;
2243 msg.len = count;
2244 msg.buf = buf;
2245
2246 ret = i2c_transfer(adap, &msg, 1);
2247
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002248 /*
2249 * If everything went ok (i.e. 1 msg received), return #bytes received,
2250 * else error code.
2251 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002252 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
David Brownellc0564602007-05-01 23:26:31 +02002254EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256/* ----------------------------------------------------
2257 * the i2c address scanning function
2258 * Will not work for 10-bit addresses!
2259 * ----------------------------------------------------
2260 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002261
Jean Delvare63e4e802010-06-03 11:33:51 +02002262/*
2263 * Legacy default probe function, mostly relevant for SMBus. The default
2264 * probe method is a quick write, but it is known to corrupt the 24RF08
2265 * EEPROMs due to a state machine bug, and could also irreversibly
2266 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2267 * we use a short byte read instead. Also, some bus drivers don't implement
2268 * quick write, so we fallback to a byte read in that case too.
2269 * On x86, there is another special case for FSC hardware monitoring chips,
2270 * which want regular byte reads (address 0x73.) Fortunately, these are the
2271 * only known chips using this I2C address on PC hardware.
2272 * Returns 1 if probe succeeded, 0 if not.
2273 */
2274static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2275{
2276 int err;
2277 union i2c_smbus_data dummy;
2278
2279#ifdef CONFIG_X86
2280 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2281 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2282 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2283 I2C_SMBUS_BYTE_DATA, &dummy);
2284 else
2285#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002286 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2287 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002288 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2289 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002290 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2291 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2292 I2C_SMBUS_BYTE, &dummy);
2293 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002294 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2295 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002296 err = -EOPNOTSUPP;
2297 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002298
2299 return err >= 0;
2300}
2301
Jean Delvareccfbbd02009-12-06 17:06:25 +01002302static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002303 struct i2c_driver *driver)
2304{
2305 struct i2c_board_info info;
2306 struct i2c_adapter *adapter = temp_client->adapter;
2307 int addr = temp_client->addr;
2308 int err;
2309
2310 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002311 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002312 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002313 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2314 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002315 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002316 }
2317
2318 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002319 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002320 return 0;
2321
Jean Delvareccfbbd02009-12-06 17:06:25 +01002322 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002323 if (!i2c_default_probe(adapter, addr))
2324 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002325
2326 /* Finally call the custom detection function */
2327 memset(&info, 0, sizeof(struct i2c_board_info));
2328 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002329 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002330 if (err) {
2331 /* -ENODEV is returned if the detection fails. We catch it
2332 here as this isn't an error. */
2333 return err == -ENODEV ? 0 : err;
2334 }
2335
2336 /* Consistency check */
2337 if (info.type[0] == '\0') {
2338 dev_err(&adapter->dev, "%s detection function provided "
2339 "no name for 0x%x\n", driver->driver.name,
2340 addr);
2341 } else {
2342 struct i2c_client *client;
2343
2344 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002345 if (adapter->class & I2C_CLASS_DEPRECATED)
2346 dev_warn(&adapter->dev,
2347 "This adapter will soon drop class based instantiation of devices. "
2348 "Please make sure client 0x%02x gets instantiated by other means. "
2349 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2350 info.addr);
2351
Jean Delvare4735c982008-07-14 22:38:36 +02002352 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2353 info.type, info.addr);
2354 client = i2c_new_device(adapter, &info);
2355 if (client)
2356 list_add_tail(&client->detected, &driver->clients);
2357 else
2358 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2359 info.type, info.addr);
2360 }
2361 return 0;
2362}
2363
2364static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2365{
Jean Delvarec3813d62009-12-14 21:17:25 +01002366 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002367 struct i2c_client *temp_client;
2368 int i, err = 0;
2369 int adap_id = i2c_adapter_id(adapter);
2370
Jean Delvarec3813d62009-12-14 21:17:25 +01002371 address_list = driver->address_list;
2372 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002373 return 0;
2374
Wolfram Sang45552272014-07-10 13:46:21 +02002375 /* Warn that the adapter lost class based instantiation */
2376 if (adapter->class == I2C_CLASS_DEPRECATED) {
2377 dev_dbg(&adapter->dev,
2378 "This adapter dropped support for I2C classes and "
2379 "won't auto-detect %s devices anymore. If you need it, check "
2380 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2381 driver->driver.name);
2382 return 0;
2383 }
2384
Jean Delvare51b54ba2010-10-24 18:16:58 +02002385 /* Stop here if the classes do not match */
2386 if (!(adapter->class & driver->class))
2387 return 0;
2388
Jean Delvare4735c982008-07-14 22:38:36 +02002389 /* Set up a temporary client to help detect callback */
2390 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2391 if (!temp_client)
2392 return -ENOMEM;
2393 temp_client->adapter = adapter;
2394
Jean Delvarec3813d62009-12-14 21:17:25 +01002395 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002396 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002397 "addr 0x%02x\n", adap_id, address_list[i]);
2398 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002399 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002400 if (unlikely(err))
2401 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002402 }
2403
Jean Delvare4735c982008-07-14 22:38:36 +02002404 kfree(temp_client);
2405 return err;
2406}
2407
Jean Delvared44f19d2010-08-11 18:20:57 +02002408int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2409{
2410 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2411 I2C_SMBUS_QUICK, NULL) >= 0;
2412}
2413EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2414
Jean Delvare12b5053a2007-05-01 23:26:31 +02002415struct i2c_client *
2416i2c_new_probed_device(struct i2c_adapter *adap,
2417 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002418 unsigned short const *addr_list,
2419 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002420{
2421 int i;
2422
Jean Delvare8031d792010-08-11 18:21:00 +02002423 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002424 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002425
Jean Delvare12b5053a2007-05-01 23:26:31 +02002426 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2427 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002428 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002429 dev_warn(&adap->dev, "Invalid 7-bit address "
2430 "0x%02x\n", addr_list[i]);
2431 continue;
2432 }
2433
2434 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002435 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002436 dev_dbg(&adap->dev, "Address 0x%02x already in "
2437 "use, not probing\n", addr_list[i]);
2438 continue;
2439 }
2440
Jean Delvare63e4e802010-06-03 11:33:51 +02002441 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002442 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002443 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002444 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002445
2446 if (addr_list[i] == I2C_CLIENT_END) {
2447 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2448 return NULL;
2449 }
2450
2451 info->addr = addr_list[i];
2452 return i2c_new_device(adap, info);
2453}
2454EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2455
Jean Delvared735b342011-03-20 14:50:52 +01002456struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002459
Jean Delvarecaada322008-01-27 18:14:49 +01002460 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002461 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002462 if (!adapter)
2463 goto exit;
2464
2465 if (try_module_get(adapter->owner))
2466 get_device(&adapter->dev);
2467 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002468 adapter = NULL;
2469
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002470 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002471 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002472 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
David Brownellc0564602007-05-01 23:26:31 +02002474EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476void i2c_put_adapter(struct i2c_adapter *adap)
2477{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002478 if (!adap)
2479 return;
2480
2481 put_device(&adap->dev);
2482 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
David Brownellc0564602007-05-01 23:26:31 +02002484EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486/* The SMBus parts */
2487
David Brownell438d6c22006-12-10 21:21:31 +01002488#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002489static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002492
Farid Hammane7225acf2010-05-21 18:40:58 +02002493 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002494 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 data = data ^ POLY;
2496 data = data << 1;
2497 }
2498 return (u8)(data >> 8);
2499}
2500
Jean Delvare421ef472005-10-26 21:28:55 +02002501/* Incremental CRC8 over count bytes in the array pointed to by p */
2502static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 int i;
2505
Farid Hammane7225acf2010-05-21 18:40:58 +02002506 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002507 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return crc;
2509}
2510
Jean Delvare421ef472005-10-26 21:28:55 +02002511/* Assume a 7-bit address, which is reasonable for SMBus */
2512static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Jean Delvare421ef472005-10-26 21:28:55 +02002514 /* The address will be sent first */
2515 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2516 pec = i2c_smbus_pec(pec, &addr, 1);
2517
2518 /* The data buffer follows */
2519 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Jean Delvare421ef472005-10-26 21:28:55 +02002522/* Used for write only transactions */
2523static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Jean Delvare421ef472005-10-26 21:28:55 +02002525 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2526 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
Jean Delvare421ef472005-10-26 21:28:55 +02002529/* Return <0 on CRC error
2530 If there was a write before this read (most cases) we need to take the
2531 partial CRC from the write part into account.
2532 Note that this function does modify the message (we need to decrease the
2533 message length to hide the CRC byte from the caller). */
2534static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Jean Delvare421ef472005-10-26 21:28:55 +02002536 u8 rpec = msg->buf[--msg->len];
2537 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (rpec != cpec) {
2540 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2541 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002542 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
David Brownell438d6c22006-12-10 21:21:31 +01002544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
David Brownella1cdeda2008-07-14 22:38:24 +02002547/**
2548 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2549 * @client: Handle to slave device
2550 *
2551 * This executes the SMBus "receive byte" protocol, returning negative errno
2552 * else the byte received from the device.
2553 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002554s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002557 int status;
2558
2559 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2560 I2C_SMBUS_READ, 0,
2561 I2C_SMBUS_BYTE, &data);
2562 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
David Brownellc0564602007-05-01 23:26:31 +02002564EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
David Brownella1cdeda2008-07-14 22:38:24 +02002566/**
2567 * i2c_smbus_write_byte - SMBus "send byte" protocol
2568 * @client: Handle to slave device
2569 * @value: Byte to be sent
2570 *
2571 * This executes the SMBus "send byte" protocol, returning negative errno
2572 * else zero on success.
2573 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002574s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Farid Hammane7225acf2010-05-21 18:40:58 +02002576 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002577 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
David Brownellc0564602007-05-01 23:26:31 +02002579EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
David Brownella1cdeda2008-07-14 22:38:24 +02002581/**
2582 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2583 * @client: Handle to slave device
2584 * @command: Byte interpreted by slave
2585 *
2586 * This executes the SMBus "read byte" protocol, returning negative errno
2587 * else a data byte received from the device.
2588 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002589s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002592 int status;
2593
2594 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2595 I2C_SMBUS_READ, command,
2596 I2C_SMBUS_BYTE_DATA, &data);
2597 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
David Brownellc0564602007-05-01 23:26:31 +02002599EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
David Brownella1cdeda2008-07-14 22:38:24 +02002601/**
2602 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2603 * @client: Handle to slave device
2604 * @command: Byte interpreted by slave
2605 * @value: Byte being written
2606 *
2607 * This executes the SMBus "write byte" protocol, returning negative errno
2608 * else zero on success.
2609 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002610s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2611 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 union i2c_smbus_data data;
2614 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002615 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2616 I2C_SMBUS_WRITE, command,
2617 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
David Brownellc0564602007-05-01 23:26:31 +02002619EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
David Brownella1cdeda2008-07-14 22:38:24 +02002621/**
2622 * i2c_smbus_read_word_data - SMBus "read word" protocol
2623 * @client: Handle to slave device
2624 * @command: Byte interpreted by slave
2625 *
2626 * This executes the SMBus "read word" protocol, returning negative errno
2627 * else a 16-bit unsigned "word" received from the device.
2628 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002629s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002632 int status;
2633
2634 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2635 I2C_SMBUS_READ, command,
2636 I2C_SMBUS_WORD_DATA, &data);
2637 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
David Brownellc0564602007-05-01 23:26:31 +02002639EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
David Brownella1cdeda2008-07-14 22:38:24 +02002641/**
2642 * i2c_smbus_write_word_data - SMBus "write word" protocol
2643 * @client: Handle to slave device
2644 * @command: Byte interpreted by slave
2645 * @value: 16-bit "word" being written
2646 *
2647 * This executes the SMBus "write word" protocol, returning negative errno
2648 * else zero on success.
2649 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002650s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2651 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 union i2c_smbus_data data;
2654 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2656 I2C_SMBUS_WRITE, command,
2657 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
David Brownellc0564602007-05-01 23:26:31 +02002659EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
David Brownella64ec072007-10-13 23:56:31 +02002661/**
David Brownella1cdeda2008-07-14 22:38:24 +02002662 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002663 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002664 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002665 * @values: Byte array into which data will be read; big enough to hold
2666 * the data returned by the slave. SMBus allows at most 32 bytes.
2667 *
David Brownella1cdeda2008-07-14 22:38:24 +02002668 * This executes the SMBus "block read" protocol, returning negative errno
2669 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002670 *
2671 * Note that using this function requires that the client's adapter support
2672 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2673 * support this; its emulation through I2C messaging relies on a specific
2674 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2675 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002676s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002677 u8 *values)
2678{
2679 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002680 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002681
David Brownell24a5bb72008-07-14 22:38:23 +02002682 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2683 I2C_SMBUS_READ, command,
2684 I2C_SMBUS_BLOCK_DATA, &data);
2685 if (status)
2686 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002687
2688 memcpy(values, &data.block[1], data.block[0]);
2689 return data.block[0];
2690}
2691EXPORT_SYMBOL(i2c_smbus_read_block_data);
2692
David Brownella1cdeda2008-07-14 22:38:24 +02002693/**
2694 * i2c_smbus_write_block_data - SMBus "block write" protocol
2695 * @client: Handle to slave device
2696 * @command: Byte interpreted by slave
2697 * @length: Size of data block; SMBus allows at most 32 bytes
2698 * @values: Byte array which will be written.
2699 *
2700 * This executes the SMBus "block write" protocol, returning negative errno
2701 * else zero on success.
2702 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002703s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002704 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
2706 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (length > I2C_SMBUS_BLOCK_MAX)
2709 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002711 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002712 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2713 I2C_SMBUS_WRITE, command,
2714 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
David Brownellc0564602007-05-01 23:26:31 +02002716EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002719s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002720 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002723 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002724
Jean Delvare4b2643d2007-07-12 14:12:29 +02002725 if (length > I2C_SMBUS_BLOCK_MAX)
2726 length = I2C_SMBUS_BLOCK_MAX;
2727 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002728 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2729 I2C_SMBUS_READ, command,
2730 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2731 if (status < 0)
2732 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002733
2734 memcpy(values, &data.block[1], data.block[0]);
2735 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
David Brownellc0564602007-05-01 23:26:31 +02002737EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Jean Delvare0cc43a12011-01-10 22:11:23 +01002739s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002740 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002741{
2742 union i2c_smbus_data data;
2743
2744 if (length > I2C_SMBUS_BLOCK_MAX)
2745 length = I2C_SMBUS_BLOCK_MAX;
2746 data.block[0] = length;
2747 memcpy(data.block + 1, values, length);
2748 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2749 I2C_SMBUS_WRITE, command,
2750 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2751}
David Brownellc0564602007-05-01 23:26:31 +02002752EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002753
David Brownell438d6c22006-12-10 21:21:31 +01002754/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002756static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2757 unsigned short flags,
2758 char read_write, u8 command, int size,
2759 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 /* So we need to generate a series of msgs. In the case of writing, we
2762 need to use only one message; when reading, we need two. We initialize
2763 most things with sane defaults, to keep the code below somewhat
2764 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002765 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2766 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002767 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002769 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002770 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002771 struct i2c_msg msg[2] = {
2772 {
2773 .addr = addr,
2774 .flags = flags,
2775 .len = 1,
2776 .buf = msgbuf0,
2777 }, {
2778 .addr = addr,
2779 .flags = flags | I2C_M_RD,
2780 .len = 0,
2781 .buf = msgbuf1,
2782 },
2783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002786 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 case I2C_SMBUS_QUICK:
2788 msg[0].len = 0;
2789 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002790 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2791 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 num = 1;
2793 break;
2794 case I2C_SMBUS_BYTE:
2795 if (read_write == I2C_SMBUS_READ) {
2796 /* Special case: only a read! */
2797 msg[0].flags = I2C_M_RD | flags;
2798 num = 1;
2799 }
2800 break;
2801 case I2C_SMBUS_BYTE_DATA:
2802 if (read_write == I2C_SMBUS_READ)
2803 msg[1].len = 1;
2804 else {
2805 msg[0].len = 2;
2806 msgbuf0[1] = data->byte;
2807 }
2808 break;
2809 case I2C_SMBUS_WORD_DATA:
2810 if (read_write == I2C_SMBUS_READ)
2811 msg[1].len = 2;
2812 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002813 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002815 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
2817 break;
2818 case I2C_SMBUS_PROC_CALL:
2819 num = 2; /* Special case */
2820 read_write = I2C_SMBUS_READ;
2821 msg[0].len = 3;
2822 msg[1].len = 2;
2823 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002824 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 break;
2826 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002828 msg[1].flags |= I2C_M_RECV_LEN;
2829 msg[1].len = 1; /* block length will be added by
2830 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 } else {
2832 msg[0].len = data->block[0] + 2;
2833 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002834 dev_err(&adapter->dev,
2835 "Invalid block write size %d\n",
2836 data->block[0]);
2837 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002839 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 msgbuf0[i] = data->block[i-1];
2841 }
2842 break;
2843 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002844 num = 2; /* Another special case */
2845 read_write = I2C_SMBUS_READ;
2846 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002847 dev_err(&adapter->dev,
2848 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002849 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002850 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002851 }
2852 msg[0].len = data->block[0] + 2;
2853 for (i = 1; i < msg[0].len; i++)
2854 msgbuf0[i] = data->block[i-1];
2855 msg[1].flags |= I2C_M_RECV_LEN;
2856 msg[1].len = 1; /* block length will be added by
2857 the underlying bus driver */
2858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 case I2C_SMBUS_I2C_BLOCK_DATA:
2860 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002861 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 } else {
2863 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002864 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002865 dev_err(&adapter->dev,
2866 "Invalid block write size %d\n",
2867 data->block[0]);
2868 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870 for (i = 1; i <= data->block[0]; i++)
2871 msgbuf0[i] = data->block[i];
2872 }
2873 break;
2874 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002875 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2876 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
Jean Delvare421ef472005-10-26 21:28:55 +02002879 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2880 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2881 if (i) {
2882 /* Compute PEC if first message is a write */
2883 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002884 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002885 i2c_smbus_add_pec(&msg[0]);
2886 else /* Write followed by read */
2887 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2888 }
2889 /* Ask for PEC if last message is a read */
2890 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002891 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002892 }
2893
David Brownell24a5bb72008-07-14 22:38:23 +02002894 status = i2c_transfer(adapter, msg, num);
2895 if (status < 0)
2896 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Jean Delvare421ef472005-10-26 21:28:55 +02002898 /* Check PEC if last message is a read */
2899 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002900 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2901 if (status < 0)
2902 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002903 }
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002906 switch (size) {
2907 case I2C_SMBUS_BYTE:
2908 data->byte = msgbuf0[0];
2909 break;
2910 case I2C_SMBUS_BYTE_DATA:
2911 data->byte = msgbuf1[0];
2912 break;
2913 case I2C_SMBUS_WORD_DATA:
2914 case I2C_SMBUS_PROC_CALL:
2915 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2916 break;
2917 case I2C_SMBUS_I2C_BLOCK_DATA:
2918 for (i = 0; i < data->block[0]; i++)
2919 data->block[i+1] = msgbuf1[i];
2920 break;
2921 case I2C_SMBUS_BLOCK_DATA:
2922 case I2C_SMBUS_BLOCK_PROC_CALL:
2923 for (i = 0; i < msgbuf1[0] + 1; i++)
2924 data->block[i] = msgbuf1[i];
2925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927 return 0;
2928}
2929
David Brownella1cdeda2008-07-14 22:38:24 +02002930/**
2931 * i2c_smbus_xfer - execute SMBus protocol operations
2932 * @adapter: Handle to I2C bus
2933 * @addr: Address of SMBus slave on that bus
2934 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2935 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2936 * @command: Byte interpreted by slave, for protocols which use such bytes
2937 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2938 * @data: Data to be read or written
2939 *
2940 * This executes an SMBus protocol operation, and returns a negative
2941 * errno code else zero on success.
2942 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002943s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002944 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002945 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002947 unsigned long orig_jiffies;
2948 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
David Howells8a325992014-03-06 13:36:06 +00002951 /* If enabled, the following two tracepoints are conditional on
2952 * read_write and protocol.
2953 */
2954 trace_smbus_write(adapter, addr, flags, read_write,
2955 command, protocol, data);
2956 trace_smbus_read(adapter, addr, flags, read_write,
2957 command, protocol);
2958
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002959 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002962 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002963
2964 /* Retry automatically on arbitration loss */
2965 orig_jiffies = jiffies;
2966 for (res = 0, try = 0; try <= adapter->retries; try++) {
2967 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2968 read_write, command,
2969 protocol, data);
2970 if (res != -EAGAIN)
2971 break;
2972 if (time_after(jiffies,
2973 orig_jiffies + adapter->timeout))
2974 break;
2975 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002976 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002978 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002979 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002980 /*
2981 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2982 * implement native support for the SMBus operation.
2983 */
2984 }
2985
David Howells8a325992014-03-06 13:36:06 +00002986 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2987 command, protocol, data);
2988
2989trace:
2990 /* If enabled, the reply tracepoint is conditional on read_write. */
2991 trace_smbus_reply(adapter, addr, flags, read_write,
2992 command, protocol, data);
2993 trace_smbus_result(adapter, addr, flags, read_write,
2994 command, protocol, res);
2995
2996 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Jean Delvared5fd1202015-01-26 20:59:31 +01003000#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003001int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3002{
3003 int ret;
3004
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003005 if (!client || !slave_cb) {
3006 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003007 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003008 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003009
3010 if (!(client->flags & I2C_CLIENT_TEN)) {
3011 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003012 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003013 if (ret) {
3014 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003015 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003016 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003017 }
3018
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003019 if (!client->adapter->algo->reg_slave) {
3020 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003021 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003022 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003023
3024 client->slave_cb = slave_cb;
3025
3026 i2c_lock_adapter(client->adapter);
3027 ret = client->adapter->algo->reg_slave(client);
3028 i2c_unlock_adapter(client->adapter);
3029
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003030 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003031 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003032 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3033 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003034
3035 return ret;
3036}
3037EXPORT_SYMBOL_GPL(i2c_slave_register);
3038
3039int i2c_slave_unregister(struct i2c_client *client)
3040{
3041 int ret;
3042
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003043 if (!client->adapter->algo->unreg_slave) {
3044 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003045 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003046 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003047
3048 i2c_lock_adapter(client->adapter);
3049 ret = client->adapter->algo->unreg_slave(client);
3050 i2c_unlock_adapter(client->adapter);
3051
3052 if (ret == 0)
3053 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003054 else
3055 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003056
3057 return ret;
3058}
3059EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003060#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3063MODULE_DESCRIPTION("I2C-Bus main module");
3064MODULE_LICENSE("GPL");