blob: 07a83f34ed58e710d829408a4dfc79bc76f89b4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/init.h>
38#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010039#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010040#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060041#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020043#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010044#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010045#include <linux/hardirq.h>
46#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020047#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010048#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020049#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000051#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020053#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare6629dcf2010-05-04 11:09:28 +020060/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020061 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000062 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010063static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_IDR(i2c_adapter_idr);
65
Jean Delvare4f8cf822009-09-18 22:45:46 +020066static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
70
71void i2c_transfer_trace_reg(void)
72{
73 static_key_slow_inc(&i2c_trace_msg);
74}
75
76void i2c_transfer_trace_unreg(void)
77{
78 static_key_slow_dec(&i2c_trace_msg);
79}
80
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020081#if defined(CONFIG_ACPI)
82struct acpi_i2c_handler_data {
83 struct acpi_connection_info info;
84 struct i2c_adapter *adapter;
85};
86
87struct gsb_buffer {
88 u8 status;
89 u8 len;
90 union {
91 u16 wdata;
92 u8 bdata;
93 u8 data[0];
94 };
95} __packed;
96
97static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
98{
99 struct i2c_board_info *info = data;
100
101 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
102 struct acpi_resource_i2c_serialbus *sb;
103
104 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1c2014-12-29 15:48:48 +0200105 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200106 info->addr = sb->slave_address;
107 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
108 info->flags |= I2C_CLIENT_TEN;
109 }
Mika Westerbergdab472e2015-05-06 13:29:07 +0300110 } else if (!info->irq) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200111 struct resource r;
112
113 if (acpi_dev_resource_interrupt(ares, 0, &r))
114 info->irq = r.start;
115 }
116
117 /* Tell the ACPI core to skip this resource */
118 return 1;
119}
120
121static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
122 void *data, void **return_value)
123{
124 struct i2c_adapter *adapter = data;
125 struct list_head resource_list;
126 struct i2c_board_info info;
127 struct acpi_device *adev;
128 int ret;
129
130 if (acpi_bus_get_device(handle, &adev))
131 return AE_OK;
132 if (acpi_bus_get_status(adev) || !adev->status.present)
133 return AE_OK;
134
135 memset(&info, 0, sizeof(info));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100136 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200137
138 INIT_LIST_HEAD(&resource_list);
139 ret = acpi_dev_get_resources(adev, &resource_list,
140 acpi_i2c_add_resource, &info);
141 acpi_dev_free_resource_list(&resource_list);
142
143 if (ret < 0 || !info.addr)
144 return AE_OK;
145
146 adev->power.flags.ignore_parent = true;
147 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
148 if (!i2c_new_device(adapter, &info)) {
149 adev->power.flags.ignore_parent = false;
150 dev_err(&adapter->dev,
151 "failed to add I2C device %s from ACPI\n",
152 dev_name(&adev->dev));
153 }
154
155 return AE_OK;
156}
157
158/**
159 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
160 * @adap: pointer to adapter
161 *
162 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
163 * namespace. When a device is found it will be added to the Linux device
164 * model and bound to the corresponding ACPI handle.
165 */
166static void acpi_i2c_register_devices(struct i2c_adapter *adap)
167{
168 acpi_handle handle;
169 acpi_status status;
170
171 if (!adap->dev.parent)
172 return;
173
174 handle = ACPI_HANDLE(adap->dev.parent);
175 if (!handle)
176 return;
177
178 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
179 acpi_i2c_add_device, NULL,
180 adap, NULL);
181 if (ACPI_FAILURE(status))
182 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
183}
184
185#else /* CONFIG_ACPI */
186static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
187#endif /* CONFIG_ACPI */
188
189#ifdef CONFIG_ACPI_I2C_OPREGION
190static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
191 u8 cmd, u8 *data, u8 data_len)
192{
193
194 struct i2c_msg msgs[2];
195 int ret;
196 u8 *buffer;
197
198 buffer = kzalloc(data_len, GFP_KERNEL);
199 if (!buffer)
200 return AE_NO_MEMORY;
201
202 msgs[0].addr = client->addr;
203 msgs[0].flags = client->flags;
204 msgs[0].len = 1;
205 msgs[0].buf = &cmd;
206
207 msgs[1].addr = client->addr;
208 msgs[1].flags = client->flags | I2C_M_RD;
209 msgs[1].len = data_len;
210 msgs[1].buf = buffer;
211
212 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
213 if (ret < 0)
214 dev_err(&client->adapter->dev, "i2c read failed\n");
215 else
216 memcpy(data, buffer, data_len);
217
218 kfree(buffer);
219 return ret;
220}
221
222static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
223 u8 cmd, u8 *data, u8 data_len)
224{
225
226 struct i2c_msg msgs[1];
227 u8 *buffer;
228 int ret = AE_OK;
229
230 buffer = kzalloc(data_len + 1, GFP_KERNEL);
231 if (!buffer)
232 return AE_NO_MEMORY;
233
234 buffer[0] = cmd;
235 memcpy(buffer + 1, data, data_len);
236
237 msgs[0].addr = client->addr;
238 msgs[0].flags = client->flags;
239 msgs[0].len = data_len + 1;
240 msgs[0].buf = buffer;
241
242 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
243 if (ret < 0)
244 dev_err(&client->adapter->dev, "i2c write failed\n");
245
246 kfree(buffer);
247 return ret;
248}
249
250static acpi_status
251acpi_i2c_space_handler(u32 function, acpi_physical_address command,
252 u32 bits, u64 *value64,
253 void *handler_context, void *region_context)
254{
255 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
256 struct acpi_i2c_handler_data *data = handler_context;
257 struct acpi_connection_info *info = &data->info;
258 struct acpi_resource_i2c_serialbus *sb;
259 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300260 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200261 struct acpi_resource *ares;
262 u32 accessor_type = function >> 16;
263 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100264 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 int status;
266
267 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
268 if (ACPI_FAILURE(ret))
269 return ret;
270
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300271 client = kzalloc(sizeof(*client), GFP_KERNEL);
272 if (!client) {
273 ret = AE_NO_MEMORY;
274 goto err;
275 }
276
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200277 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
278 ret = AE_BAD_PARAMETER;
279 goto err;
280 }
281
282 sb = &ares->data.i2c_serial_bus;
283 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
284 ret = AE_BAD_PARAMETER;
285 goto err;
286 }
287
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300288 client->adapter = adapter;
289 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200290
291 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300292 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200293
294 switch (accessor_type) {
295 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
296 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300297 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200298 if (status >= 0) {
299 gsb->bdata = status;
300 status = 0;
301 }
302 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300303 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200304 }
305 break;
306
307 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
308 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300309 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200310 if (status >= 0) {
311 gsb->bdata = status;
312 status = 0;
313 }
314 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300315 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200316 gsb->bdata);
317 }
318 break;
319
320 case ACPI_GSB_ACCESS_ATTRIB_WORD:
321 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300322 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200323 if (status >= 0) {
324 gsb->wdata = status;
325 status = 0;
326 }
327 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300328 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200329 gsb->wdata);
330 }
331 break;
332
333 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
334 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300335 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200336 gsb->data);
337 if (status >= 0) {
338 gsb->len = status;
339 status = 0;
340 }
341 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300342 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200343 gsb->len, gsb->data);
344 }
345 break;
346
347 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
348 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300349 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200350 gsb->data, info->access_length);
351 if (status > 0)
352 status = 0;
353 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300354 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200355 gsb->data, info->access_length);
356 }
357 break;
358
359 default:
360 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
361 ret = AE_BAD_PARAMETER;
362 goto err;
363 }
364
365 gsb->status = status;
366
367 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300368 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200369 ACPI_FREE(ares);
370 return ret;
371}
372
373
374static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
375{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200376 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200377 struct acpi_i2c_handler_data *data;
378 acpi_status status;
379
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200380 if (!adapter->dev.parent)
381 return -ENODEV;
382
383 handle = ACPI_HANDLE(adapter->dev.parent);
384
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200385 if (!handle)
386 return -ENODEV;
387
388 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
389 GFP_KERNEL);
390 if (!data)
391 return -ENOMEM;
392
393 data->adapter = adapter;
394 status = acpi_bus_attach_private_data(handle, (void *)data);
395 if (ACPI_FAILURE(status)) {
396 kfree(data);
397 return -ENOMEM;
398 }
399
400 status = acpi_install_address_space_handler(handle,
401 ACPI_ADR_SPACE_GSBUS,
402 &acpi_i2c_space_handler,
403 NULL,
404 data);
405 if (ACPI_FAILURE(status)) {
406 dev_err(&adapter->dev, "Error installing i2c space handler\n");
407 acpi_bus_detach_private_data(handle);
408 kfree(data);
409 return -ENOMEM;
410 }
411
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800412 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200413 return 0;
414}
415
416static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
417{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200418 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200419 struct acpi_i2c_handler_data *data;
420 acpi_status status;
421
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200422 if (!adapter->dev.parent)
423 return;
424
425 handle = ACPI_HANDLE(adapter->dev.parent);
426
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200427 if (!handle)
428 return;
429
430 acpi_remove_address_space_handler(handle,
431 ACPI_ADR_SPACE_GSBUS,
432 &acpi_i2c_space_handler);
433
434 status = acpi_bus_get_private_data(handle, (void **)&data);
435 if (ACPI_SUCCESS(status))
436 kfree(data);
437
438 acpi_bus_detach_private_data(handle);
439}
440#else /* CONFIG_ACPI_I2C_OPREGION */
441static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
442{ }
443
444static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
445{ return 0; }
446#endif /* CONFIG_ACPI_I2C_OPREGION */
447
David Brownellf37dd802007-02-13 22:09:00 +0100448/* ------------------------------------------------------------------------- */
449
Jean Delvared2653e92008-04-29 23:11:39 +0200450static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
451 const struct i2c_client *client)
452{
453 while (id->name[0]) {
454 if (strcmp(client->name, id->name) == 0)
455 return id;
456 id++;
457 }
458 return NULL;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static int i2c_device_match(struct device *dev, struct device_driver *drv)
462{
Jean Delvare51298d12009-09-18 22:45:45 +0200463 struct i2c_client *client = i2c_verify_client(dev);
464 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200465
Jean Delvare51298d12009-09-18 22:45:45 +0200466 if (!client)
467 return 0;
468
Grant Likely959e85f2010-06-08 07:48:19 -0600469 /* Attempt an OF style match */
470 if (of_driver_match_device(dev, drv))
471 return 1;
472
Mika Westerberg907ddf82012-11-23 12:23:40 +0100473 /* Then ACPI style match */
474 if (acpi_driver_match_device(dev, drv))
475 return 1;
476
Jean Delvare51298d12009-09-18 22:45:45 +0200477 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200478 /* match on an id table if there is one */
479 if (driver->id_table)
480 return i2c_match_id(driver->id_table, client) != NULL;
481
Jean Delvareeb8a7902008-05-18 20:49:41 +0200482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
David Brownell7b4fbc52007-05-01 23:26:30 +0200485
486/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200487static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200488{
489 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800490 int rc;
491
492 rc = acpi_device_uevent_modalias(dev, env);
493 if (rc != -ENODEV)
494 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200495
Jean Delvareeb8a7902008-05-18 20:49:41 +0200496 if (add_uevent_var(env, "MODALIAS=%s%s",
497 I2C_MODULE_PREFIX, client->name))
498 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200499 dev_dbg(dev, "uevent\n");
500 return 0;
501}
502
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530503/* i2c bus recovery routines */
504static int get_scl_gpio_value(struct i2c_adapter *adap)
505{
506 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
507}
508
509static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
510{
511 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
512}
513
514static int get_sda_gpio_value(struct i2c_adapter *adap)
515{
516 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
517}
518
519static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
520{
521 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
522 struct device *dev = &adap->dev;
523 int ret = 0;
524
525 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
526 GPIOF_OUT_INIT_HIGH, "i2c-scl");
527 if (ret) {
528 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
529 return ret;
530 }
531
532 if (bri->get_sda) {
533 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
534 /* work without SDA polling */
535 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
536 bri->sda_gpio);
537 bri->get_sda = NULL;
538 }
539 }
540
541 return ret;
542}
543
544static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
545{
546 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
547
548 if (bri->get_sda)
549 gpio_free(bri->sda_gpio);
550
551 gpio_free(bri->scl_gpio);
552}
553
554/*
555 * We are generating clock pulses. ndelay() determines durating of clk pulses.
556 * We will generate clock with rate 100 KHz and so duration of both clock levels
557 * is: delay in ns = (10^6 / 100) / 2
558 */
559#define RECOVERY_NDELAY 5000
560#define RECOVERY_CLK_CNT 9
561
562static int i2c_generic_recovery(struct i2c_adapter *adap)
563{
564 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
565 int i = 0, val = 1, ret = 0;
566
567 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300568 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530569
Jan Luebbe8b062602015-07-08 16:35:06 +0200570 bri->set_scl(adap, val);
571 ndelay(RECOVERY_NDELAY);
572
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530573 /*
574 * By this time SCL is high, as we need to give 9 falling-rising edges
575 */
576 while (i++ < RECOVERY_CLK_CNT * 2) {
577 if (val) {
578 /* Break if SDA is high */
579 if (bri->get_sda && bri->get_sda(adap))
580 break;
581 /* SCL shouldn't be low here */
582 if (!bri->get_scl(adap)) {
583 dev_err(&adap->dev,
584 "SCL is stuck low, exit recovery\n");
585 ret = -EBUSY;
586 break;
587 }
588 }
589
590 val = !val;
591 bri->set_scl(adap, val);
592 ndelay(RECOVERY_NDELAY);
593 }
594
595 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300596 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530597
598 return ret;
599}
600
601int i2c_generic_scl_recovery(struct i2c_adapter *adap)
602{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530603 return i2c_generic_recovery(adap);
604}
Mark Brownc1c21f42015-04-15 19:18:39 +0100605EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530606
607int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
608{
609 int ret;
610
611 ret = i2c_get_gpios_for_recovery(adap);
612 if (ret)
613 return ret;
614
615 ret = i2c_generic_recovery(adap);
616 i2c_put_gpios_for_recovery(adap);
617
618 return ret;
619}
Mark Brownc1c21f42015-04-15 19:18:39 +0100620EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530621
622int i2c_recover_bus(struct i2c_adapter *adap)
623{
624 if (!adap->bus_recovery_info)
625 return -EOPNOTSUPP;
626
627 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
628 return adap->bus_recovery_info->recover_bus(adap);
629}
Mark Brownc1c21f42015-04-15 19:18:39 +0100630EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static int i2c_device_probe(struct device *dev)
633{
Jean Delvare51298d12009-09-18 22:45:45 +0200634 struct i2c_client *client = i2c_verify_client(dev);
635 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100636 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200637
Jean Delvare51298d12009-09-18 22:45:45 +0200638 if (!client)
639 return 0;
640
Mika Westerberg845c8772015-05-06 13:29:08 +0300641 if (!client->irq) {
642 int irq = -ENOENT;
643
644 if (dev->of_node)
645 irq = of_irq_get(dev->of_node, 0);
646 else if (ACPI_COMPANION(dev))
647 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200648
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100649 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200650 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100651 if (irq < 0)
652 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200653
654 client->irq = irq;
655 }
656
Jean Delvare51298d12009-09-18 22:45:45 +0200657 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200658 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200659 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200660
Marc Pignatee354252008-08-28 08:33:22 +0200661 if (!device_can_wakeup(&client->dev))
662 device_init_wakeup(&client->dev,
663 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200664 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200665
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200666 status = of_clk_set_defaults(dev->of_node, false);
667 if (status < 0)
668 return status;
669
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200670 status = dev_pm_domain_attach(&client->dev, true);
671 if (status != -EPROBE_DEFER) {
672 status = driver->probe(client, i2c_match_id(driver->id_table,
673 client));
674 if (status)
675 dev_pm_domain_detach(&client->dev, true);
676 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100677
Hans Verkuil50c33042008-03-12 14:15:00 +0100678 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static int i2c_device_remove(struct device *dev)
682{
Jean Delvare51298d12009-09-18 22:45:45 +0200683 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200684 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100685 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200686
Jean Delvare51298d12009-09-18 22:45:45 +0200687 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200688 return 0;
689
690 driver = to_i2c_driver(dev->driver);
691 if (driver->remove) {
692 dev_dbg(dev, "remove\n");
693 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200694 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100695
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200696 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200697 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
David Brownellf37dd802007-02-13 22:09:00 +0100700static void i2c_device_shutdown(struct device *dev)
701{
Jean Delvare51298d12009-09-18 22:45:45 +0200702 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100703 struct i2c_driver *driver;
704
Jean Delvare51298d12009-09-18 22:45:45 +0200705 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100706 return;
707 driver = to_i2c_driver(dev->driver);
708 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200709 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100710}
711
David Brownell9c1600e2007-05-01 23:26:31 +0200712static void i2c_client_dev_release(struct device *dev)
713{
714 kfree(to_i2c_client(dev));
715}
716
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100717static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200718show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200719{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200720 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
721 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200722}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100723static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200724
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100725static ssize_t
726show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200727{
728 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800729 int len;
730
731 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
732 if (len != -ENODEV)
733 return len;
734
Jean Delvareeb8a7902008-05-18 20:49:41 +0200735 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200736}
Jean Delvare51298d12009-09-18 22:45:45 +0200737static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
738
739static struct attribute *i2c_dev_attrs[] = {
740 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200741 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200742 &dev_attr_modalias.attr,
743 NULL
744};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100745ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100746
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200747struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100748 .name = "i2c",
749 .match = i2c_device_match,
750 .probe = i2c_device_probe,
751 .remove = i2c_device_remove,
752 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000753};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200754EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000755
Jean Delvare51298d12009-09-18 22:45:45 +0200756static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100757 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200758 .uevent = i2c_device_uevent,
759 .release = i2c_client_dev_release,
760};
761
David Brownell9b766b82008-01-27 18:14:51 +0100762
763/**
764 * i2c_verify_client - return parameter as i2c_client, or NULL
765 * @dev: device, probably from some driver model iterator
766 *
767 * When traversing the driver model tree, perhaps using driver model
768 * iterators like @device_for_each_child(), you can't assume very much
769 * about the nodes you find. Use this function to avoid oopses caused
770 * by wrongly treating some non-I2C device as an i2c_client.
771 */
772struct i2c_client *i2c_verify_client(struct device *dev)
773{
Jean Delvare51298d12009-09-18 22:45:45 +0200774 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100775 ? to_i2c_client(dev)
776 : NULL;
777}
778EXPORT_SYMBOL(i2c_verify_client);
779
780
Jean Delvare3a89db52010-06-03 11:33:52 +0200781/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300782 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200783static int i2c_check_client_addr_validity(const struct i2c_client *client)
784{
785 if (client->flags & I2C_CLIENT_TEN) {
786 /* 10-bit address, all values are valid */
787 if (client->addr > 0x3ff)
788 return -EINVAL;
789 } else {
790 /* 7-bit address, reject the general call address */
791 if (client->addr == 0x00 || client->addr > 0x7f)
792 return -EINVAL;
793 }
794 return 0;
795}
796
Jean Delvare656b8762010-06-03 11:33:53 +0200797/* And this is a strict address validity check, used when probing. If a
798 * device uses a reserved address, then it shouldn't be probed. 7-bit
799 * addressing is assumed, 10-bit address devices are rare and should be
800 * explicitly enumerated. */
801static int i2c_check_addr_validity(unsigned short addr)
802{
803 /*
804 * Reserved addresses per I2C specification:
805 * 0x00 General call address / START byte
806 * 0x01 CBUS address
807 * 0x02 Reserved for different bus format
808 * 0x03 Reserved for future purposes
809 * 0x04-0x07 Hs-mode master code
810 * 0x78-0x7b 10-bit slave addressing
811 * 0x7c-0x7f Reserved for future purposes
812 */
813 if (addr < 0x08 || addr > 0x77)
814 return -EINVAL;
815 return 0;
816}
817
Jean Delvare3b5f7942010-06-03 11:33:55 +0200818static int __i2c_check_addr_busy(struct device *dev, void *addrp)
819{
820 struct i2c_client *client = i2c_verify_client(dev);
821 int addr = *(int *)addrp;
822
823 if (client && client->addr == addr)
824 return -EBUSY;
825 return 0;
826}
827
Michael Lawnick08263742010-08-11 18:21:02 +0200828/* walk up mux tree */
829static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
830{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200831 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200832 int result;
833
834 result = device_for_each_child(&adapter->dev, &addr,
835 __i2c_check_addr_busy);
836
Jean Delvare97cc4d42010-10-24 18:16:57 +0200837 if (!result && parent)
838 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200839
840 return result;
841}
842
843/* recurse down mux tree */
844static int i2c_check_mux_children(struct device *dev, void *addrp)
845{
846 int result;
847
848 if (dev->type == &i2c_adapter_type)
849 result = device_for_each_child(dev, addrp,
850 i2c_check_mux_children);
851 else
852 result = __i2c_check_addr_busy(dev, addrp);
853
854 return result;
855}
856
Jean Delvare3b5f7942010-06-03 11:33:55 +0200857static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
858{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200859 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200860 int result = 0;
861
Jean Delvare97cc4d42010-10-24 18:16:57 +0200862 if (parent)
863 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200864
865 if (!result)
866 result = device_for_each_child(&adapter->dev, &addr,
867 i2c_check_mux_children);
868
869 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200870}
871
David Brownell9c1600e2007-05-01 23:26:31 +0200872/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200873 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
874 * @adapter: Target I2C bus segment
875 */
876void i2c_lock_adapter(struct i2c_adapter *adapter)
877{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200878 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
879
880 if (parent)
881 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200882 else
883 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200884}
885EXPORT_SYMBOL_GPL(i2c_lock_adapter);
886
887/**
888 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
889 * @adapter: Target I2C bus segment
890 */
891static int i2c_trylock_adapter(struct i2c_adapter *adapter)
892{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200893 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
894
895 if (parent)
896 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200897 else
898 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200899}
900
901/**
902 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
903 * @adapter: Target I2C bus segment
904 */
905void i2c_unlock_adapter(struct i2c_adapter *adapter)
906{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200907 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
908
909 if (parent)
910 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200911 else
912 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200913}
914EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
915
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200916static void i2c_dev_set_name(struct i2c_adapter *adap,
917 struct i2c_client *client)
918{
919 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
920
921 if (adev) {
922 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
923 return;
924 }
925
926 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
927 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
928 client->addr | ((client->flags & I2C_CLIENT_TEN)
929 ? 0xa000 : 0));
930}
931
Jean Delvarefe61e072010-08-11 18:20:58 +0200932/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200933 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200934 * @adap: the adapter managing the device
935 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200936 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200937 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200938 * Create an i2c device. Binding is handled through driver model
939 * probe()/remove() methods. A driver may be bound to this device when we
940 * return from this function, or any later moment (e.g. maybe hotplugging will
941 * load the driver module). This call is not appropriate for use by mainboard
942 * initialization logic, which usually runs during an arch_initcall() long
943 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200944 *
945 * This returns the new i2c client, which may be saved for later use with
946 * i2c_unregister_device(); or NULL to indicate an error.
947 */
948struct i2c_client *
949i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
950{
951 struct i2c_client *client;
952 int status;
953
954 client = kzalloc(sizeof *client, GFP_KERNEL);
955 if (!client)
956 return NULL;
957
958 client->adapter = adap;
959
960 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200961
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200962 if (info->archdata)
963 client->dev.archdata = *info->archdata;
964
Marc Pignatee354252008-08-28 08:33:22 +0200965 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200966 client->addr = info->addr;
967 client->irq = info->irq;
968
David Brownell9c1600e2007-05-01 23:26:31 +0200969 strlcpy(client->name, info->type, sizeof(client->name));
970
Jean Delvare3a89db52010-06-03 11:33:52 +0200971 /* Check for address validity */
972 status = i2c_check_client_addr_validity(client);
973 if (status) {
974 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
975 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
976 goto out_err_silent;
977 }
978
Jean Delvaref8a227e2009-06-19 16:58:18 +0200979 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200980 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200981 if (status)
982 goto out_err;
983
984 client->dev.parent = &client->adapter->dev;
985 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200986 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700987 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100988 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200989
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200990 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200991 status = device_register(&client->dev);
992 if (status)
993 goto out_err;
994
Jean Delvaref8a227e2009-06-19 16:58:18 +0200995 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
996 client->name, dev_name(&client->dev));
997
David Brownell9c1600e2007-05-01 23:26:31 +0200998 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200999
1000out_err:
1001 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1002 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001003out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001004 kfree(client);
1005 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001006}
1007EXPORT_SYMBOL_GPL(i2c_new_device);
1008
1009
1010/**
1011 * i2c_unregister_device - reverse effect of i2c_new_device()
1012 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001013 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001014 */
1015void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001016{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001017 if (client->dev.of_node)
1018 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +02001019 device_unregister(&client->dev);
1020}
David Brownell9c1600e2007-05-01 23:26:31 +02001021EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001022
1023
Jean Delvare60b129d2008-05-11 20:37:06 +02001024static const struct i2c_device_id dummy_id[] = {
1025 { "dummy", 0 },
1026 { },
1027};
1028
Jean Delvared2653e92008-04-29 23:11:39 +02001029static int dummy_probe(struct i2c_client *client,
1030 const struct i2c_device_id *id)
1031{
1032 return 0;
1033}
1034
1035static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001036{
1037 return 0;
1038}
1039
1040static struct i2c_driver dummy_driver = {
1041 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001042 .probe = dummy_probe,
1043 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001044 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001045};
1046
1047/**
1048 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1049 * @adapter: the adapter managing the device
1050 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001051 * Context: can sleep
1052 *
1053 * This returns an I2C client bound to the "dummy" driver, intended for use
1054 * with devices that consume multiple addresses. Examples of such chips
1055 * include various EEPROMS (like 24c04 and 24c08 models).
1056 *
1057 * These dummy devices have two main uses. First, most I2C and SMBus calls
1058 * except i2c_transfer() need a client handle; the dummy will be that handle.
1059 * And second, this prevents the specified address from being bound to a
1060 * different driver.
1061 *
1062 * This returns the new i2c client, which should be saved for later use with
1063 * i2c_unregister_device(); or NULL to indicate an error.
1064 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001065struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001066{
1067 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001068 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001069 };
1070
David Brownelle9f13732008-01-27 18:14:52 +01001071 return i2c_new_device(adapter, &info);
1072}
1073EXPORT_SYMBOL_GPL(i2c_new_dummy);
1074
David Brownellf37dd802007-02-13 22:09:00 +01001075/* ------------------------------------------------------------------------- */
1076
David Brownell16ffadf2007-05-01 23:26:28 +02001077/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1078
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001079static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
David Brownellef2c83212007-05-01 23:26:28 +02001081 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 complete(&adap->dev_released);
1083}
1084
Jean Delvare99cd8e22009-06-19 16:58:20 +02001085/*
Jean Delvare390946b2012-09-10 10:14:02 +02001086 * This function is only needed for mutex_lock_nested, so it is never
1087 * called unless locking correctness checking is enabled. Thus we
1088 * make it inline to avoid a compiler warning. That's what gcc ends up
1089 * doing anyway.
1090 */
1091static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1092{
1093 unsigned int depth = 0;
1094
1095 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1096 depth++;
1097
1098 return depth;
1099}
1100
1101/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001102 * Let users instantiate I2C devices through sysfs. This can be used when
1103 * platform initialization code doesn't contain the proper data for
1104 * whatever reason. Also useful for drivers that do device detection and
1105 * detection fails, either because the device uses an unexpected address,
1106 * or this is a compatible device with different ID register values.
1107 *
1108 * Parameter checking may look overzealous, but we really don't want
1109 * the user to provide incorrect parameters.
1110 */
1111static ssize_t
1112i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1113 const char *buf, size_t count)
1114{
1115 struct i2c_adapter *adap = to_i2c_adapter(dev);
1116 struct i2c_board_info info;
1117 struct i2c_client *client;
1118 char *blank, end;
1119 int res;
1120
Jean Delvare99cd8e22009-06-19 16:58:20 +02001121 memset(&info, 0, sizeof(struct i2c_board_info));
1122
1123 blank = strchr(buf, ' ');
1124 if (!blank) {
1125 dev_err(dev, "%s: Missing parameters\n", "new_device");
1126 return -EINVAL;
1127 }
1128 if (blank - buf > I2C_NAME_SIZE - 1) {
1129 dev_err(dev, "%s: Invalid device name\n", "new_device");
1130 return -EINVAL;
1131 }
1132 memcpy(info.type, buf, blank - buf);
1133
1134 /* Parse remaining parameters, reject extra parameters */
1135 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1136 if (res < 1) {
1137 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1138 return -EINVAL;
1139 }
1140 if (res > 1 && end != '\n') {
1141 dev_err(dev, "%s: Extra parameters\n", "new_device");
1142 return -EINVAL;
1143 }
1144
Jean Delvare99cd8e22009-06-19 16:58:20 +02001145 client = i2c_new_device(adap, &info);
1146 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001147 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001148
1149 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001150 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001151 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001152 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001153 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1154 info.type, info.addr);
1155
1156 return count;
1157}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001158static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001159
1160/*
1161 * And of course let the users delete the devices they instantiated, if
1162 * they got it wrong. This interface can only be used to delete devices
1163 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1164 * don't delete devices to which some kernel code still has references.
1165 *
1166 * Parameter checking may look overzealous, but we really don't want
1167 * the user to delete the wrong device.
1168 */
1169static ssize_t
1170i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1171 const char *buf, size_t count)
1172{
1173 struct i2c_adapter *adap = to_i2c_adapter(dev);
1174 struct i2c_client *client, *next;
1175 unsigned short addr;
1176 char end;
1177 int res;
1178
1179 /* Parse parameters, reject extra parameters */
1180 res = sscanf(buf, "%hi%c", &addr, &end);
1181 if (res < 1) {
1182 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1183 return -EINVAL;
1184 }
1185 if (res > 1 && end != '\n') {
1186 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1187 return -EINVAL;
1188 }
1189
1190 /* Make sure the device was added through sysfs */
1191 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001192 mutex_lock_nested(&adap->userspace_clients_lock,
1193 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001194 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1195 detected) {
1196 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001197 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1198 "delete_device", client->name, client->addr);
1199
1200 list_del(&client->detected);
1201 i2c_unregister_device(client);
1202 res = count;
1203 break;
1204 }
1205 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001206 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001207
1208 if (res < 0)
1209 dev_err(dev, "%s: Can't find device in list\n",
1210 "delete_device");
1211 return res;
1212}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001213static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1214 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001215
1216static struct attribute *i2c_adapter_attrs[] = {
1217 &dev_attr_name.attr,
1218 &dev_attr_new_device.attr,
1219 &dev_attr_delete_device.attr,
1220 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001221};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001222ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001223
Michael Lawnick08263742010-08-11 18:21:02 +02001224struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001225 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001226 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001227};
Michael Lawnick08263742010-08-11 18:21:02 +02001228EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Stephen Warren643dd092012-04-17 12:43:33 -06001230/**
1231 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1232 * @dev: device, probably from some driver model iterator
1233 *
1234 * When traversing the driver model tree, perhaps using driver model
1235 * iterators like @device_for_each_child(), you can't assume very much
1236 * about the nodes you find. Use this function to avoid oopses caused
1237 * by wrongly treating some non-I2C device as an i2c_adapter.
1238 */
1239struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1240{
1241 return (dev->type == &i2c_adapter_type)
1242 ? to_i2c_adapter(dev)
1243 : NULL;
1244}
1245EXPORT_SYMBOL(i2c_verify_adapter);
1246
Jean Delvare2bb50952009-09-18 22:45:46 +02001247#ifdef CONFIG_I2C_COMPAT
1248static struct class_compat *i2c_adapter_compat_class;
1249#endif
1250
David Brownell9c1600e2007-05-01 23:26:31 +02001251static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1252{
1253 struct i2c_devinfo *devinfo;
1254
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001255 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001256 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1257 if (devinfo->busnum == adapter->nr
1258 && !i2c_new_device(adapter,
1259 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001260 dev_err(&adapter->dev,
1261 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001262 devinfo->board_info.addr);
1263 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001264 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001265}
1266
Wolfram Sang687b81d2013-07-11 12:56:15 +01001267/* OF support code */
1268
1269#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001270static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1271 struct device_node *node)
1272{
1273 struct i2c_client *result;
1274 struct i2c_board_info info = {};
1275 struct dev_archdata dev_ad = {};
1276 const __be32 *addr;
1277 int len;
1278
1279 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1280
1281 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1282 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1283 node->full_name);
1284 return ERR_PTR(-EINVAL);
1285 }
1286
1287 addr = of_get_property(node, "reg", &len);
Wolfram Sang4c1344f2015-05-19 18:29:48 +02001288 if (!addr || (len < sizeof(*addr))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001289 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1290 node->full_name);
1291 return ERR_PTR(-EINVAL);
1292 }
1293
1294 info.addr = be32_to_cpup(addr);
1295 if (info.addr > (1 << 10) - 1) {
1296 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1297 info.addr, node->full_name);
1298 return ERR_PTR(-EINVAL);
1299 }
1300
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001301 info.of_node = of_node_get(node);
1302 info.archdata = &dev_ad;
1303
1304 if (of_get_property(node, "wakeup-source", NULL))
1305 info.flags |= I2C_CLIENT_WAKE;
1306
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001307 result = i2c_new_device(adap, &info);
1308 if (result == NULL) {
1309 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1310 node->full_name);
1311 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001312 return ERR_PTR(-EINVAL);
1313 }
1314 return result;
1315}
1316
Wolfram Sang687b81d2013-07-11 12:56:15 +01001317static void of_i2c_register_devices(struct i2c_adapter *adap)
1318{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001319 struct device_node *node;
1320
1321 /* Only register child devices if the adapter has a node pointer set */
1322 if (!adap->dev.of_node)
1323 return;
1324
1325 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1326
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001327 for_each_available_child_of_node(adap->dev.of_node, node) {
1328 if (of_node_test_and_set_flag(node, OF_POPULATED))
1329 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001330 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001331 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001332}
1333
1334static int of_dev_node_match(struct device *dev, void *data)
1335{
1336 return dev->of_node == data;
1337}
1338
1339/* must call put_device() when done with returned i2c_client device */
1340struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1341{
1342 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001343 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001344
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001345 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001346 if (!dev)
1347 return NULL;
1348
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001349 client = i2c_verify_client(dev);
1350 if (!client)
1351 put_device(dev);
1352
1353 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001354}
1355EXPORT_SYMBOL(of_find_i2c_device_by_node);
1356
1357/* must call put_device() when done with returned i2c_adapter device */
1358struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1359{
1360 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001361 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001362
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001363 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001364 if (!dev)
1365 return NULL;
1366
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001367 adapter = i2c_verify_adapter(dev);
1368 if (!adapter)
1369 put_device(dev);
1370
1371 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001372}
1373EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001374
1375/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1376struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1377{
1378 struct i2c_adapter *adapter;
1379
1380 adapter = of_find_i2c_adapter_by_node(node);
1381 if (!adapter)
1382 return NULL;
1383
1384 if (!try_module_get(adapter->owner)) {
1385 put_device(&adapter->dev);
1386 adapter = NULL;
1387 }
1388
1389 return adapter;
1390}
1391EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001392#else
1393static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1394#endif /* CONFIG_OF */
1395
Jean Delvare69b00892009-12-06 17:06:27 +01001396static int i2c_do_add_adapter(struct i2c_driver *driver,
1397 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001398{
Jean Delvare4735c982008-07-14 22:38:36 +02001399 /* Detect supported devices on that bus, and instantiate them */
1400 i2c_detect(adap, driver);
1401
1402 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001403 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001404 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1405 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001406 dev_warn(&adap->dev, "Please use another way to instantiate "
1407 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001408 /* We ignore the return code; if it fails, too bad */
1409 driver->attach_adapter(adap);
1410 }
1411 return 0;
1412}
1413
Jean Delvare69b00892009-12-06 17:06:27 +01001414static int __process_new_adapter(struct device_driver *d, void *data)
1415{
1416 return i2c_do_add_adapter(to_i2c_driver(d), data);
1417}
1418
David Brownell6e13e642007-05-01 23:26:31 +02001419static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Jean Delvared6703282010-08-11 18:20:59 +02001421 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
David Brownell1d0b19c2008-10-14 17:30:05 +02001423 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001424 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1425 res = -EAGAIN;
1426 goto out_list;
1427 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001428
Jean Delvare2236baa2010-11-15 22:40:38 +01001429 /* Sanity checks */
1430 if (unlikely(adap->name[0] == '\0')) {
1431 pr_err("i2c-core: Attempt to register an adapter with "
1432 "no name!\n");
1433 return -EINVAL;
1434 }
1435 if (unlikely(!adap->algo)) {
1436 pr_err("i2c-core: Attempt to register adapter '%s' with "
1437 "no algo!\n", adap->name);
1438 return -EINVAL;
1439 }
1440
Mika Kuoppala194684e2009-12-06 17:06:22 +01001441 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001442 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001443 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Jean Delvare8fcfef62009-03-28 21:34:43 +01001445 /* Set default timeout to 1 second if not already set */
1446 if (adap->timeout == 0)
1447 adap->timeout = HZ;
1448
Kay Sievers27d9c182009-01-07 14:29:16 +01001449 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001450 adap->dev.bus = &i2c_bus_type;
1451 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001452 res = device_register(&adap->dev);
1453 if (res)
1454 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001456 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1457
Charles Keepax6ada5c12015-04-16 13:05:19 +01001458 pm_runtime_no_callbacks(&adap->dev);
1459
Jean Delvare2bb50952009-09-18 22:45:46 +02001460#ifdef CONFIG_I2C_COMPAT
1461 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1462 adap->dev.parent);
1463 if (res)
1464 dev_warn(&adap->dev,
1465 "Failed to create compatibility class link\n");
1466#endif
1467
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301468 /* bus recovery specific initialization */
1469 if (adap->bus_recovery_info) {
1470 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1471
1472 if (!bri->recover_bus) {
1473 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1474 adap->bus_recovery_info = NULL;
1475 goto exit_recovery;
1476 }
1477
1478 /* Generic GPIO recovery */
1479 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1480 if (!gpio_is_valid(bri->scl_gpio)) {
1481 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1482 adap->bus_recovery_info = NULL;
1483 goto exit_recovery;
1484 }
1485
1486 if (gpio_is_valid(bri->sda_gpio))
1487 bri->get_sda = get_sda_gpio_value;
1488 else
1489 bri->get_sda = NULL;
1490
1491 bri->get_scl = get_scl_gpio_value;
1492 bri->set_scl = set_scl_gpio_value;
1493 } else if (!bri->set_scl || !bri->get_scl) {
1494 /* Generic SCL recovery */
1495 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1496 adap->bus_recovery_info = NULL;
1497 }
1498 }
1499
1500exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001501 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001502 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001503 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001504 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001505
David Brownell6e13e642007-05-01 23:26:31 +02001506 if (adap->nr < __i2c_first_dynamic_bus_num)
1507 i2c_scan_static_board_info(adap);
1508
Jean Delvare4735c982008-07-14 22:38:36 +02001509 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001510 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001511 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001512 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001513
1514 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001515
Jean Delvareb119c6c2006-08-15 18:26:30 +02001516out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001517 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001518 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001519 mutex_unlock(&core_lock);
1520 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
David Brownell6e13e642007-05-01 23:26:31 +02001523/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001524 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1525 * @adap: the adapter to register (with adap->nr initialized)
1526 * Context: can sleep
1527 *
1528 * See i2c_add_numbered_adapter() for details.
1529 */
1530static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1531{
1532 int id;
1533
1534 mutex_lock(&core_lock);
1535 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1536 GFP_KERNEL);
1537 mutex_unlock(&core_lock);
1538 if (id < 0)
1539 return id == -ENOSPC ? -EBUSY : id;
1540
1541 return i2c_register_adapter(adap);
1542}
1543
1544/**
David Brownell6e13e642007-05-01 23:26:31 +02001545 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1546 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001547 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001548 *
1549 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001550 * doesn't matter or when its bus number is specified by an dt alias.
1551 * Examples of bases when the bus number doesn't matter: I2C adapters
1552 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001553 *
1554 * When this returns zero, a new bus number was allocated and stored
1555 * in adap->nr, and the specified adapter became available for clients.
1556 * Otherwise, a negative errno value is returned.
1557 */
1558int i2c_add_adapter(struct i2c_adapter *adapter)
1559{
Doug Andersonee5c2742013-03-01 08:57:31 -08001560 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001561 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001562
Doug Andersonee5c2742013-03-01 08:57:31 -08001563 if (dev->of_node) {
1564 id = of_alias_get_id(dev->of_node, "i2c");
1565 if (id >= 0) {
1566 adapter->nr = id;
1567 return __i2c_add_numbered_adapter(adapter);
1568 }
1569 }
1570
Jean Delvarecaada322008-01-27 18:14:49 +01001571 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001572 id = idr_alloc(&i2c_adapter_idr, adapter,
1573 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001574 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001575 if (id < 0)
1576 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001577
1578 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001579
David Brownell6e13e642007-05-01 23:26:31 +02001580 return i2c_register_adapter(adapter);
1581}
1582EXPORT_SYMBOL(i2c_add_adapter);
1583
1584/**
1585 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1586 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001587 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001588 *
1589 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001590 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1591 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001592 * is used to properly configure I2C devices.
1593 *
Grant Likely488bf312011-07-25 17:49:43 +02001594 * If the requested bus number is set to -1, then this function will behave
1595 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1596 *
David Brownell6e13e642007-05-01 23:26:31 +02001597 * If no devices have pre-been declared for this bus, then be sure to
1598 * register the adapter before any dynamically allocated ones. Otherwise
1599 * the required bus ID may not be available.
1600 *
1601 * When this returns zero, the specified adapter became available for
1602 * clients using the bus number provided in adap->nr. Also, the table
1603 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1604 * and the appropriate driver model device nodes are created. Otherwise, a
1605 * negative errno value is returned.
1606 */
1607int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1608{
Grant Likely488bf312011-07-25 17:49:43 +02001609 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1610 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001611
Doug Andersonee5c2742013-03-01 08:57:31 -08001612 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001613}
1614EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1615
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001616static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001617 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001618{
Jean Delvare4735c982008-07-14 22:38:36 +02001619 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001620
Jean Delvareacec2112009-03-28 21:34:40 +01001621 /* Remove the devices we created ourselves as the result of hardware
1622 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001623 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1624 if (client->adapter == adapter) {
1625 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1626 client->name, client->addr);
1627 list_del(&client->detected);
1628 i2c_unregister_device(client);
1629 }
1630 }
Jean Delvare026526f2008-01-27 18:14:49 +01001631}
1632
Jean Delvaree549c2b2009-06-19 16:58:19 +02001633static int __unregister_client(struct device *dev, void *dummy)
1634{
1635 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001636 if (client && strcmp(client->name, "dummy"))
1637 i2c_unregister_device(client);
1638 return 0;
1639}
1640
1641static int __unregister_dummy(struct device *dev, void *dummy)
1642{
1643 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001644 if (client)
1645 i2c_unregister_device(client);
1646 return 0;
1647}
1648
Jean Delvare69b00892009-12-06 17:06:27 +01001649static int __process_removed_adapter(struct device_driver *d, void *data)
1650{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001651 i2c_do_del_adapter(to_i2c_driver(d), data);
1652 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001653}
1654
David Brownelld64f73b2007-07-12 14:12:28 +02001655/**
1656 * i2c_del_adapter - unregister I2C adapter
1657 * @adap: the adapter being unregistered
1658 * Context: can sleep
1659 *
1660 * This unregisters an I2C adapter which was previously registered
1661 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1662 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001663void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001665 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001666 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001669 mutex_lock(&core_lock);
1670 found = idr_find(&i2c_adapter_idr, adap->nr);
1671 mutex_unlock(&core_lock);
1672 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001673 pr_debug("i2c-core: attempting to delete unregistered "
1674 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001675 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
Lan Tianyu5d98e612014-05-20 20:59:23 +08001678 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001679 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001680 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001681 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001682 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001683 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001685 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001686 mutex_lock_nested(&adap->userspace_clients_lock,
1687 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001688 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1689 detected) {
1690 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1691 client->addr);
1692 list_del(&client->detected);
1693 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001694 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001695 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001696
Jean Delvaree549c2b2009-06-19 16:58:19 +02001697 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001698 * check the returned value. This is a two-pass process, because
1699 * we can't remove the dummy devices during the first pass: they
1700 * could have been instantiated by real devices wishing to clean
1701 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001702 device_for_each_child(&adap->dev, NULL, __unregister_client);
1703 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Jean Delvare2bb50952009-09-18 22:45:46 +02001705#ifdef CONFIG_I2C_COMPAT
1706 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1707 adap->dev.parent);
1708#endif
1709
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001710 /* device name is gone after device_unregister */
1711 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1712
Wolfram Sang26680ee2015-01-29 22:45:09 +01001713 /* wait until all references to the device are gone
1714 *
1715 * FIXME: This is old code and should ideally be replaced by an
1716 * alternative which results in decoupling the lifetime of the struct
1717 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301718 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
David Brownell6e13e642007-05-01 23:26:31 +02001724 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001725 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001727 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001729 /* Clear the device structure in case this adapter is ever going to be
1730 added again */
1731 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
David Brownellc0564602007-05-01 23:26:31 +02001733EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David Brownell7b4fbc52007-05-01 23:26:30 +02001735/* ------------------------------------------------------------------------- */
1736
Jean Delvare7ae31482011-03-20 14:50:52 +01001737int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1738{
1739 int res;
1740
1741 mutex_lock(&core_lock);
1742 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1743 mutex_unlock(&core_lock);
1744
1745 return res;
1746}
1747EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1748
Jean Delvare69b00892009-12-06 17:06:27 +01001749static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001750{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001751 if (dev->type != &i2c_adapter_type)
1752 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001753 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001754}
1755
David Brownell7b4fbc52007-05-01 23:26:30 +02001756/*
1757 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001758 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 */
1760
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001761int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001763 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
David Brownell1d0b19c2008-10-14 17:30:05 +02001765 /* Can't register until after driver model init */
1766 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1767 return -EAGAIN;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001770 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jean Delvare729d6dd2009-06-19 16:58:18 +02001773 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001774 * will have called probe() for all matching-but-unbound devices.
1775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 res = driver_register(&driver->driver);
1777 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001778 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001779
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001780 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jean Delvare4735c982008-07-14 22:38:36 +02001782 INIT_LIST_HEAD(&driver->clients);
1783 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001784 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001785
Jean Delvare7eebcb72006-02-05 23:28:21 +01001786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001788EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Jean Delvare69b00892009-12-06 17:06:27 +01001790static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001791{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001792 if (dev->type == &i2c_adapter_type)
1793 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1794 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001795}
1796
David Brownella1d9e6e2007-05-01 23:26:30 +02001797/**
1798 * i2c_del_driver - unregister I2C driver
1799 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001800 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001801 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001802void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Jean Delvare7ae31482011-03-20 14:50:52 +01001804 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001807 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
David Brownellc0564602007-05-01 23:26:31 +02001809EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
David Brownell7b4fbc52007-05-01 23:26:30 +02001811/* ------------------------------------------------------------------------- */
1812
Jean Delvaree48d3312008-01-27 18:14:48 +01001813/**
1814 * i2c_use_client - increments the reference count of the i2c client structure
1815 * @client: the client being referenced
1816 *
1817 * Each live reference to a client should be refcounted. The driver model does
1818 * that automatically as part of driver binding, so that most drivers don't
1819 * need to do this explicitly: they hold a reference until they're unbound
1820 * from the device.
1821 *
1822 * A pointer to the client with the incremented reference counter is returned.
1823 */
1824struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
David Brownell6ea438e2008-07-14 22:38:24 +02001826 if (client && get_device(&client->dev))
1827 return client;
1828 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
David Brownellc0564602007-05-01 23:26:31 +02001830EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jean Delvaree48d3312008-01-27 18:14:48 +01001832/**
1833 * i2c_release_client - release a use of the i2c client structure
1834 * @client: the client being no longer referenced
1835 *
1836 * Must be called when a user of a client is finished with it.
1837 */
1838void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
David Brownell6ea438e2008-07-14 22:38:24 +02001840 if (client)
1841 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
David Brownellc0564602007-05-01 23:26:31 +02001843EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
David Brownell9b766b82008-01-27 18:14:51 +01001845struct i2c_cmd_arg {
1846 unsigned cmd;
1847 void *arg;
1848};
1849
1850static int i2c_cmd(struct device *dev, void *_arg)
1851{
1852 struct i2c_client *client = i2c_verify_client(dev);
1853 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001854 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001855
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001856 if (!client || !client->dev.driver)
1857 return 0;
1858
1859 driver = to_i2c_driver(client->dev.driver);
1860 if (driver->command)
1861 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001862 return 0;
1863}
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1866{
David Brownell9b766b82008-01-27 18:14:51 +01001867 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
David Brownell9b766b82008-01-27 18:14:51 +01001869 cmd_arg.cmd = cmd;
1870 cmd_arg.arg = arg;
1871 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
David Brownellc0564602007-05-01 23:26:31 +02001873EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001875#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1876static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1877 void *arg)
1878{
1879 struct of_reconfig_data *rd = arg;
1880 struct i2c_adapter *adap;
1881 struct i2c_client *client;
1882
1883 switch (of_reconfig_get_state_change(action, rd)) {
1884 case OF_RECONFIG_CHANGE_ADD:
1885 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1886 if (adap == NULL)
1887 return NOTIFY_OK; /* not for us */
1888
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001889 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1890 put_device(&adap->dev);
1891 return NOTIFY_OK;
1892 }
1893
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001894 client = of_i2c_register_device(adap, rd->dn);
1895 put_device(&adap->dev);
1896
1897 if (IS_ERR(client)) {
1898 pr_err("%s: failed to create for '%s'\n",
1899 __func__, rd->dn->full_name);
1900 return notifier_from_errno(PTR_ERR(client));
1901 }
1902 break;
1903 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001904 /* already depopulated? */
1905 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1906 return NOTIFY_OK;
1907
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001908 /* find our device by node */
1909 client = of_find_i2c_device_by_node(rd->dn);
1910 if (client == NULL)
1911 return NOTIFY_OK; /* no? not meant for us */
1912
1913 /* unregister takes one ref away */
1914 i2c_unregister_device(client);
1915
1916 /* and put the reference of the find */
1917 put_device(&client->dev);
1918 break;
1919 }
1920
1921 return NOTIFY_OK;
1922}
1923static struct notifier_block i2c_of_notifier = {
1924 .notifier_call = of_i2c_notify,
1925};
1926#else
1927extern struct notifier_block i2c_of_notifier;
1928#endif /* CONFIG_OF_DYNAMIC */
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930static int __init i2c_init(void)
1931{
1932 int retval;
1933
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001934 retval = of_alias_get_highest_id("i2c");
1935
1936 down_write(&__i2c_board_lock);
1937 if (retval >= __i2c_first_dynamic_bus_num)
1938 __i2c_first_dynamic_bus_num = retval + 1;
1939 up_write(&__i2c_board_lock);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 retval = bus_register(&i2c_bus_type);
1942 if (retval)
1943 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001944#ifdef CONFIG_I2C_COMPAT
1945 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1946 if (!i2c_adapter_compat_class) {
1947 retval = -ENOMEM;
1948 goto bus_err;
1949 }
1950#endif
David Brownelle9f13732008-01-27 18:14:52 +01001951 retval = i2c_add_driver(&dummy_driver);
1952 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001953 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001954
1955 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1956 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1957
David Brownelle9f13732008-01-27 18:14:52 +01001958 return 0;
1959
Jean Delvare2bb50952009-09-18 22:45:46 +02001960class_err:
1961#ifdef CONFIG_I2C_COMPAT
1962 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001963bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001964#endif
David Brownelle9f13732008-01-27 18:14:52 +01001965 bus_unregister(&i2c_bus_type);
1966 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969static void __exit i2c_exit(void)
1970{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001971 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1972 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001973 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001974#ifdef CONFIG_I2C_COMPAT
1975 class_compat_unregister(i2c_adapter_compat_class);
1976#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001978 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
David Brownella10f9e72008-10-14 17:30:06 +02001981/* We must initialize early, because some subsystems register i2c drivers
1982 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1983 */
1984postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985module_exit(i2c_exit);
1986
1987/* ----------------------------------------------------
1988 * the functional interface to the i2c busses.
1989 * ----------------------------------------------------
1990 */
1991
Wolfram Sangb7f62582015-01-05 23:45:59 +01001992/* Check if val is exceeding the quirk IFF quirk is non 0 */
1993#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1994
1995static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1996{
1997 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1998 err_msg, msg->addr, msg->len,
1999 msg->flags & I2C_M_RD ? "read" : "write");
2000 return -EOPNOTSUPP;
2001}
2002
2003static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2004{
2005 const struct i2c_adapter_quirks *q = adap->quirks;
2006 int max_num = q->max_num_msgs, i;
2007 bool do_len_check = true;
2008
2009 if (q->flags & I2C_AQ_COMB) {
2010 max_num = 2;
2011
2012 /* special checks for combined messages */
2013 if (num == 2) {
2014 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2015 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2016
2017 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2018 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2019
2020 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2021 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2022
2023 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2024 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2025
2026 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2027 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2028
2029 do_len_check = false;
2030 }
2031 }
2032
2033 if (i2c_quirk_exceeded(num, max_num))
2034 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2035
2036 for (i = 0; i < num; i++) {
2037 u16 len = msgs[i].len;
2038
2039 if (msgs[i].flags & I2C_M_RD) {
2040 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2041 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2042 } else {
2043 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2044 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2045 }
2046 }
2047
2048 return 0;
2049}
2050
David Brownella1cdeda2008-07-14 22:38:24 +02002051/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002052 * __i2c_transfer - unlocked flavor of i2c_transfer
2053 * @adap: Handle to I2C bus
2054 * @msgs: One or more messages to execute before STOP is issued to
2055 * terminate the operation; each message begins with a START.
2056 * @num: Number of messages to be executed.
2057 *
2058 * Returns negative errno, else the number of messages executed.
2059 *
2060 * Adapter lock must be held when calling this function. No debug logging
2061 * takes place. adap->algo->master_xfer existence isn't checked.
2062 */
2063int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2064{
2065 unsigned long orig_jiffies;
2066 int ret, try;
2067
Wolfram Sangb7f62582015-01-05 23:45:59 +01002068 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2069 return -EOPNOTSUPP;
2070
David Howellsd9a83d62014-03-06 13:35:59 +00002071 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2072 * enabled. This is an efficient way of keeping the for-loop from
2073 * being executed when not needed.
2074 */
2075 if (static_key_false(&i2c_trace_msg)) {
2076 int i;
2077 for (i = 0; i < num; i++)
2078 if (msgs[i].flags & I2C_M_RD)
2079 trace_i2c_read(adap, &msgs[i], i);
2080 else
2081 trace_i2c_write(adap, &msgs[i], i);
2082 }
2083
Jean Delvareb37d2a32012-06-29 07:47:19 -03002084 /* Retry automatically on arbitration loss */
2085 orig_jiffies = jiffies;
2086 for (ret = 0, try = 0; try <= adap->retries; try++) {
2087 ret = adap->algo->master_xfer(adap, msgs, num);
2088 if (ret != -EAGAIN)
2089 break;
2090 if (time_after(jiffies, orig_jiffies + adap->timeout))
2091 break;
2092 }
2093
David Howellsd9a83d62014-03-06 13:35:59 +00002094 if (static_key_false(&i2c_trace_msg)) {
2095 int i;
2096 for (i = 0; i < ret; i++)
2097 if (msgs[i].flags & I2C_M_RD)
2098 trace_i2c_reply(adap, &msgs[i], i);
2099 trace_i2c_result(adap, i, ret);
2100 }
2101
Jean Delvareb37d2a32012-06-29 07:47:19 -03002102 return ret;
2103}
2104EXPORT_SYMBOL(__i2c_transfer);
2105
2106/**
David Brownella1cdeda2008-07-14 22:38:24 +02002107 * i2c_transfer - execute a single or combined I2C message
2108 * @adap: Handle to I2C bus
2109 * @msgs: One or more messages to execute before STOP is issued to
2110 * terminate the operation; each message begins with a START.
2111 * @num: Number of messages to be executed.
2112 *
2113 * Returns negative errno, else the number of messages executed.
2114 *
2115 * Note that there is no requirement that each message be sent to
2116 * the same slave address, although that is the most common model.
2117 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002118int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002120 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
David Brownella1cdeda2008-07-14 22:38:24 +02002122 /* REVISIT the fault reporting model here is weak:
2123 *
2124 * - When we get an error after receiving N bytes from a slave,
2125 * there is no way to report "N".
2126 *
2127 * - When we get a NAK after transmitting N bytes to a slave,
2128 * there is no way to report "N" ... or to let the master
2129 * continue executing the rest of this combined message, if
2130 * that's the appropriate response.
2131 *
2132 * - When for example "num" is two and we successfully complete
2133 * the first message but get an error part way through the
2134 * second, it's unclear whether that should be reported as
2135 * one (discarding status on the second message) or errno
2136 * (discarding status on the first one).
2137 */
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (adap->algo->master_xfer) {
2140#ifdef DEBUG
2141 for (ret = 0; ret < num; ret++) {
2142 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002143 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2144 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2145 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147#endif
2148
Mike Rapoportcea443a2008-01-27 18:14:50 +01002149 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002150 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002151 if (!ret)
2152 /* I2C activity is ongoing. */
2153 return -EAGAIN;
2154 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002155 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002156 }
2157
Jean Delvareb37d2a32012-06-29 07:47:19 -03002158 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002159 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 return ret;
2162 } else {
2163 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002164 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166}
David Brownellc0564602007-05-01 23:26:31 +02002167EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
David Brownella1cdeda2008-07-14 22:38:24 +02002169/**
2170 * i2c_master_send - issue a single I2C message in master transmit mode
2171 * @client: Handle to slave device
2172 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002173 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002174 *
2175 * Returns negative errno, or else the number of bytes written.
2176 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002177int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
2179 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002180 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 struct i2c_msg msg;
2182
Jean Delvare815f55f2005-05-07 22:58:46 +02002183 msg.addr = client->addr;
2184 msg.flags = client->flags & I2C_M_TEN;
2185 msg.len = count;
2186 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002187
Jean Delvare815f55f2005-05-07 22:58:46 +02002188 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002190 /*
2191 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2192 * transmitted, else error code.
2193 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002194 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
David Brownellc0564602007-05-01 23:26:31 +02002196EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
David Brownella1cdeda2008-07-14 22:38:24 +02002198/**
2199 * i2c_master_recv - issue a single I2C message in master receive mode
2200 * @client: Handle to slave device
2201 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002202 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002203 *
2204 * Returns negative errno, or else the number of bytes read.
2205 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002206int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Farid Hammane7225acf2010-05-21 18:40:58 +02002208 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 struct i2c_msg msg;
2210 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Jean Delvare815f55f2005-05-07 22:58:46 +02002212 msg.addr = client->addr;
2213 msg.flags = client->flags & I2C_M_TEN;
2214 msg.flags |= I2C_M_RD;
2215 msg.len = count;
2216 msg.buf = buf;
2217
2218 ret = i2c_transfer(adap, &msg, 1);
2219
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002220 /*
2221 * If everything went ok (i.e. 1 msg received), return #bytes received,
2222 * else error code.
2223 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002224 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
David Brownellc0564602007-05-01 23:26:31 +02002226EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/* ----------------------------------------------------
2229 * the i2c address scanning function
2230 * Will not work for 10-bit addresses!
2231 * ----------------------------------------------------
2232 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002233
Jean Delvare63e4e802010-06-03 11:33:51 +02002234/*
2235 * Legacy default probe function, mostly relevant for SMBus. The default
2236 * probe method is a quick write, but it is known to corrupt the 24RF08
2237 * EEPROMs due to a state machine bug, and could also irreversibly
2238 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2239 * we use a short byte read instead. Also, some bus drivers don't implement
2240 * quick write, so we fallback to a byte read in that case too.
2241 * On x86, there is another special case for FSC hardware monitoring chips,
2242 * which want regular byte reads (address 0x73.) Fortunately, these are the
2243 * only known chips using this I2C address on PC hardware.
2244 * Returns 1 if probe succeeded, 0 if not.
2245 */
2246static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2247{
2248 int err;
2249 union i2c_smbus_data dummy;
2250
2251#ifdef CONFIG_X86
2252 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2253 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2254 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2255 I2C_SMBUS_BYTE_DATA, &dummy);
2256 else
2257#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002258 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2259 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002260 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2261 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002262 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2263 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2264 I2C_SMBUS_BYTE, &dummy);
2265 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002266 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2267 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002268 err = -EOPNOTSUPP;
2269 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002270
2271 return err >= 0;
2272}
2273
Jean Delvareccfbbd02009-12-06 17:06:25 +01002274static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002275 struct i2c_driver *driver)
2276{
2277 struct i2c_board_info info;
2278 struct i2c_adapter *adapter = temp_client->adapter;
2279 int addr = temp_client->addr;
2280 int err;
2281
2282 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002283 err = i2c_check_addr_validity(addr);
2284 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002285 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2286 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002287 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002288 }
2289
2290 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002291 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002292 return 0;
2293
Jean Delvareccfbbd02009-12-06 17:06:25 +01002294 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002295 if (!i2c_default_probe(adapter, addr))
2296 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002297
2298 /* Finally call the custom detection function */
2299 memset(&info, 0, sizeof(struct i2c_board_info));
2300 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002301 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002302 if (err) {
2303 /* -ENODEV is returned if the detection fails. We catch it
2304 here as this isn't an error. */
2305 return err == -ENODEV ? 0 : err;
2306 }
2307
2308 /* Consistency check */
2309 if (info.type[0] == '\0') {
2310 dev_err(&adapter->dev, "%s detection function provided "
2311 "no name for 0x%x\n", driver->driver.name,
2312 addr);
2313 } else {
2314 struct i2c_client *client;
2315
2316 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002317 if (adapter->class & I2C_CLASS_DEPRECATED)
2318 dev_warn(&adapter->dev,
2319 "This adapter will soon drop class based instantiation of devices. "
2320 "Please make sure client 0x%02x gets instantiated by other means. "
2321 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2322 info.addr);
2323
Jean Delvare4735c982008-07-14 22:38:36 +02002324 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2325 info.type, info.addr);
2326 client = i2c_new_device(adapter, &info);
2327 if (client)
2328 list_add_tail(&client->detected, &driver->clients);
2329 else
2330 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2331 info.type, info.addr);
2332 }
2333 return 0;
2334}
2335
2336static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2337{
Jean Delvarec3813d62009-12-14 21:17:25 +01002338 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002339 struct i2c_client *temp_client;
2340 int i, err = 0;
2341 int adap_id = i2c_adapter_id(adapter);
2342
Jean Delvarec3813d62009-12-14 21:17:25 +01002343 address_list = driver->address_list;
2344 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002345 return 0;
2346
Wolfram Sang45552272014-07-10 13:46:21 +02002347 /* Warn that the adapter lost class based instantiation */
2348 if (adapter->class == I2C_CLASS_DEPRECATED) {
2349 dev_dbg(&adapter->dev,
2350 "This adapter dropped support for I2C classes and "
2351 "won't auto-detect %s devices anymore. If you need it, check "
2352 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2353 driver->driver.name);
2354 return 0;
2355 }
2356
Jean Delvare51b54ba2010-10-24 18:16:58 +02002357 /* Stop here if the classes do not match */
2358 if (!(adapter->class & driver->class))
2359 return 0;
2360
Jean Delvare4735c982008-07-14 22:38:36 +02002361 /* Set up a temporary client to help detect callback */
2362 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2363 if (!temp_client)
2364 return -ENOMEM;
2365 temp_client->adapter = adapter;
2366
Jean Delvarec3813d62009-12-14 21:17:25 +01002367 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002368 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002369 "addr 0x%02x\n", adap_id, address_list[i]);
2370 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002371 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002372 if (unlikely(err))
2373 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002374 }
2375
Jean Delvare4735c982008-07-14 22:38:36 +02002376 kfree(temp_client);
2377 return err;
2378}
2379
Jean Delvared44f19d2010-08-11 18:20:57 +02002380int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2381{
2382 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2383 I2C_SMBUS_QUICK, NULL) >= 0;
2384}
2385EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2386
Jean Delvare12b5053a2007-05-01 23:26:31 +02002387struct i2c_client *
2388i2c_new_probed_device(struct i2c_adapter *adap,
2389 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002390 unsigned short const *addr_list,
2391 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002392{
2393 int i;
2394
Jean Delvare8031d792010-08-11 18:21:00 +02002395 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002396 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002397
Jean Delvare12b5053a2007-05-01 23:26:31 +02002398 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2399 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002400 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002401 dev_warn(&adap->dev, "Invalid 7-bit address "
2402 "0x%02x\n", addr_list[i]);
2403 continue;
2404 }
2405
2406 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002407 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002408 dev_dbg(&adap->dev, "Address 0x%02x already in "
2409 "use, not probing\n", addr_list[i]);
2410 continue;
2411 }
2412
Jean Delvare63e4e802010-06-03 11:33:51 +02002413 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002414 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002415 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002416 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002417
2418 if (addr_list[i] == I2C_CLIENT_END) {
2419 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2420 return NULL;
2421 }
2422
2423 info->addr = addr_list[i];
2424 return i2c_new_device(adap, info);
2425}
2426EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2427
Jean Delvared735b342011-03-20 14:50:52 +01002428struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002431
Jean Delvarecaada322008-01-27 18:14:49 +01002432 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002433 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002434 if (!adapter)
2435 goto exit;
2436
2437 if (try_module_get(adapter->owner))
2438 get_device(&adapter->dev);
2439 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002440 adapter = NULL;
2441
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002442 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002443 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002444 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
David Brownellc0564602007-05-01 23:26:31 +02002446EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448void i2c_put_adapter(struct i2c_adapter *adap)
2449{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002450 if (!adap)
2451 return;
2452
2453 put_device(&adap->dev);
2454 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
David Brownellc0564602007-05-01 23:26:31 +02002456EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458/* The SMBus parts */
2459
David Brownell438d6c22006-12-10 21:21:31 +01002460#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002461static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002464
Farid Hammane7225acf2010-05-21 18:40:58 +02002465 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002466 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 data = data ^ POLY;
2468 data = data << 1;
2469 }
2470 return (u8)(data >> 8);
2471}
2472
Jean Delvare421ef472005-10-26 21:28:55 +02002473/* Incremental CRC8 over count bytes in the array pointed to by p */
2474static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 int i;
2477
Farid Hammane7225acf2010-05-21 18:40:58 +02002478 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002479 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return crc;
2481}
2482
Jean Delvare421ef472005-10-26 21:28:55 +02002483/* Assume a 7-bit address, which is reasonable for SMBus */
2484static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Jean Delvare421ef472005-10-26 21:28:55 +02002486 /* The address will be sent first */
2487 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2488 pec = i2c_smbus_pec(pec, &addr, 1);
2489
2490 /* The data buffer follows */
2491 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Jean Delvare421ef472005-10-26 21:28:55 +02002494/* Used for write only transactions */
2495static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Jean Delvare421ef472005-10-26 21:28:55 +02002497 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2498 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Jean Delvare421ef472005-10-26 21:28:55 +02002501/* Return <0 on CRC error
2502 If there was a write before this read (most cases) we need to take the
2503 partial CRC from the write part into account.
2504 Note that this function does modify the message (we need to decrease the
2505 message length to hide the CRC byte from the caller). */
2506static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Jean Delvare421ef472005-10-26 21:28:55 +02002508 u8 rpec = msg->buf[--msg->len];
2509 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (rpec != cpec) {
2512 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2513 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002514 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
David Brownell438d6c22006-12-10 21:21:31 +01002516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
David Brownella1cdeda2008-07-14 22:38:24 +02002519/**
2520 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2521 * @client: Handle to slave device
2522 *
2523 * This executes the SMBus "receive byte" protocol, returning negative errno
2524 * else the byte received from the device.
2525 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002526s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002529 int status;
2530
2531 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2532 I2C_SMBUS_READ, 0,
2533 I2C_SMBUS_BYTE, &data);
2534 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
David Brownellc0564602007-05-01 23:26:31 +02002536EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
David Brownella1cdeda2008-07-14 22:38:24 +02002538/**
2539 * i2c_smbus_write_byte - SMBus "send byte" protocol
2540 * @client: Handle to slave device
2541 * @value: Byte to be sent
2542 *
2543 * This executes the SMBus "send byte" protocol, returning negative errno
2544 * else zero on success.
2545 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002546s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Farid Hammane7225acf2010-05-21 18:40:58 +02002548 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002549 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
David Brownellc0564602007-05-01 23:26:31 +02002551EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
David Brownella1cdeda2008-07-14 22:38:24 +02002553/**
2554 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2555 * @client: Handle to slave device
2556 * @command: Byte interpreted by slave
2557 *
2558 * This executes the SMBus "read byte" protocol, returning negative errno
2559 * else a data byte received from the device.
2560 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002561s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002564 int status;
2565
2566 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2567 I2C_SMBUS_READ, command,
2568 I2C_SMBUS_BYTE_DATA, &data);
2569 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
David Brownellc0564602007-05-01 23:26:31 +02002571EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
David Brownella1cdeda2008-07-14 22:38:24 +02002573/**
2574 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2575 * @client: Handle to slave device
2576 * @command: Byte interpreted by slave
2577 * @value: Byte being written
2578 *
2579 * This executes the SMBus "write byte" protocol, returning negative errno
2580 * else zero on success.
2581 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002582s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2583 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 union i2c_smbus_data data;
2586 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002587 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2588 I2C_SMBUS_WRITE, command,
2589 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
David Brownellc0564602007-05-01 23:26:31 +02002591EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
David Brownella1cdeda2008-07-14 22:38:24 +02002593/**
2594 * i2c_smbus_read_word_data - SMBus "read word" protocol
2595 * @client: Handle to slave device
2596 * @command: Byte interpreted by slave
2597 *
2598 * This executes the SMBus "read word" protocol, returning negative errno
2599 * else a 16-bit unsigned "word" received from the device.
2600 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002601s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002604 int status;
2605
2606 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2607 I2C_SMBUS_READ, command,
2608 I2C_SMBUS_WORD_DATA, &data);
2609 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
David Brownellc0564602007-05-01 23:26:31 +02002611EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
David Brownella1cdeda2008-07-14 22:38:24 +02002613/**
2614 * i2c_smbus_write_word_data - SMBus "write word" protocol
2615 * @client: Handle to slave device
2616 * @command: Byte interpreted by slave
2617 * @value: 16-bit "word" being written
2618 *
2619 * This executes the SMBus "write word" protocol, returning negative errno
2620 * else zero on success.
2621 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002622s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2623 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
2625 union i2c_smbus_data data;
2626 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002627 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2628 I2C_SMBUS_WRITE, command,
2629 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
David Brownellc0564602007-05-01 23:26:31 +02002631EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
David Brownella64ec072007-10-13 23:56:31 +02002633/**
David Brownella1cdeda2008-07-14 22:38:24 +02002634 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002635 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002636 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002637 * @values: Byte array into which data will be read; big enough to hold
2638 * the data returned by the slave. SMBus allows at most 32 bytes.
2639 *
David Brownella1cdeda2008-07-14 22:38:24 +02002640 * This executes the SMBus "block read" protocol, returning negative errno
2641 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002642 *
2643 * Note that using this function requires that the client's adapter support
2644 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2645 * support this; its emulation through I2C messaging relies on a specific
2646 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2647 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002648s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002649 u8 *values)
2650{
2651 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002652 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002653
David Brownell24a5bb72008-07-14 22:38:23 +02002654 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2655 I2C_SMBUS_READ, command,
2656 I2C_SMBUS_BLOCK_DATA, &data);
2657 if (status)
2658 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002659
2660 memcpy(values, &data.block[1], data.block[0]);
2661 return data.block[0];
2662}
2663EXPORT_SYMBOL(i2c_smbus_read_block_data);
2664
David Brownella1cdeda2008-07-14 22:38:24 +02002665/**
2666 * i2c_smbus_write_block_data - SMBus "block write" protocol
2667 * @client: Handle to slave device
2668 * @command: Byte interpreted by slave
2669 * @length: Size of data block; SMBus allows at most 32 bytes
2670 * @values: Byte array which will be written.
2671 *
2672 * This executes the SMBus "block write" protocol, returning negative errno
2673 * else zero on success.
2674 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002675s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002676 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (length > I2C_SMBUS_BLOCK_MAX)
2681 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002683 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002684 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2685 I2C_SMBUS_WRITE, command,
2686 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
David Brownellc0564602007-05-01 23:26:31 +02002688EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002691s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002692 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002695 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002696
Jean Delvare4b2643d2007-07-12 14:12:29 +02002697 if (length > I2C_SMBUS_BLOCK_MAX)
2698 length = I2C_SMBUS_BLOCK_MAX;
2699 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002700 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2701 I2C_SMBUS_READ, command,
2702 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2703 if (status < 0)
2704 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002705
2706 memcpy(values, &data.block[1], data.block[0]);
2707 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
David Brownellc0564602007-05-01 23:26:31 +02002709EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Jean Delvare0cc43a12011-01-10 22:11:23 +01002711s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002712 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002713{
2714 union i2c_smbus_data data;
2715
2716 if (length > I2C_SMBUS_BLOCK_MAX)
2717 length = I2C_SMBUS_BLOCK_MAX;
2718 data.block[0] = length;
2719 memcpy(data.block + 1, values, length);
2720 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2721 I2C_SMBUS_WRITE, command,
2722 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2723}
David Brownellc0564602007-05-01 23:26:31 +02002724EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002725
David Brownell438d6c22006-12-10 21:21:31 +01002726/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002728static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2729 unsigned short flags,
2730 char read_write, u8 command, int size,
2731 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 /* So we need to generate a series of msgs. In the case of writing, we
2734 need to use only one message; when reading, we need two. We initialize
2735 most things with sane defaults, to keep the code below somewhat
2736 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002737 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2738 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002739 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002741 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002742 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002743 struct i2c_msg msg[2] = {
2744 {
2745 .addr = addr,
2746 .flags = flags,
2747 .len = 1,
2748 .buf = msgbuf0,
2749 }, {
2750 .addr = addr,
2751 .flags = flags | I2C_M_RD,
2752 .len = 0,
2753 .buf = msgbuf1,
2754 },
2755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002758 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 case I2C_SMBUS_QUICK:
2760 msg[0].len = 0;
2761 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002762 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2763 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 num = 1;
2765 break;
2766 case I2C_SMBUS_BYTE:
2767 if (read_write == I2C_SMBUS_READ) {
2768 /* Special case: only a read! */
2769 msg[0].flags = I2C_M_RD | flags;
2770 num = 1;
2771 }
2772 break;
2773 case I2C_SMBUS_BYTE_DATA:
2774 if (read_write == I2C_SMBUS_READ)
2775 msg[1].len = 1;
2776 else {
2777 msg[0].len = 2;
2778 msgbuf0[1] = data->byte;
2779 }
2780 break;
2781 case I2C_SMBUS_WORD_DATA:
2782 if (read_write == I2C_SMBUS_READ)
2783 msg[1].len = 2;
2784 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002785 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002787 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789 break;
2790 case I2C_SMBUS_PROC_CALL:
2791 num = 2; /* Special case */
2792 read_write = I2C_SMBUS_READ;
2793 msg[0].len = 3;
2794 msg[1].len = 2;
2795 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002796 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 break;
2798 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002800 msg[1].flags |= I2C_M_RECV_LEN;
2801 msg[1].len = 1; /* block length will be added by
2802 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 } else {
2804 msg[0].len = data->block[0] + 2;
2805 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002806 dev_err(&adapter->dev,
2807 "Invalid block write size %d\n",
2808 data->block[0]);
2809 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002811 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 msgbuf0[i] = data->block[i-1];
2813 }
2814 break;
2815 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002816 num = 2; /* Another special case */
2817 read_write = I2C_SMBUS_READ;
2818 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002819 dev_err(&adapter->dev,
2820 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002821 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002822 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002823 }
2824 msg[0].len = data->block[0] + 2;
2825 for (i = 1; i < msg[0].len; i++)
2826 msgbuf0[i] = data->block[i-1];
2827 msg[1].flags |= I2C_M_RECV_LEN;
2828 msg[1].len = 1; /* block length will be added by
2829 the underlying bus driver */
2830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 case I2C_SMBUS_I2C_BLOCK_DATA:
2832 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002833 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 } else {
2835 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002836 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002837 dev_err(&adapter->dev,
2838 "Invalid block write size %d\n",
2839 data->block[0]);
2840 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842 for (i = 1; i <= data->block[0]; i++)
2843 msgbuf0[i] = data->block[i];
2844 }
2845 break;
2846 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002847 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2848 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850
Jean Delvare421ef472005-10-26 21:28:55 +02002851 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2852 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2853 if (i) {
2854 /* Compute PEC if first message is a write */
2855 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002856 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002857 i2c_smbus_add_pec(&msg[0]);
2858 else /* Write followed by read */
2859 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2860 }
2861 /* Ask for PEC if last message is a read */
2862 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002863 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002864 }
2865
David Brownell24a5bb72008-07-14 22:38:23 +02002866 status = i2c_transfer(adapter, msg, num);
2867 if (status < 0)
2868 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Jean Delvare421ef472005-10-26 21:28:55 +02002870 /* Check PEC if last message is a read */
2871 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002872 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2873 if (status < 0)
2874 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002875 }
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002878 switch (size) {
2879 case I2C_SMBUS_BYTE:
2880 data->byte = msgbuf0[0];
2881 break;
2882 case I2C_SMBUS_BYTE_DATA:
2883 data->byte = msgbuf1[0];
2884 break;
2885 case I2C_SMBUS_WORD_DATA:
2886 case I2C_SMBUS_PROC_CALL:
2887 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2888 break;
2889 case I2C_SMBUS_I2C_BLOCK_DATA:
2890 for (i = 0; i < data->block[0]; i++)
2891 data->block[i+1] = msgbuf1[i];
2892 break;
2893 case I2C_SMBUS_BLOCK_DATA:
2894 case I2C_SMBUS_BLOCK_PROC_CALL:
2895 for (i = 0; i < msgbuf1[0] + 1; i++)
2896 data->block[i] = msgbuf1[i];
2897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899 return 0;
2900}
2901
David Brownella1cdeda2008-07-14 22:38:24 +02002902/**
2903 * i2c_smbus_xfer - execute SMBus protocol operations
2904 * @adapter: Handle to I2C bus
2905 * @addr: Address of SMBus slave on that bus
2906 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2907 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2908 * @command: Byte interpreted by slave, for protocols which use such bytes
2909 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2910 * @data: Data to be read or written
2911 *
2912 * This executes an SMBus protocol operation, and returns a negative
2913 * errno code else zero on success.
2914 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002915s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002916 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002917 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002919 unsigned long orig_jiffies;
2920 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
David Howells8a325992014-03-06 13:36:06 +00002923 /* If enabled, the following two tracepoints are conditional on
2924 * read_write and protocol.
2925 */
2926 trace_smbus_write(adapter, addr, flags, read_write,
2927 command, protocol, data);
2928 trace_smbus_read(adapter, addr, flags, read_write,
2929 command, protocol);
2930
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002931 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002934 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002935
2936 /* Retry automatically on arbitration loss */
2937 orig_jiffies = jiffies;
2938 for (res = 0, try = 0; try <= adapter->retries; try++) {
2939 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2940 read_write, command,
2941 protocol, data);
2942 if (res != -EAGAIN)
2943 break;
2944 if (time_after(jiffies,
2945 orig_jiffies + adapter->timeout))
2946 break;
2947 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002948 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002950 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002951 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002952 /*
2953 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2954 * implement native support for the SMBus operation.
2955 */
2956 }
2957
David Howells8a325992014-03-06 13:36:06 +00002958 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2959 command, protocol, data);
2960
2961trace:
2962 /* If enabled, the reply tracepoint is conditional on read_write. */
2963 trace_smbus_reply(adapter, addr, flags, read_write,
2964 command, protocol, data);
2965 trace_smbus_result(adapter, addr, flags, read_write,
2966 command, protocol, res);
2967
2968 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Jean Delvared5fd1202015-01-26 20:59:31 +01002972#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002973int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2974{
2975 int ret;
2976
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002977 if (!client || !slave_cb) {
2978 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002979 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002980 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002981
2982 if (!(client->flags & I2C_CLIENT_TEN)) {
2983 /* Enforce stricter address checking */
2984 ret = i2c_check_addr_validity(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002985 if (ret) {
2986 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002987 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002988 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002989 }
2990
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002991 if (!client->adapter->algo->reg_slave) {
2992 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002993 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002994 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002995
2996 client->slave_cb = slave_cb;
2997
2998 i2c_lock_adapter(client->adapter);
2999 ret = client->adapter->algo->reg_slave(client);
3000 i2c_unlock_adapter(client->adapter);
3001
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003002 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003003 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003004 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3005 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003006
3007 return ret;
3008}
3009EXPORT_SYMBOL_GPL(i2c_slave_register);
3010
3011int i2c_slave_unregister(struct i2c_client *client)
3012{
3013 int ret;
3014
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003015 if (!client->adapter->algo->unreg_slave) {
3016 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003017 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003018 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003019
3020 i2c_lock_adapter(client->adapter);
3021 ret = client->adapter->algo->unreg_slave(client);
3022 i2c_unlock_adapter(client->adapter);
3023
3024 if (ret == 0)
3025 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003026 else
3027 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003028
3029 return ret;
3030}
3031EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003032#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3035MODULE_DESCRIPTION("I2C-Bus main module");
3036MODULE_LICENSE("GPL");