blob: 74e5aeaf84f9910e949c4acff240f24e54ed175b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020030#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010031#include <asm/uaccess.h>
32#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020033#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010034#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010035#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020036#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010037#include <linux/errno.h>
38#include <linux/gpio.h>
39#include <linux/hardirq.h>
40#include <linux/i2c.h>
41#include <linux/idr.h>
42#include <linux/init.h>
43#include <linux/irqflags.h>
44#include <linux/jump_label.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/mutex.h>
48#include <linux/of_device.h>
49#include <linux/of.h>
50#include <linux/of_irq.h>
51#include <linux/pm_domain.h>
52#include <linux/pm_runtime.h>
53#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010054#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010055#include <linux/rwsem.h>
56#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Brownell9c1600e2007-05-01 23:26:31 +020058#include "i2c-core.h"
59
David Howellsd9a83d62014-03-06 13:35:59 +000060#define CREATE_TRACE_POINTS
61#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Wolfram Sangda899f52015-05-18 21:09:12 +020063#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
64#define I2C_ADDR_OFFSET_SLAVE 0x1000
65
Jean Delvare6629dcf2010-05-04 11:09:28 +020066/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020067 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000068 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010069static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static DEFINE_IDR(i2c_adapter_idr);
71
Jean Delvare4f8cf822009-09-18 22:45:46 +020072static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020073static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010074
David Howellsd9a83d62014-03-06 13:35:59 +000075static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053076static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000077
78void i2c_transfer_trace_reg(void)
79{
80 static_key_slow_inc(&i2c_trace_msg);
81}
82
83void i2c_transfer_trace_unreg(void)
84{
85 static_key_slow_dec(&i2c_trace_msg);
86}
87
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020088#if defined(CONFIG_ACPI)
89struct acpi_i2c_handler_data {
90 struct acpi_connection_info info;
91 struct i2c_adapter *adapter;
92};
93
94struct gsb_buffer {
95 u8 status;
96 u8 len;
97 union {
98 u16 wdata;
99 u8 bdata;
100 u8 data[0];
101 };
102} __packed;
103
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300104struct acpi_i2c_lookup {
105 struct i2c_board_info *info;
106 acpi_handle adapter_handle;
107 acpi_handle device_handle;
108};
109
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300110static int acpi_i2c_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200111{
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300112 struct acpi_i2c_lookup *lookup = data;
113 struct i2c_board_info *info = lookup->info;
114 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300115 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200116
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300117 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
118 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200119
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300120 sb = &ares->data.i2c_serial_bus;
121 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
122 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200123
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300124 status = acpi_get_handle(lookup->device_handle,
125 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300126 &lookup->adapter_handle);
127 if (!ACPI_SUCCESS(status))
128 return 1;
129
130 info->addr = sb->slave_address;
131 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
132 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200133
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200134 return 1;
135}
136
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300137static int acpi_i2c_get_info(struct acpi_device *adev,
138 struct i2c_board_info *info,
139 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200141 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300142 struct resource_entry *entry;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300143 struct acpi_i2c_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200144 int ret;
145
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300146 if (acpi_bus_get_status(adev) || !adev->status.present ||
147 acpi_device_enumerated(adev))
148 return -EINVAL;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200149
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300150 memset(info, 0, sizeof(*info));
151 info->fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200152
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300153 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300154 lookup.device_handle = acpi_device_handle(adev);
155 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300156
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300157 /* Look up for I2cSerialBus resource */
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200158 INIT_LIST_HEAD(&resource_list);
159 ret = acpi_dev_get_resources(adev, &resource_list,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300160 acpi_i2c_fill_info, &lookup);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200161 acpi_dev_free_resource_list(&resource_list);
162
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300163 if (ret < 0 || !info->addr)
164 return -EINVAL;
165
166 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200167
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300168 /* Then fill IRQ number if any */
169 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
170 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300171 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300172
173 resource_list_for_each_entry(entry, &resource_list) {
174 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300175 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300176 break;
177 }
178 }
179
180 acpi_dev_free_resource_list(&resource_list);
181
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300182 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
183
184 return 0;
185}
186
187static void acpi_i2c_register_device(struct i2c_adapter *adapter,
188 struct acpi_device *adev,
189 struct i2c_board_info *info)
190{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200191 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300192 acpi_device_set_enumerated(adev);
193
194 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200195 adev->power.flags.ignore_parent = false;
196 dev_err(&adapter->dev,
197 "failed to add I2C device %s from ACPI\n",
198 dev_name(&adev->dev));
199 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300200}
201
202static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
203 void *data, void **return_value)
204{
205 struct i2c_adapter *adapter = data;
206 struct acpi_device *adev;
207 acpi_handle adapter_handle;
208 struct i2c_board_info info;
209
210 if (acpi_bus_get_device(handle, &adev))
211 return AE_OK;
212
213 if (acpi_i2c_get_info(adev, &info, &adapter_handle))
214 return AE_OK;
215
216 if (adapter_handle != ACPI_HANDLE(&adapter->dev))
217 return AE_OK;
218
219 acpi_i2c_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200220
221 return AE_OK;
222}
223
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300224#define ACPI_I2C_MAX_SCAN_DEPTH 32
225
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200226/**
227 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
228 * @adap: pointer to adapter
229 *
230 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
231 * namespace. When a device is found it will be added to the Linux device
232 * model and bound to the corresponding ACPI handle.
233 */
234static void acpi_i2c_register_devices(struct i2c_adapter *adap)
235{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200236 acpi_status status;
237
Dustin Byford8eb5c872015-10-23 12:27:07 -0700238 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200239 return;
240
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300241 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
242 ACPI_I2C_MAX_SCAN_DEPTH,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200243 acpi_i2c_add_device, NULL,
244 adap, NULL);
245 if (ACPI_FAILURE(status))
246 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
247}
248
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300249static int acpi_i2c_match_adapter(struct device *dev, void *data)
250{
251 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
252
253 if (!adapter)
254 return 0;
255
256 return ACPI_HANDLE(dev) == (acpi_handle)data;
257}
258
259static int acpi_i2c_match_device(struct device *dev, void *data)
260{
261 return ACPI_COMPANION(dev) == data;
262}
263
264static struct i2c_adapter *acpi_i2c_find_adapter_by_handle(acpi_handle handle)
265{
266 struct device *dev;
267
268 dev = bus_find_device(&i2c_bus_type, NULL, handle,
269 acpi_i2c_match_adapter);
270 return dev ? i2c_verify_adapter(dev) : NULL;
271}
272
273static struct i2c_client *acpi_i2c_find_client_by_adev(struct acpi_device *adev)
274{
275 struct device *dev;
276
277 dev = bus_find_device(&i2c_bus_type, NULL, adev, acpi_i2c_match_device);
278 return dev ? i2c_verify_client(dev) : NULL;
279}
280
281static int acpi_i2c_notify(struct notifier_block *nb, unsigned long value,
282 void *arg)
283{
284 struct acpi_device *adev = arg;
285 struct i2c_board_info info;
286 acpi_handle adapter_handle;
287 struct i2c_adapter *adapter;
288 struct i2c_client *client;
289
290 switch (value) {
291 case ACPI_RECONFIG_DEVICE_ADD:
292 if (acpi_i2c_get_info(adev, &info, &adapter_handle))
293 break;
294
295 adapter = acpi_i2c_find_adapter_by_handle(adapter_handle);
296 if (!adapter)
297 break;
298
299 acpi_i2c_register_device(adapter, adev, &info);
300 break;
301 case ACPI_RECONFIG_DEVICE_REMOVE:
302 if (!acpi_device_enumerated(adev))
303 break;
304
305 client = acpi_i2c_find_client_by_adev(adev);
306 if (!client)
307 break;
308
309 i2c_unregister_device(client);
310 put_device(&client->dev);
311 break;
312 }
313
314 return NOTIFY_OK;
315}
316
317static struct notifier_block i2c_acpi_notifier = {
318 .notifier_call = acpi_i2c_notify,
319};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200320#else /* CONFIG_ACPI */
321static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300322extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200323#endif /* CONFIG_ACPI */
324
325#ifdef CONFIG_ACPI_I2C_OPREGION
326static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
327 u8 cmd, u8 *data, u8 data_len)
328{
329
330 struct i2c_msg msgs[2];
331 int ret;
332 u8 *buffer;
333
334 buffer = kzalloc(data_len, GFP_KERNEL);
335 if (!buffer)
336 return AE_NO_MEMORY;
337
338 msgs[0].addr = client->addr;
339 msgs[0].flags = client->flags;
340 msgs[0].len = 1;
341 msgs[0].buf = &cmd;
342
343 msgs[1].addr = client->addr;
344 msgs[1].flags = client->flags | I2C_M_RD;
345 msgs[1].len = data_len;
346 msgs[1].buf = buffer;
347
348 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
349 if (ret < 0)
350 dev_err(&client->adapter->dev, "i2c read failed\n");
351 else
352 memcpy(data, buffer, data_len);
353
354 kfree(buffer);
355 return ret;
356}
357
358static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
359 u8 cmd, u8 *data, u8 data_len)
360{
361
362 struct i2c_msg msgs[1];
363 u8 *buffer;
364 int ret = AE_OK;
365
366 buffer = kzalloc(data_len + 1, GFP_KERNEL);
367 if (!buffer)
368 return AE_NO_MEMORY;
369
370 buffer[0] = cmd;
371 memcpy(buffer + 1, data, data_len);
372
373 msgs[0].addr = client->addr;
374 msgs[0].flags = client->flags;
375 msgs[0].len = data_len + 1;
376 msgs[0].buf = buffer;
377
378 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
379 if (ret < 0)
380 dev_err(&client->adapter->dev, "i2c write failed\n");
381
382 kfree(buffer);
383 return ret;
384}
385
386static acpi_status
387acpi_i2c_space_handler(u32 function, acpi_physical_address command,
388 u32 bits, u64 *value64,
389 void *handler_context, void *region_context)
390{
391 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
392 struct acpi_i2c_handler_data *data = handler_context;
393 struct acpi_connection_info *info = &data->info;
394 struct acpi_resource_i2c_serialbus *sb;
395 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300396 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200397 struct acpi_resource *ares;
398 u32 accessor_type = function >> 16;
399 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100400 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200401 int status;
402
403 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
404 if (ACPI_FAILURE(ret))
405 return ret;
406
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300407 client = kzalloc(sizeof(*client), GFP_KERNEL);
408 if (!client) {
409 ret = AE_NO_MEMORY;
410 goto err;
411 }
412
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200413 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
414 ret = AE_BAD_PARAMETER;
415 goto err;
416 }
417
418 sb = &ares->data.i2c_serial_bus;
419 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
420 ret = AE_BAD_PARAMETER;
421 goto err;
422 }
423
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300424 client->adapter = adapter;
425 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200426
427 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300428 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200429
430 switch (accessor_type) {
431 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
432 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300433 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200434 if (status >= 0) {
435 gsb->bdata = status;
436 status = 0;
437 }
438 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300439 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200440 }
441 break;
442
443 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
444 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300445 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200446 if (status >= 0) {
447 gsb->bdata = status;
448 status = 0;
449 }
450 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300451 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200452 gsb->bdata);
453 }
454 break;
455
456 case ACPI_GSB_ACCESS_ATTRIB_WORD:
457 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300458 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200459 if (status >= 0) {
460 gsb->wdata = status;
461 status = 0;
462 }
463 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300464 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200465 gsb->wdata);
466 }
467 break;
468
469 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
470 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300471 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200472 gsb->data);
473 if (status >= 0) {
474 gsb->len = status;
475 status = 0;
476 }
477 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300478 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200479 gsb->len, gsb->data);
480 }
481 break;
482
483 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
484 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300485 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200486 gsb->data, info->access_length);
487 if (status > 0)
488 status = 0;
489 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300490 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200491 gsb->data, info->access_length);
492 }
493 break;
494
495 default:
496 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
497 ret = AE_BAD_PARAMETER;
498 goto err;
499 }
500
501 gsb->status = status;
502
503 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300504 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200505 ACPI_FREE(ares);
506 return ret;
507}
508
509
510static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
511{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200512 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200513 struct acpi_i2c_handler_data *data;
514 acpi_status status;
515
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200516 if (!adapter->dev.parent)
517 return -ENODEV;
518
519 handle = ACPI_HANDLE(adapter->dev.parent);
520
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200521 if (!handle)
522 return -ENODEV;
523
524 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
525 GFP_KERNEL);
526 if (!data)
527 return -ENOMEM;
528
529 data->adapter = adapter;
530 status = acpi_bus_attach_private_data(handle, (void *)data);
531 if (ACPI_FAILURE(status)) {
532 kfree(data);
533 return -ENOMEM;
534 }
535
536 status = acpi_install_address_space_handler(handle,
537 ACPI_ADR_SPACE_GSBUS,
538 &acpi_i2c_space_handler,
539 NULL,
540 data);
541 if (ACPI_FAILURE(status)) {
542 dev_err(&adapter->dev, "Error installing i2c space handler\n");
543 acpi_bus_detach_private_data(handle);
544 kfree(data);
545 return -ENOMEM;
546 }
547
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800548 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200549 return 0;
550}
551
552static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
553{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200554 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200555 struct acpi_i2c_handler_data *data;
556 acpi_status status;
557
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200558 if (!adapter->dev.parent)
559 return;
560
561 handle = ACPI_HANDLE(adapter->dev.parent);
562
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200563 if (!handle)
564 return;
565
566 acpi_remove_address_space_handler(handle,
567 ACPI_ADR_SPACE_GSBUS,
568 &acpi_i2c_space_handler);
569
570 status = acpi_bus_get_private_data(handle, (void **)&data);
571 if (ACPI_SUCCESS(status))
572 kfree(data);
573
574 acpi_bus_detach_private_data(handle);
575}
576#else /* CONFIG_ACPI_I2C_OPREGION */
577static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
578{ }
579
580static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
581{ return 0; }
582#endif /* CONFIG_ACPI_I2C_OPREGION */
583
David Brownellf37dd802007-02-13 22:09:00 +0100584/* ------------------------------------------------------------------------- */
585
Jean Delvared2653e92008-04-29 23:11:39 +0200586static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
587 const struct i2c_client *client)
588{
589 while (id->name[0]) {
590 if (strcmp(client->name, id->name) == 0)
591 return id;
592 id++;
593 }
594 return NULL;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static int i2c_device_match(struct device *dev, struct device_driver *drv)
598{
Jean Delvare51298d12009-09-18 22:45:45 +0200599 struct i2c_client *client = i2c_verify_client(dev);
600 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200601
Jean Delvare51298d12009-09-18 22:45:45 +0200602 if (!client)
603 return 0;
604
Grant Likely959e85f2010-06-08 07:48:19 -0600605 /* Attempt an OF style match */
606 if (of_driver_match_device(dev, drv))
607 return 1;
608
Mika Westerberg907ddf82012-11-23 12:23:40 +0100609 /* Then ACPI style match */
610 if (acpi_driver_match_device(dev, drv))
611 return 1;
612
Jean Delvare51298d12009-09-18 22:45:45 +0200613 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200614 /* match on an id table if there is one */
615 if (driver->id_table)
616 return i2c_match_id(driver->id_table, client) != NULL;
617
Jean Delvareeb8a7902008-05-18 20:49:41 +0200618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Kay Sievers7eff2e72007-08-14 15:15:12 +0200621static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200622{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100623 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800624 int rc;
625
626 rc = acpi_device_uevent_modalias(dev, env);
627 if (rc != -ENODEV)
628 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200629
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100630 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200631}
632
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530633/* i2c bus recovery routines */
634static int get_scl_gpio_value(struct i2c_adapter *adap)
635{
636 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
637}
638
639static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
640{
641 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
642}
643
644static int get_sda_gpio_value(struct i2c_adapter *adap)
645{
646 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
647}
648
649static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
650{
651 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
652 struct device *dev = &adap->dev;
653 int ret = 0;
654
655 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
656 GPIOF_OUT_INIT_HIGH, "i2c-scl");
657 if (ret) {
658 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
659 return ret;
660 }
661
662 if (bri->get_sda) {
663 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
664 /* work without SDA polling */
665 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
666 bri->sda_gpio);
667 bri->get_sda = NULL;
668 }
669 }
670
671 return ret;
672}
673
674static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
675{
676 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
677
678 if (bri->get_sda)
679 gpio_free(bri->sda_gpio);
680
681 gpio_free(bri->scl_gpio);
682}
683
684/*
685 * We are generating clock pulses. ndelay() determines durating of clk pulses.
686 * We will generate clock with rate 100 KHz and so duration of both clock levels
687 * is: delay in ns = (10^6 / 100) / 2
688 */
689#define RECOVERY_NDELAY 5000
690#define RECOVERY_CLK_CNT 9
691
692static int i2c_generic_recovery(struct i2c_adapter *adap)
693{
694 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
695 int i = 0, val = 1, ret = 0;
696
697 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300698 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530699
Jan Luebbe8b062602015-07-08 16:35:06 +0200700 bri->set_scl(adap, val);
701 ndelay(RECOVERY_NDELAY);
702
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530703 /*
704 * By this time SCL is high, as we need to give 9 falling-rising edges
705 */
706 while (i++ < RECOVERY_CLK_CNT * 2) {
707 if (val) {
708 /* Break if SDA is high */
709 if (bri->get_sda && bri->get_sda(adap))
710 break;
711 /* SCL shouldn't be low here */
712 if (!bri->get_scl(adap)) {
713 dev_err(&adap->dev,
714 "SCL is stuck low, exit recovery\n");
715 ret = -EBUSY;
716 break;
717 }
718 }
719
720 val = !val;
721 bri->set_scl(adap, val);
722 ndelay(RECOVERY_NDELAY);
723 }
724
725 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300726 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530727
728 return ret;
729}
730
731int i2c_generic_scl_recovery(struct i2c_adapter *adap)
732{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530733 return i2c_generic_recovery(adap);
734}
Mark Brownc1c21f42015-04-15 19:18:39 +0100735EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530736
737int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
738{
739 int ret;
740
741 ret = i2c_get_gpios_for_recovery(adap);
742 if (ret)
743 return ret;
744
745 ret = i2c_generic_recovery(adap);
746 i2c_put_gpios_for_recovery(adap);
747
748 return ret;
749}
Mark Brownc1c21f42015-04-15 19:18:39 +0100750EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530751
752int i2c_recover_bus(struct i2c_adapter *adap)
753{
754 if (!adap->bus_recovery_info)
755 return -EOPNOTSUPP;
756
757 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
758 return adap->bus_recovery_info->recover_bus(adap);
759}
Mark Brownc1c21f42015-04-15 19:18:39 +0100760EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762static int i2c_device_probe(struct device *dev)
763{
Jean Delvare51298d12009-09-18 22:45:45 +0200764 struct i2c_client *client = i2c_verify_client(dev);
765 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100766 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200767
Jean Delvare51298d12009-09-18 22:45:45 +0200768 if (!client)
769 return 0;
770
Mika Westerberg845c8772015-05-06 13:29:08 +0300771 if (!client->irq) {
772 int irq = -ENOENT;
773
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700774 if (dev->of_node) {
775 irq = of_irq_get_byname(dev->of_node, "irq");
776 if (irq == -EINVAL || irq == -ENODATA)
777 irq = of_irq_get(dev->of_node, 0);
778 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300779 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700780 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100781 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200782 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100783 if (irq < 0)
784 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200785
786 client->irq = irq;
787 }
788
Jean Delvare51298d12009-09-18 22:45:45 +0200789 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200790 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200791 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200792
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700793 if (client->flags & I2C_CLIENT_WAKE) {
794 int wakeirq = -ENOENT;
795
796 if (dev->of_node) {
797 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
798 if (wakeirq == -EPROBE_DEFER)
799 return wakeirq;
800 }
801
802 device_init_wakeup(&client->dev, true);
803
804 if (wakeirq > 0 && wakeirq != client->irq)
805 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
806 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200807 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700808 else
809 status = 0;
810
811 if (status)
812 dev_warn(&client->dev, "failed to set up wakeup irq");
813 }
814
David Brownell7b4fbc52007-05-01 23:26:30 +0200815 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200816
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200817 status = of_clk_set_defaults(dev->of_node, false);
818 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700819 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200820
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200821 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100822 if (status == -EPROBE_DEFER)
823 goto err_clear_wakeup_irq;
824
825 status = driver->probe(client, i2c_match_id(driver->id_table, client));
826 if (status)
827 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100828
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700829 return 0;
830
831err_detach_pm_domain:
832 dev_pm_domain_detach(&client->dev, true);
833err_clear_wakeup_irq:
834 dev_pm_clear_wake_irq(&client->dev);
835 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100836 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static int i2c_device_remove(struct device *dev)
840{
Jean Delvare51298d12009-09-18 22:45:45 +0200841 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200842 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100843 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200844
Jean Delvare51298d12009-09-18 22:45:45 +0200845 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200846 return 0;
847
848 driver = to_i2c_driver(dev->driver);
849 if (driver->remove) {
850 dev_dbg(dev, "remove\n");
851 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200852 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100853
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200854 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700855
856 dev_pm_clear_wake_irq(&client->dev);
857 device_init_wakeup(&client->dev, false);
858
David Brownella1d9e6e2007-05-01 23:26:30 +0200859 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
David Brownellf37dd802007-02-13 22:09:00 +0100862static void i2c_device_shutdown(struct device *dev)
863{
Jean Delvare51298d12009-09-18 22:45:45 +0200864 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100865 struct i2c_driver *driver;
866
Jean Delvare51298d12009-09-18 22:45:45 +0200867 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100868 return;
869 driver = to_i2c_driver(dev->driver);
870 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200871 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100872}
873
David Brownell9c1600e2007-05-01 23:26:31 +0200874static void i2c_client_dev_release(struct device *dev)
875{
876 kfree(to_i2c_client(dev));
877}
878
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100879static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200880show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200881{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200882 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
883 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200884}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100885static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200886
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100887static ssize_t
888show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200889{
890 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800891 int len;
892
893 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
894 if (len != -ENODEV)
895 return len;
896
Jean Delvareeb8a7902008-05-18 20:49:41 +0200897 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200898}
Jean Delvare51298d12009-09-18 22:45:45 +0200899static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
900
901static struct attribute *i2c_dev_attrs[] = {
902 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200903 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200904 &dev_attr_modalias.attr,
905 NULL
906};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100907ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100908
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200909struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100910 .name = "i2c",
911 .match = i2c_device_match,
912 .probe = i2c_device_probe,
913 .remove = i2c_device_remove,
914 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000915};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200916EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000917
Jean Delvare51298d12009-09-18 22:45:45 +0200918static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100919 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200920 .uevent = i2c_device_uevent,
921 .release = i2c_client_dev_release,
922};
923
David Brownell9b766b82008-01-27 18:14:51 +0100924
925/**
926 * i2c_verify_client - return parameter as i2c_client, or NULL
927 * @dev: device, probably from some driver model iterator
928 *
929 * When traversing the driver model tree, perhaps using driver model
930 * iterators like @device_for_each_child(), you can't assume very much
931 * about the nodes you find. Use this function to avoid oopses caused
932 * by wrongly treating some non-I2C device as an i2c_client.
933 */
934struct i2c_client *i2c_verify_client(struct device *dev)
935{
Jean Delvare51298d12009-09-18 22:45:45 +0200936 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100937 ? to_i2c_client(dev)
938 : NULL;
939}
940EXPORT_SYMBOL(i2c_verify_client);
941
942
Wolfram Sangda899f52015-05-18 21:09:12 +0200943/* Return a unique address which takes the flags of the client into account */
944static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
945{
946 unsigned short addr = client->addr;
947
948 /* For some client flags, add an arbitrary offset to avoid collisions */
949 if (client->flags & I2C_CLIENT_TEN)
950 addr |= I2C_ADDR_OFFSET_TEN_BIT;
951
952 if (client->flags & I2C_CLIENT_SLAVE)
953 addr |= I2C_ADDR_OFFSET_SLAVE;
954
955 return addr;
956}
957
Jean Delvare3a89db52010-06-03 11:33:52 +0200958/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300959 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200960static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200961{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200962 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200963 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200964 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200965 return -EINVAL;
966 } else {
967 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200968 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200969 return -EINVAL;
970 }
971 return 0;
972}
973
Jean Delvare656b8762010-06-03 11:33:53 +0200974/* And this is a strict address validity check, used when probing. If a
975 * device uses a reserved address, then it shouldn't be probed. 7-bit
976 * addressing is assumed, 10-bit address devices are rare and should be
977 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +0200978static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200979{
980 /*
981 * Reserved addresses per I2C specification:
982 * 0x00 General call address / START byte
983 * 0x01 CBUS address
984 * 0x02 Reserved for different bus format
985 * 0x03 Reserved for future purposes
986 * 0x04-0x07 Hs-mode master code
987 * 0x78-0x7b 10-bit slave addressing
988 * 0x7c-0x7f Reserved for future purposes
989 */
990 if (addr < 0x08 || addr > 0x77)
991 return -EINVAL;
992 return 0;
993}
994
Jean Delvare3b5f7942010-06-03 11:33:55 +0200995static int __i2c_check_addr_busy(struct device *dev, void *addrp)
996{
997 struct i2c_client *client = i2c_verify_client(dev);
998 int addr = *(int *)addrp;
999
Wolfram Sang9bccc702015-07-17 14:48:56 +02001000 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001001 return -EBUSY;
1002 return 0;
1003}
1004
Michael Lawnick08263742010-08-11 18:21:02 +02001005/* walk up mux tree */
1006static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1007{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001008 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001009 int result;
1010
1011 result = device_for_each_child(&adapter->dev, &addr,
1012 __i2c_check_addr_busy);
1013
Jean Delvare97cc4d42010-10-24 18:16:57 +02001014 if (!result && parent)
1015 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001016
1017 return result;
1018}
1019
1020/* recurse down mux tree */
1021static int i2c_check_mux_children(struct device *dev, void *addrp)
1022{
1023 int result;
1024
1025 if (dev->type == &i2c_adapter_type)
1026 result = device_for_each_child(dev, addrp,
1027 i2c_check_mux_children);
1028 else
1029 result = __i2c_check_addr_busy(dev, addrp);
1030
1031 return result;
1032}
1033
Jean Delvare3b5f7942010-06-03 11:33:55 +02001034static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1035{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001036 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001037 int result = 0;
1038
Jean Delvare97cc4d42010-10-24 18:16:57 +02001039 if (parent)
1040 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001041
1042 if (!result)
1043 result = device_for_each_child(&adapter->dev, &addr,
1044 i2c_check_mux_children);
1045
1046 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001047}
1048
David Brownell9c1600e2007-05-01 23:26:31 +02001049/**
Peter Rosin8320f492016-05-04 22:15:27 +02001050 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001051 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001052 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1053 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001054 */
Peter Rosin8320f492016-05-04 22:15:27 +02001055static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1056 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001057{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001058 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001059}
Jean Delvarefe61e072010-08-11 18:20:58 +02001060
1061/**
Peter Rosin8320f492016-05-04 22:15:27 +02001062 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001063 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001064 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1065 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001066 */
Peter Rosin8320f492016-05-04 22:15:27 +02001067static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1068 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001069{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001070 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001071}
1072
1073/**
Peter Rosin8320f492016-05-04 22:15:27 +02001074 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001075 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001076 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1077 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001078 */
Peter Rosin8320f492016-05-04 22:15:27 +02001079static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1080 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001081{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001082 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001083}
Jean Delvarefe61e072010-08-11 18:20:58 +02001084
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001085static void i2c_dev_set_name(struct i2c_adapter *adap,
1086 struct i2c_client *client)
1087{
1088 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1089
1090 if (adev) {
1091 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1092 return;
1093 }
1094
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001095 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001096 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001097}
1098
Jean Delvarefe61e072010-08-11 18:20:58 +02001099/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001100 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001101 * @adap: the adapter managing the device
1102 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001103 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001104 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001105 * Create an i2c device. Binding is handled through driver model
1106 * probe()/remove() methods. A driver may be bound to this device when we
1107 * return from this function, or any later moment (e.g. maybe hotplugging will
1108 * load the driver module). This call is not appropriate for use by mainboard
1109 * initialization logic, which usually runs during an arch_initcall() long
1110 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001111 *
1112 * This returns the new i2c client, which may be saved for later use with
1113 * i2c_unregister_device(); or NULL to indicate an error.
1114 */
1115struct i2c_client *
1116i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1117{
1118 struct i2c_client *client;
1119 int status;
1120
1121 client = kzalloc(sizeof *client, GFP_KERNEL);
1122 if (!client)
1123 return NULL;
1124
1125 client->adapter = adap;
1126
1127 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001128
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001129 if (info->archdata)
1130 client->dev.archdata = *info->archdata;
1131
Marc Pignatee354252008-08-28 08:33:22 +02001132 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001133 client->addr = info->addr;
1134 client->irq = info->irq;
1135
David Brownell9c1600e2007-05-01 23:26:31 +02001136 strlcpy(client->name, info->type, sizeof(client->name));
1137
Wolfram Sangc4019b72015-07-17 12:50:06 +02001138 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001139 if (status) {
1140 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1141 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1142 goto out_err_silent;
1143 }
1144
Jean Delvaref8a227e2009-06-19 16:58:18 +02001145 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001146 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001147 if (status)
1148 goto out_err;
1149
1150 client->dev.parent = &client->adapter->dev;
1151 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001152 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001153 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001154 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001155
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001156 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001157 status = device_register(&client->dev);
1158 if (status)
1159 goto out_err;
1160
Jean Delvaref8a227e2009-06-19 16:58:18 +02001161 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1162 client->name, dev_name(&client->dev));
1163
David Brownell9c1600e2007-05-01 23:26:31 +02001164 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001165
1166out_err:
1167 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1168 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001169out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001170 kfree(client);
1171 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001172}
1173EXPORT_SYMBOL_GPL(i2c_new_device);
1174
1175
1176/**
1177 * i2c_unregister_device - reverse effect of i2c_new_device()
1178 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001179 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001180 */
1181void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001182{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001183 if (client->dev.of_node)
1184 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001185 if (ACPI_COMPANION(&client->dev))
1186 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001187 device_unregister(&client->dev);
1188}
David Brownell9c1600e2007-05-01 23:26:31 +02001189EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001190
1191
Jean Delvare60b129d2008-05-11 20:37:06 +02001192static const struct i2c_device_id dummy_id[] = {
1193 { "dummy", 0 },
1194 { },
1195};
1196
Jean Delvared2653e92008-04-29 23:11:39 +02001197static int dummy_probe(struct i2c_client *client,
1198 const struct i2c_device_id *id)
1199{
1200 return 0;
1201}
1202
1203static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001204{
1205 return 0;
1206}
1207
1208static struct i2c_driver dummy_driver = {
1209 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001210 .probe = dummy_probe,
1211 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001212 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001213};
1214
1215/**
1216 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1217 * @adapter: the adapter managing the device
1218 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001219 * Context: can sleep
1220 *
1221 * This returns an I2C client bound to the "dummy" driver, intended for use
1222 * with devices that consume multiple addresses. Examples of such chips
1223 * include various EEPROMS (like 24c04 and 24c08 models).
1224 *
1225 * These dummy devices have two main uses. First, most I2C and SMBus calls
1226 * except i2c_transfer() need a client handle; the dummy will be that handle.
1227 * And second, this prevents the specified address from being bound to a
1228 * different driver.
1229 *
1230 * This returns the new i2c client, which should be saved for later use with
1231 * i2c_unregister_device(); or NULL to indicate an error.
1232 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001233struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001234{
1235 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001236 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001237 };
1238
David Brownelle9f13732008-01-27 18:14:52 +01001239 return i2c_new_device(adapter, &info);
1240}
1241EXPORT_SYMBOL_GPL(i2c_new_dummy);
1242
David Brownellf37dd802007-02-13 22:09:00 +01001243/* ------------------------------------------------------------------------- */
1244
David Brownell16ffadf2007-05-01 23:26:28 +02001245/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1246
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001247static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
David Brownellef2c83212007-05-01 23:26:28 +02001249 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 complete(&adap->dev_released);
1251}
1252
Jean Delvare99cd8e22009-06-19 16:58:20 +02001253/*
Jean Delvare390946b2012-09-10 10:14:02 +02001254 * This function is only needed for mutex_lock_nested, so it is never
1255 * called unless locking correctness checking is enabled. Thus we
1256 * make it inline to avoid a compiler warning. That's what gcc ends up
1257 * doing anyway.
1258 */
1259static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1260{
1261 unsigned int depth = 0;
1262
1263 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1264 depth++;
1265
1266 return depth;
1267}
1268
1269/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001270 * Let users instantiate I2C devices through sysfs. This can be used when
1271 * platform initialization code doesn't contain the proper data for
1272 * whatever reason. Also useful for drivers that do device detection and
1273 * detection fails, either because the device uses an unexpected address,
1274 * or this is a compatible device with different ID register values.
1275 *
1276 * Parameter checking may look overzealous, but we really don't want
1277 * the user to provide incorrect parameters.
1278 */
1279static ssize_t
1280i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1281 const char *buf, size_t count)
1282{
1283 struct i2c_adapter *adap = to_i2c_adapter(dev);
1284 struct i2c_board_info info;
1285 struct i2c_client *client;
1286 char *blank, end;
1287 int res;
1288
Jean Delvare99cd8e22009-06-19 16:58:20 +02001289 memset(&info, 0, sizeof(struct i2c_board_info));
1290
1291 blank = strchr(buf, ' ');
1292 if (!blank) {
1293 dev_err(dev, "%s: Missing parameters\n", "new_device");
1294 return -EINVAL;
1295 }
1296 if (blank - buf > I2C_NAME_SIZE - 1) {
1297 dev_err(dev, "%s: Invalid device name\n", "new_device");
1298 return -EINVAL;
1299 }
1300 memcpy(info.type, buf, blank - buf);
1301
1302 /* Parse remaining parameters, reject extra parameters */
1303 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1304 if (res < 1) {
1305 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1306 return -EINVAL;
1307 }
1308 if (res > 1 && end != '\n') {
1309 dev_err(dev, "%s: Extra parameters\n", "new_device");
1310 return -EINVAL;
1311 }
1312
Wolfram Sangcfa03272015-07-27 14:03:38 +02001313 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1314 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1315 info.flags |= I2C_CLIENT_TEN;
1316 }
1317
1318 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1319 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1320 info.flags |= I2C_CLIENT_SLAVE;
1321 }
1322
Jean Delvare99cd8e22009-06-19 16:58:20 +02001323 client = i2c_new_device(adap, &info);
1324 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001325 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001326
1327 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001328 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001329 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001330 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001331 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1332 info.type, info.addr);
1333
1334 return count;
1335}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001336static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001337
1338/*
1339 * And of course let the users delete the devices they instantiated, if
1340 * they got it wrong. This interface can only be used to delete devices
1341 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1342 * don't delete devices to which some kernel code still has references.
1343 *
1344 * Parameter checking may look overzealous, but we really don't want
1345 * the user to delete the wrong device.
1346 */
1347static ssize_t
1348i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1349 const char *buf, size_t count)
1350{
1351 struct i2c_adapter *adap = to_i2c_adapter(dev);
1352 struct i2c_client *client, *next;
1353 unsigned short addr;
1354 char end;
1355 int res;
1356
1357 /* Parse parameters, reject extra parameters */
1358 res = sscanf(buf, "%hi%c", &addr, &end);
1359 if (res < 1) {
1360 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1361 return -EINVAL;
1362 }
1363 if (res > 1 && end != '\n') {
1364 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1365 return -EINVAL;
1366 }
1367
1368 /* Make sure the device was added through sysfs */
1369 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001370 mutex_lock_nested(&adap->userspace_clients_lock,
1371 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001372 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1373 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001374 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001375 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1376 "delete_device", client->name, client->addr);
1377
1378 list_del(&client->detected);
1379 i2c_unregister_device(client);
1380 res = count;
1381 break;
1382 }
1383 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001384 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001385
1386 if (res < 0)
1387 dev_err(dev, "%s: Can't find device in list\n",
1388 "delete_device");
1389 return res;
1390}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001391static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1392 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001393
1394static struct attribute *i2c_adapter_attrs[] = {
1395 &dev_attr_name.attr,
1396 &dev_attr_new_device.attr,
1397 &dev_attr_delete_device.attr,
1398 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001399};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001400ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001401
Michael Lawnick08263742010-08-11 18:21:02 +02001402struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001403 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001404 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001405};
Michael Lawnick08263742010-08-11 18:21:02 +02001406EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Stephen Warren643dd092012-04-17 12:43:33 -06001408/**
1409 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1410 * @dev: device, probably from some driver model iterator
1411 *
1412 * When traversing the driver model tree, perhaps using driver model
1413 * iterators like @device_for_each_child(), you can't assume very much
1414 * about the nodes you find. Use this function to avoid oopses caused
1415 * by wrongly treating some non-I2C device as an i2c_adapter.
1416 */
1417struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1418{
1419 return (dev->type == &i2c_adapter_type)
1420 ? to_i2c_adapter(dev)
1421 : NULL;
1422}
1423EXPORT_SYMBOL(i2c_verify_adapter);
1424
Jean Delvare2bb50952009-09-18 22:45:46 +02001425#ifdef CONFIG_I2C_COMPAT
1426static struct class_compat *i2c_adapter_compat_class;
1427#endif
1428
David Brownell9c1600e2007-05-01 23:26:31 +02001429static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1430{
1431 struct i2c_devinfo *devinfo;
1432
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001433 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001434 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1435 if (devinfo->busnum == adapter->nr
1436 && !i2c_new_device(adapter,
1437 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001438 dev_err(&adapter->dev,
1439 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001440 devinfo->board_info.addr);
1441 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001442 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001443}
1444
Wolfram Sang687b81d2013-07-11 12:56:15 +01001445/* OF support code */
1446
1447#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001448static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1449 struct device_node *node)
1450{
1451 struct i2c_client *result;
1452 struct i2c_board_info info = {};
1453 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001454 const __be32 *addr_be;
1455 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001456 int len;
1457
1458 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1459
1460 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1461 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1462 node->full_name);
1463 return ERR_PTR(-EINVAL);
1464 }
1465
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001466 addr_be = of_get_property(node, "reg", &len);
1467 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001468 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1469 node->full_name);
1470 return ERR_PTR(-EINVAL);
1471 }
1472
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001473 addr = be32_to_cpup(addr_be);
1474 if (addr & I2C_TEN_BIT_ADDRESS) {
1475 addr &= ~I2C_TEN_BIT_ADDRESS;
1476 info.flags |= I2C_CLIENT_TEN;
1477 }
1478
1479 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1480 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1481 info.flags |= I2C_CLIENT_SLAVE;
1482 }
1483
1484 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001485 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1486 info.addr, node->full_name);
1487 return ERR_PTR(-EINVAL);
1488 }
1489
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001490 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001491 info.of_node = of_node_get(node);
1492 info.archdata = &dev_ad;
1493
1494 if (of_get_property(node, "wakeup-source", NULL))
1495 info.flags |= I2C_CLIENT_WAKE;
1496
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001497 result = i2c_new_device(adap, &info);
1498 if (result == NULL) {
1499 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1500 node->full_name);
1501 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001502 return ERR_PTR(-EINVAL);
1503 }
1504 return result;
1505}
1506
Wolfram Sang687b81d2013-07-11 12:56:15 +01001507static void of_i2c_register_devices(struct i2c_adapter *adap)
1508{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001509 struct device_node *node;
1510
1511 /* Only register child devices if the adapter has a node pointer set */
1512 if (!adap->dev.of_node)
1513 return;
1514
1515 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1516
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001517 for_each_available_child_of_node(adap->dev.of_node, node) {
1518 if (of_node_test_and_set_flag(node, OF_POPULATED))
1519 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001520 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001521 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001522}
1523
1524static int of_dev_node_match(struct device *dev, void *data)
1525{
1526 return dev->of_node == data;
1527}
1528
1529/* must call put_device() when done with returned i2c_client device */
1530struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1531{
1532 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001533 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001534
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001535 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001536 if (!dev)
1537 return NULL;
1538
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001539 client = i2c_verify_client(dev);
1540 if (!client)
1541 put_device(dev);
1542
1543 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001544}
1545EXPORT_SYMBOL(of_find_i2c_device_by_node);
1546
1547/* must call put_device() when done with returned i2c_adapter device */
1548struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1549{
1550 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001551 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001552
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001553 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001554 if (!dev)
1555 return NULL;
1556
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001557 adapter = i2c_verify_adapter(dev);
1558 if (!adapter)
1559 put_device(dev);
1560
1561 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001562}
1563EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001564
1565/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1566struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1567{
1568 struct i2c_adapter *adapter;
1569
1570 adapter = of_find_i2c_adapter_by_node(node);
1571 if (!adapter)
1572 return NULL;
1573
1574 if (!try_module_get(adapter->owner)) {
1575 put_device(&adapter->dev);
1576 adapter = NULL;
1577 }
1578
1579 return adapter;
1580}
1581EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001582#else
1583static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1584#endif /* CONFIG_OF */
1585
Jean Delvare69b00892009-12-06 17:06:27 +01001586static int i2c_do_add_adapter(struct i2c_driver *driver,
1587 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001588{
Jean Delvare4735c982008-07-14 22:38:36 +02001589 /* Detect supported devices on that bus, and instantiate them */
1590 i2c_detect(adap, driver);
1591
1592 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001593 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001594 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1595 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001596 dev_warn(&adap->dev, "Please use another way to instantiate "
1597 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001598 /* We ignore the return code; if it fails, too bad */
1599 driver->attach_adapter(adap);
1600 }
1601 return 0;
1602}
1603
Jean Delvare69b00892009-12-06 17:06:27 +01001604static int __process_new_adapter(struct device_driver *d, void *data)
1605{
1606 return i2c_do_add_adapter(to_i2c_driver(d), data);
1607}
1608
David Brownell6e13e642007-05-01 23:26:31 +02001609static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Jean Delvared6703282010-08-11 18:20:59 +02001611 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
David Brownell1d0b19c2008-10-14 17:30:05 +02001613 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301614 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001615 res = -EAGAIN;
1616 goto out_list;
1617 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001618
Jean Delvare2236baa2010-11-15 22:40:38 +01001619 /* Sanity checks */
1620 if (unlikely(adap->name[0] == '\0')) {
1621 pr_err("i2c-core: Attempt to register an adapter with "
1622 "no name!\n");
1623 return -EINVAL;
1624 }
1625 if (unlikely(!adap->algo)) {
1626 pr_err("i2c-core: Attempt to register adapter '%s' with "
1627 "no algo!\n", adap->name);
1628 return -EINVAL;
1629 }
1630
Peter Rosin8320f492016-05-04 22:15:27 +02001631 if (!adap->lock_bus) {
1632 adap->lock_bus = i2c_adapter_lock_bus;
1633 adap->trylock_bus = i2c_adapter_trylock_bus;
1634 adap->unlock_bus = i2c_adapter_unlock_bus;
1635 }
1636
Mika Kuoppala194684e2009-12-06 17:06:22 +01001637 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001638 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001639 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001640 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Jean Delvare8fcfef62009-03-28 21:34:43 +01001642 /* Set default timeout to 1 second if not already set */
1643 if (adap->timeout == 0)
1644 adap->timeout = HZ;
1645
Kay Sievers27d9c182009-01-07 14:29:16 +01001646 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001647 adap->dev.bus = &i2c_bus_type;
1648 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001649 res = device_register(&adap->dev);
1650 if (res)
1651 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001653 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1654
Charles Keepax6ada5c12015-04-16 13:05:19 +01001655 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001656 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001657 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001658
Jean Delvare2bb50952009-09-18 22:45:46 +02001659#ifdef CONFIG_I2C_COMPAT
1660 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1661 adap->dev.parent);
1662 if (res)
1663 dev_warn(&adap->dev,
1664 "Failed to create compatibility class link\n");
1665#endif
1666
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301667 /* bus recovery specific initialization */
1668 if (adap->bus_recovery_info) {
1669 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1670
1671 if (!bri->recover_bus) {
1672 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1673 adap->bus_recovery_info = NULL;
1674 goto exit_recovery;
1675 }
1676
1677 /* Generic GPIO recovery */
1678 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1679 if (!gpio_is_valid(bri->scl_gpio)) {
1680 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1681 adap->bus_recovery_info = NULL;
1682 goto exit_recovery;
1683 }
1684
1685 if (gpio_is_valid(bri->sda_gpio))
1686 bri->get_sda = get_sda_gpio_value;
1687 else
1688 bri->get_sda = NULL;
1689
1690 bri->get_scl = get_scl_gpio_value;
1691 bri->set_scl = set_scl_gpio_value;
Wolfram Sang3a9ddaf2016-05-12 11:25:19 +02001692 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301693 /* Generic SCL recovery */
Wolfram Sang3a9ddaf2016-05-12 11:25:19 +02001694 if (!bri->set_scl || !bri->get_scl) {
1695 dev_err(&adap->dev, "No {get|set}_scl() found, not using recovery\n");
1696 adap->bus_recovery_info = NULL;
1697 }
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301698 }
1699 }
1700
1701exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001702 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001703 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001704 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001705 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001706
David Brownell6e13e642007-05-01 23:26:31 +02001707 if (adap->nr < __i2c_first_dynamic_bus_num)
1708 i2c_scan_static_board_info(adap);
1709
Jean Delvare4735c982008-07-14 22:38:36 +02001710 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001711 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001712 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001713 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001714
1715 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001716
Jean Delvareb119c6c2006-08-15 18:26:30 +02001717out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001718 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001719 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001720 mutex_unlock(&core_lock);
1721 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
David Brownell6e13e642007-05-01 23:26:31 +02001724/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001725 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1726 * @adap: the adapter to register (with adap->nr initialized)
1727 * Context: can sleep
1728 *
1729 * See i2c_add_numbered_adapter() for details.
1730 */
1731static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1732{
1733 int id;
1734
1735 mutex_lock(&core_lock);
1736 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1737 GFP_KERNEL);
1738 mutex_unlock(&core_lock);
1739 if (id < 0)
1740 return id == -ENOSPC ? -EBUSY : id;
1741
1742 return i2c_register_adapter(adap);
1743}
1744
1745/**
David Brownell6e13e642007-05-01 23:26:31 +02001746 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1747 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001748 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001749 *
1750 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001751 * doesn't matter or when its bus number is specified by an dt alias.
1752 * Examples of bases when the bus number doesn't matter: I2C adapters
1753 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001754 *
1755 * When this returns zero, a new bus number was allocated and stored
1756 * in adap->nr, and the specified adapter became available for clients.
1757 * Otherwise, a negative errno value is returned.
1758 */
1759int i2c_add_adapter(struct i2c_adapter *adapter)
1760{
Doug Andersonee5c2742013-03-01 08:57:31 -08001761 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001762 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001763
Doug Andersonee5c2742013-03-01 08:57:31 -08001764 if (dev->of_node) {
1765 id = of_alias_get_id(dev->of_node, "i2c");
1766 if (id >= 0) {
1767 adapter->nr = id;
1768 return __i2c_add_numbered_adapter(adapter);
1769 }
1770 }
1771
Jean Delvarecaada322008-01-27 18:14:49 +01001772 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001773 id = idr_alloc(&i2c_adapter_idr, adapter,
1774 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001775 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001776 if (id < 0)
1777 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001778
1779 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001780
David Brownell6e13e642007-05-01 23:26:31 +02001781 return i2c_register_adapter(adapter);
1782}
1783EXPORT_SYMBOL(i2c_add_adapter);
1784
1785/**
1786 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1787 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001788 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001789 *
1790 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001791 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1792 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001793 * is used to properly configure I2C devices.
1794 *
Grant Likely488bf312011-07-25 17:49:43 +02001795 * If the requested bus number is set to -1, then this function will behave
1796 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1797 *
David Brownell6e13e642007-05-01 23:26:31 +02001798 * If no devices have pre-been declared for this bus, then be sure to
1799 * register the adapter before any dynamically allocated ones. Otherwise
1800 * the required bus ID may not be available.
1801 *
1802 * When this returns zero, the specified adapter became available for
1803 * clients using the bus number provided in adap->nr. Also, the table
1804 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1805 * and the appropriate driver model device nodes are created. Otherwise, a
1806 * negative errno value is returned.
1807 */
1808int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1809{
Grant Likely488bf312011-07-25 17:49:43 +02001810 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1811 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001812
Doug Andersonee5c2742013-03-01 08:57:31 -08001813 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001814}
1815EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1816
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001817static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001818 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001819{
Jean Delvare4735c982008-07-14 22:38:36 +02001820 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001821
Jean Delvareacec2112009-03-28 21:34:40 +01001822 /* Remove the devices we created ourselves as the result of hardware
1823 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001824 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1825 if (client->adapter == adapter) {
1826 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1827 client->name, client->addr);
1828 list_del(&client->detected);
1829 i2c_unregister_device(client);
1830 }
1831 }
Jean Delvare026526f2008-01-27 18:14:49 +01001832}
1833
Jean Delvaree549c2b2009-06-19 16:58:19 +02001834static int __unregister_client(struct device *dev, void *dummy)
1835{
1836 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001837 if (client && strcmp(client->name, "dummy"))
1838 i2c_unregister_device(client);
1839 return 0;
1840}
1841
1842static int __unregister_dummy(struct device *dev, void *dummy)
1843{
1844 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001845 if (client)
1846 i2c_unregister_device(client);
1847 return 0;
1848}
1849
Jean Delvare69b00892009-12-06 17:06:27 +01001850static int __process_removed_adapter(struct device_driver *d, void *data)
1851{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001852 i2c_do_del_adapter(to_i2c_driver(d), data);
1853 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001854}
1855
David Brownelld64f73b2007-07-12 14:12:28 +02001856/**
1857 * i2c_del_adapter - unregister I2C adapter
1858 * @adap: the adapter being unregistered
1859 * Context: can sleep
1860 *
1861 * This unregisters an I2C adapter which was previously registered
1862 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1863 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001864void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001866 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001867 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001870 mutex_lock(&core_lock);
1871 found = idr_find(&i2c_adapter_idr, adap->nr);
1872 mutex_unlock(&core_lock);
1873 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001874 pr_debug("i2c-core: attempting to delete unregistered "
1875 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001876 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Lan Tianyu5d98e612014-05-20 20:59:23 +08001879 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001880 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001881 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001882 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001883 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001884 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001886 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001887 mutex_lock_nested(&adap->userspace_clients_lock,
1888 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001889 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1890 detected) {
1891 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1892 client->addr);
1893 list_del(&client->detected);
1894 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001895 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001896 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001897
Jean Delvaree549c2b2009-06-19 16:58:19 +02001898 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001899 * check the returned value. This is a two-pass process, because
1900 * we can't remove the dummy devices during the first pass: they
1901 * could have been instantiated by real devices wishing to clean
1902 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001903 device_for_each_child(&adap->dev, NULL, __unregister_client);
1904 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Jean Delvare2bb50952009-09-18 22:45:46 +02001906#ifdef CONFIG_I2C_COMPAT
1907 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1908 adap->dev.parent);
1909#endif
1910
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001911 /* device name is gone after device_unregister */
1912 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1913
Wolfram Sang9f924162015-12-23 18:19:28 +01001914 pm_runtime_disable(&adap->dev);
1915
Wolfram Sang26680ee2015-01-29 22:45:09 +01001916 /* wait until all references to the device are gone
1917 *
1918 * FIXME: This is old code and should ideally be replaced by an
1919 * alternative which results in decoupling the lifetime of the struct
1920 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301921 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
David Brownell6e13e642007-05-01 23:26:31 +02001927 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001928 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001930 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001932 /* Clear the device structure in case this adapter is ever going to be
1933 added again */
1934 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
David Brownellc0564602007-05-01 23:26:31 +02001936EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Wolfram Sang54177cc2015-12-17 13:32:36 +01001938/**
1939 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1940 * @dev: The device to scan for I2C timing properties
1941 * @t: the i2c_timings struct to be filled with values
1942 * @use_defaults: bool to use sane defaults derived from the I2C specification
1943 * when properties are not found, otherwise use 0
1944 *
1945 * Scan the device for the generic I2C properties describing timing parameters
1946 * for the signal and fill the given struct with the results. If a property was
1947 * not found and use_defaults was true, then maximum timings are assumed which
1948 * are derived from the I2C specification. If use_defaults is not used, the
1949 * results will be 0, so drivers can apply their own defaults later. The latter
1950 * is mainly intended for avoiding regressions of existing drivers which want
1951 * to switch to this function. New drivers almost always should use the defaults.
1952 */
1953
1954void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1955{
1956 int ret;
1957
1958 memset(t, 0, sizeof(*t));
1959
1960 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1961 if (ret && use_defaults)
1962 t->bus_freq_hz = 100000;
1963
1964 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1965 if (ret && use_defaults) {
1966 if (t->bus_freq_hz <= 100000)
1967 t->scl_rise_ns = 1000;
1968 else if (t->bus_freq_hz <= 400000)
1969 t->scl_rise_ns = 300;
1970 else
1971 t->scl_rise_ns = 120;
1972 }
1973
1974 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1975 if (ret && use_defaults) {
1976 if (t->bus_freq_hz <= 400000)
1977 t->scl_fall_ns = 300;
1978 else
1979 t->scl_fall_ns = 120;
1980 }
1981
1982 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1983
1984 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1985 if (ret && use_defaults)
1986 t->sda_fall_ns = t->scl_fall_ns;
1987}
1988EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1989
David Brownell7b4fbc52007-05-01 23:26:30 +02001990/* ------------------------------------------------------------------------- */
1991
Jean Delvare7ae31482011-03-20 14:50:52 +01001992int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1993{
1994 int res;
1995
1996 mutex_lock(&core_lock);
1997 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1998 mutex_unlock(&core_lock);
1999
2000 return res;
2001}
2002EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2003
Jean Delvare69b00892009-12-06 17:06:27 +01002004static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002005{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002006 if (dev->type != &i2c_adapter_type)
2007 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002008 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002009}
2010
David Brownell7b4fbc52007-05-01 23:26:30 +02002011/*
2012 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002013 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
2015
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002016int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002018 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
David Brownell1d0b19c2008-10-14 17:30:05 +02002020 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302021 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002022 return -EAGAIN;
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002025 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Jean Delvare729d6dd2009-06-19 16:58:18 +02002028 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002029 * will have called probe() for all matching-but-unbound devices.
2030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 res = driver_register(&driver->driver);
2032 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002033 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002034
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01002035 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Jean Delvare4735c982008-07-14 22:38:36 +02002037 INIT_LIST_HEAD(&driver->clients);
2038 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002039 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002040
Jean Delvare7eebcb72006-02-05 23:28:21 +01002041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002043EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jean Delvare69b00892009-12-06 17:06:27 +01002045static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002046{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002047 if (dev->type == &i2c_adapter_type)
2048 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2049 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002050}
2051
David Brownella1d9e6e2007-05-01 23:26:30 +02002052/**
2053 * i2c_del_driver - unregister I2C driver
2054 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002055 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002056 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002057void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Jean Delvare7ae31482011-03-20 14:50:52 +01002059 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01002062 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
David Brownellc0564602007-05-01 23:26:31 +02002064EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
David Brownell7b4fbc52007-05-01 23:26:30 +02002066/* ------------------------------------------------------------------------- */
2067
Jean Delvaree48d3312008-01-27 18:14:48 +01002068/**
2069 * i2c_use_client - increments the reference count of the i2c client structure
2070 * @client: the client being referenced
2071 *
2072 * Each live reference to a client should be refcounted. The driver model does
2073 * that automatically as part of driver binding, so that most drivers don't
2074 * need to do this explicitly: they hold a reference until they're unbound
2075 * from the device.
2076 *
2077 * A pointer to the client with the incremented reference counter is returned.
2078 */
2079struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
David Brownell6ea438e2008-07-14 22:38:24 +02002081 if (client && get_device(&client->dev))
2082 return client;
2083 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
David Brownellc0564602007-05-01 23:26:31 +02002085EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jean Delvaree48d3312008-01-27 18:14:48 +01002087/**
2088 * i2c_release_client - release a use of the i2c client structure
2089 * @client: the client being no longer referenced
2090 *
2091 * Must be called when a user of a client is finished with it.
2092 */
2093void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
David Brownell6ea438e2008-07-14 22:38:24 +02002095 if (client)
2096 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
David Brownellc0564602007-05-01 23:26:31 +02002098EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
David Brownell9b766b82008-01-27 18:14:51 +01002100struct i2c_cmd_arg {
2101 unsigned cmd;
2102 void *arg;
2103};
2104
2105static int i2c_cmd(struct device *dev, void *_arg)
2106{
2107 struct i2c_client *client = i2c_verify_client(dev);
2108 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002109 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002110
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002111 if (!client || !client->dev.driver)
2112 return 0;
2113
2114 driver = to_i2c_driver(client->dev.driver);
2115 if (driver->command)
2116 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002117 return 0;
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2121{
David Brownell9b766b82008-01-27 18:14:51 +01002122 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
David Brownell9b766b82008-01-27 18:14:51 +01002124 cmd_arg.cmd = cmd;
2125 cmd_arg.arg = arg;
2126 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
David Brownellc0564602007-05-01 23:26:31 +02002128EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002130#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2131static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2132 void *arg)
2133{
2134 struct of_reconfig_data *rd = arg;
2135 struct i2c_adapter *adap;
2136 struct i2c_client *client;
2137
2138 switch (of_reconfig_get_state_change(action, rd)) {
2139 case OF_RECONFIG_CHANGE_ADD:
2140 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2141 if (adap == NULL)
2142 return NOTIFY_OK; /* not for us */
2143
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002144 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2145 put_device(&adap->dev);
2146 return NOTIFY_OK;
2147 }
2148
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002149 client = of_i2c_register_device(adap, rd->dn);
2150 put_device(&adap->dev);
2151
2152 if (IS_ERR(client)) {
2153 pr_err("%s: failed to create for '%s'\n",
2154 __func__, rd->dn->full_name);
2155 return notifier_from_errno(PTR_ERR(client));
2156 }
2157 break;
2158 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002159 /* already depopulated? */
2160 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2161 return NOTIFY_OK;
2162
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002163 /* find our device by node */
2164 client = of_find_i2c_device_by_node(rd->dn);
2165 if (client == NULL)
2166 return NOTIFY_OK; /* no? not meant for us */
2167
2168 /* unregister takes one ref away */
2169 i2c_unregister_device(client);
2170
2171 /* and put the reference of the find */
2172 put_device(&client->dev);
2173 break;
2174 }
2175
2176 return NOTIFY_OK;
2177}
2178static struct notifier_block i2c_of_notifier = {
2179 .notifier_call = of_i2c_notify,
2180};
2181#else
2182extern struct notifier_block i2c_of_notifier;
2183#endif /* CONFIG_OF_DYNAMIC */
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185static int __init i2c_init(void)
2186{
2187 int retval;
2188
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002189 retval = of_alias_get_highest_id("i2c");
2190
2191 down_write(&__i2c_board_lock);
2192 if (retval >= __i2c_first_dynamic_bus_num)
2193 __i2c_first_dynamic_bus_num = retval + 1;
2194 up_write(&__i2c_board_lock);
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 retval = bus_register(&i2c_bus_type);
2197 if (retval)
2198 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002199
2200 is_registered = true;
2201
Jean Delvare2bb50952009-09-18 22:45:46 +02002202#ifdef CONFIG_I2C_COMPAT
2203 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2204 if (!i2c_adapter_compat_class) {
2205 retval = -ENOMEM;
2206 goto bus_err;
2207 }
2208#endif
David Brownelle9f13732008-01-27 18:14:52 +01002209 retval = i2c_add_driver(&dummy_driver);
2210 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002211 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002212
2213 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2214 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002215 if (IS_ENABLED(CONFIG_ACPI))
2216 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002217
David Brownelle9f13732008-01-27 18:14:52 +01002218 return 0;
2219
Jean Delvare2bb50952009-09-18 22:45:46 +02002220class_err:
2221#ifdef CONFIG_I2C_COMPAT
2222 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002223bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002224#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002225 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002226 bus_unregister(&i2c_bus_type);
2227 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230static void __exit i2c_exit(void)
2231{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002232 if (IS_ENABLED(CONFIG_ACPI))
2233 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002234 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2235 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002236 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002237#ifdef CONFIG_I2C_COMPAT
2238 class_compat_unregister(i2c_adapter_compat_class);
2239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002241 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
David Brownella10f9e72008-10-14 17:30:06 +02002244/* We must initialize early, because some subsystems register i2c drivers
2245 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2246 */
2247postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248module_exit(i2c_exit);
2249
2250/* ----------------------------------------------------
2251 * the functional interface to the i2c busses.
2252 * ----------------------------------------------------
2253 */
2254
Wolfram Sangb7f62582015-01-05 23:45:59 +01002255/* Check if val is exceeding the quirk IFF quirk is non 0 */
2256#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2257
2258static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2259{
2260 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2261 err_msg, msg->addr, msg->len,
2262 msg->flags & I2C_M_RD ? "read" : "write");
2263 return -EOPNOTSUPP;
2264}
2265
2266static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2267{
2268 const struct i2c_adapter_quirks *q = adap->quirks;
2269 int max_num = q->max_num_msgs, i;
2270 bool do_len_check = true;
2271
2272 if (q->flags & I2C_AQ_COMB) {
2273 max_num = 2;
2274
2275 /* special checks for combined messages */
2276 if (num == 2) {
2277 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2278 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2279
2280 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2281 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2282
2283 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2284 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2285
2286 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2287 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2288
2289 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2290 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2291
2292 do_len_check = false;
2293 }
2294 }
2295
2296 if (i2c_quirk_exceeded(num, max_num))
2297 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2298
2299 for (i = 0; i < num; i++) {
2300 u16 len = msgs[i].len;
2301
2302 if (msgs[i].flags & I2C_M_RD) {
2303 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2304 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2305 } else {
2306 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2307 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2308 }
2309 }
2310
2311 return 0;
2312}
2313
David Brownella1cdeda2008-07-14 22:38:24 +02002314/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002315 * __i2c_transfer - unlocked flavor of i2c_transfer
2316 * @adap: Handle to I2C bus
2317 * @msgs: One or more messages to execute before STOP is issued to
2318 * terminate the operation; each message begins with a START.
2319 * @num: Number of messages to be executed.
2320 *
2321 * Returns negative errno, else the number of messages executed.
2322 *
2323 * Adapter lock must be held when calling this function. No debug logging
2324 * takes place. adap->algo->master_xfer existence isn't checked.
2325 */
2326int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2327{
2328 unsigned long orig_jiffies;
2329 int ret, try;
2330
Wolfram Sangb7f62582015-01-05 23:45:59 +01002331 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2332 return -EOPNOTSUPP;
2333
David Howellsd9a83d62014-03-06 13:35:59 +00002334 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2335 * enabled. This is an efficient way of keeping the for-loop from
2336 * being executed when not needed.
2337 */
2338 if (static_key_false(&i2c_trace_msg)) {
2339 int i;
2340 for (i = 0; i < num; i++)
2341 if (msgs[i].flags & I2C_M_RD)
2342 trace_i2c_read(adap, &msgs[i], i);
2343 else
2344 trace_i2c_write(adap, &msgs[i], i);
2345 }
2346
Jean Delvareb37d2a32012-06-29 07:47:19 -03002347 /* Retry automatically on arbitration loss */
2348 orig_jiffies = jiffies;
2349 for (ret = 0, try = 0; try <= adap->retries; try++) {
2350 ret = adap->algo->master_xfer(adap, msgs, num);
2351 if (ret != -EAGAIN)
2352 break;
2353 if (time_after(jiffies, orig_jiffies + adap->timeout))
2354 break;
2355 }
2356
David Howellsd9a83d62014-03-06 13:35:59 +00002357 if (static_key_false(&i2c_trace_msg)) {
2358 int i;
2359 for (i = 0; i < ret; i++)
2360 if (msgs[i].flags & I2C_M_RD)
2361 trace_i2c_reply(adap, &msgs[i], i);
2362 trace_i2c_result(adap, i, ret);
2363 }
2364
Jean Delvareb37d2a32012-06-29 07:47:19 -03002365 return ret;
2366}
2367EXPORT_SYMBOL(__i2c_transfer);
2368
2369/**
David Brownella1cdeda2008-07-14 22:38:24 +02002370 * i2c_transfer - execute a single or combined I2C message
2371 * @adap: Handle to I2C bus
2372 * @msgs: One or more messages to execute before STOP is issued to
2373 * terminate the operation; each message begins with a START.
2374 * @num: Number of messages to be executed.
2375 *
2376 * Returns negative errno, else the number of messages executed.
2377 *
2378 * Note that there is no requirement that each message be sent to
2379 * the same slave address, although that is the most common model.
2380 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002381int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002383 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
David Brownella1cdeda2008-07-14 22:38:24 +02002385 /* REVISIT the fault reporting model here is weak:
2386 *
2387 * - When we get an error after receiving N bytes from a slave,
2388 * there is no way to report "N".
2389 *
2390 * - When we get a NAK after transmitting N bytes to a slave,
2391 * there is no way to report "N" ... or to let the master
2392 * continue executing the rest of this combined message, if
2393 * that's the appropriate response.
2394 *
2395 * - When for example "num" is two and we successfully complete
2396 * the first message but get an error part way through the
2397 * second, it's unclear whether that should be reported as
2398 * one (discarding status on the second message) or errno
2399 * (discarding status on the first one).
2400 */
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (adap->algo->master_xfer) {
2403#ifdef DEBUG
2404 for (ret = 0; ret < num; ret++) {
2405 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002406 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2407 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2408 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410#endif
2411
Mike Rapoportcea443a2008-01-27 18:14:50 +01002412 if (in_atomic() || irqs_disabled()) {
Peter Rosin8320f492016-05-04 22:15:27 +02002413 ret = adap->trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002414 if (!ret)
2415 /* I2C activity is ongoing. */
2416 return -EAGAIN;
2417 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002418 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002419 }
2420
Jean Delvareb37d2a32012-06-29 07:47:19 -03002421 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002422 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 return ret;
2425 } else {
2426 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002427 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429}
David Brownellc0564602007-05-01 23:26:31 +02002430EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
David Brownella1cdeda2008-07-14 22:38:24 +02002432/**
2433 * i2c_master_send - issue a single I2C message in master transmit mode
2434 * @client: Handle to slave device
2435 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002436 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002437 *
2438 * Returns negative errno, or else the number of bytes written.
2439 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002440int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
2442 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002443 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct i2c_msg msg;
2445
Jean Delvare815f55f2005-05-07 22:58:46 +02002446 msg.addr = client->addr;
2447 msg.flags = client->flags & I2C_M_TEN;
2448 msg.len = count;
2449 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002450
Jean Delvare815f55f2005-05-07 22:58:46 +02002451 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002453 /*
2454 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2455 * transmitted, else error code.
2456 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002457 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
David Brownellc0564602007-05-01 23:26:31 +02002459EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
David Brownella1cdeda2008-07-14 22:38:24 +02002461/**
2462 * i2c_master_recv - issue a single I2C message in master receive mode
2463 * @client: Handle to slave device
2464 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002465 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002466 *
2467 * Returns negative errno, or else the number of bytes read.
2468 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002469int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Farid Hammane7225acf2010-05-21 18:40:58 +02002471 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct i2c_msg msg;
2473 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Jean Delvare815f55f2005-05-07 22:58:46 +02002475 msg.addr = client->addr;
2476 msg.flags = client->flags & I2C_M_TEN;
2477 msg.flags |= I2C_M_RD;
2478 msg.len = count;
2479 msg.buf = buf;
2480
2481 ret = i2c_transfer(adap, &msg, 1);
2482
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002483 /*
2484 * If everything went ok (i.e. 1 msg received), return #bytes received,
2485 * else error code.
2486 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002487 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
David Brownellc0564602007-05-01 23:26:31 +02002489EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/* ----------------------------------------------------
2492 * the i2c address scanning function
2493 * Will not work for 10-bit addresses!
2494 * ----------------------------------------------------
2495 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002496
Jean Delvare63e4e802010-06-03 11:33:51 +02002497/*
2498 * Legacy default probe function, mostly relevant for SMBus. The default
2499 * probe method is a quick write, but it is known to corrupt the 24RF08
2500 * EEPROMs due to a state machine bug, and could also irreversibly
2501 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2502 * we use a short byte read instead. Also, some bus drivers don't implement
2503 * quick write, so we fallback to a byte read in that case too.
2504 * On x86, there is another special case for FSC hardware monitoring chips,
2505 * which want regular byte reads (address 0x73.) Fortunately, these are the
2506 * only known chips using this I2C address on PC hardware.
2507 * Returns 1 if probe succeeded, 0 if not.
2508 */
2509static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2510{
2511 int err;
2512 union i2c_smbus_data dummy;
2513
2514#ifdef CONFIG_X86
2515 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2516 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2517 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2518 I2C_SMBUS_BYTE_DATA, &dummy);
2519 else
2520#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002521 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2522 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002523 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2524 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002525 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2526 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2527 I2C_SMBUS_BYTE, &dummy);
2528 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002529 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2530 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002531 err = -EOPNOTSUPP;
2532 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002533
2534 return err >= 0;
2535}
2536
Jean Delvareccfbbd02009-12-06 17:06:25 +01002537static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002538 struct i2c_driver *driver)
2539{
2540 struct i2c_board_info info;
2541 struct i2c_adapter *adapter = temp_client->adapter;
2542 int addr = temp_client->addr;
2543 int err;
2544
2545 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002546 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002547 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002548 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2549 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002550 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002551 }
2552
Wolfram Sang9bccc702015-07-17 14:48:56 +02002553 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002554 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002555 return 0;
2556
Jean Delvareccfbbd02009-12-06 17:06:25 +01002557 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002558 if (!i2c_default_probe(adapter, addr))
2559 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002560
2561 /* Finally call the custom detection function */
2562 memset(&info, 0, sizeof(struct i2c_board_info));
2563 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002564 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002565 if (err) {
2566 /* -ENODEV is returned if the detection fails. We catch it
2567 here as this isn't an error. */
2568 return err == -ENODEV ? 0 : err;
2569 }
2570
2571 /* Consistency check */
2572 if (info.type[0] == '\0') {
2573 dev_err(&adapter->dev, "%s detection function provided "
2574 "no name for 0x%x\n", driver->driver.name,
2575 addr);
2576 } else {
2577 struct i2c_client *client;
2578
2579 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002580 if (adapter->class & I2C_CLASS_DEPRECATED)
2581 dev_warn(&adapter->dev,
2582 "This adapter will soon drop class based instantiation of devices. "
2583 "Please make sure client 0x%02x gets instantiated by other means. "
2584 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2585 info.addr);
2586
Jean Delvare4735c982008-07-14 22:38:36 +02002587 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2588 info.type, info.addr);
2589 client = i2c_new_device(adapter, &info);
2590 if (client)
2591 list_add_tail(&client->detected, &driver->clients);
2592 else
2593 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2594 info.type, info.addr);
2595 }
2596 return 0;
2597}
2598
2599static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2600{
Jean Delvarec3813d62009-12-14 21:17:25 +01002601 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002602 struct i2c_client *temp_client;
2603 int i, err = 0;
2604 int adap_id = i2c_adapter_id(adapter);
2605
Jean Delvarec3813d62009-12-14 21:17:25 +01002606 address_list = driver->address_list;
2607 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002608 return 0;
2609
Wolfram Sang45552272014-07-10 13:46:21 +02002610 /* Warn that the adapter lost class based instantiation */
2611 if (adapter->class == I2C_CLASS_DEPRECATED) {
2612 dev_dbg(&adapter->dev,
2613 "This adapter dropped support for I2C classes and "
2614 "won't auto-detect %s devices anymore. If you need it, check "
2615 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2616 driver->driver.name);
2617 return 0;
2618 }
2619
Jean Delvare51b54ba2010-10-24 18:16:58 +02002620 /* Stop here if the classes do not match */
2621 if (!(adapter->class & driver->class))
2622 return 0;
2623
Jean Delvare4735c982008-07-14 22:38:36 +02002624 /* Set up a temporary client to help detect callback */
2625 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2626 if (!temp_client)
2627 return -ENOMEM;
2628 temp_client->adapter = adapter;
2629
Jean Delvarec3813d62009-12-14 21:17:25 +01002630 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002631 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002632 "addr 0x%02x\n", adap_id, address_list[i]);
2633 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002634 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002635 if (unlikely(err))
2636 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002637 }
2638
Jean Delvare4735c982008-07-14 22:38:36 +02002639 kfree(temp_client);
2640 return err;
2641}
2642
Jean Delvared44f19d2010-08-11 18:20:57 +02002643int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2644{
2645 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2646 I2C_SMBUS_QUICK, NULL) >= 0;
2647}
2648EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2649
Jean Delvare12b5053a2007-05-01 23:26:31 +02002650struct i2c_client *
2651i2c_new_probed_device(struct i2c_adapter *adap,
2652 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002653 unsigned short const *addr_list,
2654 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002655{
2656 int i;
2657
Jean Delvare8031d792010-08-11 18:21:00 +02002658 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002659 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002660
Jean Delvare12b5053a2007-05-01 23:26:31 +02002661 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2662 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002663 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002664 dev_warn(&adap->dev, "Invalid 7-bit address "
2665 "0x%02x\n", addr_list[i]);
2666 continue;
2667 }
2668
Wolfram Sang9bccc702015-07-17 14:48:56 +02002669 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002670 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002671 dev_dbg(&adap->dev, "Address 0x%02x already in "
2672 "use, not probing\n", addr_list[i]);
2673 continue;
2674 }
2675
Jean Delvare63e4e802010-06-03 11:33:51 +02002676 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002677 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002678 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002679 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002680
2681 if (addr_list[i] == I2C_CLIENT_END) {
2682 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2683 return NULL;
2684 }
2685
2686 info->addr = addr_list[i];
2687 return i2c_new_device(adap, info);
2688}
2689EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2690
Jean Delvared735b342011-03-20 14:50:52 +01002691struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002694
Jean Delvarecaada322008-01-27 18:14:49 +01002695 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002696 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002697 if (!adapter)
2698 goto exit;
2699
2700 if (try_module_get(adapter->owner))
2701 get_device(&adapter->dev);
2702 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002703 adapter = NULL;
2704
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002705 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002706 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002707 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
David Brownellc0564602007-05-01 23:26:31 +02002709EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711void i2c_put_adapter(struct i2c_adapter *adap)
2712{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002713 if (!adap)
2714 return;
2715
2716 put_device(&adap->dev);
2717 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
David Brownellc0564602007-05-01 23:26:31 +02002719EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721/* The SMBus parts */
2722
David Brownell438d6c22006-12-10 21:21:31 +01002723#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002724static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002727
Farid Hammane7225acf2010-05-21 18:40:58 +02002728 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002729 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 data = data ^ POLY;
2731 data = data << 1;
2732 }
2733 return (u8)(data >> 8);
2734}
2735
Jean Delvare421ef472005-10-26 21:28:55 +02002736/* Incremental CRC8 over count bytes in the array pointed to by p */
2737static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 int i;
2740
Farid Hammane7225acf2010-05-21 18:40:58 +02002741 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002742 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return crc;
2744}
2745
Jean Delvare421ef472005-10-26 21:28:55 +02002746/* Assume a 7-bit address, which is reasonable for SMBus */
2747static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Jean Delvare421ef472005-10-26 21:28:55 +02002749 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002750 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002751 pec = i2c_smbus_pec(pec, &addr, 1);
2752
2753 /* The data buffer follows */
2754 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Jean Delvare421ef472005-10-26 21:28:55 +02002757/* Used for write only transactions */
2758static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
Jean Delvare421ef472005-10-26 21:28:55 +02002760 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2761 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Jean Delvare421ef472005-10-26 21:28:55 +02002764/* Return <0 on CRC error
2765 If there was a write before this read (most cases) we need to take the
2766 partial CRC from the write part into account.
2767 Note that this function does modify the message (we need to decrease the
2768 message length to hide the CRC byte from the caller). */
2769static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Jean Delvare421ef472005-10-26 21:28:55 +02002771 u8 rpec = msg->buf[--msg->len];
2772 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (rpec != cpec) {
2775 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2776 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002777 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
David Brownell438d6c22006-12-10 21:21:31 +01002779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
David Brownella1cdeda2008-07-14 22:38:24 +02002782/**
2783 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2784 * @client: Handle to slave device
2785 *
2786 * This executes the SMBus "receive byte" protocol, returning negative errno
2787 * else the byte received from the device.
2788 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002789s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002792 int status;
2793
2794 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2795 I2C_SMBUS_READ, 0,
2796 I2C_SMBUS_BYTE, &data);
2797 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
David Brownellc0564602007-05-01 23:26:31 +02002799EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
David Brownella1cdeda2008-07-14 22:38:24 +02002801/**
2802 * i2c_smbus_write_byte - SMBus "send byte" protocol
2803 * @client: Handle to slave device
2804 * @value: Byte to be sent
2805 *
2806 * This executes the SMBus "send byte" protocol, returning negative errno
2807 * else zero on success.
2808 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002809s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Farid Hammane7225acf2010-05-21 18:40:58 +02002811 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002812 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
David Brownellc0564602007-05-01 23:26:31 +02002814EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
David Brownella1cdeda2008-07-14 22:38:24 +02002816/**
2817 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2818 * @client: Handle to slave device
2819 * @command: Byte interpreted by slave
2820 *
2821 * This executes the SMBus "read byte" protocol, returning negative errno
2822 * else a data byte received from the device.
2823 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002824s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002827 int status;
2828
2829 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2830 I2C_SMBUS_READ, command,
2831 I2C_SMBUS_BYTE_DATA, &data);
2832 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
David Brownellc0564602007-05-01 23:26:31 +02002834EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
David Brownella1cdeda2008-07-14 22:38:24 +02002836/**
2837 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2838 * @client: Handle to slave device
2839 * @command: Byte interpreted by slave
2840 * @value: Byte being written
2841 *
2842 * This executes the SMBus "write byte" protocol, returning negative errno
2843 * else zero on success.
2844 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002845s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2846 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 union i2c_smbus_data data;
2849 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002850 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2851 I2C_SMBUS_WRITE, command,
2852 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
David Brownellc0564602007-05-01 23:26:31 +02002854EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
David Brownella1cdeda2008-07-14 22:38:24 +02002856/**
2857 * i2c_smbus_read_word_data - SMBus "read word" protocol
2858 * @client: Handle to slave device
2859 * @command: Byte interpreted by slave
2860 *
2861 * This executes the SMBus "read word" protocol, returning negative errno
2862 * else a 16-bit unsigned "word" received from the device.
2863 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002864s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002867 int status;
2868
2869 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2870 I2C_SMBUS_READ, command,
2871 I2C_SMBUS_WORD_DATA, &data);
2872 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
David Brownellc0564602007-05-01 23:26:31 +02002874EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
David Brownella1cdeda2008-07-14 22:38:24 +02002876/**
2877 * i2c_smbus_write_word_data - SMBus "write word" protocol
2878 * @client: Handle to slave device
2879 * @command: Byte interpreted by slave
2880 * @value: 16-bit "word" being written
2881 *
2882 * This executes the SMBus "write word" protocol, returning negative errno
2883 * else zero on success.
2884 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002885s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2886 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 union i2c_smbus_data data;
2889 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002890 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2891 I2C_SMBUS_WRITE, command,
2892 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
David Brownellc0564602007-05-01 23:26:31 +02002894EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
David Brownella64ec072007-10-13 23:56:31 +02002896/**
David Brownella1cdeda2008-07-14 22:38:24 +02002897 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002898 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002899 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002900 * @values: Byte array into which data will be read; big enough to hold
2901 * the data returned by the slave. SMBus allows at most 32 bytes.
2902 *
David Brownella1cdeda2008-07-14 22:38:24 +02002903 * This executes the SMBus "block read" protocol, returning negative errno
2904 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002905 *
2906 * Note that using this function requires that the client's adapter support
2907 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2908 * support this; its emulation through I2C messaging relies on a specific
2909 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2910 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002911s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002912 u8 *values)
2913{
2914 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002915 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002916
David Brownell24a5bb72008-07-14 22:38:23 +02002917 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2918 I2C_SMBUS_READ, command,
2919 I2C_SMBUS_BLOCK_DATA, &data);
2920 if (status)
2921 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002922
2923 memcpy(values, &data.block[1], data.block[0]);
2924 return data.block[0];
2925}
2926EXPORT_SYMBOL(i2c_smbus_read_block_data);
2927
David Brownella1cdeda2008-07-14 22:38:24 +02002928/**
2929 * i2c_smbus_write_block_data - SMBus "block write" protocol
2930 * @client: Handle to slave device
2931 * @command: Byte interpreted by slave
2932 * @length: Size of data block; SMBus allows at most 32 bytes
2933 * @values: Byte array which will be written.
2934 *
2935 * This executes the SMBus "block write" protocol, returning negative errno
2936 * else zero on success.
2937 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002938s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002939 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (length > I2C_SMBUS_BLOCK_MAX)
2944 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002946 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002947 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2948 I2C_SMBUS_WRITE, command,
2949 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
David Brownellc0564602007-05-01 23:26:31 +02002951EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002954s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002955 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002958 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002959
Jean Delvare4b2643d2007-07-12 14:12:29 +02002960 if (length > I2C_SMBUS_BLOCK_MAX)
2961 length = I2C_SMBUS_BLOCK_MAX;
2962 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002963 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2964 I2C_SMBUS_READ, command,
2965 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2966 if (status < 0)
2967 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002968
2969 memcpy(values, &data.block[1], data.block[0]);
2970 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
David Brownellc0564602007-05-01 23:26:31 +02002972EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Jean Delvare0cc43a12011-01-10 22:11:23 +01002974s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002975 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002976{
2977 union i2c_smbus_data data;
2978
2979 if (length > I2C_SMBUS_BLOCK_MAX)
2980 length = I2C_SMBUS_BLOCK_MAX;
2981 data.block[0] = length;
2982 memcpy(data.block + 1, values, length);
2983 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2984 I2C_SMBUS_WRITE, command,
2985 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2986}
David Brownellc0564602007-05-01 23:26:31 +02002987EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002988
David Brownell438d6c22006-12-10 21:21:31 +01002989/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002991static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2992 unsigned short flags,
2993 char read_write, u8 command, int size,
2994 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 /* So we need to generate a series of msgs. In the case of writing, we
2997 need to use only one message; when reading, we need two. We initialize
2998 most things with sane defaults, to keep the code below somewhat
2999 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003000 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3001 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003002 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003004 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003005 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003006 struct i2c_msg msg[2] = {
3007 {
3008 .addr = addr,
3009 .flags = flags,
3010 .len = 1,
3011 .buf = msgbuf0,
3012 }, {
3013 .addr = addr,
3014 .flags = flags | I2C_M_RD,
3015 .len = 0,
3016 .buf = msgbuf1,
3017 },
3018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003021 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 case I2C_SMBUS_QUICK:
3023 msg[0].len = 0;
3024 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003025 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3026 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 num = 1;
3028 break;
3029 case I2C_SMBUS_BYTE:
3030 if (read_write == I2C_SMBUS_READ) {
3031 /* Special case: only a read! */
3032 msg[0].flags = I2C_M_RD | flags;
3033 num = 1;
3034 }
3035 break;
3036 case I2C_SMBUS_BYTE_DATA:
3037 if (read_write == I2C_SMBUS_READ)
3038 msg[1].len = 1;
3039 else {
3040 msg[0].len = 2;
3041 msgbuf0[1] = data->byte;
3042 }
3043 break;
3044 case I2C_SMBUS_WORD_DATA:
3045 if (read_write == I2C_SMBUS_READ)
3046 msg[1].len = 2;
3047 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003048 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003050 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052 break;
3053 case I2C_SMBUS_PROC_CALL:
3054 num = 2; /* Special case */
3055 read_write = I2C_SMBUS_READ;
3056 msg[0].len = 3;
3057 msg[1].len = 2;
3058 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003059 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 break;
3061 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003063 msg[1].flags |= I2C_M_RECV_LEN;
3064 msg[1].len = 1; /* block length will be added by
3065 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 } else {
3067 msg[0].len = data->block[0] + 2;
3068 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003069 dev_err(&adapter->dev,
3070 "Invalid block write size %d\n",
3071 data->block[0]);
3072 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003074 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 msgbuf0[i] = data->block[i-1];
3076 }
3077 break;
3078 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003079 num = 2; /* Another special case */
3080 read_write = I2C_SMBUS_READ;
3081 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003082 dev_err(&adapter->dev,
3083 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003084 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003085 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003086 }
3087 msg[0].len = data->block[0] + 2;
3088 for (i = 1; i < msg[0].len; i++)
3089 msgbuf0[i] = data->block[i-1];
3090 msg[1].flags |= I2C_M_RECV_LEN;
3091 msg[1].len = 1; /* block length will be added by
3092 the underlying bus driver */
3093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 case I2C_SMBUS_I2C_BLOCK_DATA:
3095 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003096 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 } else {
3098 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003099 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003100 dev_err(&adapter->dev,
3101 "Invalid block write size %d\n",
3102 data->block[0]);
3103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105 for (i = 1; i <= data->block[0]; i++)
3106 msgbuf0[i] = data->block[i];
3107 }
3108 break;
3109 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003110 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3111 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
Jean Delvare421ef472005-10-26 21:28:55 +02003114 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3115 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3116 if (i) {
3117 /* Compute PEC if first message is a write */
3118 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003119 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003120 i2c_smbus_add_pec(&msg[0]);
3121 else /* Write followed by read */
3122 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3123 }
3124 /* Ask for PEC if last message is a read */
3125 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003126 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003127 }
3128
David Brownell24a5bb72008-07-14 22:38:23 +02003129 status = i2c_transfer(adapter, msg, num);
3130 if (status < 0)
3131 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jean Delvare421ef472005-10-26 21:28:55 +02003133 /* Check PEC if last message is a read */
3134 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003135 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3136 if (status < 0)
3137 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003138 }
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003141 switch (size) {
3142 case I2C_SMBUS_BYTE:
3143 data->byte = msgbuf0[0];
3144 break;
3145 case I2C_SMBUS_BYTE_DATA:
3146 data->byte = msgbuf1[0];
3147 break;
3148 case I2C_SMBUS_WORD_DATA:
3149 case I2C_SMBUS_PROC_CALL:
3150 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3151 break;
3152 case I2C_SMBUS_I2C_BLOCK_DATA:
3153 for (i = 0; i < data->block[0]; i++)
3154 data->block[i+1] = msgbuf1[i];
3155 break;
3156 case I2C_SMBUS_BLOCK_DATA:
3157 case I2C_SMBUS_BLOCK_PROC_CALL:
3158 for (i = 0; i < msgbuf1[0] + 1; i++)
3159 data->block[i] = msgbuf1[i];
3160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
3162 return 0;
3163}
3164
David Brownella1cdeda2008-07-14 22:38:24 +02003165/**
3166 * i2c_smbus_xfer - execute SMBus protocol operations
3167 * @adapter: Handle to I2C bus
3168 * @addr: Address of SMBus slave on that bus
3169 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3170 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3171 * @command: Byte interpreted by slave, for protocols which use such bytes
3172 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3173 * @data: Data to be read or written
3174 *
3175 * This executes an SMBus protocol operation, and returns a negative
3176 * errno code else zero on success.
3177 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003178s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003179 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003180 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003182 unsigned long orig_jiffies;
3183 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
David Howells8a325992014-03-06 13:36:06 +00003186 /* If enabled, the following two tracepoints are conditional on
3187 * read_write and protocol.
3188 */
3189 trace_smbus_write(adapter, addr, flags, read_write,
3190 command, protocol, data);
3191 trace_smbus_read(adapter, addr, flags, read_write,
3192 command, protocol);
3193
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003194 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003197 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003198
3199 /* Retry automatically on arbitration loss */
3200 orig_jiffies = jiffies;
3201 for (res = 0, try = 0; try <= adapter->retries; try++) {
3202 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3203 read_write, command,
3204 protocol, data);
3205 if (res != -EAGAIN)
3206 break;
3207 if (time_after(jiffies,
3208 orig_jiffies + adapter->timeout))
3209 break;
3210 }
Peter Rosin8320f492016-05-04 22:15:27 +02003211 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003213 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003214 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003215 /*
3216 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3217 * implement native support for the SMBus operation.
3218 */
3219 }
3220
David Howells8a325992014-03-06 13:36:06 +00003221 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3222 command, protocol, data);
3223
3224trace:
3225 /* If enabled, the reply tracepoint is conditional on read_write. */
3226 trace_smbus_reply(adapter, addr, flags, read_write,
3227 command, protocol, data);
3228 trace_smbus_result(adapter, addr, flags, read_write,
3229 command, protocol, res);
3230
3231 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Irina Tirdea01eef962015-08-12 17:31:33 +03003235/**
3236 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3237 * @client: Handle to slave device
3238 * @command: Byte interpreted by slave
3239 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3240 * @values: Byte array into which data will be read; big enough to hold
3241 * the data returned by the slave. SMBus allows at most
3242 * I2C_SMBUS_BLOCK_MAX bytes.
3243 *
3244 * This executes the SMBus "block read" protocol if supported by the adapter.
3245 * If block read is not supported, it emulates it using either word or byte
3246 * read protocols depending on availability.
3247 *
3248 * The addresses of the I2C slave device that are accessed with this function
3249 * must be mapped to a linear region, so that a block read will have the same
3250 * effect as a byte read. Before using this function you must double-check
3251 * if the I2C slave does support exchanging a block transfer with a byte
3252 * transfer.
3253 */
3254s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3255 u8 command, u8 length, u8 *values)
3256{
3257 u8 i = 0;
3258 int status;
3259
3260 if (length > I2C_SMBUS_BLOCK_MAX)
3261 length = I2C_SMBUS_BLOCK_MAX;
3262
3263 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3264 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3265
3266 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3267 return -EOPNOTSUPP;
3268
3269 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3270 while ((i + 2) <= length) {
3271 status = i2c_smbus_read_word_data(client, command + i);
3272 if (status < 0)
3273 return status;
3274 values[i] = status & 0xff;
3275 values[i + 1] = status >> 8;
3276 i += 2;
3277 }
3278 }
3279
3280 while (i < length) {
3281 status = i2c_smbus_read_byte_data(client, command + i);
3282 if (status < 0)
3283 return status;
3284 values[i] = status;
3285 i++;
3286 }
3287
3288 return i;
3289}
3290EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3291
Jean Delvared5fd1202015-01-26 20:59:31 +01003292#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003293int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3294{
3295 int ret;
3296
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003297 if (!client || !slave_cb) {
3298 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003299 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003300 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003301
Wolfram Sangc6909d62015-08-05 21:12:54 +02003302 if (!(client->flags & I2C_CLIENT_SLAVE))
3303 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3304 __func__);
3305
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003306 if (!(client->flags & I2C_CLIENT_TEN)) {
3307 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003308 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003309 if (ret) {
3310 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003311 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003312 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003313 }
3314
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003315 if (!client->adapter->algo->reg_slave) {
3316 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003317 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003318 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003319
3320 client->slave_cb = slave_cb;
3321
3322 i2c_lock_adapter(client->adapter);
3323 ret = client->adapter->algo->reg_slave(client);
3324 i2c_unlock_adapter(client->adapter);
3325
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003326 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003327 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003328 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3329 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003330
3331 return ret;
3332}
3333EXPORT_SYMBOL_GPL(i2c_slave_register);
3334
3335int i2c_slave_unregister(struct i2c_client *client)
3336{
3337 int ret;
3338
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003339 if (!client->adapter->algo->unreg_slave) {
3340 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003341 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003342 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003343
3344 i2c_lock_adapter(client->adapter);
3345 ret = client->adapter->algo->unreg_slave(client);
3346 i2c_unlock_adapter(client->adapter);
3347
3348 if (ret == 0)
3349 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003350 else
3351 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003352
3353 return ret;
3354}
3355EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003356#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3359MODULE_DESCRIPTION("I2C-Bus main module");
3360MODULE_LICENSE("GPL");