blob: 4ffe06451081e86ede0cc1a9688d692db13305e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/init.h>
38#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010039#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010040#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060041#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020043#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010044#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010045#include <linux/hardirq.h>
46#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020047#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010048#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020049#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000051#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020053#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Wolfram Sangda899f52015-05-18 21:09:12 +020060#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
61#define I2C_ADDR_OFFSET_SLAVE 0x1000
62
Jean Delvare6629dcf2010-05-04 11:09:28 +020063/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020064 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000065 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010066static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_IDR(i2c_adapter_idr);
68
Jean Delvare4f8cf822009-09-18 22:45:46 +020069static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020070static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010071
David Howellsd9a83d62014-03-06 13:35:59 +000072static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
73
74void i2c_transfer_trace_reg(void)
75{
76 static_key_slow_inc(&i2c_trace_msg);
77}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020084#if defined(CONFIG_ACPI)
85struct acpi_i2c_handler_data {
86 struct acpi_connection_info info;
87 struct i2c_adapter *adapter;
88};
89
90struct gsb_buffer {
91 u8 status;
92 u8 len;
93 union {
94 u16 wdata;
95 u8 bdata;
96 u8 data[0];
97 };
98} __packed;
99
100static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
101{
102 struct i2c_board_info *info = data;
103
104 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
105 struct acpi_resource_i2c_serialbus *sb;
106
107 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1ce2014-12-29 15:48:48 +0200108 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200109 info->addr = sb->slave_address;
110 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
111 info->flags |= I2C_CLIENT_TEN;
112 }
Mika Westerbergdab472e2015-05-06 13:29:07 +0300113 } else if (!info->irq) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200114 struct resource r;
115
116 if (acpi_dev_resource_interrupt(ares, 0, &r))
117 info->irq = r.start;
118 }
119
120 /* Tell the ACPI core to skip this resource */
121 return 1;
122}
123
124static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
125 void *data, void **return_value)
126{
127 struct i2c_adapter *adapter = data;
128 struct list_head resource_list;
129 struct i2c_board_info info;
130 struct acpi_device *adev;
131 int ret;
132
133 if (acpi_bus_get_device(handle, &adev))
134 return AE_OK;
135 if (acpi_bus_get_status(adev) || !adev->status.present)
136 return AE_OK;
137
138 memset(&info, 0, sizeof(info));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100139 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140
141 INIT_LIST_HEAD(&resource_list);
142 ret = acpi_dev_get_resources(adev, &resource_list,
143 acpi_i2c_add_resource, &info);
144 acpi_dev_free_resource_list(&resource_list);
145
146 if (ret < 0 || !info.addr)
147 return AE_OK;
148
149 adev->power.flags.ignore_parent = true;
150 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
151 if (!i2c_new_device(adapter, &info)) {
152 adev->power.flags.ignore_parent = false;
153 dev_err(&adapter->dev,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev->dev));
156 }
157
158 return AE_OK;
159}
160
161/**
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
164 *
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
168 */
169static void acpi_i2c_register_devices(struct i2c_adapter *adap)
170{
171 acpi_handle handle;
172 acpi_status status;
173
174 if (!adap->dev.parent)
175 return;
176
177 handle = ACPI_HANDLE(adap->dev.parent);
178 if (!handle)
179 return;
180
181 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
182 acpi_i2c_add_device, NULL,
183 adap, NULL);
184 if (ACPI_FAILURE(status))
185 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
186}
187
188#else /* CONFIG_ACPI */
189static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
190#endif /* CONFIG_ACPI */
191
192#ifdef CONFIG_ACPI_I2C_OPREGION
193static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
194 u8 cmd, u8 *data, u8 data_len)
195{
196
197 struct i2c_msg msgs[2];
198 int ret;
199 u8 *buffer;
200
201 buffer = kzalloc(data_len, GFP_KERNEL);
202 if (!buffer)
203 return AE_NO_MEMORY;
204
205 msgs[0].addr = client->addr;
206 msgs[0].flags = client->flags;
207 msgs[0].len = 1;
208 msgs[0].buf = &cmd;
209
210 msgs[1].addr = client->addr;
211 msgs[1].flags = client->flags | I2C_M_RD;
212 msgs[1].len = data_len;
213 msgs[1].buf = buffer;
214
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
216 if (ret < 0)
217 dev_err(&client->adapter->dev, "i2c read failed\n");
218 else
219 memcpy(data, buffer, data_len);
220
221 kfree(buffer);
222 return ret;
223}
224
225static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
226 u8 cmd, u8 *data, u8 data_len)
227{
228
229 struct i2c_msg msgs[1];
230 u8 *buffer;
231 int ret = AE_OK;
232
233 buffer = kzalloc(data_len + 1, GFP_KERNEL);
234 if (!buffer)
235 return AE_NO_MEMORY;
236
237 buffer[0] = cmd;
238 memcpy(buffer + 1, data, data_len);
239
240 msgs[0].addr = client->addr;
241 msgs[0].flags = client->flags;
242 msgs[0].len = data_len + 1;
243 msgs[0].buf = buffer;
244
245 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
246 if (ret < 0)
247 dev_err(&client->adapter->dev, "i2c write failed\n");
248
249 kfree(buffer);
250 return ret;
251}
252
253static acpi_status
254acpi_i2c_space_handler(u32 function, acpi_physical_address command,
255 u32 bits, u64 *value64,
256 void *handler_context, void *region_context)
257{
258 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
259 struct acpi_i2c_handler_data *data = handler_context;
260 struct acpi_connection_info *info = &data->info;
261 struct acpi_resource_i2c_serialbus *sb;
262 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300263 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200264 struct acpi_resource *ares;
265 u32 accessor_type = function >> 16;
266 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100267 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200268 int status;
269
270 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
271 if (ACPI_FAILURE(ret))
272 return ret;
273
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300274 client = kzalloc(sizeof(*client), GFP_KERNEL);
275 if (!client) {
276 ret = AE_NO_MEMORY;
277 goto err;
278 }
279
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200280 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
281 ret = AE_BAD_PARAMETER;
282 goto err;
283 }
284
285 sb = &ares->data.i2c_serial_bus;
286 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
287 ret = AE_BAD_PARAMETER;
288 goto err;
289 }
290
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300291 client->adapter = adapter;
292 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200293
294 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300295 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200296
297 switch (accessor_type) {
298 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
299 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300300 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200301 if (status >= 0) {
302 gsb->bdata = status;
303 status = 0;
304 }
305 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300306 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200307 }
308 break;
309
310 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
311 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300312 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200313 if (status >= 0) {
314 gsb->bdata = status;
315 status = 0;
316 }
317 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300318 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200319 gsb->bdata);
320 }
321 break;
322
323 case ACPI_GSB_ACCESS_ATTRIB_WORD:
324 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300325 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200326 if (status >= 0) {
327 gsb->wdata = status;
328 status = 0;
329 }
330 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300331 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200332 gsb->wdata);
333 }
334 break;
335
336 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
337 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300338 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200339 gsb->data);
340 if (status >= 0) {
341 gsb->len = status;
342 status = 0;
343 }
344 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300345 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200346 gsb->len, gsb->data);
347 }
348 break;
349
350 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
351 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300352 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200353 gsb->data, info->access_length);
354 if (status > 0)
355 status = 0;
356 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300357 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200358 gsb->data, info->access_length);
359 }
360 break;
361
362 default:
363 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
364 ret = AE_BAD_PARAMETER;
365 goto err;
366 }
367
368 gsb->status = status;
369
370 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300371 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200372 ACPI_FREE(ares);
373 return ret;
374}
375
376
377static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
378{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200379 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200380 struct acpi_i2c_handler_data *data;
381 acpi_status status;
382
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200383 if (!adapter->dev.parent)
384 return -ENODEV;
385
386 handle = ACPI_HANDLE(adapter->dev.parent);
387
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200388 if (!handle)
389 return -ENODEV;
390
391 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
392 GFP_KERNEL);
393 if (!data)
394 return -ENOMEM;
395
396 data->adapter = adapter;
397 status = acpi_bus_attach_private_data(handle, (void *)data);
398 if (ACPI_FAILURE(status)) {
399 kfree(data);
400 return -ENOMEM;
401 }
402
403 status = acpi_install_address_space_handler(handle,
404 ACPI_ADR_SPACE_GSBUS,
405 &acpi_i2c_space_handler,
406 NULL,
407 data);
408 if (ACPI_FAILURE(status)) {
409 dev_err(&adapter->dev, "Error installing i2c space handler\n");
410 acpi_bus_detach_private_data(handle);
411 kfree(data);
412 return -ENOMEM;
413 }
414
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800415 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200416 return 0;
417}
418
419static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
420{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200421 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200422 struct acpi_i2c_handler_data *data;
423 acpi_status status;
424
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200425 if (!adapter->dev.parent)
426 return;
427
428 handle = ACPI_HANDLE(adapter->dev.parent);
429
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200430 if (!handle)
431 return;
432
433 acpi_remove_address_space_handler(handle,
434 ACPI_ADR_SPACE_GSBUS,
435 &acpi_i2c_space_handler);
436
437 status = acpi_bus_get_private_data(handle, (void **)&data);
438 if (ACPI_SUCCESS(status))
439 kfree(data);
440
441 acpi_bus_detach_private_data(handle);
442}
443#else /* CONFIG_ACPI_I2C_OPREGION */
444static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
445{ }
446
447static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
448{ return 0; }
449#endif /* CONFIG_ACPI_I2C_OPREGION */
450
David Brownellf37dd802007-02-13 22:09:00 +0100451/* ------------------------------------------------------------------------- */
452
Jean Delvared2653e92008-04-29 23:11:39 +0200453static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
454 const struct i2c_client *client)
455{
456 while (id->name[0]) {
457 if (strcmp(client->name, id->name) == 0)
458 return id;
459 id++;
460 }
461 return NULL;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static int i2c_device_match(struct device *dev, struct device_driver *drv)
465{
Jean Delvare51298d12009-09-18 22:45:45 +0200466 struct i2c_client *client = i2c_verify_client(dev);
467 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200468
Jean Delvare51298d12009-09-18 22:45:45 +0200469 if (!client)
470 return 0;
471
Grant Likely959e85f2010-06-08 07:48:19 -0600472 /* Attempt an OF style match */
473 if (of_driver_match_device(dev, drv))
474 return 1;
475
Mika Westerberg907ddf82012-11-23 12:23:40 +0100476 /* Then ACPI style match */
477 if (acpi_driver_match_device(dev, drv))
478 return 1;
479
Jean Delvare51298d12009-09-18 22:45:45 +0200480 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200481 /* match on an id table if there is one */
482 if (driver->id_table)
483 return i2c_match_id(driver->id_table, client) != NULL;
484
Jean Delvareeb8a7902008-05-18 20:49:41 +0200485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
David Brownell7b4fbc52007-05-01 23:26:30 +0200488
489/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200490static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200491{
492 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800493 int rc;
494
495 rc = acpi_device_uevent_modalias(dev, env);
496 if (rc != -ENODEV)
497 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200498
Jean Delvareeb8a7902008-05-18 20:49:41 +0200499 if (add_uevent_var(env, "MODALIAS=%s%s",
500 I2C_MODULE_PREFIX, client->name))
501 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200502 dev_dbg(dev, "uevent\n");
503 return 0;
504}
505
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530506/* i2c bus recovery routines */
507static int get_scl_gpio_value(struct i2c_adapter *adap)
508{
509 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
510}
511
512static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
513{
514 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
515}
516
517static int get_sda_gpio_value(struct i2c_adapter *adap)
518{
519 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
520}
521
522static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
523{
524 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
525 struct device *dev = &adap->dev;
526 int ret = 0;
527
528 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
529 GPIOF_OUT_INIT_HIGH, "i2c-scl");
530 if (ret) {
531 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
532 return ret;
533 }
534
535 if (bri->get_sda) {
536 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
537 /* work without SDA polling */
538 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
539 bri->sda_gpio);
540 bri->get_sda = NULL;
541 }
542 }
543
544 return ret;
545}
546
547static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
548{
549 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
550
551 if (bri->get_sda)
552 gpio_free(bri->sda_gpio);
553
554 gpio_free(bri->scl_gpio);
555}
556
557/*
558 * We are generating clock pulses. ndelay() determines durating of clk pulses.
559 * We will generate clock with rate 100 KHz and so duration of both clock levels
560 * is: delay in ns = (10^6 / 100) / 2
561 */
562#define RECOVERY_NDELAY 5000
563#define RECOVERY_CLK_CNT 9
564
565static int i2c_generic_recovery(struct i2c_adapter *adap)
566{
567 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
568 int i = 0, val = 1, ret = 0;
569
570 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300571 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530572
Jan Luebbe8b062602015-07-08 16:35:06 +0200573 bri->set_scl(adap, val);
574 ndelay(RECOVERY_NDELAY);
575
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530576 /*
577 * By this time SCL is high, as we need to give 9 falling-rising edges
578 */
579 while (i++ < RECOVERY_CLK_CNT * 2) {
580 if (val) {
581 /* Break if SDA is high */
582 if (bri->get_sda && bri->get_sda(adap))
583 break;
584 /* SCL shouldn't be low here */
585 if (!bri->get_scl(adap)) {
586 dev_err(&adap->dev,
587 "SCL is stuck low, exit recovery\n");
588 ret = -EBUSY;
589 break;
590 }
591 }
592
593 val = !val;
594 bri->set_scl(adap, val);
595 ndelay(RECOVERY_NDELAY);
596 }
597
598 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300599 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530600
601 return ret;
602}
603
604int i2c_generic_scl_recovery(struct i2c_adapter *adap)
605{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530606 return i2c_generic_recovery(adap);
607}
Mark Brownc1c21f42015-04-15 19:18:39 +0100608EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530609
610int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
611{
612 int ret;
613
614 ret = i2c_get_gpios_for_recovery(adap);
615 if (ret)
616 return ret;
617
618 ret = i2c_generic_recovery(adap);
619 i2c_put_gpios_for_recovery(adap);
620
621 return ret;
622}
Mark Brownc1c21f42015-04-15 19:18:39 +0100623EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530624
625int i2c_recover_bus(struct i2c_adapter *adap)
626{
627 if (!adap->bus_recovery_info)
628 return -EOPNOTSUPP;
629
630 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
631 return adap->bus_recovery_info->recover_bus(adap);
632}
Mark Brownc1c21f42015-04-15 19:18:39 +0100633EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static int i2c_device_probe(struct device *dev)
636{
Jean Delvare51298d12009-09-18 22:45:45 +0200637 struct i2c_client *client = i2c_verify_client(dev);
638 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100639 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200640
Jean Delvare51298d12009-09-18 22:45:45 +0200641 if (!client)
642 return 0;
643
Mika Westerberg845c8772015-05-06 13:29:08 +0300644 if (!client->irq) {
645 int irq = -ENOENT;
646
647 if (dev->of_node)
648 irq = of_irq_get(dev->of_node, 0);
649 else if (ACPI_COMPANION(dev))
650 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200651
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100652 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200653 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100654 if (irq < 0)
655 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200656
657 client->irq = irq;
658 }
659
Jean Delvare51298d12009-09-18 22:45:45 +0200660 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200661 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200662 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200663
Marc Pignatee354252008-08-28 08:33:22 +0200664 if (!device_can_wakeup(&client->dev))
665 device_init_wakeup(&client->dev,
666 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200667 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200668
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200669 status = of_clk_set_defaults(dev->of_node, false);
670 if (status < 0)
671 return status;
672
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200673 status = dev_pm_domain_attach(&client->dev, true);
674 if (status != -EPROBE_DEFER) {
675 status = driver->probe(client, i2c_match_id(driver->id_table,
676 client));
677 if (status)
678 dev_pm_domain_detach(&client->dev, true);
679 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100680
Hans Verkuil50c33042008-03-12 14:15:00 +0100681 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static int i2c_device_remove(struct device *dev)
685{
Jean Delvare51298d12009-09-18 22:45:45 +0200686 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200687 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100688 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200689
Jean Delvare51298d12009-09-18 22:45:45 +0200690 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200691 return 0;
692
693 driver = to_i2c_driver(dev->driver);
694 if (driver->remove) {
695 dev_dbg(dev, "remove\n");
696 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200697 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100698
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200699 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200700 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
David Brownellf37dd802007-02-13 22:09:00 +0100703static void i2c_device_shutdown(struct device *dev)
704{
Jean Delvare51298d12009-09-18 22:45:45 +0200705 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100706 struct i2c_driver *driver;
707
Jean Delvare51298d12009-09-18 22:45:45 +0200708 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100709 return;
710 driver = to_i2c_driver(dev->driver);
711 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200712 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100713}
714
David Brownell9c1600e2007-05-01 23:26:31 +0200715static void i2c_client_dev_release(struct device *dev)
716{
717 kfree(to_i2c_client(dev));
718}
719
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100720static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200721show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200722{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200723 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
724 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200725}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100726static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200727
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100728static ssize_t
729show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200730{
731 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800732 int len;
733
734 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
735 if (len != -ENODEV)
736 return len;
737
Jean Delvareeb8a7902008-05-18 20:49:41 +0200738 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200739}
Jean Delvare51298d12009-09-18 22:45:45 +0200740static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
741
742static struct attribute *i2c_dev_attrs[] = {
743 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200744 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200745 &dev_attr_modalias.attr,
746 NULL
747};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100748ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100749
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200750struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100751 .name = "i2c",
752 .match = i2c_device_match,
753 .probe = i2c_device_probe,
754 .remove = i2c_device_remove,
755 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000756};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200757EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000758
Jean Delvare51298d12009-09-18 22:45:45 +0200759static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100760 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200761 .uevent = i2c_device_uevent,
762 .release = i2c_client_dev_release,
763};
764
David Brownell9b766b82008-01-27 18:14:51 +0100765
766/**
767 * i2c_verify_client - return parameter as i2c_client, or NULL
768 * @dev: device, probably from some driver model iterator
769 *
770 * When traversing the driver model tree, perhaps using driver model
771 * iterators like @device_for_each_child(), you can't assume very much
772 * about the nodes you find. Use this function to avoid oopses caused
773 * by wrongly treating some non-I2C device as an i2c_client.
774 */
775struct i2c_client *i2c_verify_client(struct device *dev)
776{
Jean Delvare51298d12009-09-18 22:45:45 +0200777 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100778 ? to_i2c_client(dev)
779 : NULL;
780}
781EXPORT_SYMBOL(i2c_verify_client);
782
783
Wolfram Sangda899f52015-05-18 21:09:12 +0200784/* Return a unique address which takes the flags of the client into account */
785static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
786{
787 unsigned short addr = client->addr;
788
789 /* For some client flags, add an arbitrary offset to avoid collisions */
790 if (client->flags & I2C_CLIENT_TEN)
791 addr |= I2C_ADDR_OFFSET_TEN_BIT;
792
793 if (client->flags & I2C_CLIENT_SLAVE)
794 addr |= I2C_ADDR_OFFSET_SLAVE;
795
796 return addr;
797}
798
Jean Delvare3a89db52010-06-03 11:33:52 +0200799/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300800 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200801static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200802{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200803 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200804 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200805 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200806 return -EINVAL;
807 } else {
808 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200809 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200810 return -EINVAL;
811 }
812 return 0;
813}
814
Jean Delvare656b8762010-06-03 11:33:53 +0200815/* And this is a strict address validity check, used when probing. If a
816 * device uses a reserved address, then it shouldn't be probed. 7-bit
817 * addressing is assumed, 10-bit address devices are rare and should be
818 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +0200819static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200820{
821 /*
822 * Reserved addresses per I2C specification:
823 * 0x00 General call address / START byte
824 * 0x01 CBUS address
825 * 0x02 Reserved for different bus format
826 * 0x03 Reserved for future purposes
827 * 0x04-0x07 Hs-mode master code
828 * 0x78-0x7b 10-bit slave addressing
829 * 0x7c-0x7f Reserved for future purposes
830 */
831 if (addr < 0x08 || addr > 0x77)
832 return -EINVAL;
833 return 0;
834}
835
Jean Delvare3b5f7942010-06-03 11:33:55 +0200836static int __i2c_check_addr_busy(struct device *dev, void *addrp)
837{
838 struct i2c_client *client = i2c_verify_client(dev);
839 int addr = *(int *)addrp;
840
841 if (client && client->addr == addr)
842 return -EBUSY;
843 return 0;
844}
845
Michael Lawnick08263742010-08-11 18:21:02 +0200846/* walk up mux tree */
847static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
848{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200849 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200850 int result;
851
852 result = device_for_each_child(&adapter->dev, &addr,
853 __i2c_check_addr_busy);
854
Jean Delvare97cc4d42010-10-24 18:16:57 +0200855 if (!result && parent)
856 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200857
858 return result;
859}
860
861/* recurse down mux tree */
862static int i2c_check_mux_children(struct device *dev, void *addrp)
863{
864 int result;
865
866 if (dev->type == &i2c_adapter_type)
867 result = device_for_each_child(dev, addrp,
868 i2c_check_mux_children);
869 else
870 result = __i2c_check_addr_busy(dev, addrp);
871
872 return result;
873}
874
Jean Delvare3b5f7942010-06-03 11:33:55 +0200875static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
876{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200877 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200878 int result = 0;
879
Jean Delvare97cc4d42010-10-24 18:16:57 +0200880 if (parent)
881 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200882
883 if (!result)
884 result = device_for_each_child(&adapter->dev, &addr,
885 i2c_check_mux_children);
886
887 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200888}
889
David Brownell9c1600e2007-05-01 23:26:31 +0200890/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200891 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
892 * @adapter: Target I2C bus segment
893 */
894void i2c_lock_adapter(struct i2c_adapter *adapter)
895{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200896 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
897
898 if (parent)
899 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200900 else
901 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200902}
903EXPORT_SYMBOL_GPL(i2c_lock_adapter);
904
905/**
906 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
907 * @adapter: Target I2C bus segment
908 */
909static int i2c_trylock_adapter(struct i2c_adapter *adapter)
910{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200911 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
912
913 if (parent)
914 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200915 else
916 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200917}
918
919/**
920 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
921 * @adapter: Target I2C bus segment
922 */
923void i2c_unlock_adapter(struct i2c_adapter *adapter)
924{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200925 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
926
927 if (parent)
928 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200929 else
930 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200931}
932EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
933
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200934static void i2c_dev_set_name(struct i2c_adapter *adap,
935 struct i2c_client *client)
936{
937 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
938
939 if (adev) {
940 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
941 return;
942 }
943
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200944 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200945 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200946}
947
Jean Delvarefe61e072010-08-11 18:20:58 +0200948/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200949 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200950 * @adap: the adapter managing the device
951 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200952 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200953 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200954 * Create an i2c device. Binding is handled through driver model
955 * probe()/remove() methods. A driver may be bound to this device when we
956 * return from this function, or any later moment (e.g. maybe hotplugging will
957 * load the driver module). This call is not appropriate for use by mainboard
958 * initialization logic, which usually runs during an arch_initcall() long
959 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200960 *
961 * This returns the new i2c client, which may be saved for later use with
962 * i2c_unregister_device(); or NULL to indicate an error.
963 */
964struct i2c_client *
965i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
966{
967 struct i2c_client *client;
968 int status;
969
970 client = kzalloc(sizeof *client, GFP_KERNEL);
971 if (!client)
972 return NULL;
973
974 client->adapter = adap;
975
976 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200977
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200978 if (info->archdata)
979 client->dev.archdata = *info->archdata;
980
Marc Pignatee354252008-08-28 08:33:22 +0200981 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200982 client->addr = info->addr;
983 client->irq = info->irq;
984
David Brownell9c1600e2007-05-01 23:26:31 +0200985 strlcpy(client->name, info->type, sizeof(client->name));
986
Wolfram Sangc4019b72015-07-17 12:50:06 +0200987 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200988 if (status) {
989 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
990 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
991 goto out_err_silent;
992 }
993
Jean Delvaref8a227e2009-06-19 16:58:18 +0200994 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200995 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200996 if (status)
997 goto out_err;
998
999 client->dev.parent = &client->adapter->dev;
1000 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001001 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001002 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001003 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001004
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001005 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001006 status = device_register(&client->dev);
1007 if (status)
1008 goto out_err;
1009
Jean Delvaref8a227e2009-06-19 16:58:18 +02001010 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1011 client->name, dev_name(&client->dev));
1012
David Brownell9c1600e2007-05-01 23:26:31 +02001013 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001014
1015out_err:
1016 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1017 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001018out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001019 kfree(client);
1020 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001021}
1022EXPORT_SYMBOL_GPL(i2c_new_device);
1023
1024
1025/**
1026 * i2c_unregister_device - reverse effect of i2c_new_device()
1027 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001028 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001029 */
1030void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001031{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001032 if (client->dev.of_node)
1033 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +02001034 device_unregister(&client->dev);
1035}
David Brownell9c1600e2007-05-01 23:26:31 +02001036EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001037
1038
Jean Delvare60b129d2008-05-11 20:37:06 +02001039static const struct i2c_device_id dummy_id[] = {
1040 { "dummy", 0 },
1041 { },
1042};
1043
Jean Delvared2653e92008-04-29 23:11:39 +02001044static int dummy_probe(struct i2c_client *client,
1045 const struct i2c_device_id *id)
1046{
1047 return 0;
1048}
1049
1050static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001051{
1052 return 0;
1053}
1054
1055static struct i2c_driver dummy_driver = {
1056 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001057 .probe = dummy_probe,
1058 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001059 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001060};
1061
1062/**
1063 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1064 * @adapter: the adapter managing the device
1065 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001066 * Context: can sleep
1067 *
1068 * This returns an I2C client bound to the "dummy" driver, intended for use
1069 * with devices that consume multiple addresses. Examples of such chips
1070 * include various EEPROMS (like 24c04 and 24c08 models).
1071 *
1072 * These dummy devices have two main uses. First, most I2C and SMBus calls
1073 * except i2c_transfer() need a client handle; the dummy will be that handle.
1074 * And second, this prevents the specified address from being bound to a
1075 * different driver.
1076 *
1077 * This returns the new i2c client, which should be saved for later use with
1078 * i2c_unregister_device(); or NULL to indicate an error.
1079 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001080struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001081{
1082 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001083 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001084 };
1085
David Brownelle9f13732008-01-27 18:14:52 +01001086 return i2c_new_device(adapter, &info);
1087}
1088EXPORT_SYMBOL_GPL(i2c_new_dummy);
1089
David Brownellf37dd802007-02-13 22:09:00 +01001090/* ------------------------------------------------------------------------- */
1091
David Brownell16ffadf2007-05-01 23:26:28 +02001092/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1093
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001094static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
David Brownellef2c83212007-05-01 23:26:28 +02001096 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 complete(&adap->dev_released);
1098}
1099
Jean Delvare99cd8e22009-06-19 16:58:20 +02001100/*
Jean Delvare390946b2012-09-10 10:14:02 +02001101 * This function is only needed for mutex_lock_nested, so it is never
1102 * called unless locking correctness checking is enabled. Thus we
1103 * make it inline to avoid a compiler warning. That's what gcc ends up
1104 * doing anyway.
1105 */
1106static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1107{
1108 unsigned int depth = 0;
1109
1110 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1111 depth++;
1112
1113 return depth;
1114}
1115
1116/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001117 * Let users instantiate I2C devices through sysfs. This can be used when
1118 * platform initialization code doesn't contain the proper data for
1119 * whatever reason. Also useful for drivers that do device detection and
1120 * detection fails, either because the device uses an unexpected address,
1121 * or this is a compatible device with different ID register values.
1122 *
1123 * Parameter checking may look overzealous, but we really don't want
1124 * the user to provide incorrect parameters.
1125 */
1126static ssize_t
1127i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1128 const char *buf, size_t count)
1129{
1130 struct i2c_adapter *adap = to_i2c_adapter(dev);
1131 struct i2c_board_info info;
1132 struct i2c_client *client;
1133 char *blank, end;
1134 int res;
1135
Jean Delvare99cd8e22009-06-19 16:58:20 +02001136 memset(&info, 0, sizeof(struct i2c_board_info));
1137
1138 blank = strchr(buf, ' ');
1139 if (!blank) {
1140 dev_err(dev, "%s: Missing parameters\n", "new_device");
1141 return -EINVAL;
1142 }
1143 if (blank - buf > I2C_NAME_SIZE - 1) {
1144 dev_err(dev, "%s: Invalid device name\n", "new_device");
1145 return -EINVAL;
1146 }
1147 memcpy(info.type, buf, blank - buf);
1148
1149 /* Parse remaining parameters, reject extra parameters */
1150 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1151 if (res < 1) {
1152 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1153 return -EINVAL;
1154 }
1155 if (res > 1 && end != '\n') {
1156 dev_err(dev, "%s: Extra parameters\n", "new_device");
1157 return -EINVAL;
1158 }
1159
Jean Delvare99cd8e22009-06-19 16:58:20 +02001160 client = i2c_new_device(adap, &info);
1161 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001162 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001163
1164 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001165 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001166 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001167 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001168 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1169 info.type, info.addr);
1170
1171 return count;
1172}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001173static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001174
1175/*
1176 * And of course let the users delete the devices they instantiated, if
1177 * they got it wrong. This interface can only be used to delete devices
1178 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1179 * don't delete devices to which some kernel code still has references.
1180 *
1181 * Parameter checking may look overzealous, but we really don't want
1182 * the user to delete the wrong device.
1183 */
1184static ssize_t
1185i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1186 const char *buf, size_t count)
1187{
1188 struct i2c_adapter *adap = to_i2c_adapter(dev);
1189 struct i2c_client *client, *next;
1190 unsigned short addr;
1191 char end;
1192 int res;
1193
1194 /* Parse parameters, reject extra parameters */
1195 res = sscanf(buf, "%hi%c", &addr, &end);
1196 if (res < 1) {
1197 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1198 return -EINVAL;
1199 }
1200 if (res > 1 && end != '\n') {
1201 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1202 return -EINVAL;
1203 }
1204
1205 /* Make sure the device was added through sysfs */
1206 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001207 mutex_lock_nested(&adap->userspace_clients_lock,
1208 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001209 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1210 detected) {
1211 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001212 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1213 "delete_device", client->name, client->addr);
1214
1215 list_del(&client->detected);
1216 i2c_unregister_device(client);
1217 res = count;
1218 break;
1219 }
1220 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001221 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001222
1223 if (res < 0)
1224 dev_err(dev, "%s: Can't find device in list\n",
1225 "delete_device");
1226 return res;
1227}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001228static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1229 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001230
1231static struct attribute *i2c_adapter_attrs[] = {
1232 &dev_attr_name.attr,
1233 &dev_attr_new_device.attr,
1234 &dev_attr_delete_device.attr,
1235 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001236};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001237ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001238
Michael Lawnick08263742010-08-11 18:21:02 +02001239struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001240 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001241 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001242};
Michael Lawnick08263742010-08-11 18:21:02 +02001243EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Stephen Warren643dd092012-04-17 12:43:33 -06001245/**
1246 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1247 * @dev: device, probably from some driver model iterator
1248 *
1249 * When traversing the driver model tree, perhaps using driver model
1250 * iterators like @device_for_each_child(), you can't assume very much
1251 * about the nodes you find. Use this function to avoid oopses caused
1252 * by wrongly treating some non-I2C device as an i2c_adapter.
1253 */
1254struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1255{
1256 return (dev->type == &i2c_adapter_type)
1257 ? to_i2c_adapter(dev)
1258 : NULL;
1259}
1260EXPORT_SYMBOL(i2c_verify_adapter);
1261
Jean Delvare2bb50952009-09-18 22:45:46 +02001262#ifdef CONFIG_I2C_COMPAT
1263static struct class_compat *i2c_adapter_compat_class;
1264#endif
1265
David Brownell9c1600e2007-05-01 23:26:31 +02001266static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1267{
1268 struct i2c_devinfo *devinfo;
1269
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001270 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001271 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1272 if (devinfo->busnum == adapter->nr
1273 && !i2c_new_device(adapter,
1274 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001275 dev_err(&adapter->dev,
1276 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001277 devinfo->board_info.addr);
1278 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001279 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001280}
1281
Wolfram Sang687b81d2013-07-11 12:56:15 +01001282/* OF support code */
1283
1284#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001285static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1286 struct device_node *node)
1287{
1288 struct i2c_client *result;
1289 struct i2c_board_info info = {};
1290 struct dev_archdata dev_ad = {};
1291 const __be32 *addr;
1292 int len;
1293
1294 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1295
1296 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1297 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1298 node->full_name);
1299 return ERR_PTR(-EINVAL);
1300 }
1301
1302 addr = of_get_property(node, "reg", &len);
Wolfram Sang4c1344f2015-05-19 18:29:48 +02001303 if (!addr || (len < sizeof(*addr))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001304 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1305 node->full_name);
1306 return ERR_PTR(-EINVAL);
1307 }
1308
1309 info.addr = be32_to_cpup(addr);
1310 if (info.addr > (1 << 10) - 1) {
1311 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1312 info.addr, node->full_name);
1313 return ERR_PTR(-EINVAL);
1314 }
1315
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001316 info.of_node = of_node_get(node);
1317 info.archdata = &dev_ad;
1318
1319 if (of_get_property(node, "wakeup-source", NULL))
1320 info.flags |= I2C_CLIENT_WAKE;
1321
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001322 result = i2c_new_device(adap, &info);
1323 if (result == NULL) {
1324 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1325 node->full_name);
1326 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001327 return ERR_PTR(-EINVAL);
1328 }
1329 return result;
1330}
1331
Wolfram Sang687b81d2013-07-11 12:56:15 +01001332static void of_i2c_register_devices(struct i2c_adapter *adap)
1333{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001334 struct device_node *node;
1335
1336 /* Only register child devices if the adapter has a node pointer set */
1337 if (!adap->dev.of_node)
1338 return;
1339
1340 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1341
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001342 for_each_available_child_of_node(adap->dev.of_node, node) {
1343 if (of_node_test_and_set_flag(node, OF_POPULATED))
1344 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001345 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001346 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001347}
1348
1349static int of_dev_node_match(struct device *dev, void *data)
1350{
1351 return dev->of_node == data;
1352}
1353
1354/* must call put_device() when done with returned i2c_client device */
1355struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1356{
1357 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001358 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001359
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001360 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001361 if (!dev)
1362 return NULL;
1363
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001364 client = i2c_verify_client(dev);
1365 if (!client)
1366 put_device(dev);
1367
1368 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001369}
1370EXPORT_SYMBOL(of_find_i2c_device_by_node);
1371
1372/* must call put_device() when done with returned i2c_adapter device */
1373struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1374{
1375 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001376 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001377
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001378 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001379 if (!dev)
1380 return NULL;
1381
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001382 adapter = i2c_verify_adapter(dev);
1383 if (!adapter)
1384 put_device(dev);
1385
1386 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001387}
1388EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001389
1390/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1391struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1392{
1393 struct i2c_adapter *adapter;
1394
1395 adapter = of_find_i2c_adapter_by_node(node);
1396 if (!adapter)
1397 return NULL;
1398
1399 if (!try_module_get(adapter->owner)) {
1400 put_device(&adapter->dev);
1401 adapter = NULL;
1402 }
1403
1404 return adapter;
1405}
1406EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001407#else
1408static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1409#endif /* CONFIG_OF */
1410
Jean Delvare69b00892009-12-06 17:06:27 +01001411static int i2c_do_add_adapter(struct i2c_driver *driver,
1412 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001413{
Jean Delvare4735c982008-07-14 22:38:36 +02001414 /* Detect supported devices on that bus, and instantiate them */
1415 i2c_detect(adap, driver);
1416
1417 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001418 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001419 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1420 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001421 dev_warn(&adap->dev, "Please use another way to instantiate "
1422 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001423 /* We ignore the return code; if it fails, too bad */
1424 driver->attach_adapter(adap);
1425 }
1426 return 0;
1427}
1428
Jean Delvare69b00892009-12-06 17:06:27 +01001429static int __process_new_adapter(struct device_driver *d, void *data)
1430{
1431 return i2c_do_add_adapter(to_i2c_driver(d), data);
1432}
1433
David Brownell6e13e642007-05-01 23:26:31 +02001434static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Jean Delvared6703282010-08-11 18:20:59 +02001436 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
David Brownell1d0b19c2008-10-14 17:30:05 +02001438 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001439 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1440 res = -EAGAIN;
1441 goto out_list;
1442 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001443
Jean Delvare2236baa2010-11-15 22:40:38 +01001444 /* Sanity checks */
1445 if (unlikely(adap->name[0] == '\0')) {
1446 pr_err("i2c-core: Attempt to register an adapter with "
1447 "no name!\n");
1448 return -EINVAL;
1449 }
1450 if (unlikely(!adap->algo)) {
1451 pr_err("i2c-core: Attempt to register adapter '%s' with "
1452 "no algo!\n", adap->name);
1453 return -EINVAL;
1454 }
1455
Mika Kuoppala194684e2009-12-06 17:06:22 +01001456 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001457 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001458 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jean Delvare8fcfef62009-03-28 21:34:43 +01001460 /* Set default timeout to 1 second if not already set */
1461 if (adap->timeout == 0)
1462 adap->timeout = HZ;
1463
Kay Sievers27d9c182009-01-07 14:29:16 +01001464 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001465 adap->dev.bus = &i2c_bus_type;
1466 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001467 res = device_register(&adap->dev);
1468 if (res)
1469 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001471 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1472
Charles Keepax6ada5c12015-04-16 13:05:19 +01001473 pm_runtime_no_callbacks(&adap->dev);
1474
Jean Delvare2bb50952009-09-18 22:45:46 +02001475#ifdef CONFIG_I2C_COMPAT
1476 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1477 adap->dev.parent);
1478 if (res)
1479 dev_warn(&adap->dev,
1480 "Failed to create compatibility class link\n");
1481#endif
1482
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301483 /* bus recovery specific initialization */
1484 if (adap->bus_recovery_info) {
1485 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1486
1487 if (!bri->recover_bus) {
1488 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1489 adap->bus_recovery_info = NULL;
1490 goto exit_recovery;
1491 }
1492
1493 /* Generic GPIO recovery */
1494 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1495 if (!gpio_is_valid(bri->scl_gpio)) {
1496 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1497 adap->bus_recovery_info = NULL;
1498 goto exit_recovery;
1499 }
1500
1501 if (gpio_is_valid(bri->sda_gpio))
1502 bri->get_sda = get_sda_gpio_value;
1503 else
1504 bri->get_sda = NULL;
1505
1506 bri->get_scl = get_scl_gpio_value;
1507 bri->set_scl = set_scl_gpio_value;
1508 } else if (!bri->set_scl || !bri->get_scl) {
1509 /* Generic SCL recovery */
1510 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1511 adap->bus_recovery_info = NULL;
1512 }
1513 }
1514
1515exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001516 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001517 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001518 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001519 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001520
David Brownell6e13e642007-05-01 23:26:31 +02001521 if (adap->nr < __i2c_first_dynamic_bus_num)
1522 i2c_scan_static_board_info(adap);
1523
Jean Delvare4735c982008-07-14 22:38:36 +02001524 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001525 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001526 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001527 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001528
1529 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001530
Jean Delvareb119c6c2006-08-15 18:26:30 +02001531out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001532 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001533 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001534 mutex_unlock(&core_lock);
1535 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
David Brownell6e13e642007-05-01 23:26:31 +02001538/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001539 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1540 * @adap: the adapter to register (with adap->nr initialized)
1541 * Context: can sleep
1542 *
1543 * See i2c_add_numbered_adapter() for details.
1544 */
1545static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1546{
1547 int id;
1548
1549 mutex_lock(&core_lock);
1550 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1551 GFP_KERNEL);
1552 mutex_unlock(&core_lock);
1553 if (id < 0)
1554 return id == -ENOSPC ? -EBUSY : id;
1555
1556 return i2c_register_adapter(adap);
1557}
1558
1559/**
David Brownell6e13e642007-05-01 23:26:31 +02001560 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1561 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001562 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001563 *
1564 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001565 * doesn't matter or when its bus number is specified by an dt alias.
1566 * Examples of bases when the bus number doesn't matter: I2C adapters
1567 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001568 *
1569 * When this returns zero, a new bus number was allocated and stored
1570 * in adap->nr, and the specified adapter became available for clients.
1571 * Otherwise, a negative errno value is returned.
1572 */
1573int i2c_add_adapter(struct i2c_adapter *adapter)
1574{
Doug Andersonee5c2742013-03-01 08:57:31 -08001575 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001576 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001577
Doug Andersonee5c2742013-03-01 08:57:31 -08001578 if (dev->of_node) {
1579 id = of_alias_get_id(dev->of_node, "i2c");
1580 if (id >= 0) {
1581 adapter->nr = id;
1582 return __i2c_add_numbered_adapter(adapter);
1583 }
1584 }
1585
Jean Delvarecaada322008-01-27 18:14:49 +01001586 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001587 id = idr_alloc(&i2c_adapter_idr, adapter,
1588 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001589 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001590 if (id < 0)
1591 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001592
1593 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001594
David Brownell6e13e642007-05-01 23:26:31 +02001595 return i2c_register_adapter(adapter);
1596}
1597EXPORT_SYMBOL(i2c_add_adapter);
1598
1599/**
1600 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1601 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001602 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001603 *
1604 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001605 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1606 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001607 * is used to properly configure I2C devices.
1608 *
Grant Likely488bf312011-07-25 17:49:43 +02001609 * If the requested bus number is set to -1, then this function will behave
1610 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1611 *
David Brownell6e13e642007-05-01 23:26:31 +02001612 * If no devices have pre-been declared for this bus, then be sure to
1613 * register the adapter before any dynamically allocated ones. Otherwise
1614 * the required bus ID may not be available.
1615 *
1616 * When this returns zero, the specified adapter became available for
1617 * clients using the bus number provided in adap->nr. Also, the table
1618 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1619 * and the appropriate driver model device nodes are created. Otherwise, a
1620 * negative errno value is returned.
1621 */
1622int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1623{
Grant Likely488bf312011-07-25 17:49:43 +02001624 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1625 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001626
Doug Andersonee5c2742013-03-01 08:57:31 -08001627 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001628}
1629EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1630
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001631static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001632 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001633{
Jean Delvare4735c982008-07-14 22:38:36 +02001634 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001635
Jean Delvareacec2112009-03-28 21:34:40 +01001636 /* Remove the devices we created ourselves as the result of hardware
1637 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001638 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1639 if (client->adapter == adapter) {
1640 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1641 client->name, client->addr);
1642 list_del(&client->detected);
1643 i2c_unregister_device(client);
1644 }
1645 }
Jean Delvare026526f2008-01-27 18:14:49 +01001646}
1647
Jean Delvaree549c2b2009-06-19 16:58:19 +02001648static int __unregister_client(struct device *dev, void *dummy)
1649{
1650 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001651 if (client && strcmp(client->name, "dummy"))
1652 i2c_unregister_device(client);
1653 return 0;
1654}
1655
1656static int __unregister_dummy(struct device *dev, void *dummy)
1657{
1658 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001659 if (client)
1660 i2c_unregister_device(client);
1661 return 0;
1662}
1663
Jean Delvare69b00892009-12-06 17:06:27 +01001664static int __process_removed_adapter(struct device_driver *d, void *data)
1665{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001666 i2c_do_del_adapter(to_i2c_driver(d), data);
1667 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001668}
1669
David Brownelld64f73b2007-07-12 14:12:28 +02001670/**
1671 * i2c_del_adapter - unregister I2C adapter
1672 * @adap: the adapter being unregistered
1673 * Context: can sleep
1674 *
1675 * This unregisters an I2C adapter which was previously registered
1676 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1677 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001678void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001680 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001681 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001684 mutex_lock(&core_lock);
1685 found = idr_find(&i2c_adapter_idr, adap->nr);
1686 mutex_unlock(&core_lock);
1687 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001688 pr_debug("i2c-core: attempting to delete unregistered "
1689 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001690 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
Lan Tianyu5d98e612014-05-20 20:59:23 +08001693 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001694 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001695 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001696 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001697 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001698 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001700 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001701 mutex_lock_nested(&adap->userspace_clients_lock,
1702 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001703 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1704 detected) {
1705 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1706 client->addr);
1707 list_del(&client->detected);
1708 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001709 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001710 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001711
Jean Delvaree549c2b2009-06-19 16:58:19 +02001712 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001713 * check the returned value. This is a two-pass process, because
1714 * we can't remove the dummy devices during the first pass: they
1715 * could have been instantiated by real devices wishing to clean
1716 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001717 device_for_each_child(&adap->dev, NULL, __unregister_client);
1718 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jean Delvare2bb50952009-09-18 22:45:46 +02001720#ifdef CONFIG_I2C_COMPAT
1721 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1722 adap->dev.parent);
1723#endif
1724
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001725 /* device name is gone after device_unregister */
1726 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1727
Wolfram Sang26680ee2015-01-29 22:45:09 +01001728 /* wait until all references to the device are gone
1729 *
1730 * FIXME: This is old code and should ideally be replaced by an
1731 * alternative which results in decoupling the lifetime of the struct
1732 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301733 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001734 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
David Brownell6e13e642007-05-01 23:26:31 +02001739 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001740 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001742 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001744 /* Clear the device structure in case this adapter is ever going to be
1745 added again */
1746 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
David Brownellc0564602007-05-01 23:26:31 +02001748EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
David Brownell7b4fbc52007-05-01 23:26:30 +02001750/* ------------------------------------------------------------------------- */
1751
Jean Delvare7ae31482011-03-20 14:50:52 +01001752int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1753{
1754 int res;
1755
1756 mutex_lock(&core_lock);
1757 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1758 mutex_unlock(&core_lock);
1759
1760 return res;
1761}
1762EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1763
Jean Delvare69b00892009-12-06 17:06:27 +01001764static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001765{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001766 if (dev->type != &i2c_adapter_type)
1767 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001768 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001769}
1770
David Brownell7b4fbc52007-05-01 23:26:30 +02001771/*
1772 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001773 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
1775
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001776int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001778 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
David Brownell1d0b19c2008-10-14 17:30:05 +02001780 /* Can't register until after driver model init */
1781 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1782 return -EAGAIN;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001785 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Jean Delvare729d6dd2009-06-19 16:58:18 +02001788 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001789 * will have called probe() for all matching-but-unbound devices.
1790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 res = driver_register(&driver->driver);
1792 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001793 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001794
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001795 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jean Delvare4735c982008-07-14 22:38:36 +02001797 INIT_LIST_HEAD(&driver->clients);
1798 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001799 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001800
Jean Delvare7eebcb72006-02-05 23:28:21 +01001801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001803EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jean Delvare69b00892009-12-06 17:06:27 +01001805static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001806{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001807 if (dev->type == &i2c_adapter_type)
1808 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1809 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001810}
1811
David Brownella1d9e6e2007-05-01 23:26:30 +02001812/**
1813 * i2c_del_driver - unregister I2C driver
1814 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001815 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001816 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001817void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Jean Delvare7ae31482011-03-20 14:50:52 +01001819 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001822 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
David Brownellc0564602007-05-01 23:26:31 +02001824EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
David Brownell7b4fbc52007-05-01 23:26:30 +02001826/* ------------------------------------------------------------------------- */
1827
Jean Delvaree48d3312008-01-27 18:14:48 +01001828/**
1829 * i2c_use_client - increments the reference count of the i2c client structure
1830 * @client: the client being referenced
1831 *
1832 * Each live reference to a client should be refcounted. The driver model does
1833 * that automatically as part of driver binding, so that most drivers don't
1834 * need to do this explicitly: they hold a reference until they're unbound
1835 * from the device.
1836 *
1837 * A pointer to the client with the incremented reference counter is returned.
1838 */
1839struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
David Brownell6ea438e2008-07-14 22:38:24 +02001841 if (client && get_device(&client->dev))
1842 return client;
1843 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
David Brownellc0564602007-05-01 23:26:31 +02001845EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Jean Delvaree48d3312008-01-27 18:14:48 +01001847/**
1848 * i2c_release_client - release a use of the i2c client structure
1849 * @client: the client being no longer referenced
1850 *
1851 * Must be called when a user of a client is finished with it.
1852 */
1853void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
David Brownell6ea438e2008-07-14 22:38:24 +02001855 if (client)
1856 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
David Brownellc0564602007-05-01 23:26:31 +02001858EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
David Brownell9b766b82008-01-27 18:14:51 +01001860struct i2c_cmd_arg {
1861 unsigned cmd;
1862 void *arg;
1863};
1864
1865static int i2c_cmd(struct device *dev, void *_arg)
1866{
1867 struct i2c_client *client = i2c_verify_client(dev);
1868 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001869 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001870
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001871 if (!client || !client->dev.driver)
1872 return 0;
1873
1874 driver = to_i2c_driver(client->dev.driver);
1875 if (driver->command)
1876 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001877 return 0;
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1881{
David Brownell9b766b82008-01-27 18:14:51 +01001882 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
David Brownell9b766b82008-01-27 18:14:51 +01001884 cmd_arg.cmd = cmd;
1885 cmd_arg.arg = arg;
1886 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
David Brownellc0564602007-05-01 23:26:31 +02001888EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001890#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1891static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1892 void *arg)
1893{
1894 struct of_reconfig_data *rd = arg;
1895 struct i2c_adapter *adap;
1896 struct i2c_client *client;
1897
1898 switch (of_reconfig_get_state_change(action, rd)) {
1899 case OF_RECONFIG_CHANGE_ADD:
1900 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1901 if (adap == NULL)
1902 return NOTIFY_OK; /* not for us */
1903
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001904 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1905 put_device(&adap->dev);
1906 return NOTIFY_OK;
1907 }
1908
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001909 client = of_i2c_register_device(adap, rd->dn);
1910 put_device(&adap->dev);
1911
1912 if (IS_ERR(client)) {
1913 pr_err("%s: failed to create for '%s'\n",
1914 __func__, rd->dn->full_name);
1915 return notifier_from_errno(PTR_ERR(client));
1916 }
1917 break;
1918 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001919 /* already depopulated? */
1920 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1921 return NOTIFY_OK;
1922
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001923 /* find our device by node */
1924 client = of_find_i2c_device_by_node(rd->dn);
1925 if (client == NULL)
1926 return NOTIFY_OK; /* no? not meant for us */
1927
1928 /* unregister takes one ref away */
1929 i2c_unregister_device(client);
1930
1931 /* and put the reference of the find */
1932 put_device(&client->dev);
1933 break;
1934 }
1935
1936 return NOTIFY_OK;
1937}
1938static struct notifier_block i2c_of_notifier = {
1939 .notifier_call = of_i2c_notify,
1940};
1941#else
1942extern struct notifier_block i2c_of_notifier;
1943#endif /* CONFIG_OF_DYNAMIC */
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945static int __init i2c_init(void)
1946{
1947 int retval;
1948
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001949 retval = of_alias_get_highest_id("i2c");
1950
1951 down_write(&__i2c_board_lock);
1952 if (retval >= __i2c_first_dynamic_bus_num)
1953 __i2c_first_dynamic_bus_num = retval + 1;
1954 up_write(&__i2c_board_lock);
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 retval = bus_register(&i2c_bus_type);
1957 if (retval)
1958 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001959#ifdef CONFIG_I2C_COMPAT
1960 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1961 if (!i2c_adapter_compat_class) {
1962 retval = -ENOMEM;
1963 goto bus_err;
1964 }
1965#endif
David Brownelle9f13732008-01-27 18:14:52 +01001966 retval = i2c_add_driver(&dummy_driver);
1967 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001968 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001969
1970 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1971 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1972
David Brownelle9f13732008-01-27 18:14:52 +01001973 return 0;
1974
Jean Delvare2bb50952009-09-18 22:45:46 +02001975class_err:
1976#ifdef CONFIG_I2C_COMPAT
1977 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001978bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001979#endif
David Brownelle9f13732008-01-27 18:14:52 +01001980 bus_unregister(&i2c_bus_type);
1981 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984static void __exit i2c_exit(void)
1985{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001986 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1987 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001988 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001989#ifdef CONFIG_I2C_COMPAT
1990 class_compat_unregister(i2c_adapter_compat_class);
1991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001993 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
David Brownella10f9e72008-10-14 17:30:06 +02001996/* We must initialize early, because some subsystems register i2c drivers
1997 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1998 */
1999postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000module_exit(i2c_exit);
2001
2002/* ----------------------------------------------------
2003 * the functional interface to the i2c busses.
2004 * ----------------------------------------------------
2005 */
2006
Wolfram Sangb7f62582015-01-05 23:45:59 +01002007/* Check if val is exceeding the quirk IFF quirk is non 0 */
2008#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2009
2010static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2011{
2012 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2013 err_msg, msg->addr, msg->len,
2014 msg->flags & I2C_M_RD ? "read" : "write");
2015 return -EOPNOTSUPP;
2016}
2017
2018static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2019{
2020 const struct i2c_adapter_quirks *q = adap->quirks;
2021 int max_num = q->max_num_msgs, i;
2022 bool do_len_check = true;
2023
2024 if (q->flags & I2C_AQ_COMB) {
2025 max_num = 2;
2026
2027 /* special checks for combined messages */
2028 if (num == 2) {
2029 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2030 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2031
2032 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2033 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2034
2035 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2036 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2037
2038 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2039 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2040
2041 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2042 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2043
2044 do_len_check = false;
2045 }
2046 }
2047
2048 if (i2c_quirk_exceeded(num, max_num))
2049 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2050
2051 for (i = 0; i < num; i++) {
2052 u16 len = msgs[i].len;
2053
2054 if (msgs[i].flags & I2C_M_RD) {
2055 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2056 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2057 } else {
2058 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2059 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2060 }
2061 }
2062
2063 return 0;
2064}
2065
David Brownella1cdeda2008-07-14 22:38:24 +02002066/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002067 * __i2c_transfer - unlocked flavor of i2c_transfer
2068 * @adap: Handle to I2C bus
2069 * @msgs: One or more messages to execute before STOP is issued to
2070 * terminate the operation; each message begins with a START.
2071 * @num: Number of messages to be executed.
2072 *
2073 * Returns negative errno, else the number of messages executed.
2074 *
2075 * Adapter lock must be held when calling this function. No debug logging
2076 * takes place. adap->algo->master_xfer existence isn't checked.
2077 */
2078int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2079{
2080 unsigned long orig_jiffies;
2081 int ret, try;
2082
Wolfram Sangb7f62582015-01-05 23:45:59 +01002083 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2084 return -EOPNOTSUPP;
2085
David Howellsd9a83d62014-03-06 13:35:59 +00002086 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2087 * enabled. This is an efficient way of keeping the for-loop from
2088 * being executed when not needed.
2089 */
2090 if (static_key_false(&i2c_trace_msg)) {
2091 int i;
2092 for (i = 0; i < num; i++)
2093 if (msgs[i].flags & I2C_M_RD)
2094 trace_i2c_read(adap, &msgs[i], i);
2095 else
2096 trace_i2c_write(adap, &msgs[i], i);
2097 }
2098
Jean Delvareb37d2a32012-06-29 07:47:19 -03002099 /* Retry automatically on arbitration loss */
2100 orig_jiffies = jiffies;
2101 for (ret = 0, try = 0; try <= adap->retries; try++) {
2102 ret = adap->algo->master_xfer(adap, msgs, num);
2103 if (ret != -EAGAIN)
2104 break;
2105 if (time_after(jiffies, orig_jiffies + adap->timeout))
2106 break;
2107 }
2108
David Howellsd9a83d62014-03-06 13:35:59 +00002109 if (static_key_false(&i2c_trace_msg)) {
2110 int i;
2111 for (i = 0; i < ret; i++)
2112 if (msgs[i].flags & I2C_M_RD)
2113 trace_i2c_reply(adap, &msgs[i], i);
2114 trace_i2c_result(adap, i, ret);
2115 }
2116
Jean Delvareb37d2a32012-06-29 07:47:19 -03002117 return ret;
2118}
2119EXPORT_SYMBOL(__i2c_transfer);
2120
2121/**
David Brownella1cdeda2008-07-14 22:38:24 +02002122 * i2c_transfer - execute a single or combined I2C message
2123 * @adap: Handle to I2C bus
2124 * @msgs: One or more messages to execute before STOP is issued to
2125 * terminate the operation; each message begins with a START.
2126 * @num: Number of messages to be executed.
2127 *
2128 * Returns negative errno, else the number of messages executed.
2129 *
2130 * Note that there is no requirement that each message be sent to
2131 * the same slave address, although that is the most common model.
2132 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002133int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002135 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
David Brownella1cdeda2008-07-14 22:38:24 +02002137 /* REVISIT the fault reporting model here is weak:
2138 *
2139 * - When we get an error after receiving N bytes from a slave,
2140 * there is no way to report "N".
2141 *
2142 * - When we get a NAK after transmitting N bytes to a slave,
2143 * there is no way to report "N" ... or to let the master
2144 * continue executing the rest of this combined message, if
2145 * that's the appropriate response.
2146 *
2147 * - When for example "num" is two and we successfully complete
2148 * the first message but get an error part way through the
2149 * second, it's unclear whether that should be reported as
2150 * one (discarding status on the second message) or errno
2151 * (discarding status on the first one).
2152 */
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (adap->algo->master_xfer) {
2155#ifdef DEBUG
2156 for (ret = 0; ret < num; ret++) {
2157 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002158 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2159 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2160 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162#endif
2163
Mike Rapoportcea443a82008-01-27 18:14:50 +01002164 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002165 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002166 if (!ret)
2167 /* I2C activity is ongoing. */
2168 return -EAGAIN;
2169 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002170 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002171 }
2172
Jean Delvareb37d2a32012-06-29 07:47:19 -03002173 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002174 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 return ret;
2177 } else {
2178 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002179 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181}
David Brownellc0564602007-05-01 23:26:31 +02002182EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
David Brownella1cdeda2008-07-14 22:38:24 +02002184/**
2185 * i2c_master_send - issue a single I2C message in master transmit mode
2186 * @client: Handle to slave device
2187 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002188 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002189 *
2190 * Returns negative errno, or else the number of bytes written.
2191 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002192int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002195 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 struct i2c_msg msg;
2197
Jean Delvare815f55f2005-05-07 22:58:46 +02002198 msg.addr = client->addr;
2199 msg.flags = client->flags & I2C_M_TEN;
2200 msg.len = count;
2201 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002202
Jean Delvare815f55f2005-05-07 22:58:46 +02002203 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002205 /*
2206 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2207 * transmitted, else error code.
2208 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002209 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
David Brownellc0564602007-05-01 23:26:31 +02002211EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
David Brownella1cdeda2008-07-14 22:38:24 +02002213/**
2214 * i2c_master_recv - issue a single I2C message in master receive mode
2215 * @client: Handle to slave device
2216 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002217 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002218 *
2219 * Returns negative errno, or else the number of bytes read.
2220 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002221int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Farid Hammane7225acf2010-05-21 18:40:58 +02002223 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 struct i2c_msg msg;
2225 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jean Delvare815f55f2005-05-07 22:58:46 +02002227 msg.addr = client->addr;
2228 msg.flags = client->flags & I2C_M_TEN;
2229 msg.flags |= I2C_M_RD;
2230 msg.len = count;
2231 msg.buf = buf;
2232
2233 ret = i2c_transfer(adap, &msg, 1);
2234
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002235 /*
2236 * If everything went ok (i.e. 1 msg received), return #bytes received,
2237 * else error code.
2238 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002239 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
David Brownellc0564602007-05-01 23:26:31 +02002241EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/* ----------------------------------------------------
2244 * the i2c address scanning function
2245 * Will not work for 10-bit addresses!
2246 * ----------------------------------------------------
2247 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002248
Jean Delvare63e4e802010-06-03 11:33:51 +02002249/*
2250 * Legacy default probe function, mostly relevant for SMBus. The default
2251 * probe method is a quick write, but it is known to corrupt the 24RF08
2252 * EEPROMs due to a state machine bug, and could also irreversibly
2253 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2254 * we use a short byte read instead. Also, some bus drivers don't implement
2255 * quick write, so we fallback to a byte read in that case too.
2256 * On x86, there is another special case for FSC hardware monitoring chips,
2257 * which want regular byte reads (address 0x73.) Fortunately, these are the
2258 * only known chips using this I2C address on PC hardware.
2259 * Returns 1 if probe succeeded, 0 if not.
2260 */
2261static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2262{
2263 int err;
2264 union i2c_smbus_data dummy;
2265
2266#ifdef CONFIG_X86
2267 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2268 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2269 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2270 I2C_SMBUS_BYTE_DATA, &dummy);
2271 else
2272#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002273 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2274 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002275 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2276 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002277 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2278 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2279 I2C_SMBUS_BYTE, &dummy);
2280 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002281 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2282 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002283 err = -EOPNOTSUPP;
2284 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002285
2286 return err >= 0;
2287}
2288
Jean Delvareccfbbd02009-12-06 17:06:25 +01002289static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002290 struct i2c_driver *driver)
2291{
2292 struct i2c_board_info info;
2293 struct i2c_adapter *adapter = temp_client->adapter;
2294 int addr = temp_client->addr;
2295 int err;
2296
2297 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002298 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002299 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002300 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2301 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002302 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002303 }
2304
2305 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002306 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002307 return 0;
2308
Jean Delvareccfbbd02009-12-06 17:06:25 +01002309 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002310 if (!i2c_default_probe(adapter, addr))
2311 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002312
2313 /* Finally call the custom detection function */
2314 memset(&info, 0, sizeof(struct i2c_board_info));
2315 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002316 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002317 if (err) {
2318 /* -ENODEV is returned if the detection fails. We catch it
2319 here as this isn't an error. */
2320 return err == -ENODEV ? 0 : err;
2321 }
2322
2323 /* Consistency check */
2324 if (info.type[0] == '\0') {
2325 dev_err(&adapter->dev, "%s detection function provided "
2326 "no name for 0x%x\n", driver->driver.name,
2327 addr);
2328 } else {
2329 struct i2c_client *client;
2330
2331 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002332 if (adapter->class & I2C_CLASS_DEPRECATED)
2333 dev_warn(&adapter->dev,
2334 "This adapter will soon drop class based instantiation of devices. "
2335 "Please make sure client 0x%02x gets instantiated by other means. "
2336 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2337 info.addr);
2338
Jean Delvare4735c982008-07-14 22:38:36 +02002339 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2340 info.type, info.addr);
2341 client = i2c_new_device(adapter, &info);
2342 if (client)
2343 list_add_tail(&client->detected, &driver->clients);
2344 else
2345 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2346 info.type, info.addr);
2347 }
2348 return 0;
2349}
2350
2351static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2352{
Jean Delvarec3813d62009-12-14 21:17:25 +01002353 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002354 struct i2c_client *temp_client;
2355 int i, err = 0;
2356 int adap_id = i2c_adapter_id(adapter);
2357
Jean Delvarec3813d62009-12-14 21:17:25 +01002358 address_list = driver->address_list;
2359 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002360 return 0;
2361
Wolfram Sang45552272014-07-10 13:46:21 +02002362 /* Warn that the adapter lost class based instantiation */
2363 if (adapter->class == I2C_CLASS_DEPRECATED) {
2364 dev_dbg(&adapter->dev,
2365 "This adapter dropped support for I2C classes and "
2366 "won't auto-detect %s devices anymore. If you need it, check "
2367 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2368 driver->driver.name);
2369 return 0;
2370 }
2371
Jean Delvare51b54ba2010-10-24 18:16:58 +02002372 /* Stop here if the classes do not match */
2373 if (!(adapter->class & driver->class))
2374 return 0;
2375
Jean Delvare4735c982008-07-14 22:38:36 +02002376 /* Set up a temporary client to help detect callback */
2377 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2378 if (!temp_client)
2379 return -ENOMEM;
2380 temp_client->adapter = adapter;
2381
Jean Delvarec3813d62009-12-14 21:17:25 +01002382 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002383 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002384 "addr 0x%02x\n", adap_id, address_list[i]);
2385 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002386 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002387 if (unlikely(err))
2388 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002389 }
2390
Jean Delvare4735c982008-07-14 22:38:36 +02002391 kfree(temp_client);
2392 return err;
2393}
2394
Jean Delvared44f19d2010-08-11 18:20:57 +02002395int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2396{
2397 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2398 I2C_SMBUS_QUICK, NULL) >= 0;
2399}
2400EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2401
Jean Delvare12b5053a2007-05-01 23:26:31 +02002402struct i2c_client *
2403i2c_new_probed_device(struct i2c_adapter *adap,
2404 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002405 unsigned short const *addr_list,
2406 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002407{
2408 int i;
2409
Jean Delvare8031d792010-08-11 18:21:00 +02002410 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002411 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002412
Jean Delvare12b5053a2007-05-01 23:26:31 +02002413 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2414 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002415 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002416 dev_warn(&adap->dev, "Invalid 7-bit address "
2417 "0x%02x\n", addr_list[i]);
2418 continue;
2419 }
2420
2421 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002422 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002423 dev_dbg(&adap->dev, "Address 0x%02x already in "
2424 "use, not probing\n", addr_list[i]);
2425 continue;
2426 }
2427
Jean Delvare63e4e802010-06-03 11:33:51 +02002428 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002429 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002430 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002431 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002432
2433 if (addr_list[i] == I2C_CLIENT_END) {
2434 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2435 return NULL;
2436 }
2437
2438 info->addr = addr_list[i];
2439 return i2c_new_device(adap, info);
2440}
2441EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2442
Jean Delvared735b342011-03-20 14:50:52 +01002443struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002446
Jean Delvarecaada322008-01-27 18:14:49 +01002447 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002448 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002449 if (!adapter)
2450 goto exit;
2451
2452 if (try_module_get(adapter->owner))
2453 get_device(&adapter->dev);
2454 else
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002455 adapter = NULL;
2456
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002457 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002458 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002459 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
David Brownellc0564602007-05-01 23:26:31 +02002461EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463void i2c_put_adapter(struct i2c_adapter *adap)
2464{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002465 if (!adap)
2466 return;
2467
2468 put_device(&adap->dev);
2469 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
David Brownellc0564602007-05-01 23:26:31 +02002471EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473/* The SMBus parts */
2474
David Brownell438d6c22006-12-10 21:21:31 +01002475#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002476static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002479
Farid Hammane7225acf2010-05-21 18:40:58 +02002480 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002481 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 data = data ^ POLY;
2483 data = data << 1;
2484 }
2485 return (u8)(data >> 8);
2486}
2487
Jean Delvare421ef472005-10-26 21:28:55 +02002488/* Incremental CRC8 over count bytes in the array pointed to by p */
2489static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 int i;
2492
Farid Hammane7225acf2010-05-21 18:40:58 +02002493 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002494 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return crc;
2496}
2497
Jean Delvare421ef472005-10-26 21:28:55 +02002498/* Assume a 7-bit address, which is reasonable for SMBus */
2499static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Jean Delvare421ef472005-10-26 21:28:55 +02002501 /* The address will be sent first */
2502 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2503 pec = i2c_smbus_pec(pec, &addr, 1);
2504
2505 /* The data buffer follows */
2506 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Jean Delvare421ef472005-10-26 21:28:55 +02002509/* Used for write only transactions */
2510static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Jean Delvare421ef472005-10-26 21:28:55 +02002512 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2513 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Jean Delvare421ef472005-10-26 21:28:55 +02002516/* Return <0 on CRC error
2517 If there was a write before this read (most cases) we need to take the
2518 partial CRC from the write part into account.
2519 Note that this function does modify the message (we need to decrease the
2520 message length to hide the CRC byte from the caller). */
2521static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Jean Delvare421ef472005-10-26 21:28:55 +02002523 u8 rpec = msg->buf[--msg->len];
2524 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (rpec != cpec) {
2527 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2528 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002529 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
David Brownell438d6c22006-12-10 21:21:31 +01002531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
David Brownella1cdeda2008-07-14 22:38:24 +02002534/**
2535 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2536 * @client: Handle to slave device
2537 *
2538 * This executes the SMBus "receive byte" protocol, returning negative errno
2539 * else the byte received from the device.
2540 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002541s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002544 int status;
2545
2546 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2547 I2C_SMBUS_READ, 0,
2548 I2C_SMBUS_BYTE, &data);
2549 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
David Brownellc0564602007-05-01 23:26:31 +02002551EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
David Brownella1cdeda2008-07-14 22:38:24 +02002553/**
2554 * i2c_smbus_write_byte - SMBus "send byte" protocol
2555 * @client: Handle to slave device
2556 * @value: Byte to be sent
2557 *
2558 * This executes the SMBus "send byte" protocol, returning negative errno
2559 * else zero on success.
2560 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002561s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Farid Hammane7225acf2010-05-21 18:40:58 +02002563 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002564 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
David Brownellc0564602007-05-01 23:26:31 +02002566EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
David Brownella1cdeda2008-07-14 22:38:24 +02002568/**
2569 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2570 * @client: Handle to slave device
2571 * @command: Byte interpreted by slave
2572 *
2573 * This executes the SMBus "read byte" protocol, returning negative errno
2574 * else a data byte received from the device.
2575 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002576s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002579 int status;
2580
2581 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2582 I2C_SMBUS_READ, command,
2583 I2C_SMBUS_BYTE_DATA, &data);
2584 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
David Brownellc0564602007-05-01 23:26:31 +02002586EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
David Brownella1cdeda2008-07-14 22:38:24 +02002588/**
2589 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2590 * @client: Handle to slave device
2591 * @command: Byte interpreted by slave
2592 * @value: Byte being written
2593 *
2594 * This executes the SMBus "write byte" protocol, returning negative errno
2595 * else zero on success.
2596 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002597s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2598 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 union i2c_smbus_data data;
2601 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002602 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2603 I2C_SMBUS_WRITE, command,
2604 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
David Brownellc0564602007-05-01 23:26:31 +02002606EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
David Brownella1cdeda2008-07-14 22:38:24 +02002608/**
2609 * i2c_smbus_read_word_data - SMBus "read word" protocol
2610 * @client: Handle to slave device
2611 * @command: Byte interpreted by slave
2612 *
2613 * This executes the SMBus "read word" protocol, returning negative errno
2614 * else a 16-bit unsigned "word" received from the device.
2615 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002616s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002619 int status;
2620
2621 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2622 I2C_SMBUS_READ, command,
2623 I2C_SMBUS_WORD_DATA, &data);
2624 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
David Brownellc0564602007-05-01 23:26:31 +02002626EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
David Brownella1cdeda2008-07-14 22:38:24 +02002628/**
2629 * i2c_smbus_write_word_data - SMBus "write word" protocol
2630 * @client: Handle to slave device
2631 * @command: Byte interpreted by slave
2632 * @value: 16-bit "word" being written
2633 *
2634 * This executes the SMBus "write word" protocol, returning negative errno
2635 * else zero on success.
2636 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002637s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2638 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 union i2c_smbus_data data;
2641 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002642 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2643 I2C_SMBUS_WRITE, command,
2644 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
David Brownellc0564602007-05-01 23:26:31 +02002646EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
David Brownella64ec072007-10-13 23:56:31 +02002648/**
David Brownella1cdeda2008-07-14 22:38:24 +02002649 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002650 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002651 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002652 * @values: Byte array into which data will be read; big enough to hold
2653 * the data returned by the slave. SMBus allows at most 32 bytes.
2654 *
David Brownella1cdeda2008-07-14 22:38:24 +02002655 * This executes the SMBus "block read" protocol, returning negative errno
2656 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002657 *
2658 * Note that using this function requires that the client's adapter support
2659 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2660 * support this; its emulation through I2C messaging relies on a specific
2661 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2662 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002663s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002664 u8 *values)
2665{
2666 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002667 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002668
David Brownell24a5bb72008-07-14 22:38:23 +02002669 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2670 I2C_SMBUS_READ, command,
2671 I2C_SMBUS_BLOCK_DATA, &data);
2672 if (status)
2673 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002674
2675 memcpy(values, &data.block[1], data.block[0]);
2676 return data.block[0];
2677}
2678EXPORT_SYMBOL(i2c_smbus_read_block_data);
2679
David Brownella1cdeda2008-07-14 22:38:24 +02002680/**
2681 * i2c_smbus_write_block_data - SMBus "block write" protocol
2682 * @client: Handle to slave device
2683 * @command: Byte interpreted by slave
2684 * @length: Size of data block; SMBus allows at most 32 bytes
2685 * @values: Byte array which will be written.
2686 *
2687 * This executes the SMBus "block write" protocol, returning negative errno
2688 * else zero on success.
2689 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002690s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002691 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (length > I2C_SMBUS_BLOCK_MAX)
2696 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002698 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002699 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2700 I2C_SMBUS_WRITE, command,
2701 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
David Brownellc0564602007-05-01 23:26:31 +02002703EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002706s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002707 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
2709 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002710 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002711
Jean Delvare4b2643d2007-07-12 14:12:29 +02002712 if (length > I2C_SMBUS_BLOCK_MAX)
2713 length = I2C_SMBUS_BLOCK_MAX;
2714 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002715 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2716 I2C_SMBUS_READ, command,
2717 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2718 if (status < 0)
2719 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002720
2721 memcpy(values, &data.block[1], data.block[0]);
2722 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
David Brownellc0564602007-05-01 23:26:31 +02002724EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Jean Delvare0cc43a12011-01-10 22:11:23 +01002726s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002727 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002728{
2729 union i2c_smbus_data data;
2730
2731 if (length > I2C_SMBUS_BLOCK_MAX)
2732 length = I2C_SMBUS_BLOCK_MAX;
2733 data.block[0] = length;
2734 memcpy(data.block + 1, values, length);
2735 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2736 I2C_SMBUS_WRITE, command,
2737 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2738}
David Brownellc0564602007-05-01 23:26:31 +02002739EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002740
David Brownell438d6c22006-12-10 21:21:31 +01002741/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002743static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2744 unsigned short flags,
2745 char read_write, u8 command, int size,
2746 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 /* So we need to generate a series of msgs. In the case of writing, we
2749 need to use only one message; when reading, we need two. We initialize
2750 most things with sane defaults, to keep the code below somewhat
2751 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002752 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2753 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002754 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002756 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002757 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002758 struct i2c_msg msg[2] = {
2759 {
2760 .addr = addr,
2761 .flags = flags,
2762 .len = 1,
2763 .buf = msgbuf0,
2764 }, {
2765 .addr = addr,
2766 .flags = flags | I2C_M_RD,
2767 .len = 0,
2768 .buf = msgbuf1,
2769 },
2770 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002773 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 case I2C_SMBUS_QUICK:
2775 msg[0].len = 0;
2776 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002777 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2778 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 num = 1;
2780 break;
2781 case I2C_SMBUS_BYTE:
2782 if (read_write == I2C_SMBUS_READ) {
2783 /* Special case: only a read! */
2784 msg[0].flags = I2C_M_RD | flags;
2785 num = 1;
2786 }
2787 break;
2788 case I2C_SMBUS_BYTE_DATA:
2789 if (read_write == I2C_SMBUS_READ)
2790 msg[1].len = 1;
2791 else {
2792 msg[0].len = 2;
2793 msgbuf0[1] = data->byte;
2794 }
2795 break;
2796 case I2C_SMBUS_WORD_DATA:
2797 if (read_write == I2C_SMBUS_READ)
2798 msg[1].len = 2;
2799 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002800 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002802 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 break;
2805 case I2C_SMBUS_PROC_CALL:
2806 num = 2; /* Special case */
2807 read_write = I2C_SMBUS_READ;
2808 msg[0].len = 3;
2809 msg[1].len = 2;
2810 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002811 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 break;
2813 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002815 msg[1].flags |= I2C_M_RECV_LEN;
2816 msg[1].len = 1; /* block length will be added by
2817 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 } else {
2819 msg[0].len = data->block[0] + 2;
2820 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002821 dev_err(&adapter->dev,
2822 "Invalid block write size %d\n",
2823 data->block[0]);
2824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002826 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 msgbuf0[i] = data->block[i-1];
2828 }
2829 break;
2830 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002831 num = 2; /* Another special case */
2832 read_write = I2C_SMBUS_READ;
2833 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002834 dev_err(&adapter->dev,
2835 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002836 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002837 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002838 }
2839 msg[0].len = data->block[0] + 2;
2840 for (i = 1; i < msg[0].len; i++)
2841 msgbuf0[i] = data->block[i-1];
2842 msg[1].flags |= I2C_M_RECV_LEN;
2843 msg[1].len = 1; /* block length will be added by
2844 the underlying bus driver */
2845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 case I2C_SMBUS_I2C_BLOCK_DATA:
2847 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002848 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 } else {
2850 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002851 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002852 dev_err(&adapter->dev,
2853 "Invalid block write size %d\n",
2854 data->block[0]);
2855 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857 for (i = 1; i <= data->block[0]; i++)
2858 msgbuf0[i] = data->block[i];
2859 }
2860 break;
2861 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002862 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2863 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
Jean Delvare421ef472005-10-26 21:28:55 +02002866 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2867 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2868 if (i) {
2869 /* Compute PEC if first message is a write */
2870 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002871 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002872 i2c_smbus_add_pec(&msg[0]);
2873 else /* Write followed by read */
2874 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2875 }
2876 /* Ask for PEC if last message is a read */
2877 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002878 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002879 }
2880
David Brownell24a5bb72008-07-14 22:38:23 +02002881 status = i2c_transfer(adapter, msg, num);
2882 if (status < 0)
2883 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Jean Delvare421ef472005-10-26 21:28:55 +02002885 /* Check PEC if last message is a read */
2886 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002887 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2888 if (status < 0)
2889 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002890 }
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002893 switch (size) {
2894 case I2C_SMBUS_BYTE:
2895 data->byte = msgbuf0[0];
2896 break;
2897 case I2C_SMBUS_BYTE_DATA:
2898 data->byte = msgbuf1[0];
2899 break;
2900 case I2C_SMBUS_WORD_DATA:
2901 case I2C_SMBUS_PROC_CALL:
2902 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2903 break;
2904 case I2C_SMBUS_I2C_BLOCK_DATA:
2905 for (i = 0; i < data->block[0]; i++)
2906 data->block[i+1] = msgbuf1[i];
2907 break;
2908 case I2C_SMBUS_BLOCK_DATA:
2909 case I2C_SMBUS_BLOCK_PROC_CALL:
2910 for (i = 0; i < msgbuf1[0] + 1; i++)
2911 data->block[i] = msgbuf1[i];
2912 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 return 0;
2915}
2916
David Brownella1cdeda2008-07-14 22:38:24 +02002917/**
2918 * i2c_smbus_xfer - execute SMBus protocol operations
2919 * @adapter: Handle to I2C bus
2920 * @addr: Address of SMBus slave on that bus
2921 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2922 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2923 * @command: Byte interpreted by slave, for protocols which use such bytes
2924 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2925 * @data: Data to be read or written
2926 *
2927 * This executes an SMBus protocol operation, and returns a negative
2928 * errno code else zero on success.
2929 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002930s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002931 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002932 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002934 unsigned long orig_jiffies;
2935 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
David Howells8a325992014-03-06 13:36:06 +00002938 /* If enabled, the following two tracepoints are conditional on
2939 * read_write and protocol.
2940 */
2941 trace_smbus_write(adapter, addr, flags, read_write,
2942 command, protocol, data);
2943 trace_smbus_read(adapter, addr, flags, read_write,
2944 command, protocol);
2945
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002946 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002949 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002950
2951 /* Retry automatically on arbitration loss */
2952 orig_jiffies = jiffies;
2953 for (res = 0, try = 0; try <= adapter->retries; try++) {
2954 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2955 read_write, command,
2956 protocol, data);
2957 if (res != -EAGAIN)
2958 break;
2959 if (time_after(jiffies,
2960 orig_jiffies + adapter->timeout))
2961 break;
2962 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002963 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002965 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002966 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002967 /*
2968 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2969 * implement native support for the SMBus operation.
2970 */
2971 }
2972
David Howells8a325992014-03-06 13:36:06 +00002973 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2974 command, protocol, data);
2975
2976trace:
2977 /* If enabled, the reply tracepoint is conditional on read_write. */
2978 trace_smbus_reply(adapter, addr, flags, read_write,
2979 command, protocol, data);
2980 trace_smbus_result(adapter, addr, flags, read_write,
2981 command, protocol, res);
2982
2983 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Jean Delvared5fd1202015-01-26 20:59:31 +01002987#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002988int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2989{
2990 int ret;
2991
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002992 if (!client || !slave_cb) {
2993 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002994 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002995 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002996
2997 if (!(client->flags & I2C_CLIENT_TEN)) {
2998 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02002999 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003000 if (ret) {
3001 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003002 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003003 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003004 }
3005
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003006 if (!client->adapter->algo->reg_slave) {
3007 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003008 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003009 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003010
3011 client->slave_cb = slave_cb;
3012
3013 i2c_lock_adapter(client->adapter);
3014 ret = client->adapter->algo->reg_slave(client);
3015 i2c_unlock_adapter(client->adapter);
3016
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003017 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003018 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003019 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3020 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003021
3022 return ret;
3023}
3024EXPORT_SYMBOL_GPL(i2c_slave_register);
3025
3026int i2c_slave_unregister(struct i2c_client *client)
3027{
3028 int ret;
3029
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003030 if (!client->adapter->algo->unreg_slave) {
3031 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003032 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003033 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003034
3035 i2c_lock_adapter(client->adapter);
3036 ret = client->adapter->algo->unreg_slave(client);
3037 i2c_unlock_adapter(client->adapter);
3038
3039 if (ret == 0)
3040 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003041 else
3042 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003043
3044 return ret;
3045}
3046EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003047#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3050MODULE_DESCRIPTION("I2C-Bus main module");
3051MODULE_LICENSE("GPL");