blob: e6d4935161e4902762f6042847838428ec34faf2 [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
570 /*
571 * By this time SCL is high, as we need to give 9 falling-rising edges
572 */
573 while (i++ < RECOVERY_CLK_CNT * 2) {
574 if (val) {
575 /* Break if SDA is high */
576 if (bri->get_sda && bri->get_sda(adap))
577 break;
578 /* SCL shouldn't be low here */
579 if (!bri->get_scl(adap)) {
580 dev_err(&adap->dev,
581 "SCL is stuck low, exit recovery\n");
582 ret = -EBUSY;
583 break;
584 }
585 }
586
587 val = !val;
588 bri->set_scl(adap, val);
589 ndelay(RECOVERY_NDELAY);
590 }
591
592 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300593 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530594
595 return ret;
596}
597
598int i2c_generic_scl_recovery(struct i2c_adapter *adap)
599{
600 adap->bus_recovery_info->set_scl(adap, 1);
601 return i2c_generic_recovery(adap);
602}
Mark Brownc1c21f42015-04-15 19:18:39 +0100603EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530604
605int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
606{
607 int ret;
608
609 ret = i2c_get_gpios_for_recovery(adap);
610 if (ret)
611 return ret;
612
613 ret = i2c_generic_recovery(adap);
614 i2c_put_gpios_for_recovery(adap);
615
616 return ret;
617}
Mark Brownc1c21f42015-04-15 19:18:39 +0100618EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530619
620int i2c_recover_bus(struct i2c_adapter *adap)
621{
622 if (!adap->bus_recovery_info)
623 return -EOPNOTSUPP;
624
625 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
626 return adap->bus_recovery_info->recover_bus(adap);
627}
Mark Brownc1c21f42015-04-15 19:18:39 +0100628EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static int i2c_device_probe(struct device *dev)
631{
Jean Delvare51298d12009-09-18 22:45:45 +0200632 struct i2c_client *client = i2c_verify_client(dev);
633 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100634 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200635
Jean Delvare51298d12009-09-18 22:45:45 +0200636 if (!client)
637 return 0;
638
Mika Westerberg845c8772015-05-06 13:29:08 +0300639 if (!client->irq) {
640 int irq = -ENOENT;
641
642 if (dev->of_node)
643 irq = of_irq_get(dev->of_node, 0);
644 else if (ACPI_COMPANION(dev))
645 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200646
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100647 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200648 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100649 if (irq < 0)
650 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200651
652 client->irq = irq;
653 }
654
Jean Delvare51298d12009-09-18 22:45:45 +0200655 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200656 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200657 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200658
Marc Pignatee354252008-08-28 08:33:22 +0200659 if (!device_can_wakeup(&client->dev))
660 device_init_wakeup(&client->dev,
661 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200662 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200663
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200664 status = of_clk_set_defaults(dev->of_node, false);
665 if (status < 0)
666 return status;
667
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200668 status = dev_pm_domain_attach(&client->dev, true);
669 if (status != -EPROBE_DEFER) {
670 status = driver->probe(client, i2c_match_id(driver->id_table,
671 client));
672 if (status)
673 dev_pm_domain_detach(&client->dev, true);
674 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100675
Hans Verkuil50c33042008-03-12 14:15:00 +0100676 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static int i2c_device_remove(struct device *dev)
680{
Jean Delvare51298d12009-09-18 22:45:45 +0200681 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200682 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100683 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200684
Jean Delvare51298d12009-09-18 22:45:45 +0200685 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200686 return 0;
687
688 driver = to_i2c_driver(dev->driver);
689 if (driver->remove) {
690 dev_dbg(dev, "remove\n");
691 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200692 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100693
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200694 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
David Brownellf37dd802007-02-13 22:09:00 +0100698static void i2c_device_shutdown(struct device *dev)
699{
Jean Delvare51298d12009-09-18 22:45:45 +0200700 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100701 struct i2c_driver *driver;
702
Jean Delvare51298d12009-09-18 22:45:45 +0200703 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100704 return;
705 driver = to_i2c_driver(dev->driver);
706 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200707 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100708}
709
David Brownell9c1600e2007-05-01 23:26:31 +0200710static void i2c_client_dev_release(struct device *dev)
711{
712 kfree(to_i2c_client(dev));
713}
714
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100715static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200716show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200717{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200718 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
719 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200720}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100721static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200722
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100723static ssize_t
724show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200725{
726 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800727 int len;
728
729 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
730 if (len != -ENODEV)
731 return len;
732
Jean Delvareeb8a7902008-05-18 20:49:41 +0200733 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200734}
Jean Delvare51298d12009-09-18 22:45:45 +0200735static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
736
737static struct attribute *i2c_dev_attrs[] = {
738 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200739 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200740 &dev_attr_modalias.attr,
741 NULL
742};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100743ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100744
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200745struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100746 .name = "i2c",
747 .match = i2c_device_match,
748 .probe = i2c_device_probe,
749 .remove = i2c_device_remove,
750 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000751};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200752EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000753
Jean Delvare51298d12009-09-18 22:45:45 +0200754static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100755 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200756 .uevent = i2c_device_uevent,
757 .release = i2c_client_dev_release,
758};
759
David Brownell9b766b82008-01-27 18:14:51 +0100760
761/**
762 * i2c_verify_client - return parameter as i2c_client, or NULL
763 * @dev: device, probably from some driver model iterator
764 *
765 * When traversing the driver model tree, perhaps using driver model
766 * iterators like @device_for_each_child(), you can't assume very much
767 * about the nodes you find. Use this function to avoid oopses caused
768 * by wrongly treating some non-I2C device as an i2c_client.
769 */
770struct i2c_client *i2c_verify_client(struct device *dev)
771{
Jean Delvare51298d12009-09-18 22:45:45 +0200772 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100773 ? to_i2c_client(dev)
774 : NULL;
775}
776EXPORT_SYMBOL(i2c_verify_client);
777
778
Jean Delvare3a89db52010-06-03 11:33:52 +0200779/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300780 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200781static int i2c_check_client_addr_validity(const struct i2c_client *client)
782{
783 if (client->flags & I2C_CLIENT_TEN) {
784 /* 10-bit address, all values are valid */
785 if (client->addr > 0x3ff)
786 return -EINVAL;
787 } else {
788 /* 7-bit address, reject the general call address */
789 if (client->addr == 0x00 || client->addr > 0x7f)
790 return -EINVAL;
791 }
792 return 0;
793}
794
Jean Delvare656b8762010-06-03 11:33:53 +0200795/* And this is a strict address validity check, used when probing. If a
796 * device uses a reserved address, then it shouldn't be probed. 7-bit
797 * addressing is assumed, 10-bit address devices are rare and should be
798 * explicitly enumerated. */
799static int i2c_check_addr_validity(unsigned short addr)
800{
801 /*
802 * Reserved addresses per I2C specification:
803 * 0x00 General call address / START byte
804 * 0x01 CBUS address
805 * 0x02 Reserved for different bus format
806 * 0x03 Reserved for future purposes
807 * 0x04-0x07 Hs-mode master code
808 * 0x78-0x7b 10-bit slave addressing
809 * 0x7c-0x7f Reserved for future purposes
810 */
811 if (addr < 0x08 || addr > 0x77)
812 return -EINVAL;
813 return 0;
814}
815
Jean Delvare3b5f7942010-06-03 11:33:55 +0200816static int __i2c_check_addr_busy(struct device *dev, void *addrp)
817{
818 struct i2c_client *client = i2c_verify_client(dev);
819 int addr = *(int *)addrp;
820
821 if (client && client->addr == addr)
822 return -EBUSY;
823 return 0;
824}
825
Michael Lawnick08263742010-08-11 18:21:02 +0200826/* walk up mux tree */
827static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
828{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200829 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200830 int result;
831
832 result = device_for_each_child(&adapter->dev, &addr,
833 __i2c_check_addr_busy);
834
Jean Delvare97cc4d42010-10-24 18:16:57 +0200835 if (!result && parent)
836 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200837
838 return result;
839}
840
841/* recurse down mux tree */
842static int i2c_check_mux_children(struct device *dev, void *addrp)
843{
844 int result;
845
846 if (dev->type == &i2c_adapter_type)
847 result = device_for_each_child(dev, addrp,
848 i2c_check_mux_children);
849 else
850 result = __i2c_check_addr_busy(dev, addrp);
851
852 return result;
853}
854
Jean Delvare3b5f7942010-06-03 11:33:55 +0200855static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
856{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200857 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200858 int result = 0;
859
Jean Delvare97cc4d42010-10-24 18:16:57 +0200860 if (parent)
861 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200862
863 if (!result)
864 result = device_for_each_child(&adapter->dev, &addr,
865 i2c_check_mux_children);
866
867 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200868}
869
David Brownell9c1600e2007-05-01 23:26:31 +0200870/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200871 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
872 * @adapter: Target I2C bus segment
873 */
874void i2c_lock_adapter(struct i2c_adapter *adapter)
875{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200876 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
877
878 if (parent)
879 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200880 else
881 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200882}
883EXPORT_SYMBOL_GPL(i2c_lock_adapter);
884
885/**
886 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
887 * @adapter: Target I2C bus segment
888 */
889static int i2c_trylock_adapter(struct i2c_adapter *adapter)
890{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200891 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
892
893 if (parent)
894 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200895 else
896 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200897}
898
899/**
900 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
901 * @adapter: Target I2C bus segment
902 */
903void i2c_unlock_adapter(struct i2c_adapter *adapter)
904{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200905 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
906
907 if (parent)
908 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200909 else
910 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200911}
912EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
913
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200914static void i2c_dev_set_name(struct i2c_adapter *adap,
915 struct i2c_client *client)
916{
917 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
918
919 if (adev) {
920 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
921 return;
922 }
923
924 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
925 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
926 client->addr | ((client->flags & I2C_CLIENT_TEN)
927 ? 0xa000 : 0));
928}
929
Jean Delvarefe61e072010-08-11 18:20:58 +0200930/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200931 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200932 * @adap: the adapter managing the device
933 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200934 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200935 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200936 * Create an i2c device. Binding is handled through driver model
937 * probe()/remove() methods. A driver may be bound to this device when we
938 * return from this function, or any later moment (e.g. maybe hotplugging will
939 * load the driver module). This call is not appropriate for use by mainboard
940 * initialization logic, which usually runs during an arch_initcall() long
941 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200942 *
943 * This returns the new i2c client, which may be saved for later use with
944 * i2c_unregister_device(); or NULL to indicate an error.
945 */
946struct i2c_client *
947i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
948{
949 struct i2c_client *client;
950 int status;
951
952 client = kzalloc(sizeof *client, GFP_KERNEL);
953 if (!client)
954 return NULL;
955
956 client->adapter = adap;
957
958 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200959
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200960 if (info->archdata)
961 client->dev.archdata = *info->archdata;
962
Marc Pignatee354252008-08-28 08:33:22 +0200963 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200964 client->addr = info->addr;
965 client->irq = info->irq;
966
David Brownell9c1600e2007-05-01 23:26:31 +0200967 strlcpy(client->name, info->type, sizeof(client->name));
968
Jean Delvare3a89db52010-06-03 11:33:52 +0200969 /* Check for address validity */
970 status = i2c_check_client_addr_validity(client);
971 if (status) {
972 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
973 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
974 goto out_err_silent;
975 }
976
Jean Delvaref8a227e2009-06-19 16:58:18 +0200977 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200978 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200979 if (status)
980 goto out_err;
981
982 client->dev.parent = &client->adapter->dev;
983 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200984 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700985 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100986 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200987
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200988 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200989 status = device_register(&client->dev);
990 if (status)
991 goto out_err;
992
Jean Delvaref8a227e2009-06-19 16:58:18 +0200993 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
994 client->name, dev_name(&client->dev));
995
David Brownell9c1600e2007-05-01 23:26:31 +0200996 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200997
998out_err:
999 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1000 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001001out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001002 kfree(client);
1003 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001004}
1005EXPORT_SYMBOL_GPL(i2c_new_device);
1006
1007
1008/**
1009 * i2c_unregister_device - reverse effect of i2c_new_device()
1010 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001011 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001012 */
1013void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001014{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001015 if (client->dev.of_node)
1016 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +02001017 device_unregister(&client->dev);
1018}
David Brownell9c1600e2007-05-01 23:26:31 +02001019EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001020
1021
Jean Delvare60b129d2008-05-11 20:37:06 +02001022static const struct i2c_device_id dummy_id[] = {
1023 { "dummy", 0 },
1024 { },
1025};
1026
Jean Delvared2653e92008-04-29 23:11:39 +02001027static int dummy_probe(struct i2c_client *client,
1028 const struct i2c_device_id *id)
1029{
1030 return 0;
1031}
1032
1033static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001034{
1035 return 0;
1036}
1037
1038static struct i2c_driver dummy_driver = {
1039 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001040 .probe = dummy_probe,
1041 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001042 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001043};
1044
1045/**
1046 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1047 * @adapter: the adapter managing the device
1048 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001049 * Context: can sleep
1050 *
1051 * This returns an I2C client bound to the "dummy" driver, intended for use
1052 * with devices that consume multiple addresses. Examples of such chips
1053 * include various EEPROMS (like 24c04 and 24c08 models).
1054 *
1055 * These dummy devices have two main uses. First, most I2C and SMBus calls
1056 * except i2c_transfer() need a client handle; the dummy will be that handle.
1057 * And second, this prevents the specified address from being bound to a
1058 * different driver.
1059 *
1060 * This returns the new i2c client, which should be saved for later use with
1061 * i2c_unregister_device(); or NULL to indicate an error.
1062 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001063struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001064{
1065 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001066 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001067 };
1068
David Brownelle9f13732008-01-27 18:14:52 +01001069 return i2c_new_device(adapter, &info);
1070}
1071EXPORT_SYMBOL_GPL(i2c_new_dummy);
1072
David Brownellf37dd802007-02-13 22:09:00 +01001073/* ------------------------------------------------------------------------- */
1074
David Brownell16ffadf2007-05-01 23:26:28 +02001075/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1076
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001077static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
David Brownellef2c83212007-05-01 23:26:28 +02001079 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 complete(&adap->dev_released);
1081}
1082
Jean Delvare99cd8e22009-06-19 16:58:20 +02001083/*
Jean Delvare390946b2012-09-10 10:14:02 +02001084 * This function is only needed for mutex_lock_nested, so it is never
1085 * called unless locking correctness checking is enabled. Thus we
1086 * make it inline to avoid a compiler warning. That's what gcc ends up
1087 * doing anyway.
1088 */
1089static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1090{
1091 unsigned int depth = 0;
1092
1093 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1094 depth++;
1095
1096 return depth;
1097}
1098
1099/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001100 * Let users instantiate I2C devices through sysfs. This can be used when
1101 * platform initialization code doesn't contain the proper data for
1102 * whatever reason. Also useful for drivers that do device detection and
1103 * detection fails, either because the device uses an unexpected address,
1104 * or this is a compatible device with different ID register values.
1105 *
1106 * Parameter checking may look overzealous, but we really don't want
1107 * the user to provide incorrect parameters.
1108 */
1109static ssize_t
1110i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1111 const char *buf, size_t count)
1112{
1113 struct i2c_adapter *adap = to_i2c_adapter(dev);
1114 struct i2c_board_info info;
1115 struct i2c_client *client;
1116 char *blank, end;
1117 int res;
1118
Jean Delvare99cd8e22009-06-19 16:58:20 +02001119 memset(&info, 0, sizeof(struct i2c_board_info));
1120
1121 blank = strchr(buf, ' ');
1122 if (!blank) {
1123 dev_err(dev, "%s: Missing parameters\n", "new_device");
1124 return -EINVAL;
1125 }
1126 if (blank - buf > I2C_NAME_SIZE - 1) {
1127 dev_err(dev, "%s: Invalid device name\n", "new_device");
1128 return -EINVAL;
1129 }
1130 memcpy(info.type, buf, blank - buf);
1131
1132 /* Parse remaining parameters, reject extra parameters */
1133 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1134 if (res < 1) {
1135 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1136 return -EINVAL;
1137 }
1138 if (res > 1 && end != '\n') {
1139 dev_err(dev, "%s: Extra parameters\n", "new_device");
1140 return -EINVAL;
1141 }
1142
Jean Delvare99cd8e22009-06-19 16:58:20 +02001143 client = i2c_new_device(adap, &info);
1144 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001145 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001146
1147 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001148 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001149 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001150 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001151 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1152 info.type, info.addr);
1153
1154 return count;
1155}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001156static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001157
1158/*
1159 * And of course let the users delete the devices they instantiated, if
1160 * they got it wrong. This interface can only be used to delete devices
1161 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1162 * don't delete devices to which some kernel code still has references.
1163 *
1164 * Parameter checking may look overzealous, but we really don't want
1165 * the user to delete the wrong device.
1166 */
1167static ssize_t
1168i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1169 const char *buf, size_t count)
1170{
1171 struct i2c_adapter *adap = to_i2c_adapter(dev);
1172 struct i2c_client *client, *next;
1173 unsigned short addr;
1174 char end;
1175 int res;
1176
1177 /* Parse parameters, reject extra parameters */
1178 res = sscanf(buf, "%hi%c", &addr, &end);
1179 if (res < 1) {
1180 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1181 return -EINVAL;
1182 }
1183 if (res > 1 && end != '\n') {
1184 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1185 return -EINVAL;
1186 }
1187
1188 /* Make sure the device was added through sysfs */
1189 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001190 mutex_lock_nested(&adap->userspace_clients_lock,
1191 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001192 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1193 detected) {
1194 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001195 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1196 "delete_device", client->name, client->addr);
1197
1198 list_del(&client->detected);
1199 i2c_unregister_device(client);
1200 res = count;
1201 break;
1202 }
1203 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001204 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001205
1206 if (res < 0)
1207 dev_err(dev, "%s: Can't find device in list\n",
1208 "delete_device");
1209 return res;
1210}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001211static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1212 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001213
1214static struct attribute *i2c_adapter_attrs[] = {
1215 &dev_attr_name.attr,
1216 &dev_attr_new_device.attr,
1217 &dev_attr_delete_device.attr,
1218 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001219};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001220ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001221
Michael Lawnick08263742010-08-11 18:21:02 +02001222struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001223 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001224 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001225};
Michael Lawnick08263742010-08-11 18:21:02 +02001226EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Stephen Warren643dd092012-04-17 12:43:33 -06001228/**
1229 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1230 * @dev: device, probably from some driver model iterator
1231 *
1232 * When traversing the driver model tree, perhaps using driver model
1233 * iterators like @device_for_each_child(), you can't assume very much
1234 * about the nodes you find. Use this function to avoid oopses caused
1235 * by wrongly treating some non-I2C device as an i2c_adapter.
1236 */
1237struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1238{
1239 return (dev->type == &i2c_adapter_type)
1240 ? to_i2c_adapter(dev)
1241 : NULL;
1242}
1243EXPORT_SYMBOL(i2c_verify_adapter);
1244
Jean Delvare2bb50952009-09-18 22:45:46 +02001245#ifdef CONFIG_I2C_COMPAT
1246static struct class_compat *i2c_adapter_compat_class;
1247#endif
1248
David Brownell9c1600e2007-05-01 23:26:31 +02001249static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1250{
1251 struct i2c_devinfo *devinfo;
1252
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001253 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001254 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1255 if (devinfo->busnum == adapter->nr
1256 && !i2c_new_device(adapter,
1257 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001258 dev_err(&adapter->dev,
1259 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001260 devinfo->board_info.addr);
1261 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001262 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001263}
1264
Wolfram Sang687b81d2013-07-11 12:56:15 +01001265/* OF support code */
1266
1267#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001268static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1269 struct device_node *node)
1270{
1271 struct i2c_client *result;
1272 struct i2c_board_info info = {};
1273 struct dev_archdata dev_ad = {};
1274 const __be32 *addr;
1275 int len;
1276
1277 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1278
1279 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1280 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1281 node->full_name);
1282 return ERR_PTR(-EINVAL);
1283 }
1284
1285 addr = of_get_property(node, "reg", &len);
Wolfram Sang4c1344f2015-05-19 18:29:48 +02001286 if (!addr || (len < sizeof(*addr))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001287 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1288 node->full_name);
1289 return ERR_PTR(-EINVAL);
1290 }
1291
1292 info.addr = be32_to_cpup(addr);
1293 if (info.addr > (1 << 10) - 1) {
1294 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1295 info.addr, node->full_name);
1296 return ERR_PTR(-EINVAL);
1297 }
1298
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001299 info.of_node = of_node_get(node);
1300 info.archdata = &dev_ad;
1301
1302 if (of_get_property(node, "wakeup-source", NULL))
1303 info.flags |= I2C_CLIENT_WAKE;
1304
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001305 result = i2c_new_device(adap, &info);
1306 if (result == NULL) {
1307 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1308 node->full_name);
1309 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001310 return ERR_PTR(-EINVAL);
1311 }
1312 return result;
1313}
1314
Wolfram Sang687b81d2013-07-11 12:56:15 +01001315static void of_i2c_register_devices(struct i2c_adapter *adap)
1316{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001317 struct device_node *node;
1318
1319 /* Only register child devices if the adapter has a node pointer set */
1320 if (!adap->dev.of_node)
1321 return;
1322
1323 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1324
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001325 for_each_available_child_of_node(adap->dev.of_node, node) {
1326 if (of_node_test_and_set_flag(node, OF_POPULATED))
1327 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001328 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001329 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001330}
1331
1332static int of_dev_node_match(struct device *dev, void *data)
1333{
1334 return dev->of_node == data;
1335}
1336
1337/* must call put_device() when done with returned i2c_client device */
1338struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1339{
1340 struct device *dev;
1341
1342 dev = bus_find_device(&i2c_bus_type, NULL, node,
1343 of_dev_node_match);
1344 if (!dev)
1345 return NULL;
1346
1347 return i2c_verify_client(dev);
1348}
1349EXPORT_SYMBOL(of_find_i2c_device_by_node);
1350
1351/* must call put_device() when done with returned i2c_adapter device */
1352struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1353{
1354 struct device *dev;
1355
1356 dev = bus_find_device(&i2c_bus_type, NULL, node,
1357 of_dev_node_match);
1358 if (!dev)
1359 return NULL;
1360
1361 return i2c_verify_adapter(dev);
1362}
1363EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1364#else
1365static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1366#endif /* CONFIG_OF */
1367
Jean Delvare69b00892009-12-06 17:06:27 +01001368static int i2c_do_add_adapter(struct i2c_driver *driver,
1369 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001370{
Jean Delvare4735c982008-07-14 22:38:36 +02001371 /* Detect supported devices on that bus, and instantiate them */
1372 i2c_detect(adap, driver);
1373
1374 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001375 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001376 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1377 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001378 dev_warn(&adap->dev, "Please use another way to instantiate "
1379 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001380 /* We ignore the return code; if it fails, too bad */
1381 driver->attach_adapter(adap);
1382 }
1383 return 0;
1384}
1385
Jean Delvare69b00892009-12-06 17:06:27 +01001386static int __process_new_adapter(struct device_driver *d, void *data)
1387{
1388 return i2c_do_add_adapter(to_i2c_driver(d), data);
1389}
1390
David Brownell6e13e642007-05-01 23:26:31 +02001391static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Jean Delvared6703282010-08-11 18:20:59 +02001393 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
David Brownell1d0b19c2008-10-14 17:30:05 +02001395 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001396 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1397 res = -EAGAIN;
1398 goto out_list;
1399 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001400
Jean Delvare2236baa2010-11-15 22:40:38 +01001401 /* Sanity checks */
1402 if (unlikely(adap->name[0] == '\0')) {
1403 pr_err("i2c-core: Attempt to register an adapter with "
1404 "no name!\n");
1405 return -EINVAL;
1406 }
1407 if (unlikely(!adap->algo)) {
1408 pr_err("i2c-core: Attempt to register adapter '%s' with "
1409 "no algo!\n", adap->name);
1410 return -EINVAL;
1411 }
1412
Mika Kuoppala194684e2009-12-06 17:06:22 +01001413 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001414 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001415 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Jean Delvare8fcfef62009-03-28 21:34:43 +01001417 /* Set default timeout to 1 second if not already set */
1418 if (adap->timeout == 0)
1419 adap->timeout = HZ;
1420
Kay Sievers27d9c182009-01-07 14:29:16 +01001421 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001422 adap->dev.bus = &i2c_bus_type;
1423 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001424 res = device_register(&adap->dev);
1425 if (res)
1426 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001428 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1429
Charles Keepax6ada5c12015-04-16 13:05:19 +01001430 pm_runtime_no_callbacks(&adap->dev);
1431
Jean Delvare2bb50952009-09-18 22:45:46 +02001432#ifdef CONFIG_I2C_COMPAT
1433 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1434 adap->dev.parent);
1435 if (res)
1436 dev_warn(&adap->dev,
1437 "Failed to create compatibility class link\n");
1438#endif
1439
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301440 /* bus recovery specific initialization */
1441 if (adap->bus_recovery_info) {
1442 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1443
1444 if (!bri->recover_bus) {
1445 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1446 adap->bus_recovery_info = NULL;
1447 goto exit_recovery;
1448 }
1449
1450 /* Generic GPIO recovery */
1451 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1452 if (!gpio_is_valid(bri->scl_gpio)) {
1453 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1454 adap->bus_recovery_info = NULL;
1455 goto exit_recovery;
1456 }
1457
1458 if (gpio_is_valid(bri->sda_gpio))
1459 bri->get_sda = get_sda_gpio_value;
1460 else
1461 bri->get_sda = NULL;
1462
1463 bri->get_scl = get_scl_gpio_value;
1464 bri->set_scl = set_scl_gpio_value;
1465 } else if (!bri->set_scl || !bri->get_scl) {
1466 /* Generic SCL recovery */
1467 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1468 adap->bus_recovery_info = NULL;
1469 }
1470 }
1471
1472exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001473 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001474 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001475 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001476 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001477
David Brownell6e13e642007-05-01 23:26:31 +02001478 if (adap->nr < __i2c_first_dynamic_bus_num)
1479 i2c_scan_static_board_info(adap);
1480
Jean Delvare4735c982008-07-14 22:38:36 +02001481 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001482 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001483 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001484 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001485
1486 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001487
Jean Delvareb119c6c2006-08-15 18:26:30 +02001488out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001489 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001490 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001491 mutex_unlock(&core_lock);
1492 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
David Brownell6e13e642007-05-01 23:26:31 +02001495/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001496 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1497 * @adap: the adapter to register (with adap->nr initialized)
1498 * Context: can sleep
1499 *
1500 * See i2c_add_numbered_adapter() for details.
1501 */
1502static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1503{
1504 int id;
1505
1506 mutex_lock(&core_lock);
1507 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1508 GFP_KERNEL);
1509 mutex_unlock(&core_lock);
1510 if (id < 0)
1511 return id == -ENOSPC ? -EBUSY : id;
1512
1513 return i2c_register_adapter(adap);
1514}
1515
1516/**
David Brownell6e13e642007-05-01 23:26:31 +02001517 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1518 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001519 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001520 *
1521 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001522 * doesn't matter or when its bus number is specified by an dt alias.
1523 * Examples of bases when the bus number doesn't matter: I2C adapters
1524 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001525 *
1526 * When this returns zero, a new bus number was allocated and stored
1527 * in adap->nr, and the specified adapter became available for clients.
1528 * Otherwise, a negative errno value is returned.
1529 */
1530int i2c_add_adapter(struct i2c_adapter *adapter)
1531{
Doug Andersonee5c2742013-03-01 08:57:31 -08001532 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001533 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001534
Doug Andersonee5c2742013-03-01 08:57:31 -08001535 if (dev->of_node) {
1536 id = of_alias_get_id(dev->of_node, "i2c");
1537 if (id >= 0) {
1538 adapter->nr = id;
1539 return __i2c_add_numbered_adapter(adapter);
1540 }
1541 }
1542
Jean Delvarecaada322008-01-27 18:14:49 +01001543 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001544 id = idr_alloc(&i2c_adapter_idr, adapter,
1545 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001546 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001547 if (id < 0)
1548 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001549
1550 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001551
David Brownell6e13e642007-05-01 23:26:31 +02001552 return i2c_register_adapter(adapter);
1553}
1554EXPORT_SYMBOL(i2c_add_adapter);
1555
1556/**
1557 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1558 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001559 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001560 *
1561 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001562 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1563 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001564 * is used to properly configure I2C devices.
1565 *
Grant Likely488bf312011-07-25 17:49:43 +02001566 * If the requested bus number is set to -1, then this function will behave
1567 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1568 *
David Brownell6e13e642007-05-01 23:26:31 +02001569 * If no devices have pre-been declared for this bus, then be sure to
1570 * register the adapter before any dynamically allocated ones. Otherwise
1571 * the required bus ID may not be available.
1572 *
1573 * When this returns zero, the specified adapter became available for
1574 * clients using the bus number provided in adap->nr. Also, the table
1575 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1576 * and the appropriate driver model device nodes are created. Otherwise, a
1577 * negative errno value is returned.
1578 */
1579int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1580{
Grant Likely488bf312011-07-25 17:49:43 +02001581 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1582 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001583
Doug Andersonee5c2742013-03-01 08:57:31 -08001584 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001585}
1586EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1587
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001588static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001589 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001590{
Jean Delvare4735c982008-07-14 22:38:36 +02001591 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001592
Jean Delvareacec2112009-03-28 21:34:40 +01001593 /* Remove the devices we created ourselves as the result of hardware
1594 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001595 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1596 if (client->adapter == adapter) {
1597 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1598 client->name, client->addr);
1599 list_del(&client->detected);
1600 i2c_unregister_device(client);
1601 }
1602 }
Jean Delvare026526f2008-01-27 18:14:49 +01001603}
1604
Jean Delvaree549c2b2009-06-19 16:58:19 +02001605static int __unregister_client(struct device *dev, void *dummy)
1606{
1607 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001608 if (client && strcmp(client->name, "dummy"))
1609 i2c_unregister_device(client);
1610 return 0;
1611}
1612
1613static int __unregister_dummy(struct device *dev, void *dummy)
1614{
1615 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001616 if (client)
1617 i2c_unregister_device(client);
1618 return 0;
1619}
1620
Jean Delvare69b00892009-12-06 17:06:27 +01001621static int __process_removed_adapter(struct device_driver *d, void *data)
1622{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001623 i2c_do_del_adapter(to_i2c_driver(d), data);
1624 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001625}
1626
David Brownelld64f73b2007-07-12 14:12:28 +02001627/**
1628 * i2c_del_adapter - unregister I2C adapter
1629 * @adap: the adapter being unregistered
1630 * Context: can sleep
1631 *
1632 * This unregisters an I2C adapter which was previously registered
1633 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1634 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001635void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001637 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001638 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001641 mutex_lock(&core_lock);
1642 found = idr_find(&i2c_adapter_idr, adap->nr);
1643 mutex_unlock(&core_lock);
1644 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001645 pr_debug("i2c-core: attempting to delete unregistered "
1646 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Lan Tianyu5d98e612014-05-20 20:59:23 +08001650 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001651 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001652 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001653 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001654 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001655 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001657 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001658 mutex_lock_nested(&adap->userspace_clients_lock,
1659 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001660 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1661 detected) {
1662 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1663 client->addr);
1664 list_del(&client->detected);
1665 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001666 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001667 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001668
Jean Delvaree549c2b2009-06-19 16:58:19 +02001669 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001670 * check the returned value. This is a two-pass process, because
1671 * we can't remove the dummy devices during the first pass: they
1672 * could have been instantiated by real devices wishing to clean
1673 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001674 device_for_each_child(&adap->dev, NULL, __unregister_client);
1675 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jean Delvare2bb50952009-09-18 22:45:46 +02001677#ifdef CONFIG_I2C_COMPAT
1678 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1679 adap->dev.parent);
1680#endif
1681
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001682 /* device name is gone after device_unregister */
1683 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1684
Wolfram Sang26680ee2015-01-29 22:45:09 +01001685 /* wait until all references to the device are gone
1686 *
1687 * FIXME: This is old code and should ideally be replaced by an
1688 * alternative which results in decoupling the lifetime of the struct
1689 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301690 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David Brownell6e13e642007-05-01 23:26:31 +02001696 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001697 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001699 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001701 /* Clear the device structure in case this adapter is ever going to be
1702 added again */
1703 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
David Brownellc0564602007-05-01 23:26:31 +02001705EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
David Brownell7b4fbc52007-05-01 23:26:30 +02001707/* ------------------------------------------------------------------------- */
1708
Jean Delvare7ae31482011-03-20 14:50:52 +01001709int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1710{
1711 int res;
1712
1713 mutex_lock(&core_lock);
1714 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1715 mutex_unlock(&core_lock);
1716
1717 return res;
1718}
1719EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1720
Jean Delvare69b00892009-12-06 17:06:27 +01001721static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001722{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001723 if (dev->type != &i2c_adapter_type)
1724 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001725 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001726}
1727
David Brownell7b4fbc52007-05-01 23:26:30 +02001728/*
1729 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001730 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 */
1732
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001733int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001735 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
David Brownell1d0b19c2008-10-14 17:30:05 +02001737 /* Can't register until after driver model init */
1738 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1739 return -EAGAIN;
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001742 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jean Delvare729d6dd2009-06-19 16:58:18 +02001745 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001746 * will have called probe() for all matching-but-unbound devices.
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 res = driver_register(&driver->driver);
1749 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001750 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001751
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001752 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Jean Delvare4735c982008-07-14 22:38:36 +02001754 INIT_LIST_HEAD(&driver->clients);
1755 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001756 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001757
Jean Delvare7eebcb72006-02-05 23:28:21 +01001758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001760EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Jean Delvare69b00892009-12-06 17:06:27 +01001762static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001763{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001764 if (dev->type == &i2c_adapter_type)
1765 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1766 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001767}
1768
David Brownella1d9e6e2007-05-01 23:26:30 +02001769/**
1770 * i2c_del_driver - unregister I2C driver
1771 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001772 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001773 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001774void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Jean Delvare7ae31482011-03-20 14:50:52 +01001776 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001779 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
David Brownellc0564602007-05-01 23:26:31 +02001781EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
David Brownell7b4fbc52007-05-01 23:26:30 +02001783/* ------------------------------------------------------------------------- */
1784
Jean Delvaree48d3312008-01-27 18:14:48 +01001785/**
1786 * i2c_use_client - increments the reference count of the i2c client structure
1787 * @client: the client being referenced
1788 *
1789 * Each live reference to a client should be refcounted. The driver model does
1790 * that automatically as part of driver binding, so that most drivers don't
1791 * need to do this explicitly: they hold a reference until they're unbound
1792 * from the device.
1793 *
1794 * A pointer to the client with the incremented reference counter is returned.
1795 */
1796struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
David Brownell6ea438e2008-07-14 22:38:24 +02001798 if (client && get_device(&client->dev))
1799 return client;
1800 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
David Brownellc0564602007-05-01 23:26:31 +02001802EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Jean Delvaree48d3312008-01-27 18:14:48 +01001804/**
1805 * i2c_release_client - release a use of the i2c client structure
1806 * @client: the client being no longer referenced
1807 *
1808 * Must be called when a user of a client is finished with it.
1809 */
1810void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
David Brownell6ea438e2008-07-14 22:38:24 +02001812 if (client)
1813 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
David Brownellc0564602007-05-01 23:26:31 +02001815EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
David Brownell9b766b82008-01-27 18:14:51 +01001817struct i2c_cmd_arg {
1818 unsigned cmd;
1819 void *arg;
1820};
1821
1822static int i2c_cmd(struct device *dev, void *_arg)
1823{
1824 struct i2c_client *client = i2c_verify_client(dev);
1825 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001826 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001827
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001828 if (!client || !client->dev.driver)
1829 return 0;
1830
1831 driver = to_i2c_driver(client->dev.driver);
1832 if (driver->command)
1833 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001834 return 0;
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1838{
David Brownell9b766b82008-01-27 18:14:51 +01001839 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
David Brownell9b766b82008-01-27 18:14:51 +01001841 cmd_arg.cmd = cmd;
1842 cmd_arg.arg = arg;
1843 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
David Brownellc0564602007-05-01 23:26:31 +02001845EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001847#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1848static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1849 void *arg)
1850{
1851 struct of_reconfig_data *rd = arg;
1852 struct i2c_adapter *adap;
1853 struct i2c_client *client;
1854
1855 switch (of_reconfig_get_state_change(action, rd)) {
1856 case OF_RECONFIG_CHANGE_ADD:
1857 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1858 if (adap == NULL)
1859 return NOTIFY_OK; /* not for us */
1860
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001861 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1862 put_device(&adap->dev);
1863 return NOTIFY_OK;
1864 }
1865
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001866 client = of_i2c_register_device(adap, rd->dn);
1867 put_device(&adap->dev);
1868
1869 if (IS_ERR(client)) {
1870 pr_err("%s: failed to create for '%s'\n",
1871 __func__, rd->dn->full_name);
1872 return notifier_from_errno(PTR_ERR(client));
1873 }
1874 break;
1875 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001876 /* already depopulated? */
1877 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1878 return NOTIFY_OK;
1879
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001880 /* find our device by node */
1881 client = of_find_i2c_device_by_node(rd->dn);
1882 if (client == NULL)
1883 return NOTIFY_OK; /* no? not meant for us */
1884
1885 /* unregister takes one ref away */
1886 i2c_unregister_device(client);
1887
1888 /* and put the reference of the find */
1889 put_device(&client->dev);
1890 break;
1891 }
1892
1893 return NOTIFY_OK;
1894}
1895static struct notifier_block i2c_of_notifier = {
1896 .notifier_call = of_i2c_notify,
1897};
1898#else
1899extern struct notifier_block i2c_of_notifier;
1900#endif /* CONFIG_OF_DYNAMIC */
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902static int __init i2c_init(void)
1903{
1904 int retval;
1905
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001906 retval = of_alias_get_highest_id("i2c");
1907
1908 down_write(&__i2c_board_lock);
1909 if (retval >= __i2c_first_dynamic_bus_num)
1910 __i2c_first_dynamic_bus_num = retval + 1;
1911 up_write(&__i2c_board_lock);
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 retval = bus_register(&i2c_bus_type);
1914 if (retval)
1915 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001916#ifdef CONFIG_I2C_COMPAT
1917 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1918 if (!i2c_adapter_compat_class) {
1919 retval = -ENOMEM;
1920 goto bus_err;
1921 }
1922#endif
David Brownelle9f13732008-01-27 18:14:52 +01001923 retval = i2c_add_driver(&dummy_driver);
1924 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001925 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001926
1927 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1928 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1929
David Brownelle9f13732008-01-27 18:14:52 +01001930 return 0;
1931
Jean Delvare2bb50952009-09-18 22:45:46 +02001932class_err:
1933#ifdef CONFIG_I2C_COMPAT
1934 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001935bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001936#endif
David Brownelle9f13732008-01-27 18:14:52 +01001937 bus_unregister(&i2c_bus_type);
1938 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941static void __exit i2c_exit(void)
1942{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001943 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1944 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001945 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001946#ifdef CONFIG_I2C_COMPAT
1947 class_compat_unregister(i2c_adapter_compat_class);
1948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001950 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
David Brownella10f9e72008-10-14 17:30:06 +02001953/* We must initialize early, because some subsystems register i2c drivers
1954 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1955 */
1956postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957module_exit(i2c_exit);
1958
1959/* ----------------------------------------------------
1960 * the functional interface to the i2c busses.
1961 * ----------------------------------------------------
1962 */
1963
Wolfram Sangb7f62582015-01-05 23:45:59 +01001964/* Check if val is exceeding the quirk IFF quirk is non 0 */
1965#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1966
1967static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1968{
1969 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1970 err_msg, msg->addr, msg->len,
1971 msg->flags & I2C_M_RD ? "read" : "write");
1972 return -EOPNOTSUPP;
1973}
1974
1975static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1976{
1977 const struct i2c_adapter_quirks *q = adap->quirks;
1978 int max_num = q->max_num_msgs, i;
1979 bool do_len_check = true;
1980
1981 if (q->flags & I2C_AQ_COMB) {
1982 max_num = 2;
1983
1984 /* special checks for combined messages */
1985 if (num == 2) {
1986 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1987 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1988
1989 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1990 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1991
1992 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1993 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1994
1995 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1996 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1997
1998 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1999 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2000
2001 do_len_check = false;
2002 }
2003 }
2004
2005 if (i2c_quirk_exceeded(num, max_num))
2006 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2007
2008 for (i = 0; i < num; i++) {
2009 u16 len = msgs[i].len;
2010
2011 if (msgs[i].flags & I2C_M_RD) {
2012 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2013 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2014 } else {
2015 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2016 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2017 }
2018 }
2019
2020 return 0;
2021}
2022
David Brownella1cdeda2008-07-14 22:38:24 +02002023/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002024 * __i2c_transfer - unlocked flavor of i2c_transfer
2025 * @adap: Handle to I2C bus
2026 * @msgs: One or more messages to execute before STOP is issued to
2027 * terminate the operation; each message begins with a START.
2028 * @num: Number of messages to be executed.
2029 *
2030 * Returns negative errno, else the number of messages executed.
2031 *
2032 * Adapter lock must be held when calling this function. No debug logging
2033 * takes place. adap->algo->master_xfer existence isn't checked.
2034 */
2035int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2036{
2037 unsigned long orig_jiffies;
2038 int ret, try;
2039
Wolfram Sangb7f62582015-01-05 23:45:59 +01002040 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2041 return -EOPNOTSUPP;
2042
David Howellsd9a83d62014-03-06 13:35:59 +00002043 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2044 * enabled. This is an efficient way of keeping the for-loop from
2045 * being executed when not needed.
2046 */
2047 if (static_key_false(&i2c_trace_msg)) {
2048 int i;
2049 for (i = 0; i < num; i++)
2050 if (msgs[i].flags & I2C_M_RD)
2051 trace_i2c_read(adap, &msgs[i], i);
2052 else
2053 trace_i2c_write(adap, &msgs[i], i);
2054 }
2055
Jean Delvareb37d2a32012-06-29 07:47:19 -03002056 /* Retry automatically on arbitration loss */
2057 orig_jiffies = jiffies;
2058 for (ret = 0, try = 0; try <= adap->retries; try++) {
2059 ret = adap->algo->master_xfer(adap, msgs, num);
2060 if (ret != -EAGAIN)
2061 break;
2062 if (time_after(jiffies, orig_jiffies + adap->timeout))
2063 break;
2064 }
2065
David Howellsd9a83d62014-03-06 13:35:59 +00002066 if (static_key_false(&i2c_trace_msg)) {
2067 int i;
2068 for (i = 0; i < ret; i++)
2069 if (msgs[i].flags & I2C_M_RD)
2070 trace_i2c_reply(adap, &msgs[i], i);
2071 trace_i2c_result(adap, i, ret);
2072 }
2073
Jean Delvareb37d2a32012-06-29 07:47:19 -03002074 return ret;
2075}
2076EXPORT_SYMBOL(__i2c_transfer);
2077
2078/**
David Brownella1cdeda2008-07-14 22:38:24 +02002079 * i2c_transfer - execute a single or combined I2C message
2080 * @adap: Handle to I2C bus
2081 * @msgs: One or more messages to execute before STOP is issued to
2082 * terminate the operation; each message begins with a START.
2083 * @num: Number of messages to be executed.
2084 *
2085 * Returns negative errno, else the number of messages executed.
2086 *
2087 * Note that there is no requirement that each message be sent to
2088 * the same slave address, although that is the most common model.
2089 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002090int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
David Brownella1cdeda2008-07-14 22:38:24 +02002094 /* REVISIT the fault reporting model here is weak:
2095 *
2096 * - When we get an error after receiving N bytes from a slave,
2097 * there is no way to report "N".
2098 *
2099 * - When we get a NAK after transmitting N bytes to a slave,
2100 * there is no way to report "N" ... or to let the master
2101 * continue executing the rest of this combined message, if
2102 * that's the appropriate response.
2103 *
2104 * - When for example "num" is two and we successfully complete
2105 * the first message but get an error part way through the
2106 * second, it's unclear whether that should be reported as
2107 * one (discarding status on the second message) or errno
2108 * (discarding status on the first one).
2109 */
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (adap->algo->master_xfer) {
2112#ifdef DEBUG
2113 for (ret = 0; ret < num; ret++) {
2114 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002115 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2116 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2117 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119#endif
2120
Mike Rapoportcea443a2008-01-27 18:14:50 +01002121 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002122 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002123 if (!ret)
2124 /* I2C activity is ongoing. */
2125 return -EAGAIN;
2126 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002127 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002128 }
2129
Jean Delvareb37d2a32012-06-29 07:47:19 -03002130 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002131 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 return ret;
2134 } else {
2135 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002136 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138}
David Brownellc0564602007-05-01 23:26:31 +02002139EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
David Brownella1cdeda2008-07-14 22:38:24 +02002141/**
2142 * i2c_master_send - issue a single I2C message in master transmit mode
2143 * @client: Handle to slave device
2144 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002145 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002146 *
2147 * Returns negative errno, or else the number of bytes written.
2148 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002149int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002152 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct i2c_msg msg;
2154
Jean Delvare815f55f2005-05-07 22:58:46 +02002155 msg.addr = client->addr;
2156 msg.flags = client->flags & I2C_M_TEN;
2157 msg.len = count;
2158 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002159
Jean Delvare815f55f2005-05-07 22:58:46 +02002160 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002162 /*
2163 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2164 * transmitted, else error code.
2165 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002166 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
David Brownellc0564602007-05-01 23:26:31 +02002168EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David Brownella1cdeda2008-07-14 22:38:24 +02002170/**
2171 * i2c_master_recv - issue a single I2C message in master receive mode
2172 * @client: Handle to slave device
2173 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002174 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002175 *
2176 * Returns negative errno, or else the number of bytes read.
2177 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002178int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
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 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Jean Delvare815f55f2005-05-07 22:58:46 +02002184 msg.addr = client->addr;
2185 msg.flags = client->flags & I2C_M_TEN;
2186 msg.flags |= I2C_M_RD;
2187 msg.len = count;
2188 msg.buf = buf;
2189
2190 ret = i2c_transfer(adap, &msg, 1);
2191
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002192 /*
2193 * If everything went ok (i.e. 1 msg received), return #bytes received,
2194 * else error code.
2195 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002196 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
David Brownellc0564602007-05-01 23:26:31 +02002198EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/* ----------------------------------------------------
2201 * the i2c address scanning function
2202 * Will not work for 10-bit addresses!
2203 * ----------------------------------------------------
2204 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002205
Jean Delvare63e4e802010-06-03 11:33:51 +02002206/*
2207 * Legacy default probe function, mostly relevant for SMBus. The default
2208 * probe method is a quick write, but it is known to corrupt the 24RF08
2209 * EEPROMs due to a state machine bug, and could also irreversibly
2210 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2211 * we use a short byte read instead. Also, some bus drivers don't implement
2212 * quick write, so we fallback to a byte read in that case too.
2213 * On x86, there is another special case for FSC hardware monitoring chips,
2214 * which want regular byte reads (address 0x73.) Fortunately, these are the
2215 * only known chips using this I2C address on PC hardware.
2216 * Returns 1 if probe succeeded, 0 if not.
2217 */
2218static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2219{
2220 int err;
2221 union i2c_smbus_data dummy;
2222
2223#ifdef CONFIG_X86
2224 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2225 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2226 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2227 I2C_SMBUS_BYTE_DATA, &dummy);
2228 else
2229#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002230 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2231 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002232 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2233 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002234 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2235 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2236 I2C_SMBUS_BYTE, &dummy);
2237 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002238 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2239 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002240 err = -EOPNOTSUPP;
2241 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002242
2243 return err >= 0;
2244}
2245
Jean Delvareccfbbd02009-12-06 17:06:25 +01002246static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002247 struct i2c_driver *driver)
2248{
2249 struct i2c_board_info info;
2250 struct i2c_adapter *adapter = temp_client->adapter;
2251 int addr = temp_client->addr;
2252 int err;
2253
2254 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002255 err = i2c_check_addr_validity(addr);
2256 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002257 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2258 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002259 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002260 }
2261
2262 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002263 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002264 return 0;
2265
Jean Delvareccfbbd02009-12-06 17:06:25 +01002266 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002267 if (!i2c_default_probe(adapter, addr))
2268 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002269
2270 /* Finally call the custom detection function */
2271 memset(&info, 0, sizeof(struct i2c_board_info));
2272 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002273 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002274 if (err) {
2275 /* -ENODEV is returned if the detection fails. We catch it
2276 here as this isn't an error. */
2277 return err == -ENODEV ? 0 : err;
2278 }
2279
2280 /* Consistency check */
2281 if (info.type[0] == '\0') {
2282 dev_err(&adapter->dev, "%s detection function provided "
2283 "no name for 0x%x\n", driver->driver.name,
2284 addr);
2285 } else {
2286 struct i2c_client *client;
2287
2288 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002289 if (adapter->class & I2C_CLASS_DEPRECATED)
2290 dev_warn(&adapter->dev,
2291 "This adapter will soon drop class based instantiation of devices. "
2292 "Please make sure client 0x%02x gets instantiated by other means. "
2293 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2294 info.addr);
2295
Jean Delvare4735c982008-07-14 22:38:36 +02002296 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2297 info.type, info.addr);
2298 client = i2c_new_device(adapter, &info);
2299 if (client)
2300 list_add_tail(&client->detected, &driver->clients);
2301 else
2302 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2303 info.type, info.addr);
2304 }
2305 return 0;
2306}
2307
2308static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2309{
Jean Delvarec3813d62009-12-14 21:17:25 +01002310 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002311 struct i2c_client *temp_client;
2312 int i, err = 0;
2313 int adap_id = i2c_adapter_id(adapter);
2314
Jean Delvarec3813d62009-12-14 21:17:25 +01002315 address_list = driver->address_list;
2316 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002317 return 0;
2318
Wolfram Sang45552272014-07-10 13:46:21 +02002319 /* Warn that the adapter lost class based instantiation */
2320 if (adapter->class == I2C_CLASS_DEPRECATED) {
2321 dev_dbg(&adapter->dev,
2322 "This adapter dropped support for I2C classes and "
2323 "won't auto-detect %s devices anymore. If you need it, check "
2324 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2325 driver->driver.name);
2326 return 0;
2327 }
2328
Jean Delvare51b54ba2010-10-24 18:16:58 +02002329 /* Stop here if the classes do not match */
2330 if (!(adapter->class & driver->class))
2331 return 0;
2332
Jean Delvare4735c982008-07-14 22:38:36 +02002333 /* Set up a temporary client to help detect callback */
2334 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2335 if (!temp_client)
2336 return -ENOMEM;
2337 temp_client->adapter = adapter;
2338
Jean Delvarec3813d62009-12-14 21:17:25 +01002339 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002340 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002341 "addr 0x%02x\n", adap_id, address_list[i]);
2342 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002343 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002344 if (unlikely(err))
2345 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002346 }
2347
Jean Delvare4735c982008-07-14 22:38:36 +02002348 kfree(temp_client);
2349 return err;
2350}
2351
Jean Delvared44f19d2010-08-11 18:20:57 +02002352int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2353{
2354 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2355 I2C_SMBUS_QUICK, NULL) >= 0;
2356}
2357EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2358
Jean Delvare12b5053a2007-05-01 23:26:31 +02002359struct i2c_client *
2360i2c_new_probed_device(struct i2c_adapter *adap,
2361 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002362 unsigned short const *addr_list,
2363 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002364{
2365 int i;
2366
Jean Delvare8031d792010-08-11 18:21:00 +02002367 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002368 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002369
Jean Delvare12b5053a2007-05-01 23:26:31 +02002370 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2371 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002372 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002373 dev_warn(&adap->dev, "Invalid 7-bit address "
2374 "0x%02x\n", addr_list[i]);
2375 continue;
2376 }
2377
2378 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002379 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002380 dev_dbg(&adap->dev, "Address 0x%02x already in "
2381 "use, not probing\n", addr_list[i]);
2382 continue;
2383 }
2384
Jean Delvare63e4e802010-06-03 11:33:51 +02002385 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002386 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002387 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002388 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002389
2390 if (addr_list[i] == I2C_CLIENT_END) {
2391 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2392 return NULL;
2393 }
2394
2395 info->addr = addr_list[i];
2396 return i2c_new_device(adap, info);
2397}
2398EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2399
Jean Delvared735b342011-03-20 14:50:52 +01002400struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002403
Jean Delvarecaada322008-01-27 18:14:49 +01002404 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002405 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002406 if (adapter && !try_module_get(adapter->owner))
2407 adapter = NULL;
2408
Jean Delvarecaada322008-01-27 18:14:49 +01002409 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002410 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
David Brownellc0564602007-05-01 23:26:31 +02002412EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414void i2c_put_adapter(struct i2c_adapter *adap)
2415{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002416 if (adap)
2417 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
David Brownellc0564602007-05-01 23:26:31 +02002419EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421/* The SMBus parts */
2422
David Brownell438d6c22006-12-10 21:21:31 +01002423#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002424static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002427
Farid Hammane7225acf2010-05-21 18:40:58 +02002428 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002429 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 data = data ^ POLY;
2431 data = data << 1;
2432 }
2433 return (u8)(data >> 8);
2434}
2435
Jean Delvare421ef472005-10-26 21:28:55 +02002436/* Incremental CRC8 over count bytes in the array pointed to by p */
2437static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 int i;
2440
Farid Hammane7225acf2010-05-21 18:40:58 +02002441 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002442 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return crc;
2444}
2445
Jean Delvare421ef472005-10-26 21:28:55 +02002446/* Assume a 7-bit address, which is reasonable for SMBus */
2447static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Jean Delvare421ef472005-10-26 21:28:55 +02002449 /* The address will be sent first */
2450 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2451 pec = i2c_smbus_pec(pec, &addr, 1);
2452
2453 /* The data buffer follows */
2454 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Jean Delvare421ef472005-10-26 21:28:55 +02002457/* Used for write only transactions */
2458static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Jean Delvare421ef472005-10-26 21:28:55 +02002460 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2461 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Jean Delvare421ef472005-10-26 21:28:55 +02002464/* Return <0 on CRC error
2465 If there was a write before this read (most cases) we need to take the
2466 partial CRC from the write part into account.
2467 Note that this function does modify the message (we need to decrease the
2468 message length to hide the CRC byte from the caller). */
2469static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Jean Delvare421ef472005-10-26 21:28:55 +02002471 u8 rpec = msg->buf[--msg->len];
2472 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (rpec != cpec) {
2475 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2476 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002477 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
David Brownell438d6c22006-12-10 21:21:31 +01002479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
David Brownella1cdeda2008-07-14 22:38:24 +02002482/**
2483 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2484 * @client: Handle to slave device
2485 *
2486 * This executes the SMBus "receive byte" protocol, returning negative errno
2487 * else the byte received from the device.
2488 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002489s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
2491 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002492 int status;
2493
2494 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2495 I2C_SMBUS_READ, 0,
2496 I2C_SMBUS_BYTE, &data);
2497 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
David Brownellc0564602007-05-01 23:26:31 +02002499EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
David Brownella1cdeda2008-07-14 22:38:24 +02002501/**
2502 * i2c_smbus_write_byte - SMBus "send byte" protocol
2503 * @client: Handle to slave device
2504 * @value: Byte to be sent
2505 *
2506 * This executes the SMBus "send byte" protocol, returning negative errno
2507 * else zero on success.
2508 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002509s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
Farid Hammane7225acf2010-05-21 18:40:58 +02002511 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002512 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
David Brownellc0564602007-05-01 23:26:31 +02002514EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
David Brownella1cdeda2008-07-14 22:38:24 +02002516/**
2517 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2518 * @client: Handle to slave device
2519 * @command: Byte interpreted by slave
2520 *
2521 * This executes the SMBus "read byte" protocol, returning negative errno
2522 * else a data byte received from the device.
2523 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002524s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002527 int status;
2528
2529 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2530 I2C_SMBUS_READ, command,
2531 I2C_SMBUS_BYTE_DATA, &data);
2532 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
David Brownellc0564602007-05-01 23:26:31 +02002534EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
David Brownella1cdeda2008-07-14 22:38:24 +02002536/**
2537 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2538 * @client: Handle to slave device
2539 * @command: Byte interpreted by slave
2540 * @value: Byte being written
2541 *
2542 * This executes the SMBus "write byte" protocol, returning negative errno
2543 * else zero on success.
2544 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002545s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2546 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
2548 union i2c_smbus_data data;
2549 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002550 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2551 I2C_SMBUS_WRITE, command,
2552 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
David Brownellc0564602007-05-01 23:26:31 +02002554EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
David Brownella1cdeda2008-07-14 22:38:24 +02002556/**
2557 * i2c_smbus_read_word_data - SMBus "read word" protocol
2558 * @client: Handle to slave device
2559 * @command: Byte interpreted by slave
2560 *
2561 * This executes the SMBus "read word" protocol, returning negative errno
2562 * else a 16-bit unsigned "word" received from the device.
2563 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002564s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002567 int status;
2568
2569 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2570 I2C_SMBUS_READ, command,
2571 I2C_SMBUS_WORD_DATA, &data);
2572 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
David Brownellc0564602007-05-01 23:26:31 +02002574EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David Brownella1cdeda2008-07-14 22:38:24 +02002576/**
2577 * i2c_smbus_write_word_data - SMBus "write word" protocol
2578 * @client: Handle to slave device
2579 * @command: Byte interpreted by slave
2580 * @value: 16-bit "word" being written
2581 *
2582 * This executes the SMBus "write word" protocol, returning negative errno
2583 * else zero on success.
2584 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002585s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2586 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 union i2c_smbus_data data;
2589 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002590 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2591 I2C_SMBUS_WRITE, command,
2592 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
David Brownellc0564602007-05-01 23:26:31 +02002594EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
David Brownella64ec072007-10-13 23:56:31 +02002596/**
David Brownella1cdeda2008-07-14 22:38:24 +02002597 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002598 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002599 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002600 * @values: Byte array into which data will be read; big enough to hold
2601 * the data returned by the slave. SMBus allows at most 32 bytes.
2602 *
David Brownella1cdeda2008-07-14 22:38:24 +02002603 * This executes the SMBus "block read" protocol, returning negative errno
2604 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002605 *
2606 * Note that using this function requires that the client's adapter support
2607 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2608 * support this; its emulation through I2C messaging relies on a specific
2609 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2610 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002611s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002612 u8 *values)
2613{
2614 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002615 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002616
David Brownell24a5bb72008-07-14 22:38:23 +02002617 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2618 I2C_SMBUS_READ, command,
2619 I2C_SMBUS_BLOCK_DATA, &data);
2620 if (status)
2621 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002622
2623 memcpy(values, &data.block[1], data.block[0]);
2624 return data.block[0];
2625}
2626EXPORT_SYMBOL(i2c_smbus_read_block_data);
2627
David Brownella1cdeda2008-07-14 22:38:24 +02002628/**
2629 * i2c_smbus_write_block_data - SMBus "block write" protocol
2630 * @client: Handle to slave device
2631 * @command: Byte interpreted by slave
2632 * @length: Size of data block; SMBus allows at most 32 bytes
2633 * @values: Byte array which will be written.
2634 *
2635 * This executes the SMBus "block write" protocol, returning negative errno
2636 * else zero on success.
2637 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002638s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002639 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (length > I2C_SMBUS_BLOCK_MAX)
2644 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002646 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002647 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2648 I2C_SMBUS_WRITE, command,
2649 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
David Brownellc0564602007-05-01 23:26:31 +02002651EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002654s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002655 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002658 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002659
Jean Delvare4b2643d2007-07-12 14:12:29 +02002660 if (length > I2C_SMBUS_BLOCK_MAX)
2661 length = I2C_SMBUS_BLOCK_MAX;
2662 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002663 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2664 I2C_SMBUS_READ, command,
2665 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2666 if (status < 0)
2667 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002668
2669 memcpy(values, &data.block[1], data.block[0]);
2670 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
David Brownellc0564602007-05-01 23:26:31 +02002672EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Jean Delvare0cc43a12011-01-10 22:11:23 +01002674s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002675 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002676{
2677 union i2c_smbus_data data;
2678
2679 if (length > I2C_SMBUS_BLOCK_MAX)
2680 length = I2C_SMBUS_BLOCK_MAX;
2681 data.block[0] = length;
2682 memcpy(data.block + 1, values, length);
2683 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2684 I2C_SMBUS_WRITE, command,
2685 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2686}
David Brownellc0564602007-05-01 23:26:31 +02002687EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002688
David Brownell438d6c22006-12-10 21:21:31 +01002689/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002691static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2692 unsigned short flags,
2693 char read_write, u8 command, int size,
2694 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 /* So we need to generate a series of msgs. In the case of writing, we
2697 need to use only one message; when reading, we need two. We initialize
2698 most things with sane defaults, to keep the code below somewhat
2699 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002700 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2701 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002702 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002704 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002705 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002706 struct i2c_msg msg[2] = {
2707 {
2708 .addr = addr,
2709 .flags = flags,
2710 .len = 1,
2711 .buf = msgbuf0,
2712 }, {
2713 .addr = addr,
2714 .flags = flags | I2C_M_RD,
2715 .len = 0,
2716 .buf = msgbuf1,
2717 },
2718 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002721 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 case I2C_SMBUS_QUICK:
2723 msg[0].len = 0;
2724 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002725 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2726 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 num = 1;
2728 break;
2729 case I2C_SMBUS_BYTE:
2730 if (read_write == I2C_SMBUS_READ) {
2731 /* Special case: only a read! */
2732 msg[0].flags = I2C_M_RD | flags;
2733 num = 1;
2734 }
2735 break;
2736 case I2C_SMBUS_BYTE_DATA:
2737 if (read_write == I2C_SMBUS_READ)
2738 msg[1].len = 1;
2739 else {
2740 msg[0].len = 2;
2741 msgbuf0[1] = data->byte;
2742 }
2743 break;
2744 case I2C_SMBUS_WORD_DATA:
2745 if (read_write == I2C_SMBUS_READ)
2746 msg[1].len = 2;
2747 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002748 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002750 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
2752 break;
2753 case I2C_SMBUS_PROC_CALL:
2754 num = 2; /* Special case */
2755 read_write = I2C_SMBUS_READ;
2756 msg[0].len = 3;
2757 msg[1].len = 2;
2758 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002759 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 break;
2761 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002763 msg[1].flags |= I2C_M_RECV_LEN;
2764 msg[1].len = 1; /* block length will be added by
2765 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 } else {
2767 msg[0].len = data->block[0] + 2;
2768 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002769 dev_err(&adapter->dev,
2770 "Invalid block write size %d\n",
2771 data->block[0]);
2772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002774 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 msgbuf0[i] = data->block[i-1];
2776 }
2777 break;
2778 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002779 num = 2; /* Another special case */
2780 read_write = I2C_SMBUS_READ;
2781 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002782 dev_err(&adapter->dev,
2783 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002784 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002785 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002786 }
2787 msg[0].len = data->block[0] + 2;
2788 for (i = 1; i < msg[0].len; i++)
2789 msgbuf0[i] = data->block[i-1];
2790 msg[1].flags |= I2C_M_RECV_LEN;
2791 msg[1].len = 1; /* block length will be added by
2792 the underlying bus driver */
2793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case I2C_SMBUS_I2C_BLOCK_DATA:
2795 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002796 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 } else {
2798 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002799 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002800 dev_err(&adapter->dev,
2801 "Invalid block write size %d\n",
2802 data->block[0]);
2803 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805 for (i = 1; i <= data->block[0]; i++)
2806 msgbuf0[i] = data->block[i];
2807 }
2808 break;
2809 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002810 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2811 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813
Jean Delvare421ef472005-10-26 21:28:55 +02002814 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2815 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2816 if (i) {
2817 /* Compute PEC if first message is a write */
2818 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002819 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002820 i2c_smbus_add_pec(&msg[0]);
2821 else /* Write followed by read */
2822 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2823 }
2824 /* Ask for PEC if last message is a read */
2825 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002826 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002827 }
2828
David Brownell24a5bb72008-07-14 22:38:23 +02002829 status = i2c_transfer(adapter, msg, num);
2830 if (status < 0)
2831 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Jean Delvare421ef472005-10-26 21:28:55 +02002833 /* Check PEC if last message is a read */
2834 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002835 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2836 if (status < 0)
2837 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002838 }
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002841 switch (size) {
2842 case I2C_SMBUS_BYTE:
2843 data->byte = msgbuf0[0];
2844 break;
2845 case I2C_SMBUS_BYTE_DATA:
2846 data->byte = msgbuf1[0];
2847 break;
2848 case I2C_SMBUS_WORD_DATA:
2849 case I2C_SMBUS_PROC_CALL:
2850 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2851 break;
2852 case I2C_SMBUS_I2C_BLOCK_DATA:
2853 for (i = 0; i < data->block[0]; i++)
2854 data->block[i+1] = msgbuf1[i];
2855 break;
2856 case I2C_SMBUS_BLOCK_DATA:
2857 case I2C_SMBUS_BLOCK_PROC_CALL:
2858 for (i = 0; i < msgbuf1[0] + 1; i++)
2859 data->block[i] = msgbuf1[i];
2860 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
2862 return 0;
2863}
2864
David Brownella1cdeda2008-07-14 22:38:24 +02002865/**
2866 * i2c_smbus_xfer - execute SMBus protocol operations
2867 * @adapter: Handle to I2C bus
2868 * @addr: Address of SMBus slave on that bus
2869 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2870 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2871 * @command: Byte interpreted by slave, for protocols which use such bytes
2872 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2873 * @data: Data to be read or written
2874 *
2875 * This executes an SMBus protocol operation, and returns a negative
2876 * errno code else zero on success.
2877 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002878s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002879 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002880 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002882 unsigned long orig_jiffies;
2883 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
David Howells8a325992014-03-06 13:36:06 +00002886 /* If enabled, the following two tracepoints are conditional on
2887 * read_write and protocol.
2888 */
2889 trace_smbus_write(adapter, addr, flags, read_write,
2890 command, protocol, data);
2891 trace_smbus_read(adapter, addr, flags, read_write,
2892 command, protocol);
2893
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002894 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002897 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002898
2899 /* Retry automatically on arbitration loss */
2900 orig_jiffies = jiffies;
2901 for (res = 0, try = 0; try <= adapter->retries; try++) {
2902 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2903 read_write, command,
2904 protocol, data);
2905 if (res != -EAGAIN)
2906 break;
2907 if (time_after(jiffies,
2908 orig_jiffies + adapter->timeout))
2909 break;
2910 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002911 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002913 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002914 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002915 /*
2916 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2917 * implement native support for the SMBus operation.
2918 */
2919 }
2920
David Howells8a325992014-03-06 13:36:06 +00002921 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2922 command, protocol, data);
2923
2924trace:
2925 /* If enabled, the reply tracepoint is conditional on read_write. */
2926 trace_smbus_reply(adapter, addr, flags, read_write,
2927 command, protocol, data);
2928 trace_smbus_result(adapter, addr, flags, read_write,
2929 command, protocol, res);
2930
2931 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Jean Delvared5fd1202015-01-26 20:59:31 +01002935#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002936int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2937{
2938 int ret;
2939
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002940 if (!client || !slave_cb) {
2941 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002942 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002943 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002944
2945 if (!(client->flags & I2C_CLIENT_TEN)) {
2946 /* Enforce stricter address checking */
2947 ret = i2c_check_addr_validity(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002948 if (ret) {
2949 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002950 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002951 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002952 }
2953
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002954 if (!client->adapter->algo->reg_slave) {
2955 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002956 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002957 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002958
2959 client->slave_cb = slave_cb;
2960
2961 i2c_lock_adapter(client->adapter);
2962 ret = client->adapter->algo->reg_slave(client);
2963 i2c_unlock_adapter(client->adapter);
2964
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002965 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002966 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002967 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
2968 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002969
2970 return ret;
2971}
2972EXPORT_SYMBOL_GPL(i2c_slave_register);
2973
2974int i2c_slave_unregister(struct i2c_client *client)
2975{
2976 int ret;
2977
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002978 if (!client->adapter->algo->unreg_slave) {
2979 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002980 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002981 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002982
2983 i2c_lock_adapter(client->adapter);
2984 ret = client->adapter->algo->unreg_slave(client);
2985 i2c_unlock_adapter(client->adapter);
2986
2987 if (ret == 0)
2988 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02002989 else
2990 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002991
2992 return ret;
2993}
2994EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01002995#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2998MODULE_DESCRIPTION("I2C-Bus main module");
2999MODULE_LICENSE("GPL");