blob: 679a31fcb6d6f4c4cd3e96860ec87c82f2ebacc2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram Sang44239a52016-07-09 13:35:04 +090030#define pr_fmt(fmt) "i2c-core: " fmt
31
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020032#include <dt-bindings/i2c/i2c.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010034#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020035#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010036#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010037#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020038#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010039#include <linux/errno.h>
40#include <linux/gpio.h>
41#include <linux/hardirq.h>
42#include <linux/i2c.h>
43#include <linux/idr.h>
44#include <linux/init.h>
45#include <linux/irqflags.h>
46#include <linux/jump_label.h>
47#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/mutex.h>
50#include <linux/of_device.h>
51#include <linux/of.h>
52#include <linux/of_irq.h>
53#include <linux/pm_domain.h>
54#include <linux/pm_runtime.h>
55#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010056#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010057#include <linux/rwsem.h>
58#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David Brownell9c1600e2007-05-01 23:26:31 +020060#include "i2c-core.h"
61
David Howellsd9a83d62014-03-06 13:35:59 +000062#define CREATE_TRACE_POINTS
63#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Wolfram Sangda899f52015-05-18 21:09:12 +020065#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
66#define I2C_ADDR_OFFSET_SLAVE 0x1000
67
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020068#define I2C_ADDR_7BITS_MAX 0x77
69#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
70
Jean Delvare6629dcf2010-05-04 11:09:28 +020071/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020072 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000073 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010074static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static DEFINE_IDR(i2c_adapter_idr);
76
Jean Delvare4735c982008-07-14 22:38:36 +020077static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010078
David Howellsd9a83d62014-03-06 13:35:59 +000079static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053080static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000081
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050082int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000083{
84 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050085 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000086}
87
88void i2c_transfer_trace_unreg(void)
89{
90 static_key_slow_dec(&i2c_trace_msg);
91}
92
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020093#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030094struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020095 struct acpi_connection_info info;
96 struct i2c_adapter *adapter;
97};
98
99struct gsb_buffer {
100 u8 status;
101 u8 len;
102 union {
103 u16 wdata;
104 u8 bdata;
105 u8 data[0];
106 };
107} __packed;
108
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300109struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300110 struct i2c_board_info *info;
111 acpi_handle adapter_handle;
112 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300113 acpi_handle search_handle;
114 u32 speed;
115 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300116};
117
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300118static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200119{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300120 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300121 struct i2c_board_info *info = lookup->info;
122 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300123 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200124
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300125 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
126 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200127
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300128 sb = &ares->data.i2c_serial_bus;
129 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
130 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200131
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300132 status = acpi_get_handle(lookup->device_handle,
133 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300134 &lookup->adapter_handle);
135 if (!ACPI_SUCCESS(status))
136 return 1;
137
138 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300139 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300140 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
141 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200142
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200143 return 1;
144}
145
Jarkko Nikula5853b222016-08-12 17:02:53 +0300146static int i2c_acpi_do_lookup(struct acpi_device *adev,
147 struct i2c_acpi_lookup *lookup)
148{
149 struct i2c_board_info *info = lookup->info;
150 struct list_head resource_list;
151 int ret;
152
153 if (acpi_bus_get_status(adev) || !adev->status.present ||
154 acpi_device_enumerated(adev))
155 return -EINVAL;
156
157 memset(info, 0, sizeof(*info));
158 lookup->device_handle = acpi_device_handle(adev);
159
160 /* Look up for I2cSerialBus resource */
161 INIT_LIST_HEAD(&resource_list);
162 ret = acpi_dev_get_resources(adev, &resource_list,
163 i2c_acpi_fill_info, lookup);
164 acpi_dev_free_resource_list(&resource_list);
165
166 if (ret < 0 || !info->addr)
167 return -EINVAL;
168
169 return 0;
170}
171
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300172static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300173 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300174 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300175 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200176{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200177 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300178 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300179 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200180 int ret;
181
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300182 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300183 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300184
Jarkko Nikula5853b222016-08-12 17:02:53 +0300185 ret = i2c_acpi_do_lookup(adev, &lookup);
186 if (ret)
187 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200188
Mika Westerberg318ce272016-09-20 16:59:25 +0300189 if (adapter) {
190 /* The adapter must match the one in I2cSerialBus() connector */
191 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
192 return -ENODEV;
193 } else {
194 struct acpi_device *adapter_adev;
195
196 /* The adapter must be present */
197 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
198 return -ENODEV;
199 if (acpi_bus_get_status(adapter_adev) ||
200 !adapter_adev->status.present)
201 return -ENODEV;
202 }
203
Jarkko Nikula5853b222016-08-12 17:02:53 +0300204 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300205 if (adapter_handle)
206 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200207
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300208 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300209 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300210 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
211 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300212 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300213
214 resource_list_for_each_entry(entry, &resource_list) {
215 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300216 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300217 break;
218 }
219 }
220
221 acpi_dev_free_resource_list(&resource_list);
222
Dan O'Donovan622b3072017-02-05 16:30:13 +0000223 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
224 sizeof(info->type));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300225
226 return 0;
227}
228
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300229static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300230 struct acpi_device *adev,
231 struct i2c_board_info *info)
232{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200233 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300234 acpi_device_set_enumerated(adev);
235
236 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200237 adev->power.flags.ignore_parent = false;
238 dev_err(&adapter->dev,
239 "failed to add I2C device %s from ACPI\n",
240 dev_name(&adev->dev));
241 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300242}
243
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300244static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300245 void *data, void **return_value)
246{
247 struct i2c_adapter *adapter = data;
248 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300249 struct i2c_board_info info;
250
251 if (acpi_bus_get_device(handle, &adev))
252 return AE_OK;
253
Mika Westerberg318ce272016-09-20 16:59:25 +0300254 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300255 return AE_OK;
256
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300257 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200258
259 return AE_OK;
260}
261
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300262#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300263
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200264/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300265 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200266 * @adap: pointer to adapter
267 *
268 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
269 * namespace. When a device is found it will be added to the Linux device
270 * model and bound to the corresponding ACPI handle.
271 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300272static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200273{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200274 acpi_status status;
275
Dustin Byford8eb5c872015-10-23 12:27:07 -0700276 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200277 return;
278
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300279 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300280 I2C_ACPI_MAX_SCAN_DEPTH,
281 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200282 adap, NULL);
283 if (ACPI_FAILURE(status))
284 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
285}
286
Jarkko Nikula5853b222016-08-12 17:02:53 +0300287static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
288 void *data, void **return_value)
289{
290 struct i2c_acpi_lookup *lookup = data;
291 struct acpi_device *adev;
292
293 if (acpi_bus_get_device(handle, &adev))
294 return AE_OK;
295
296 if (i2c_acpi_do_lookup(adev, lookup))
297 return AE_OK;
298
299 if (lookup->search_handle != lookup->adapter_handle)
300 return AE_OK;
301
302 if (lookup->speed <= lookup->min_speed)
303 lookup->min_speed = lookup->speed;
304
305 return AE_OK;
306}
307
308/**
309 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
310 * @dev: The device owning the bus
311 *
312 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
313 * devices connected to this bus and use the speed of slowest device.
314 *
315 * Returns the speed in Hz or zero
316 */
317u32 i2c_acpi_find_bus_speed(struct device *dev)
318{
319 struct i2c_acpi_lookup lookup;
320 struct i2c_board_info dummy;
321 acpi_status status;
322
323 if (!has_acpi_companion(dev))
324 return 0;
325
326 memset(&lookup, 0, sizeof(lookup));
327 lookup.search_handle = ACPI_HANDLE(dev);
328 lookup.min_speed = UINT_MAX;
329 lookup.info = &dummy;
330
331 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
332 I2C_ACPI_MAX_SCAN_DEPTH,
333 i2c_acpi_lookup_speed, NULL,
334 &lookup, NULL);
335
336 if (ACPI_FAILURE(status)) {
337 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
338 return 0;
339 }
340
341 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
342}
343EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
344
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300345static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300346{
347 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
348
349 if (!adapter)
350 return 0;
351
352 return ACPI_HANDLE(dev) == (acpi_handle)data;
353}
354
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300355static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300356{
357 return ACPI_COMPANION(dev) == data;
358}
359
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300360static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300361{
362 struct device *dev;
363
364 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300365 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300366 return dev ? i2c_verify_adapter(dev) : NULL;
367}
368
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300369static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300370{
371 struct device *dev;
372
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300373 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300374 return dev ? i2c_verify_client(dev) : NULL;
375}
376
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300377static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300378 void *arg)
379{
380 struct acpi_device *adev = arg;
381 struct i2c_board_info info;
382 acpi_handle adapter_handle;
383 struct i2c_adapter *adapter;
384 struct i2c_client *client;
385
386 switch (value) {
387 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300388 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300389 break;
390
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300391 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300392 if (!adapter)
393 break;
394
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300395 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300396 break;
397 case ACPI_RECONFIG_DEVICE_REMOVE:
398 if (!acpi_device_enumerated(adev))
399 break;
400
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300401 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300402 if (!client)
403 break;
404
405 i2c_unregister_device(client);
406 put_device(&client->dev);
407 break;
408 }
409
410 return NOTIFY_OK;
411}
412
413static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300414 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300415};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200416#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300417static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300418extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200419#endif /* CONFIG_ACPI */
420
421#ifdef CONFIG_ACPI_I2C_OPREGION
422static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
423 u8 cmd, u8 *data, u8 data_len)
424{
425
426 struct i2c_msg msgs[2];
427 int ret;
428 u8 *buffer;
429
430 buffer = kzalloc(data_len, GFP_KERNEL);
431 if (!buffer)
432 return AE_NO_MEMORY;
433
434 msgs[0].addr = client->addr;
435 msgs[0].flags = client->flags;
436 msgs[0].len = 1;
437 msgs[0].buf = &cmd;
438
439 msgs[1].addr = client->addr;
440 msgs[1].flags = client->flags | I2C_M_RD;
441 msgs[1].len = data_len;
442 msgs[1].buf = buffer;
443
444 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
445 if (ret < 0)
446 dev_err(&client->adapter->dev, "i2c read failed\n");
447 else
448 memcpy(data, buffer, data_len);
449
450 kfree(buffer);
451 return ret;
452}
453
454static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
455 u8 cmd, u8 *data, u8 data_len)
456{
457
458 struct i2c_msg msgs[1];
459 u8 *buffer;
460 int ret = AE_OK;
461
462 buffer = kzalloc(data_len + 1, GFP_KERNEL);
463 if (!buffer)
464 return AE_NO_MEMORY;
465
466 buffer[0] = cmd;
467 memcpy(buffer + 1, data, data_len);
468
469 msgs[0].addr = client->addr;
470 msgs[0].flags = client->flags;
471 msgs[0].len = data_len + 1;
472 msgs[0].buf = buffer;
473
474 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
475 if (ret < 0)
476 dev_err(&client->adapter->dev, "i2c write failed\n");
477
478 kfree(buffer);
479 return ret;
480}
481
482static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300483i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200484 u32 bits, u64 *value64,
485 void *handler_context, void *region_context)
486{
487 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300488 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200489 struct acpi_connection_info *info = &data->info;
490 struct acpi_resource_i2c_serialbus *sb;
491 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300492 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200493 struct acpi_resource *ares;
494 u32 accessor_type = function >> 16;
495 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100496 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200497 int status;
498
499 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
500 if (ACPI_FAILURE(ret))
501 return ret;
502
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300503 client = kzalloc(sizeof(*client), GFP_KERNEL);
504 if (!client) {
505 ret = AE_NO_MEMORY;
506 goto err;
507 }
508
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200509 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
510 ret = AE_BAD_PARAMETER;
511 goto err;
512 }
513
514 sb = &ares->data.i2c_serial_bus;
515 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
516 ret = AE_BAD_PARAMETER;
517 goto err;
518 }
519
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300520 client->adapter = adapter;
521 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200522
523 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300524 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200525
526 switch (accessor_type) {
527 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
528 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300529 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200530 if (status >= 0) {
531 gsb->bdata = status;
532 status = 0;
533 }
534 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300535 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200536 }
537 break;
538
539 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
540 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300541 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200542 if (status >= 0) {
543 gsb->bdata = status;
544 status = 0;
545 }
546 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300547 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200548 gsb->bdata);
549 }
550 break;
551
552 case ACPI_GSB_ACCESS_ATTRIB_WORD:
553 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300554 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200555 if (status >= 0) {
556 gsb->wdata = status;
557 status = 0;
558 }
559 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300560 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200561 gsb->wdata);
562 }
563 break;
564
565 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
566 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300567 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200568 gsb->data);
569 if (status >= 0) {
570 gsb->len = status;
571 status = 0;
572 }
573 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300574 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200575 gsb->len, gsb->data);
576 }
577 break;
578
579 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
580 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300581 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200582 gsb->data, info->access_length);
583 if (status > 0)
584 status = 0;
585 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300586 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200587 gsb->data, info->access_length);
588 }
589 break;
590
591 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900592 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
593 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200594 ret = AE_BAD_PARAMETER;
595 goto err;
596 }
597
598 gsb->status = status;
599
600 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300601 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200602 ACPI_FREE(ares);
603 return ret;
604}
605
606
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300607static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200608{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200609 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300610 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200611 acpi_status status;
612
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200613 if (!adapter->dev.parent)
614 return -ENODEV;
615
616 handle = ACPI_HANDLE(adapter->dev.parent);
617
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200618 if (!handle)
619 return -ENODEV;
620
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300621 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200622 GFP_KERNEL);
623 if (!data)
624 return -ENOMEM;
625
626 data->adapter = adapter;
627 status = acpi_bus_attach_private_data(handle, (void *)data);
628 if (ACPI_FAILURE(status)) {
629 kfree(data);
630 return -ENOMEM;
631 }
632
633 status = acpi_install_address_space_handler(handle,
634 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300635 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200636 NULL,
637 data);
638 if (ACPI_FAILURE(status)) {
639 dev_err(&adapter->dev, "Error installing i2c space handler\n");
640 acpi_bus_detach_private_data(handle);
641 kfree(data);
642 return -ENOMEM;
643 }
644
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800645 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200646 return 0;
647}
648
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300649static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200650{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200651 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300652 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200653 acpi_status status;
654
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200655 if (!adapter->dev.parent)
656 return;
657
658 handle = ACPI_HANDLE(adapter->dev.parent);
659
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200660 if (!handle)
661 return;
662
663 acpi_remove_address_space_handler(handle,
664 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300665 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200666
667 status = acpi_bus_get_private_data(handle, (void **)&data);
668 if (ACPI_SUCCESS(status))
669 kfree(data);
670
671 acpi_bus_detach_private_data(handle);
672}
673#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300674static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200675{ }
676
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300677static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200678{ return 0; }
679#endif /* CONFIG_ACPI_I2C_OPREGION */
680
David Brownellf37dd802007-02-13 22:09:00 +0100681/* ------------------------------------------------------------------------- */
682
Lee Jones5f441fc2016-11-07 12:47:40 +0000683const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +0200684 const struct i2c_client *client)
685{
Lee Jones811073b2016-11-07 12:47:36 +0000686 if (!(id && client))
687 return NULL;
688
Jean Delvared2653e92008-04-29 23:11:39 +0200689 while (id->name[0]) {
690 if (strcmp(client->name, id->name) == 0)
691 return id;
692 id++;
693 }
694 return NULL;
695}
Lee Jones5f441fc2016-11-07 12:47:40 +0000696EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static int i2c_device_match(struct device *dev, struct device_driver *drv)
699{
Jean Delvare51298d12009-09-18 22:45:45 +0200700 struct i2c_client *client = i2c_verify_client(dev);
701 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200702
Jean Delvare51298d12009-09-18 22:45:45 +0200703
Grant Likely959e85f2010-06-08 07:48:19 -0600704 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000705 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600706 return 1;
707
Mika Westerberg907ddf82012-11-23 12:23:40 +0100708 /* Then ACPI style match */
709 if (acpi_driver_match_device(dev, drv))
710 return 1;
711
Jean Delvare51298d12009-09-18 22:45:45 +0200712 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000713
714 /* Finally an I2C match */
715 if (i2c_match_id(driver->id_table, client))
716 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200717
Jean Delvareeb8a7902008-05-18 20:49:41 +0200718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Kay Sievers7eff2e72007-08-14 15:15:12 +0200721static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200722{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100723 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800724 int rc;
725
726 rc = acpi_device_uevent_modalias(dev, env);
727 if (rc != -ENODEV)
728 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200729
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100730 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200731}
732
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530733/* i2c bus recovery routines */
734static int get_scl_gpio_value(struct i2c_adapter *adap)
735{
736 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
737}
738
739static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
740{
741 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
742}
743
744static int get_sda_gpio_value(struct i2c_adapter *adap)
745{
746 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
747}
748
749static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
750{
751 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
752 struct device *dev = &adap->dev;
753 int ret = 0;
754
755 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
756 GPIOF_OUT_INIT_HIGH, "i2c-scl");
757 if (ret) {
758 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
759 return ret;
760 }
761
762 if (bri->get_sda) {
763 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
764 /* work without SDA polling */
765 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
766 bri->sda_gpio);
767 bri->get_sda = NULL;
768 }
769 }
770
771 return ret;
772}
773
774static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
775{
776 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
777
778 if (bri->get_sda)
779 gpio_free(bri->sda_gpio);
780
781 gpio_free(bri->scl_gpio);
782}
783
784/*
785 * We are generating clock pulses. ndelay() determines durating of clk pulses.
786 * We will generate clock with rate 100 KHz and so duration of both clock levels
787 * is: delay in ns = (10^6 / 100) / 2
788 */
789#define RECOVERY_NDELAY 5000
790#define RECOVERY_CLK_CNT 9
791
792static int i2c_generic_recovery(struct i2c_adapter *adap)
793{
794 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
795 int i = 0, val = 1, ret = 0;
796
797 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300798 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530799
Jan Luebbe8b062602015-07-08 16:35:06 +0200800 bri->set_scl(adap, val);
801 ndelay(RECOVERY_NDELAY);
802
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530803 /*
804 * By this time SCL is high, as we need to give 9 falling-rising edges
805 */
806 while (i++ < RECOVERY_CLK_CNT * 2) {
807 if (val) {
808 /* Break if SDA is high */
809 if (bri->get_sda && bri->get_sda(adap))
810 break;
811 /* SCL shouldn't be low here */
812 if (!bri->get_scl(adap)) {
813 dev_err(&adap->dev,
814 "SCL is stuck low, exit recovery\n");
815 ret = -EBUSY;
816 break;
817 }
818 }
819
820 val = !val;
821 bri->set_scl(adap, val);
822 ndelay(RECOVERY_NDELAY);
823 }
824
825 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300826 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530827
828 return ret;
829}
830
831int i2c_generic_scl_recovery(struct i2c_adapter *adap)
832{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530833 return i2c_generic_recovery(adap);
834}
Mark Brownc1c21f42015-04-15 19:18:39 +0100835EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530836
837int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
838{
839 int ret;
840
841 ret = i2c_get_gpios_for_recovery(adap);
842 if (ret)
843 return ret;
844
845 ret = i2c_generic_recovery(adap);
846 i2c_put_gpios_for_recovery(adap);
847
848 return ret;
849}
Mark Brownc1c21f42015-04-15 19:18:39 +0100850EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530851
852int i2c_recover_bus(struct i2c_adapter *adap)
853{
854 if (!adap->bus_recovery_info)
855 return -EOPNOTSUPP;
856
857 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
858 return adap->bus_recovery_info->recover_bus(adap);
859}
Mark Brownc1c21f42015-04-15 19:18:39 +0100860EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530861
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900862static void i2c_init_recovery(struct i2c_adapter *adap)
863{
864 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
865 char *err_str;
866
867 if (!bri)
868 return;
869
870 if (!bri->recover_bus) {
871 err_str = "no recover_bus() found";
872 goto err;
873 }
874
875 /* Generic GPIO recovery */
876 if (bri->recover_bus == i2c_generic_gpio_recovery) {
877 if (!gpio_is_valid(bri->scl_gpio)) {
878 err_str = "invalid SCL gpio";
879 goto err;
880 }
881
882 if (gpio_is_valid(bri->sda_gpio))
883 bri->get_sda = get_sda_gpio_value;
884 else
885 bri->get_sda = NULL;
886
887 bri->get_scl = get_scl_gpio_value;
888 bri->set_scl = set_scl_gpio_value;
889 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
890 /* Generic SCL recovery */
891 if (!bri->set_scl || !bri->get_scl) {
892 err_str = "no {get|set}_scl() found";
893 goto err;
894 }
895 }
896
897 return;
898 err:
899 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
900 adap->bus_recovery_info = NULL;
901}
902
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200903static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
904{
905 struct i2c_adapter *adap = client->adapter;
906 unsigned int irq;
907
908 if (!adap->host_notify_domain)
909 return -ENXIO;
910
911 if (client->flags & I2C_CLIENT_TEN)
912 return -EINVAL;
913
914 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
915 if (!irq)
916 irq = irq_create_mapping(adap->host_notify_domain,
917 client->addr);
918
919 return irq > 0 ? irq : -ENXIO;
920}
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static int i2c_device_probe(struct device *dev)
923{
Jean Delvare51298d12009-09-18 22:45:45 +0200924 struct i2c_client *client = i2c_verify_client(dev);
925 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100926 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200927
Jean Delvare51298d12009-09-18 22:45:45 +0200928 if (!client)
929 return 0;
930
Mika Westerberg845c8772015-05-06 13:29:08 +0300931 if (!client->irq) {
932 int irq = -ENOENT;
933
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800934 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
935 dev_dbg(dev, "Using Host Notify IRQ\n");
936 irq = i2c_smbus_host_notify_to_irq(client);
937 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700938 irq = of_irq_get_byname(dev->of_node, "irq");
939 if (irq == -EINVAL || irq == -ENODATA)
940 irq = of_irq_get(dev->of_node, 0);
941 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300942 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700943 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100944 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200945 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800946
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100947 if (irq < 0)
948 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200949
950 client->irq = irq;
951 }
952
Jean Delvare51298d12009-09-18 22:45:45 +0200953 driver = to_i2c_driver(dev->driver);
Lee Jonesda10c062016-11-07 12:47:39 +0000954
955 /*
956 * An I2C ID table is not mandatory, if and only if, a suitable Device
957 * Tree match table entry is supplied for the probing device.
958 */
959 if (!driver->id_table &&
960 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200961 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200962
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700963 if (client->flags & I2C_CLIENT_WAKE) {
964 int wakeirq = -ENOENT;
965
966 if (dev->of_node) {
967 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
968 if (wakeirq == -EPROBE_DEFER)
969 return wakeirq;
970 }
971
972 device_init_wakeup(&client->dev, true);
973
974 if (wakeirq > 0 && wakeirq != client->irq)
975 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
976 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200977 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700978 else
979 status = 0;
980
981 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300982 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700983 }
984
David Brownell7b4fbc52007-05-01 23:26:30 +0200985 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200986
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200987 status = of_clk_set_defaults(dev->of_node, false);
988 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700989 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200990
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200991 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100992 if (status == -EPROBE_DEFER)
993 goto err_clear_wakeup_irq;
994
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000995 /*
996 * When there are no more users of probe(),
997 * rename probe_new to probe.
998 */
999 if (driver->probe_new)
1000 status = driver->probe_new(client);
1001 else if (driver->probe)
1002 status = driver->probe(client,
1003 i2c_match_id(driver->id_table, client));
1004 else
1005 status = -EINVAL;
1006
Kieran Bingham74cedd32015-10-12 21:54:43 +01001007 if (status)
1008 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001009
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001010 return 0;
1011
1012err_detach_pm_domain:
1013 dev_pm_domain_detach(&client->dev, true);
1014err_clear_wakeup_irq:
1015 dev_pm_clear_wake_irq(&client->dev);
1016 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +01001017 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020static int i2c_device_remove(struct device *dev)
1021{
Jean Delvare51298d12009-09-18 22:45:45 +02001022 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +02001023 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001024 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +02001025
Jean Delvare51298d12009-09-18 22:45:45 +02001026 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +02001027 return 0;
1028
1029 driver = to_i2c_driver(dev->driver);
1030 if (driver->remove) {
1031 dev_dbg(dev, "remove\n");
1032 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +02001033 }
Wolfram Sang72fa8182014-01-21 17:48:34 +01001034
Ulf Hanssone09b0d42014-09-19 20:27:39 +02001035 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001036
1037 dev_pm_clear_wake_irq(&client->dev);
1038 device_init_wakeup(&client->dev, false);
1039
David Brownella1d9e6e2007-05-01 23:26:30 +02001040 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
David Brownellf37dd802007-02-13 22:09:00 +01001043static void i2c_device_shutdown(struct device *dev)
1044{
Jean Delvare51298d12009-09-18 22:45:45 +02001045 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001046 struct i2c_driver *driver;
1047
Jean Delvare51298d12009-09-18 22:45:45 +02001048 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001049 return;
1050 driver = to_i2c_driver(dev->driver);
1051 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001052 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001053}
1054
David Brownell9c1600e2007-05-01 23:26:31 +02001055static void i2c_client_dev_release(struct device *dev)
1056{
1057 kfree(to_i2c_client(dev));
1058}
1059
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001060static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001061show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001062{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001063 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1064 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001065}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001066static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001067
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001068static ssize_t
1069show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001070{
1071 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001072 int len;
1073
1074 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1075 if (len != -ENODEV)
1076 return len;
1077
Jean Delvareeb8a7902008-05-18 20:49:41 +02001078 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001079}
Jean Delvare51298d12009-09-18 22:45:45 +02001080static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1081
1082static struct attribute *i2c_dev_attrs[] = {
1083 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001084 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001085 &dev_attr_modalias.attr,
1086 NULL
1087};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001088ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001089
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001090struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001091 .name = "i2c",
1092 .match = i2c_device_match,
1093 .probe = i2c_device_probe,
1094 .remove = i2c_device_remove,
1095 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001096};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001097EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001098
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001099struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001100 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001101 .uevent = i2c_device_uevent,
1102 .release = i2c_client_dev_release,
1103};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001104EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +02001105
David Brownell9b766b82008-01-27 18:14:51 +01001106
1107/**
1108 * i2c_verify_client - return parameter as i2c_client, or NULL
1109 * @dev: device, probably from some driver model iterator
1110 *
1111 * When traversing the driver model tree, perhaps using driver model
1112 * iterators like @device_for_each_child(), you can't assume very much
1113 * about the nodes you find. Use this function to avoid oopses caused
1114 * by wrongly treating some non-I2C device as an i2c_client.
1115 */
1116struct i2c_client *i2c_verify_client(struct device *dev)
1117{
Jean Delvare51298d12009-09-18 22:45:45 +02001118 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001119 ? to_i2c_client(dev)
1120 : NULL;
1121}
1122EXPORT_SYMBOL(i2c_verify_client);
1123
1124
Wolfram Sangda899f52015-05-18 21:09:12 +02001125/* Return a unique address which takes the flags of the client into account */
1126static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1127{
1128 unsigned short addr = client->addr;
1129
1130 /* For some client flags, add an arbitrary offset to avoid collisions */
1131 if (client->flags & I2C_CLIENT_TEN)
1132 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1133
1134 if (client->flags & I2C_CLIENT_SLAVE)
1135 addr |= I2C_ADDR_OFFSET_SLAVE;
1136
1137 return addr;
1138}
1139
Jean Delvare3a89db52010-06-03 11:33:52 +02001140/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001141 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001142static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001143{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001144 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001145 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001146 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001147 return -EINVAL;
1148 } else {
1149 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001150 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001151 return -EINVAL;
1152 }
1153 return 0;
1154}
1155
Jean Delvare656b8762010-06-03 11:33:53 +02001156/* And this is a strict address validity check, used when probing. If a
1157 * device uses a reserved address, then it shouldn't be probed. 7-bit
1158 * addressing is assumed, 10-bit address devices are rare and should be
1159 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001160static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001161{
1162 /*
1163 * Reserved addresses per I2C specification:
1164 * 0x00 General call address / START byte
1165 * 0x01 CBUS address
1166 * 0x02 Reserved for different bus format
1167 * 0x03 Reserved for future purposes
1168 * 0x04-0x07 Hs-mode master code
1169 * 0x78-0x7b 10-bit slave addressing
1170 * 0x7c-0x7f Reserved for future purposes
1171 */
1172 if (addr < 0x08 || addr > 0x77)
1173 return -EINVAL;
1174 return 0;
1175}
1176
Jean Delvare3b5f7942010-06-03 11:33:55 +02001177static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1178{
1179 struct i2c_client *client = i2c_verify_client(dev);
1180 int addr = *(int *)addrp;
1181
Wolfram Sang9bccc702015-07-17 14:48:56 +02001182 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001183 return -EBUSY;
1184 return 0;
1185}
1186
Michael Lawnick08263742010-08-11 18:21:02 +02001187/* walk up mux tree */
1188static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1189{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001190 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001191 int result;
1192
1193 result = device_for_each_child(&adapter->dev, &addr,
1194 __i2c_check_addr_busy);
1195
Jean Delvare97cc4d42010-10-24 18:16:57 +02001196 if (!result && parent)
1197 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001198
1199 return result;
1200}
1201
1202/* recurse down mux tree */
1203static int i2c_check_mux_children(struct device *dev, void *addrp)
1204{
1205 int result;
1206
1207 if (dev->type == &i2c_adapter_type)
1208 result = device_for_each_child(dev, addrp,
1209 i2c_check_mux_children);
1210 else
1211 result = __i2c_check_addr_busy(dev, addrp);
1212
1213 return result;
1214}
1215
Jean Delvare3b5f7942010-06-03 11:33:55 +02001216static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1217{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001218 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001219 int result = 0;
1220
Jean Delvare97cc4d42010-10-24 18:16:57 +02001221 if (parent)
1222 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001223
1224 if (!result)
1225 result = device_for_each_child(&adapter->dev, &addr,
1226 i2c_check_mux_children);
1227
1228 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001229}
1230
David Brownell9c1600e2007-05-01 23:26:31 +02001231/**
Peter Rosin8320f492016-05-04 22:15:27 +02001232 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001233 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001234 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1235 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001236 */
Peter Rosin8320f492016-05-04 22:15:27 +02001237static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1238 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001239{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001240 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001241}
Jean Delvarefe61e072010-08-11 18:20:58 +02001242
1243/**
Peter Rosin8320f492016-05-04 22:15:27 +02001244 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001245 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001246 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1247 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001248 */
Peter Rosin8320f492016-05-04 22:15:27 +02001249static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1250 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001251{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001252 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001253}
1254
1255/**
Peter Rosin8320f492016-05-04 22:15:27 +02001256 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001257 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001258 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1259 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001260 */
Peter Rosin8320f492016-05-04 22:15:27 +02001261static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1262 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001263{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001264 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001265}
Jean Delvarefe61e072010-08-11 18:20:58 +02001266
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001267static void i2c_dev_set_name(struct i2c_adapter *adap,
1268 struct i2c_client *client)
1269{
1270 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1271
1272 if (adev) {
1273 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1274 return;
1275 }
1276
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001277 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001278 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001279}
1280
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001281static int i2c_dev_irq_from_resources(const struct resource *resources,
1282 unsigned int num_resources)
1283{
1284 struct irq_data *irqd;
1285 int i;
1286
1287 for (i = 0; i < num_resources; i++) {
1288 const struct resource *r = &resources[i];
1289
1290 if (resource_type(r) != IORESOURCE_IRQ)
1291 continue;
1292
1293 if (r->flags & IORESOURCE_BITS) {
1294 irqd = irq_get_irq_data(r->start);
1295 if (!irqd)
1296 break;
1297
1298 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
1299 }
1300
1301 return r->start;
1302 }
1303
1304 return 0;
1305}
1306
Jean Delvarefe61e072010-08-11 18:20:58 +02001307/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001308 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001309 * @adap: the adapter managing the device
1310 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001311 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001312 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001313 * Create an i2c device. Binding is handled through driver model
1314 * probe()/remove() methods. A driver may be bound to this device when we
1315 * return from this function, or any later moment (e.g. maybe hotplugging will
1316 * load the driver module). This call is not appropriate for use by mainboard
1317 * initialization logic, which usually runs during an arch_initcall() long
1318 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001319 *
1320 * This returns the new i2c client, which may be saved for later use with
1321 * i2c_unregister_device(); or NULL to indicate an error.
1322 */
1323struct i2c_client *
1324i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1325{
1326 struct i2c_client *client;
1327 int status;
1328
1329 client = kzalloc(sizeof *client, GFP_KERNEL);
1330 if (!client)
1331 return NULL;
1332
1333 client->adapter = adap;
1334
1335 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001336
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001337 if (info->archdata)
1338 client->dev.archdata = *info->archdata;
1339
Marc Pignatee354252008-08-28 08:33:22 +02001340 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001341 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001342
David Brownell9c1600e2007-05-01 23:26:31 +02001343 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001344 if (!client->irq)
1345 client->irq = i2c_dev_irq_from_resources(info->resources,
1346 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +02001347
David Brownell9c1600e2007-05-01 23:26:31 +02001348 strlcpy(client->name, info->type, sizeof(client->name));
1349
Wolfram Sangc4019b72015-07-17 12:50:06 +02001350 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001351 if (status) {
1352 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1353 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1354 goto out_err_silent;
1355 }
1356
Jean Delvaref8a227e2009-06-19 16:58:18 +02001357 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001358 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001359 if (status)
1360 goto out_err;
1361
1362 client->dev.parent = &client->adapter->dev;
1363 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001364 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001365 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001366 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001367
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001368 i2c_dev_set_name(adap, client);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001369
1370 if (info->properties) {
1371 status = device_add_properties(&client->dev, info->properties);
1372 if (status) {
1373 dev_err(&adap->dev,
1374 "Failed to add properties to client %s: %d\n",
1375 client->name, status);
1376 goto out_err;
1377 }
1378 }
1379
Jean Delvaref8a227e2009-06-19 16:58:18 +02001380 status = device_register(&client->dev);
1381 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001382 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001383
Jean Delvaref8a227e2009-06-19 16:58:18 +02001384 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1385 client->name, dev_name(&client->dev));
1386
David Brownell9c1600e2007-05-01 23:26:31 +02001387 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001388
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001389out_free_props:
1390 if (info->properties)
1391 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001392out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001393 dev_err(&adap->dev,
1394 "Failed to register i2c client %s at 0x%02x (%d)\n",
1395 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001396out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001397 kfree(client);
1398 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001399}
1400EXPORT_SYMBOL_GPL(i2c_new_device);
1401
1402
1403/**
1404 * i2c_unregister_device - reverse effect of i2c_new_device()
1405 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001406 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001407 */
1408void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001409{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001410 if (client->dev.of_node)
1411 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001412 if (ACPI_COMPANION(&client->dev))
1413 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001414 device_unregister(&client->dev);
1415}
David Brownell9c1600e2007-05-01 23:26:31 +02001416EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001417
1418
Jean Delvare60b129d2008-05-11 20:37:06 +02001419static const struct i2c_device_id dummy_id[] = {
1420 { "dummy", 0 },
1421 { },
1422};
1423
Jean Delvared2653e92008-04-29 23:11:39 +02001424static int dummy_probe(struct i2c_client *client,
1425 const struct i2c_device_id *id)
1426{
1427 return 0;
1428}
1429
1430static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001431{
1432 return 0;
1433}
1434
1435static struct i2c_driver dummy_driver = {
1436 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001437 .probe = dummy_probe,
1438 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001439 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001440};
1441
1442/**
1443 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1444 * @adapter: the adapter managing the device
1445 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001446 * Context: can sleep
1447 *
1448 * This returns an I2C client bound to the "dummy" driver, intended for use
1449 * with devices that consume multiple addresses. Examples of such chips
1450 * include various EEPROMS (like 24c04 and 24c08 models).
1451 *
1452 * These dummy devices have two main uses. First, most I2C and SMBus calls
1453 * except i2c_transfer() need a client handle; the dummy will be that handle.
1454 * And second, this prevents the specified address from being bound to a
1455 * different driver.
1456 *
1457 * This returns the new i2c client, which should be saved for later use with
1458 * i2c_unregister_device(); or NULL to indicate an error.
1459 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001460struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001461{
1462 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001463 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001464 };
1465
David Brownelle9f13732008-01-27 18:14:52 +01001466 return i2c_new_device(adapter, &info);
1467}
1468EXPORT_SYMBOL_GPL(i2c_new_dummy);
1469
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001470/**
1471 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1472 * and create the associated device
1473 * @client: Handle to the primary client
1474 * @name: Handle to specify which secondary address to get
1475 * @default_addr: Used as a fallback if no secondary address was specified
1476 * Context: can sleep
1477 *
1478 * I2C clients can be composed of multiple I2C slaves bound together in a single
1479 * component. The I2C client driver then binds to the master I2C slave and needs
1480 * to create I2C dummy clients to communicate with all the other slaves.
1481 *
1482 * This function creates and returns an I2C dummy client whose I2C address is
1483 * retrieved from the platform firmware based on the given slave name. If no
1484 * address is specified by the firmware default_addr is used.
1485 *
1486 * On DT-based platforms the address is retrieved from the "reg" property entry
1487 * cell whose "reg-names" value matches the slave name.
1488 *
1489 * This returns the new i2c client, which should be saved for later use with
1490 * i2c_unregister_device(); or NULL to indicate an error.
1491 */
1492struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1493 const char *name,
1494 u16 default_addr)
1495{
1496 struct device_node *np = client->dev.of_node;
1497 u32 addr = default_addr;
1498 int i;
1499
1500 if (np) {
1501 i = of_property_match_string(np, "reg-names", name);
1502 if (i >= 0)
1503 of_property_read_u32_index(np, "reg", i, &addr);
1504 }
1505
1506 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1507 return i2c_new_dummy(client->adapter, addr);
1508}
1509EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1510
David Brownellf37dd802007-02-13 22:09:00 +01001511/* ------------------------------------------------------------------------- */
1512
David Brownell16ffadf2007-05-01 23:26:28 +02001513/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1514
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001515static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
David Brownellef2c83212007-05-01 23:26:28 +02001517 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 complete(&adap->dev_released);
1519}
1520
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001521unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001522{
1523 unsigned int depth = 0;
1524
1525 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1526 depth++;
1527
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001528 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1529 "adapter depth exceeds lockdep subclass limit\n");
1530
Jean Delvare390946b2012-09-10 10:14:02 +02001531 return depth;
1532}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001533EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001534
1535/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001536 * Let users instantiate I2C devices through sysfs. This can be used when
1537 * platform initialization code doesn't contain the proper data for
1538 * whatever reason. Also useful for drivers that do device detection and
1539 * detection fails, either because the device uses an unexpected address,
1540 * or this is a compatible device with different ID register values.
1541 *
1542 * Parameter checking may look overzealous, but we really don't want
1543 * the user to provide incorrect parameters.
1544 */
1545static ssize_t
1546i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1547 const char *buf, size_t count)
1548{
1549 struct i2c_adapter *adap = to_i2c_adapter(dev);
1550 struct i2c_board_info info;
1551 struct i2c_client *client;
1552 char *blank, end;
1553 int res;
1554
Jean Delvare99cd8e22009-06-19 16:58:20 +02001555 memset(&info, 0, sizeof(struct i2c_board_info));
1556
1557 blank = strchr(buf, ' ');
1558 if (!blank) {
1559 dev_err(dev, "%s: Missing parameters\n", "new_device");
1560 return -EINVAL;
1561 }
1562 if (blank - buf > I2C_NAME_SIZE - 1) {
1563 dev_err(dev, "%s: Invalid device name\n", "new_device");
1564 return -EINVAL;
1565 }
1566 memcpy(info.type, buf, blank - buf);
1567
1568 /* Parse remaining parameters, reject extra parameters */
1569 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1570 if (res < 1) {
1571 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1572 return -EINVAL;
1573 }
1574 if (res > 1 && end != '\n') {
1575 dev_err(dev, "%s: Extra parameters\n", "new_device");
1576 return -EINVAL;
1577 }
1578
Wolfram Sangcfa03272015-07-27 14:03:38 +02001579 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1580 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1581 info.flags |= I2C_CLIENT_TEN;
1582 }
1583
1584 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1585 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1586 info.flags |= I2C_CLIENT_SLAVE;
1587 }
1588
Jean Delvare99cd8e22009-06-19 16:58:20 +02001589 client = i2c_new_device(adap, &info);
1590 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001591 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001592
1593 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001594 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001595 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001596 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001597 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1598 info.type, info.addr);
1599
1600 return count;
1601}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001602static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001603
1604/*
1605 * And of course let the users delete the devices they instantiated, if
1606 * they got it wrong. This interface can only be used to delete devices
1607 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1608 * don't delete devices to which some kernel code still has references.
1609 *
1610 * Parameter checking may look overzealous, but we really don't want
1611 * the user to delete the wrong device.
1612 */
1613static ssize_t
1614i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1615 const char *buf, size_t count)
1616{
1617 struct i2c_adapter *adap = to_i2c_adapter(dev);
1618 struct i2c_client *client, *next;
1619 unsigned short addr;
1620 char end;
1621 int res;
1622
1623 /* Parse parameters, reject extra parameters */
1624 res = sscanf(buf, "%hi%c", &addr, &end);
1625 if (res < 1) {
1626 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1627 return -EINVAL;
1628 }
1629 if (res > 1 && end != '\n') {
1630 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1631 return -EINVAL;
1632 }
1633
1634 /* Make sure the device was added through sysfs */
1635 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001636 mutex_lock_nested(&adap->userspace_clients_lock,
1637 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001638 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1639 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001640 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001641 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1642 "delete_device", client->name, client->addr);
1643
1644 list_del(&client->detected);
1645 i2c_unregister_device(client);
1646 res = count;
1647 break;
1648 }
1649 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001650 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001651
1652 if (res < 0)
1653 dev_err(dev, "%s: Can't find device in list\n",
1654 "delete_device");
1655 return res;
1656}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001657static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1658 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001659
1660static struct attribute *i2c_adapter_attrs[] = {
1661 &dev_attr_name.attr,
1662 &dev_attr_new_device.attr,
1663 &dev_attr_delete_device.attr,
1664 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001665};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001666ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001667
Michael Lawnick08263742010-08-11 18:21:02 +02001668struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001669 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001670 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001671};
Michael Lawnick08263742010-08-11 18:21:02 +02001672EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Stephen Warren643dd092012-04-17 12:43:33 -06001674/**
1675 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1676 * @dev: device, probably from some driver model iterator
1677 *
1678 * When traversing the driver model tree, perhaps using driver model
1679 * iterators like @device_for_each_child(), you can't assume very much
1680 * about the nodes you find. Use this function to avoid oopses caused
1681 * by wrongly treating some non-I2C device as an i2c_adapter.
1682 */
1683struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1684{
1685 return (dev->type == &i2c_adapter_type)
1686 ? to_i2c_adapter(dev)
1687 : NULL;
1688}
1689EXPORT_SYMBOL(i2c_verify_adapter);
1690
Jean Delvare2bb50952009-09-18 22:45:46 +02001691#ifdef CONFIG_I2C_COMPAT
1692static struct class_compat *i2c_adapter_compat_class;
1693#endif
1694
David Brownell9c1600e2007-05-01 23:26:31 +02001695static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1696{
1697 struct i2c_devinfo *devinfo;
1698
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001699 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001700 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1701 if (devinfo->busnum == adapter->nr
1702 && !i2c_new_device(adapter,
1703 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001704 dev_err(&adapter->dev,
1705 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001706 devinfo->board_info.addr);
1707 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001708 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001709}
1710
Wolfram Sang687b81d2013-07-11 12:56:15 +01001711/* OF support code */
1712
1713#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001714static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1715 struct device_node *node)
1716{
1717 struct i2c_client *result;
1718 struct i2c_board_info info = {};
1719 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001720 const __be32 *addr_be;
1721 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001722 int len;
1723
1724 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1725
1726 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1727 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1728 node->full_name);
1729 return ERR_PTR(-EINVAL);
1730 }
1731
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001732 addr_be = of_get_property(node, "reg", &len);
1733 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001734 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1735 node->full_name);
1736 return ERR_PTR(-EINVAL);
1737 }
1738
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001739 addr = be32_to_cpup(addr_be);
1740 if (addr & I2C_TEN_BIT_ADDRESS) {
1741 addr &= ~I2C_TEN_BIT_ADDRESS;
1742 info.flags |= I2C_CLIENT_TEN;
1743 }
1744
1745 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1746 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1747 info.flags |= I2C_CLIENT_SLAVE;
1748 }
1749
1750 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001751 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
John Garry6f724fb2017-01-06 19:02:57 +08001752 addr, node->full_name);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001753 return ERR_PTR(-EINVAL);
1754 }
1755
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001756 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001757 info.of_node = of_node_get(node);
1758 info.archdata = &dev_ad;
1759
Dmitry Torokhov331c3422017-01-04 20:57:22 -08001760 if (of_property_read_bool(node, "host-notify"))
1761 info.flags |= I2C_CLIENT_HOST_NOTIFY;
1762
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001763 if (of_get_property(node, "wakeup-source", NULL))
1764 info.flags |= I2C_CLIENT_WAKE;
1765
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001766 result = i2c_new_device(adap, &info);
1767 if (result == NULL) {
1768 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1769 node->full_name);
1770 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001771 return ERR_PTR(-EINVAL);
1772 }
1773 return result;
1774}
1775
Wolfram Sang687b81d2013-07-11 12:56:15 +01001776static void of_i2c_register_devices(struct i2c_adapter *adap)
1777{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001778 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001779 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001780
1781 /* Only register child devices if the adapter has a node pointer set */
1782 if (!adap->dev.of_node)
1783 return;
1784
1785 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1786
Jon Hunter7e4c2242016-06-29 10:17:53 +01001787 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1788 if (!bus)
1789 bus = of_node_get(adap->dev.of_node);
1790
1791 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001792 if (of_node_test_and_set_flag(node, OF_POPULATED))
1793 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001794
1795 client = of_i2c_register_device(adap, node);
1796 if (IS_ERR(client)) {
1797 dev_warn(&adap->dev,
1798 "Failed to create I2C device for %s\n",
1799 node->full_name);
1800 of_node_clear_flag(node, OF_POPULATED);
1801 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001802 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001803
1804 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001805}
1806
1807static int of_dev_node_match(struct device *dev, void *data)
1808{
1809 return dev->of_node == data;
1810}
1811
1812/* must call put_device() when done with returned i2c_client device */
1813struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1814{
1815 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001816 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001817
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001818 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001819 if (!dev)
1820 return NULL;
1821
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001822 client = i2c_verify_client(dev);
1823 if (!client)
1824 put_device(dev);
1825
1826 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001827}
1828EXPORT_SYMBOL(of_find_i2c_device_by_node);
1829
1830/* must call put_device() when done with returned i2c_adapter device */
1831struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1832{
1833 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001834 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001835
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001836 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001837 if (!dev)
1838 return NULL;
1839
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001840 adapter = i2c_verify_adapter(dev);
1841 if (!adapter)
1842 put_device(dev);
1843
1844 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001845}
1846EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001847
1848/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1849struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1850{
1851 struct i2c_adapter *adapter;
1852
1853 adapter = of_find_i2c_adapter_by_node(node);
1854 if (!adapter)
1855 return NULL;
1856
1857 if (!try_module_get(adapter->owner)) {
1858 put_device(&adapter->dev);
1859 adapter = NULL;
1860 }
1861
1862 return adapter;
1863}
1864EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Lee Jonescabcf4f2016-11-07 12:47:37 +00001865
1866static const struct of_device_id*
1867i2c_of_match_device_sysfs(const struct of_device_id *matches,
1868 struct i2c_client *client)
1869{
1870 const char *name;
1871
1872 for (; matches->compatible[0]; matches++) {
1873 /*
1874 * Adding devices through the i2c sysfs interface provides us
1875 * a string to match which may be compatible with the device
1876 * tree compatible strings, however with no actual of_node the
1877 * of_match_device() will not match
1878 */
1879 if (sysfs_streq(client->name, matches->compatible))
1880 return matches;
1881
1882 name = strchr(matches->compatible, ',');
1883 if (!name)
1884 name = matches->compatible;
1885 else
1886 name++;
1887
1888 if (sysfs_streq(client->name, name))
1889 return matches;
1890 }
1891
1892 return NULL;
1893}
1894
Lee Jones298d4de2016-11-07 12:47:38 +00001895const struct of_device_id
1896*i2c_of_match_device(const struct of_device_id *matches,
1897 struct i2c_client *client)
1898{
1899 const struct of_device_id *match;
1900
1901 if (!(client && matches))
1902 return NULL;
1903
1904 match = of_match_device(matches, &client->dev);
1905 if (match)
1906 return match;
1907
1908 return i2c_of_match_device_sysfs(matches, client);
1909}
1910EXPORT_SYMBOL_GPL(i2c_of_match_device);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001911#else
1912static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1913#endif /* CONFIG_OF */
1914
Jean Delvare69b00892009-12-06 17:06:27 +01001915static int i2c_do_add_adapter(struct i2c_driver *driver,
1916 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001917{
Jean Delvare4735c982008-07-14 22:38:36 +02001918 /* Detect supported devices on that bus, and instantiate them */
1919 i2c_detect(adap, driver);
1920
1921 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001922 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001923 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1924 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001925 dev_warn(&adap->dev,
1926 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001927 /* We ignore the return code; if it fails, too bad */
1928 driver->attach_adapter(adap);
1929 }
1930 return 0;
1931}
1932
Jean Delvare69b00892009-12-06 17:06:27 +01001933static int __process_new_adapter(struct device_driver *d, void *data)
1934{
1935 return i2c_do_add_adapter(to_i2c_driver(d), data);
1936}
1937
Peter Rosind1ed7982016-08-25 23:07:01 +02001938static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1939 .lock_bus = i2c_adapter_lock_bus,
1940 .trylock_bus = i2c_adapter_trylock_bus,
1941 .unlock_bus = i2c_adapter_unlock_bus,
1942};
1943
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001944static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1945{
1946 struct irq_domain *domain = adap->host_notify_domain;
1947 irq_hw_number_t hwirq;
1948
1949 if (!domain)
1950 return;
1951
1952 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1953 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1954
1955 irq_domain_remove(domain);
1956 adap->host_notify_domain = NULL;
1957}
1958
1959static int i2c_host_notify_irq_map(struct irq_domain *h,
1960 unsigned int virq,
1961 irq_hw_number_t hw_irq_num)
1962{
1963 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1964
1965 return 0;
1966}
1967
1968static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1969 .map = i2c_host_notify_irq_map,
1970};
1971
1972static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1973{
1974 struct irq_domain *domain;
1975
1976 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1977 return 0;
1978
1979 domain = irq_domain_create_linear(adap->dev.fwnode,
1980 I2C_ADDR_7BITS_COUNT,
1981 &i2c_host_notify_irq_ops, adap);
1982 if (!domain)
1983 return -ENOMEM;
1984
1985 adap->host_notify_domain = domain;
1986
1987 return 0;
1988}
1989
1990/**
1991 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1992 * I2C client.
1993 * @adap: the adapter
1994 * @addr: the I2C address of the notifying device
1995 * Context: can't sleep
1996 *
1997 * Helper function to be called from an I2C bus driver's interrupt
1998 * handler. It will schedule the Host Notify IRQ.
1999 */
2000int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
2001{
2002 int irq;
2003
2004 if (!adap)
2005 return -EINVAL;
2006
2007 irq = irq_find_mapping(adap->host_notify_domain, addr);
2008 if (irq <= 0)
2009 return -ENXIO;
2010
2011 generic_handle_irq(irq);
2012
2013 return 0;
2014}
2015EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
2016
David Brownell6e13e642007-05-01 23:26:31 +02002017static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002019 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
David Brownell1d0b19c2008-10-14 17:30:05 +02002021 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302022 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02002023 res = -EAGAIN;
2024 goto out_list;
2025 }
David Brownell1d0b19c2008-10-14 17:30:05 +02002026
Jean Delvare2236baa2010-11-15 22:40:38 +01002027 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002028 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002029 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002030
2031 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002032 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002033 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01002034 }
2035
Peter Rosind1ed7982016-08-25 23:07:01 +02002036 if (!adap->lock_ops)
2037 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02002038
Mika Kuoppala194684e2009-12-06 17:06:22 +01002039 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02002040 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02002041 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02002042 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Jean Delvare8fcfef62009-03-28 21:34:43 +01002044 /* Set default timeout to 1 second if not already set */
2045 if (adap->timeout == 0)
2046 adap->timeout = HZ;
2047
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02002048 /* register soft irqs for Host Notify */
2049 res = i2c_setup_host_notify_irq_domain(adap);
2050 if (res) {
2051 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
2052 adap->name, res);
2053 goto out_list;
2054 }
2055
Kay Sievers27d9c182009-01-07 14:29:16 +01002056 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02002057 adap->dev.bus = &i2c_bus_type;
2058 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02002059 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002060 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002061 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02002062 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02002065 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
2066
Charles Keepax6ada5c12015-04-16 13:05:19 +01002067 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02002068 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01002069 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01002070
Jean Delvare2bb50952009-09-18 22:45:46 +02002071#ifdef CONFIG_I2C_COMPAT
2072 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
2073 adap->dev.parent);
2074 if (res)
2075 dev_warn(&adap->dev,
2076 "Failed to create compatibility class link\n");
2077#endif
2078
Wolfram Sangd3b11d82016-07-09 13:34:59 +09002079 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05302080
Jean Delvare729d6dd2009-06-19 16:58:18 +02002081 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01002082 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002083 i2c_acpi_register_devices(adap);
2084 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01002085
David Brownell6e13e642007-05-01 23:26:31 +02002086 if (adap->nr < __i2c_first_dynamic_bus_num)
2087 i2c_scan_static_board_info(adap);
2088
Jean Delvare4735c982008-07-14 22:38:36 +02002089 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002090 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02002091 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01002092 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002093
2094 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02002095
Jean Delvareb119c6c2006-08-15 18:26:30 +02002096out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02002097 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02002098 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002099 mutex_unlock(&core_lock);
2100 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
David Brownell6e13e642007-05-01 23:26:31 +02002103/**
Doug Andersonee5c2742013-03-01 08:57:31 -08002104 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
2105 * @adap: the adapter to register (with adap->nr initialized)
2106 * Context: can sleep
2107 *
2108 * See i2c_add_numbered_adapter() for details.
2109 */
2110static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
2111{
Wolfram Sang84d0b612016-07-09 13:35:01 +09002112 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08002113
2114 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002115 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08002116 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002117 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08002118 return id == -ENOSPC ? -EBUSY : id;
2119
2120 return i2c_register_adapter(adap);
2121}
2122
2123/**
David Brownell6e13e642007-05-01 23:26:31 +02002124 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
2125 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02002126 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002127 *
2128 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08002129 * doesn't matter or when its bus number is specified by an dt alias.
2130 * Examples of bases when the bus number doesn't matter: I2C adapters
2131 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02002132 *
2133 * When this returns zero, a new bus number was allocated and stored
2134 * in adap->nr, and the specified adapter became available for clients.
2135 * Otherwise, a negative errno value is returned.
2136 */
2137int i2c_add_adapter(struct i2c_adapter *adapter)
2138{
Doug Andersonee5c2742013-03-01 08:57:31 -08002139 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08002140 int id;
David Brownell6e13e642007-05-01 23:26:31 +02002141
Doug Andersonee5c2742013-03-01 08:57:31 -08002142 if (dev->of_node) {
2143 id = of_alias_get_id(dev->of_node, "i2c");
2144 if (id >= 0) {
2145 adapter->nr = id;
2146 return __i2c_add_numbered_adapter(adapter);
2147 }
2148 }
2149
Jean Delvarecaada322008-01-27 18:14:49 +01002150 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08002151 id = idr_alloc(&i2c_adapter_idr, adapter,
2152 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01002153 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002154 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08002155 return id;
David Brownell6e13e642007-05-01 23:26:31 +02002156
2157 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08002158
David Brownell6e13e642007-05-01 23:26:31 +02002159 return i2c_register_adapter(adapter);
2160}
2161EXPORT_SYMBOL(i2c_add_adapter);
2162
2163/**
2164 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
2165 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02002166 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002167 *
2168 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01002169 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
2170 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02002171 * is used to properly configure I2C devices.
2172 *
Grant Likely488bf312011-07-25 17:49:43 +02002173 * If the requested bus number is set to -1, then this function will behave
2174 * identically to i2c_add_adapter, and will dynamically assign a bus number.
2175 *
David Brownell6e13e642007-05-01 23:26:31 +02002176 * If no devices have pre-been declared for this bus, then be sure to
2177 * register the adapter before any dynamically allocated ones. Otherwise
2178 * the required bus ID may not be available.
2179 *
2180 * When this returns zero, the specified adapter became available for
2181 * clients using the bus number provided in adap->nr. Also, the table
2182 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
2183 * and the appropriate driver model device nodes are created. Otherwise, a
2184 * negative errno value is returned.
2185 */
2186int i2c_add_numbered_adapter(struct i2c_adapter *adap)
2187{
Grant Likely488bf312011-07-25 17:49:43 +02002188 if (adap->nr == -1) /* -1 means dynamically assign bus id */
2189 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002190
Doug Andersonee5c2742013-03-01 08:57:31 -08002191 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002192}
2193EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2194
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002195static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002196 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002197{
Jean Delvare4735c982008-07-14 22:38:36 +02002198 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002199
Jean Delvareacec2112009-03-28 21:34:40 +01002200 /* Remove the devices we created ourselves as the result of hardware
2201 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002202 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2203 if (client->adapter == adapter) {
2204 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2205 client->name, client->addr);
2206 list_del(&client->detected);
2207 i2c_unregister_device(client);
2208 }
2209 }
Jean Delvare026526f2008-01-27 18:14:49 +01002210}
2211
Jean Delvaree549c2b2009-06-19 16:58:19 +02002212static int __unregister_client(struct device *dev, void *dummy)
2213{
2214 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002215 if (client && strcmp(client->name, "dummy"))
2216 i2c_unregister_device(client);
2217 return 0;
2218}
2219
2220static int __unregister_dummy(struct device *dev, void *dummy)
2221{
2222 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002223 if (client)
2224 i2c_unregister_device(client);
2225 return 0;
2226}
2227
Jean Delvare69b00892009-12-06 17:06:27 +01002228static int __process_removed_adapter(struct device_driver *d, void *data)
2229{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002230 i2c_do_del_adapter(to_i2c_driver(d), data);
2231 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002232}
2233
David Brownelld64f73b2007-07-12 14:12:28 +02002234/**
2235 * i2c_del_adapter - unregister I2C adapter
2236 * @adap: the adapter being unregistered
2237 * Context: can sleep
2238 *
2239 * This unregisters an I2C adapter which was previously registered
2240 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2241 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002242void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002244 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002245 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002248 mutex_lock(&core_lock);
2249 found = idr_find(&i2c_adapter_idr, adap->nr);
2250 mutex_unlock(&core_lock);
2251 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002252 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002253 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002256 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002257 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002258 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002259 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002260 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002261 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002263 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002264 mutex_lock_nested(&adap->userspace_clients_lock,
2265 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002266 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2267 detected) {
2268 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2269 client->addr);
2270 list_del(&client->detected);
2271 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002272 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002273 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002274
Jean Delvaree549c2b2009-06-19 16:58:19 +02002275 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002276 * check the returned value. This is a two-pass process, because
2277 * we can't remove the dummy devices during the first pass: they
2278 * could have been instantiated by real devices wishing to clean
2279 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002280 device_for_each_child(&adap->dev, NULL, __unregister_client);
2281 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Jean Delvare2bb50952009-09-18 22:45:46 +02002283#ifdef CONFIG_I2C_COMPAT
2284 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2285 adap->dev.parent);
2286#endif
2287
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002288 /* device name is gone after device_unregister */
2289 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2290
Wolfram Sang9f924162015-12-23 18:19:28 +01002291 pm_runtime_disable(&adap->dev);
2292
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02002293 i2c_host_notify_irq_teardown(adap);
2294
Wolfram Sang26680ee2015-01-29 22:45:09 +01002295 /* wait until all references to the device are gone
2296 *
2297 * FIXME: This is old code and should ideally be replaced by an
2298 * alternative which results in decoupling the lifetime of the struct
2299 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302300 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
David Brownell6e13e642007-05-01 23:26:31 +02002306 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002307 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002309 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002311 /* Clear the device structure in case this adapter is ever going to be
2312 added again */
2313 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
David Brownellc0564602007-05-01 23:26:31 +02002315EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Wolfram Sang54177cc2015-12-17 13:32:36 +01002317/**
2318 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2319 * @dev: The device to scan for I2C timing properties
2320 * @t: the i2c_timings struct to be filled with values
2321 * @use_defaults: bool to use sane defaults derived from the I2C specification
2322 * when properties are not found, otherwise use 0
2323 *
2324 * Scan the device for the generic I2C properties describing timing parameters
2325 * for the signal and fill the given struct with the results. If a property was
2326 * not found and use_defaults was true, then maximum timings are assumed which
2327 * are derived from the I2C specification. If use_defaults is not used, the
2328 * results will be 0, so drivers can apply their own defaults later. The latter
2329 * is mainly intended for avoiding regressions of existing drivers which want
2330 * to switch to this function. New drivers almost always should use the defaults.
2331 */
2332
2333void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2334{
2335 int ret;
2336
2337 memset(t, 0, sizeof(*t));
2338
2339 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2340 if (ret && use_defaults)
2341 t->bus_freq_hz = 100000;
2342
2343 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2344 if (ret && use_defaults) {
2345 if (t->bus_freq_hz <= 100000)
2346 t->scl_rise_ns = 1000;
2347 else if (t->bus_freq_hz <= 400000)
2348 t->scl_rise_ns = 300;
2349 else
2350 t->scl_rise_ns = 120;
2351 }
2352
2353 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2354 if (ret && use_defaults) {
2355 if (t->bus_freq_hz <= 400000)
2356 t->scl_fall_ns = 300;
2357 else
2358 t->scl_fall_ns = 120;
2359 }
2360
2361 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2362
2363 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2364 if (ret && use_defaults)
2365 t->sda_fall_ns = t->scl_fall_ns;
2366}
2367EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2368
David Brownell7b4fbc52007-05-01 23:26:30 +02002369/* ------------------------------------------------------------------------- */
2370
Jean Delvare7ae31482011-03-20 14:50:52 +01002371int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2372{
2373 int res;
2374
2375 mutex_lock(&core_lock);
2376 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2377 mutex_unlock(&core_lock);
2378
2379 return res;
2380}
2381EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2382
Jean Delvare69b00892009-12-06 17:06:27 +01002383static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002384{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002385 if (dev->type != &i2c_adapter_type)
2386 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002387 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002388}
2389
David Brownell7b4fbc52007-05-01 23:26:30 +02002390/*
2391 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002392 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 */
2394
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002395int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002397 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
David Brownell1d0b19c2008-10-14 17:30:05 +02002399 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302400 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002401 return -EAGAIN;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002404 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002406 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Jean Delvare729d6dd2009-06-19 16:58:18 +02002408 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002409 * will have called probe() for all matching-but-unbound devices.
2410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 res = driver_register(&driver->driver);
2412 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002413 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002414
Wolfram Sang44239a52016-07-09 13:35:04 +09002415 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Jean Delvare4735c982008-07-14 22:38:36 +02002417 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002418 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002419
Jean Delvare7eebcb72006-02-05 23:28:21 +01002420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002422EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Jean Delvare69b00892009-12-06 17:06:27 +01002424static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002425{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002426 if (dev->type == &i2c_adapter_type)
2427 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2428 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002429}
2430
David Brownella1d9e6e2007-05-01 23:26:30 +02002431/**
2432 * i2c_del_driver - unregister I2C driver
2433 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002434 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002435 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002436void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Jean Delvare7ae31482011-03-20 14:50:52 +01002438 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002441 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
David Brownellc0564602007-05-01 23:26:31 +02002443EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
David Brownell7b4fbc52007-05-01 23:26:30 +02002445/* ------------------------------------------------------------------------- */
2446
Jean Delvaree48d3312008-01-27 18:14:48 +01002447/**
2448 * i2c_use_client - increments the reference count of the i2c client structure
2449 * @client: the client being referenced
2450 *
2451 * Each live reference to a client should be refcounted. The driver model does
2452 * that automatically as part of driver binding, so that most drivers don't
2453 * need to do this explicitly: they hold a reference until they're unbound
2454 * from the device.
2455 *
2456 * A pointer to the client with the incremented reference counter is returned.
2457 */
2458struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
David Brownell6ea438e2008-07-14 22:38:24 +02002460 if (client && get_device(&client->dev))
2461 return client;
2462 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
David Brownellc0564602007-05-01 23:26:31 +02002464EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Jean Delvaree48d3312008-01-27 18:14:48 +01002466/**
2467 * i2c_release_client - release a use of the i2c client structure
2468 * @client: the client being no longer referenced
2469 *
2470 * Must be called when a user of a client is finished with it.
2471 */
2472void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
David Brownell6ea438e2008-07-14 22:38:24 +02002474 if (client)
2475 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
David Brownellc0564602007-05-01 23:26:31 +02002477EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
David Brownell9b766b82008-01-27 18:14:51 +01002479struct i2c_cmd_arg {
2480 unsigned cmd;
2481 void *arg;
2482};
2483
2484static int i2c_cmd(struct device *dev, void *_arg)
2485{
2486 struct i2c_client *client = i2c_verify_client(dev);
2487 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002488 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002489
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002490 if (!client || !client->dev.driver)
2491 return 0;
2492
2493 driver = to_i2c_driver(client->dev.driver);
2494 if (driver->command)
2495 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002496 return 0;
2497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2500{
David Brownell9b766b82008-01-27 18:14:51 +01002501 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
David Brownell9b766b82008-01-27 18:14:51 +01002503 cmd_arg.cmd = cmd;
2504 cmd_arg.arg = arg;
2505 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
David Brownellc0564602007-05-01 23:26:31 +02002507EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002509#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2510static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2511 void *arg)
2512{
2513 struct of_reconfig_data *rd = arg;
2514 struct i2c_adapter *adap;
2515 struct i2c_client *client;
2516
2517 switch (of_reconfig_get_state_change(action, rd)) {
2518 case OF_RECONFIG_CHANGE_ADD:
2519 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2520 if (adap == NULL)
2521 return NOTIFY_OK; /* not for us */
2522
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002523 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2524 put_device(&adap->dev);
2525 return NOTIFY_OK;
2526 }
2527
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002528 client = of_i2c_register_device(adap, rd->dn);
2529 put_device(&adap->dev);
2530
2531 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002532 dev_err(&adap->dev, "failed to create client for '%s'\n",
2533 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002534 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002535 return notifier_from_errno(PTR_ERR(client));
2536 }
2537 break;
2538 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002539 /* already depopulated? */
2540 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2541 return NOTIFY_OK;
2542
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002543 /* find our device by node */
2544 client = of_find_i2c_device_by_node(rd->dn);
2545 if (client == NULL)
2546 return NOTIFY_OK; /* no? not meant for us */
2547
2548 /* unregister takes one ref away */
2549 i2c_unregister_device(client);
2550
2551 /* and put the reference of the find */
2552 put_device(&client->dev);
2553 break;
2554 }
2555
2556 return NOTIFY_OK;
2557}
2558static struct notifier_block i2c_of_notifier = {
2559 .notifier_call = of_i2c_notify,
2560};
2561#else
2562extern struct notifier_block i2c_of_notifier;
2563#endif /* CONFIG_OF_DYNAMIC */
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565static int __init i2c_init(void)
2566{
2567 int retval;
2568
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002569 retval = of_alias_get_highest_id("i2c");
2570
2571 down_write(&__i2c_board_lock);
2572 if (retval >= __i2c_first_dynamic_bus_num)
2573 __i2c_first_dynamic_bus_num = retval + 1;
2574 up_write(&__i2c_board_lock);
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 retval = bus_register(&i2c_bus_type);
2577 if (retval)
2578 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002579
2580 is_registered = true;
2581
Jean Delvare2bb50952009-09-18 22:45:46 +02002582#ifdef CONFIG_I2C_COMPAT
2583 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2584 if (!i2c_adapter_compat_class) {
2585 retval = -ENOMEM;
2586 goto bus_err;
2587 }
2588#endif
David Brownelle9f13732008-01-27 18:14:52 +01002589 retval = i2c_add_driver(&dummy_driver);
2590 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002591 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002592
2593 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2594 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002595 if (IS_ENABLED(CONFIG_ACPI))
2596 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002597
David Brownelle9f13732008-01-27 18:14:52 +01002598 return 0;
2599
Jean Delvare2bb50952009-09-18 22:45:46 +02002600class_err:
2601#ifdef CONFIG_I2C_COMPAT
2602 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002603bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002604#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002605 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002606 bus_unregister(&i2c_bus_type);
2607 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
2610static void __exit i2c_exit(void)
2611{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002612 if (IS_ENABLED(CONFIG_ACPI))
2613 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002614 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2615 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002616 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002617#ifdef CONFIG_I2C_COMPAT
2618 class_compat_unregister(i2c_adapter_compat_class);
2619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002621 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
David Brownella10f9e72008-10-14 17:30:06 +02002624/* We must initialize early, because some subsystems register i2c drivers
2625 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2626 */
2627postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628module_exit(i2c_exit);
2629
2630/* ----------------------------------------------------
2631 * the functional interface to the i2c busses.
2632 * ----------------------------------------------------
2633 */
2634
Wolfram Sangb7f62582015-01-05 23:45:59 +01002635/* Check if val is exceeding the quirk IFF quirk is non 0 */
2636#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2637
2638static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2639{
2640 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2641 err_msg, msg->addr, msg->len,
2642 msg->flags & I2C_M_RD ? "read" : "write");
2643 return -EOPNOTSUPP;
2644}
2645
2646static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2647{
2648 const struct i2c_adapter_quirks *q = adap->quirks;
2649 int max_num = q->max_num_msgs, i;
2650 bool do_len_check = true;
2651
2652 if (q->flags & I2C_AQ_COMB) {
2653 max_num = 2;
2654
2655 /* special checks for combined messages */
2656 if (num == 2) {
2657 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2658 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2659
2660 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2661 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2662
2663 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2664 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2665
2666 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2667 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2668
2669 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2670 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2671
2672 do_len_check = false;
2673 }
2674 }
2675
2676 if (i2c_quirk_exceeded(num, max_num))
2677 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2678
2679 for (i = 0; i < num; i++) {
2680 u16 len = msgs[i].len;
2681
2682 if (msgs[i].flags & I2C_M_RD) {
2683 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2684 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2685 } else {
2686 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2687 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2688 }
2689 }
2690
2691 return 0;
2692}
2693
David Brownella1cdeda2008-07-14 22:38:24 +02002694/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002695 * __i2c_transfer - unlocked flavor of i2c_transfer
2696 * @adap: Handle to I2C bus
2697 * @msgs: One or more messages to execute before STOP is issued to
2698 * terminate the operation; each message begins with a START.
2699 * @num: Number of messages to be executed.
2700 *
2701 * Returns negative errno, else the number of messages executed.
2702 *
2703 * Adapter lock must be held when calling this function. No debug logging
2704 * takes place. adap->algo->master_xfer existence isn't checked.
2705 */
2706int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2707{
2708 unsigned long orig_jiffies;
2709 int ret, try;
2710
Wolfram Sangb7f62582015-01-05 23:45:59 +01002711 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2712 return -EOPNOTSUPP;
2713
David Howellsd9a83d62014-03-06 13:35:59 +00002714 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2715 * enabled. This is an efficient way of keeping the for-loop from
2716 * being executed when not needed.
2717 */
2718 if (static_key_false(&i2c_trace_msg)) {
2719 int i;
2720 for (i = 0; i < num; i++)
2721 if (msgs[i].flags & I2C_M_RD)
2722 trace_i2c_read(adap, &msgs[i], i);
2723 else
2724 trace_i2c_write(adap, &msgs[i], i);
2725 }
2726
Jean Delvareb37d2a32012-06-29 07:47:19 -03002727 /* Retry automatically on arbitration loss */
2728 orig_jiffies = jiffies;
2729 for (ret = 0, try = 0; try <= adap->retries; try++) {
2730 ret = adap->algo->master_xfer(adap, msgs, num);
2731 if (ret != -EAGAIN)
2732 break;
2733 if (time_after(jiffies, orig_jiffies + adap->timeout))
2734 break;
2735 }
2736
David Howellsd9a83d62014-03-06 13:35:59 +00002737 if (static_key_false(&i2c_trace_msg)) {
2738 int i;
2739 for (i = 0; i < ret; i++)
2740 if (msgs[i].flags & I2C_M_RD)
2741 trace_i2c_reply(adap, &msgs[i], i);
2742 trace_i2c_result(adap, i, ret);
2743 }
2744
Jean Delvareb37d2a32012-06-29 07:47:19 -03002745 return ret;
2746}
2747EXPORT_SYMBOL(__i2c_transfer);
2748
2749/**
David Brownella1cdeda2008-07-14 22:38:24 +02002750 * i2c_transfer - execute a single or combined I2C message
2751 * @adap: Handle to I2C bus
2752 * @msgs: One or more messages to execute before STOP is issued to
2753 * terminate the operation; each message begins with a START.
2754 * @num: Number of messages to be executed.
2755 *
2756 * Returns negative errno, else the number of messages executed.
2757 *
2758 * Note that there is no requirement that each message be sent to
2759 * the same slave address, although that is the most common model.
2760 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002761int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002763 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
David Brownella1cdeda2008-07-14 22:38:24 +02002765 /* REVISIT the fault reporting model here is weak:
2766 *
2767 * - When we get an error after receiving N bytes from a slave,
2768 * there is no way to report "N".
2769 *
2770 * - When we get a NAK after transmitting N bytes to a slave,
2771 * there is no way to report "N" ... or to let the master
2772 * continue executing the rest of this combined message, if
2773 * that's the appropriate response.
2774 *
2775 * - When for example "num" is two and we successfully complete
2776 * the first message but get an error part way through the
2777 * second, it's unclear whether that should be reported as
2778 * one (discarding status on the second message) or errno
2779 * (discarding status on the first one).
2780 */
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (adap->algo->master_xfer) {
2783#ifdef DEBUG
2784 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002785 dev_dbg(&adap->dev,
2786 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2787 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2788 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002789 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791#endif
2792
Mike Rapoportcea443a82008-01-27 18:14:50 +01002793 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002794 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002795 if (!ret)
2796 /* I2C activity is ongoing. */
2797 return -EAGAIN;
2798 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002799 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002800 }
2801
Jean Delvareb37d2a32012-06-29 07:47:19 -03002802 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002803 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 return ret;
2806 } else {
2807 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002808 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810}
David Brownellc0564602007-05-01 23:26:31 +02002811EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
David Brownella1cdeda2008-07-14 22:38:24 +02002813/**
2814 * i2c_master_send - issue a single I2C message in master transmit mode
2815 * @client: Handle to slave device
2816 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002817 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002818 *
2819 * Returns negative errno, or else the number of bytes written.
2820 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002821int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
2823 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002824 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct i2c_msg msg;
2826
Jean Delvare815f55f2005-05-07 22:58:46 +02002827 msg.addr = client->addr;
2828 msg.flags = client->flags & I2C_M_TEN;
2829 msg.len = count;
2830 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002831
Jean Delvare815f55f2005-05-07 22:58:46 +02002832 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002834 /*
2835 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2836 * transmitted, else error code.
2837 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002838 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
David Brownellc0564602007-05-01 23:26:31 +02002840EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
David Brownella1cdeda2008-07-14 22:38:24 +02002842/**
2843 * i2c_master_recv - issue a single I2C message in master receive mode
2844 * @client: Handle to slave device
2845 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002846 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002847 *
2848 * Returns negative errno, or else the number of bytes read.
2849 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002850int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Farid Hammane7225acf2010-05-21 18:40:58 +02002852 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct i2c_msg msg;
2854 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Jean Delvare815f55f2005-05-07 22:58:46 +02002856 msg.addr = client->addr;
2857 msg.flags = client->flags & I2C_M_TEN;
2858 msg.flags |= I2C_M_RD;
2859 msg.len = count;
2860 msg.buf = buf;
2861
2862 ret = i2c_transfer(adap, &msg, 1);
2863
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002864 /*
2865 * If everything went ok (i.e. 1 msg received), return #bytes received,
2866 * else error code.
2867 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002868 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
David Brownellc0564602007-05-01 23:26:31 +02002870EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872/* ----------------------------------------------------
2873 * the i2c address scanning function
2874 * Will not work for 10-bit addresses!
2875 * ----------------------------------------------------
2876 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002877
Jean Delvare63e4e802010-06-03 11:33:51 +02002878/*
2879 * Legacy default probe function, mostly relevant for SMBus. The default
2880 * probe method is a quick write, but it is known to corrupt the 24RF08
2881 * EEPROMs due to a state machine bug, and could also irreversibly
2882 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2883 * we use a short byte read instead. Also, some bus drivers don't implement
2884 * quick write, so we fallback to a byte read in that case too.
2885 * On x86, there is another special case for FSC hardware monitoring chips,
2886 * which want regular byte reads (address 0x73.) Fortunately, these are the
2887 * only known chips using this I2C address on PC hardware.
2888 * Returns 1 if probe succeeded, 0 if not.
2889 */
2890static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2891{
2892 int err;
2893 union i2c_smbus_data dummy;
2894
2895#ifdef CONFIG_X86
2896 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2897 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2898 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2899 I2C_SMBUS_BYTE_DATA, &dummy);
2900 else
2901#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002902 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2903 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002904 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2905 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002906 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2907 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2908 I2C_SMBUS_BYTE, &dummy);
2909 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002910 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2911 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002912 err = -EOPNOTSUPP;
2913 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002914
2915 return err >= 0;
2916}
2917
Jean Delvareccfbbd02009-12-06 17:06:25 +01002918static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002919 struct i2c_driver *driver)
2920{
2921 struct i2c_board_info info;
2922 struct i2c_adapter *adapter = temp_client->adapter;
2923 int addr = temp_client->addr;
2924 int err;
2925
2926 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002927 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002928 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002929 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2930 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002931 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002932 }
2933
Wolfram Sang9bccc702015-07-17 14:48:56 +02002934 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002935 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002936 return 0;
2937
Jean Delvareccfbbd02009-12-06 17:06:25 +01002938 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002939 if (!i2c_default_probe(adapter, addr))
2940 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002941
2942 /* Finally call the custom detection function */
2943 memset(&info, 0, sizeof(struct i2c_board_info));
2944 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002945 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002946 if (err) {
2947 /* -ENODEV is returned if the detection fails. We catch it
2948 here as this isn't an error. */
2949 return err == -ENODEV ? 0 : err;
2950 }
2951
2952 /* Consistency check */
2953 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002954 dev_err(&adapter->dev,
2955 "%s detection function provided no name for 0x%x\n",
2956 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002957 } else {
2958 struct i2c_client *client;
2959
2960 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002961 if (adapter->class & I2C_CLASS_DEPRECATED)
2962 dev_warn(&adapter->dev,
2963 "This adapter will soon drop class based instantiation of devices. "
2964 "Please make sure client 0x%02x gets instantiated by other means. "
2965 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2966 info.addr);
2967
Jean Delvare4735c982008-07-14 22:38:36 +02002968 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2969 info.type, info.addr);
2970 client = i2c_new_device(adapter, &info);
2971 if (client)
2972 list_add_tail(&client->detected, &driver->clients);
2973 else
2974 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2975 info.type, info.addr);
2976 }
2977 return 0;
2978}
2979
2980static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2981{
Jean Delvarec3813d62009-12-14 21:17:25 +01002982 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002983 struct i2c_client *temp_client;
2984 int i, err = 0;
2985 int adap_id = i2c_adapter_id(adapter);
2986
Jean Delvarec3813d62009-12-14 21:17:25 +01002987 address_list = driver->address_list;
2988 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002989 return 0;
2990
Wolfram Sang45552272014-07-10 13:46:21 +02002991 /* Warn that the adapter lost class based instantiation */
2992 if (adapter->class == I2C_CLASS_DEPRECATED) {
2993 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002994 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2995 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002996 driver->driver.name);
2997 return 0;
2998 }
2999
Jean Delvare51b54ba2010-10-24 18:16:58 +02003000 /* Stop here if the classes do not match */
3001 if (!(adapter->class & driver->class))
3002 return 0;
3003
Jean Delvare4735c982008-07-14 22:38:36 +02003004 /* Set up a temporary client to help detect callback */
3005 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
3006 if (!temp_client)
3007 return -ENOMEM;
3008 temp_client->adapter = adapter;
3009
Jean Delvarec3813d62009-12-14 21:17:25 +01003010 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003011 dev_dbg(&adapter->dev,
3012 "found normal entry for adapter %d, addr 0x%02x\n",
3013 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01003014 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01003015 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02003016 if (unlikely(err))
3017 break;
Jean Delvare4735c982008-07-14 22:38:36 +02003018 }
3019
Jean Delvare4735c982008-07-14 22:38:36 +02003020 kfree(temp_client);
3021 return err;
3022}
3023
Jean Delvared44f19d2010-08-11 18:20:57 +02003024int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
3025{
3026 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
3027 I2C_SMBUS_QUICK, NULL) >= 0;
3028}
3029EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
3030
Jean Delvare12b5053a2007-05-01 23:26:31 +02003031struct i2c_client *
3032i2c_new_probed_device(struct i2c_adapter *adap,
3033 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02003034 unsigned short const *addr_list,
3035 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02003036{
3037 int i;
3038
Jean Delvare8031d792010-08-11 18:21:00 +02003039 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02003040 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02003041
Jean Delvare12b5053a2007-05-01 23:26:31 +02003042 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
3043 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02003044 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003045 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
3046 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02003047 continue;
3048 }
3049
Wolfram Sang9bccc702015-07-17 14:48:56 +02003050 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02003051 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003052 dev_dbg(&adap->dev,
3053 "Address 0x%02x already in use, not probing\n",
3054 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02003055 continue;
3056 }
3057
Jean Delvare63e4e802010-06-03 11:33:51 +02003058 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02003059 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02003060 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02003061 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02003062
3063 if (addr_list[i] == I2C_CLIENT_END) {
3064 dev_dbg(&adap->dev, "Probing failed, no device found\n");
3065 return NULL;
3066 }
3067
3068 info->addr = addr_list[i];
3069 return i2c_new_device(adap, info);
3070}
3071EXPORT_SYMBOL_GPL(i2c_new_probed_device);
3072
Jean Delvared735b342011-03-20 14:50:52 +01003073struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01003076
Jean Delvarecaada322008-01-27 18:14:49 +01003077 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01003078 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003079 if (!adapter)
3080 goto exit;
3081
3082 if (try_module_get(adapter->owner))
3083 get_device(&adapter->dev);
3084 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04003085 adapter = NULL;
3086
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003087 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01003088 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04003089 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
David Brownellc0564602007-05-01 23:26:31 +02003091EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093void i2c_put_adapter(struct i2c_adapter *adap)
3094{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003095 if (!adap)
3096 return;
3097
3098 put_device(&adap->dev);
3099 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
David Brownellc0564602007-05-01 23:26:31 +02003101EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
3103/* The SMBus parts */
3104
David Brownell438d6c22006-12-10 21:21:31 +01003105#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003106static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 int i;
David Brownell438d6c22006-12-10 21:21:31 +01003109
Farid Hammane7225acf2010-05-21 18:40:58 +02003110 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01003111 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 data = data ^ POLY;
3113 data = data << 1;
3114 }
3115 return (u8)(data >> 8);
3116}
3117
Jean Delvare421ef472005-10-26 21:28:55 +02003118/* Incremental CRC8 over count bytes in the array pointed to by p */
3119static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
3121 int i;
3122
Farid Hammane7225acf2010-05-21 18:40:58 +02003123 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02003124 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return crc;
3126}
3127
Jean Delvare421ef472005-10-26 21:28:55 +02003128/* Assume a 7-bit address, which is reasonable for SMBus */
3129static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Jean Delvare421ef472005-10-26 21:28:55 +02003131 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02003132 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02003133 pec = i2c_smbus_pec(pec, &addr, 1);
3134
3135 /* The data buffer follows */
3136 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
Jean Delvare421ef472005-10-26 21:28:55 +02003139/* Used for write only transactions */
3140static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
Jean Delvare421ef472005-10-26 21:28:55 +02003142 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
3143 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Jean Delvare421ef472005-10-26 21:28:55 +02003146/* Return <0 on CRC error
3147 If there was a write before this read (most cases) we need to take the
3148 partial CRC from the write part into account.
3149 Note that this function does modify the message (we need to decrease the
3150 message length to hide the CRC byte from the caller). */
3151static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Jean Delvare421ef472005-10-26 21:28:55 +02003153 u8 rpec = msg->buf[--msg->len];
3154 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09003157 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02003159 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
David Brownell438d6c22006-12-10 21:21:31 +01003161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
David Brownella1cdeda2008-07-14 22:38:24 +02003164/**
3165 * i2c_smbus_read_byte - SMBus "receive byte" protocol
3166 * @client: Handle to slave device
3167 *
3168 * This executes the SMBus "receive byte" protocol, returning negative errno
3169 * else the byte received from the device.
3170 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003171s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003174 int status;
3175
3176 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3177 I2C_SMBUS_READ, 0,
3178 I2C_SMBUS_BYTE, &data);
3179 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
David Brownellc0564602007-05-01 23:26:31 +02003181EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
David Brownella1cdeda2008-07-14 22:38:24 +02003183/**
3184 * i2c_smbus_write_byte - SMBus "send byte" protocol
3185 * @client: Handle to slave device
3186 * @value: Byte to be sent
3187 *
3188 * This executes the SMBus "send byte" protocol, returning negative errno
3189 * else zero on success.
3190 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003191s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Farid Hammane7225acf2010-05-21 18:40:58 +02003193 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02003194 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
David Brownellc0564602007-05-01 23:26:31 +02003196EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
David Brownella1cdeda2008-07-14 22:38:24 +02003198/**
3199 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
3200 * @client: Handle to slave device
3201 * @command: Byte interpreted by slave
3202 *
3203 * This executes the SMBus "read byte" protocol, returning negative errno
3204 * else a data byte received from the device.
3205 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003206s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
3208 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003209 int status;
3210
3211 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3212 I2C_SMBUS_READ, command,
3213 I2C_SMBUS_BYTE_DATA, &data);
3214 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
David Brownellc0564602007-05-01 23:26:31 +02003216EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
David Brownella1cdeda2008-07-14 22:38:24 +02003218/**
3219 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
3220 * @client: Handle to slave device
3221 * @command: Byte interpreted by slave
3222 * @value: Byte being written
3223 *
3224 * This executes the SMBus "write byte" protocol, returning negative errno
3225 * else zero on success.
3226 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003227s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3228 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 union i2c_smbus_data data;
3231 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003232 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3233 I2C_SMBUS_WRITE, command,
3234 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
David Brownellc0564602007-05-01 23:26:31 +02003236EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
David Brownella1cdeda2008-07-14 22:38:24 +02003238/**
3239 * i2c_smbus_read_word_data - SMBus "read word" protocol
3240 * @client: Handle to slave device
3241 * @command: Byte interpreted by slave
3242 *
3243 * This executes the SMBus "read word" protocol, returning negative errno
3244 * else a 16-bit unsigned "word" received from the device.
3245 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003246s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
3248 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003249 int status;
3250
3251 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3252 I2C_SMBUS_READ, command,
3253 I2C_SMBUS_WORD_DATA, &data);
3254 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
David Brownellc0564602007-05-01 23:26:31 +02003256EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
David Brownella1cdeda2008-07-14 22:38:24 +02003258/**
3259 * i2c_smbus_write_word_data - SMBus "write word" protocol
3260 * @client: Handle to slave device
3261 * @command: Byte interpreted by slave
3262 * @value: 16-bit "word" being written
3263 *
3264 * This executes the SMBus "write word" protocol, returning negative errno
3265 * else zero on success.
3266 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003267s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3268 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 union i2c_smbus_data data;
3271 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003272 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3273 I2C_SMBUS_WRITE, command,
3274 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
David Brownellc0564602007-05-01 23:26:31 +02003276EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
David Brownella64ec072007-10-13 23:56:31 +02003278/**
David Brownella1cdeda2008-07-14 22:38:24 +02003279 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003280 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003281 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003282 * @values: Byte array into which data will be read; big enough to hold
3283 * the data returned by the slave. SMBus allows at most 32 bytes.
3284 *
David Brownella1cdeda2008-07-14 22:38:24 +02003285 * This executes the SMBus "block read" protocol, returning negative errno
3286 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003287 *
3288 * Note that using this function requires that the client's adapter support
3289 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3290 * support this; its emulation through I2C messaging relies on a specific
3291 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3292 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003293s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003294 u8 *values)
3295{
3296 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003297 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003298
David Brownell24a5bb72008-07-14 22:38:23 +02003299 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3300 I2C_SMBUS_READ, command,
3301 I2C_SMBUS_BLOCK_DATA, &data);
3302 if (status)
3303 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003304
3305 memcpy(values, &data.block[1], data.block[0]);
3306 return data.block[0];
3307}
3308EXPORT_SYMBOL(i2c_smbus_read_block_data);
3309
David Brownella1cdeda2008-07-14 22:38:24 +02003310/**
3311 * i2c_smbus_write_block_data - SMBus "block write" protocol
3312 * @client: Handle to slave device
3313 * @command: Byte interpreted by slave
3314 * @length: Size of data block; SMBus allows at most 32 bytes
3315 * @values: Byte array which will be written.
3316 *
3317 * This executes the SMBus "block write" protocol, returning negative errno
3318 * else zero on success.
3319 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003320s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003321 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 if (length > I2C_SMBUS_BLOCK_MAX)
3326 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003328 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003329 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3330 I2C_SMBUS_WRITE, command,
3331 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
David Brownellc0564602007-05-01 23:26:31 +02003333EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003336s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003337 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003340 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003341
Jean Delvare4b2643d2007-07-12 14:12:29 +02003342 if (length > I2C_SMBUS_BLOCK_MAX)
3343 length = I2C_SMBUS_BLOCK_MAX;
3344 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003345 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3346 I2C_SMBUS_READ, command,
3347 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3348 if (status < 0)
3349 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003350
3351 memcpy(values, &data.block[1], data.block[0]);
3352 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
David Brownellc0564602007-05-01 23:26:31 +02003354EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Jean Delvare0cc43a12011-01-10 22:11:23 +01003356s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003357 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003358{
3359 union i2c_smbus_data data;
3360
3361 if (length > I2C_SMBUS_BLOCK_MAX)
3362 length = I2C_SMBUS_BLOCK_MAX;
3363 data.block[0] = length;
3364 memcpy(data.block + 1, values, length);
3365 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3366 I2C_SMBUS_WRITE, command,
3367 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3368}
David Brownellc0564602007-05-01 23:26:31 +02003369EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003370
David Brownell438d6c22006-12-10 21:21:31 +01003371/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003373static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3374 unsigned short flags,
3375 char read_write, u8 command, int size,
3376 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 /* So we need to generate a series of msgs. In the case of writing, we
3379 need to use only one message; when reading, we need two. We initialize
3380 most things with sane defaults, to keep the code below somewhat
3381 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003382 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3383 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003384 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003386 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003387 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003388 struct i2c_msg msg[2] = {
3389 {
3390 .addr = addr,
3391 .flags = flags,
3392 .len = 1,
3393 .buf = msgbuf0,
3394 }, {
3395 .addr = addr,
3396 .flags = flags | I2C_M_RD,
3397 .len = 0,
3398 .buf = msgbuf1,
3399 },
3400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
3402 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003403 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 case I2C_SMBUS_QUICK:
3405 msg[0].len = 0;
3406 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003407 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3408 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 num = 1;
3410 break;
3411 case I2C_SMBUS_BYTE:
3412 if (read_write == I2C_SMBUS_READ) {
3413 /* Special case: only a read! */
3414 msg[0].flags = I2C_M_RD | flags;
3415 num = 1;
3416 }
3417 break;
3418 case I2C_SMBUS_BYTE_DATA:
3419 if (read_write == I2C_SMBUS_READ)
3420 msg[1].len = 1;
3421 else {
3422 msg[0].len = 2;
3423 msgbuf0[1] = data->byte;
3424 }
3425 break;
3426 case I2C_SMBUS_WORD_DATA:
3427 if (read_write == I2C_SMBUS_READ)
3428 msg[1].len = 2;
3429 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003430 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003432 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
3434 break;
3435 case I2C_SMBUS_PROC_CALL:
3436 num = 2; /* Special case */
3437 read_write = I2C_SMBUS_READ;
3438 msg[0].len = 3;
3439 msg[1].len = 2;
3440 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003441 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 break;
3443 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003445 msg[1].flags |= I2C_M_RECV_LEN;
3446 msg[1].len = 1; /* block length will be added by
3447 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 } else {
3449 msg[0].len = data->block[0] + 2;
3450 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003451 dev_err(&adapter->dev,
3452 "Invalid block write size %d\n",
3453 data->block[0]);
3454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003456 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 msgbuf0[i] = data->block[i-1];
3458 }
3459 break;
3460 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003461 num = 2; /* Another special case */
3462 read_write = I2C_SMBUS_READ;
3463 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003464 dev_err(&adapter->dev,
3465 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003466 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003467 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003468 }
3469 msg[0].len = data->block[0] + 2;
3470 for (i = 1; i < msg[0].len; i++)
3471 msgbuf0[i] = data->block[i-1];
3472 msg[1].flags |= I2C_M_RECV_LEN;
3473 msg[1].len = 1; /* block length will be added by
3474 the underlying bus driver */
3475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 case I2C_SMBUS_I2C_BLOCK_DATA:
3477 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003478 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 } else {
3480 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003481 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003482 dev_err(&adapter->dev,
3483 "Invalid block write size %d\n",
3484 data->block[0]);
3485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
3487 for (i = 1; i <= data->block[0]; i++)
3488 msgbuf0[i] = data->block[i];
3489 }
3490 break;
3491 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003492 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3493 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495
Jean Delvare421ef472005-10-26 21:28:55 +02003496 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3497 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3498 if (i) {
3499 /* Compute PEC if first message is a write */
3500 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003501 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003502 i2c_smbus_add_pec(&msg[0]);
3503 else /* Write followed by read */
3504 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3505 }
3506 /* Ask for PEC if last message is a read */
3507 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003508 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003509 }
3510
David Brownell24a5bb72008-07-14 22:38:23 +02003511 status = i2c_transfer(adapter, msg, num);
3512 if (status < 0)
3513 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Jean Delvare421ef472005-10-26 21:28:55 +02003515 /* Check PEC if last message is a read */
3516 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003517 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3518 if (status < 0)
3519 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003520 }
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003523 switch (size) {
3524 case I2C_SMBUS_BYTE:
3525 data->byte = msgbuf0[0];
3526 break;
3527 case I2C_SMBUS_BYTE_DATA:
3528 data->byte = msgbuf1[0];
3529 break;
3530 case I2C_SMBUS_WORD_DATA:
3531 case I2C_SMBUS_PROC_CALL:
3532 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3533 break;
3534 case I2C_SMBUS_I2C_BLOCK_DATA:
3535 for (i = 0; i < data->block[0]; i++)
3536 data->block[i+1] = msgbuf1[i];
3537 break;
3538 case I2C_SMBUS_BLOCK_DATA:
3539 case I2C_SMBUS_BLOCK_PROC_CALL:
3540 for (i = 0; i < msgbuf1[0] + 1; i++)
3541 data->block[i] = msgbuf1[i];
3542 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544 return 0;
3545}
3546
David Brownella1cdeda2008-07-14 22:38:24 +02003547/**
3548 * i2c_smbus_xfer - execute SMBus protocol operations
3549 * @adapter: Handle to I2C bus
3550 * @addr: Address of SMBus slave on that bus
3551 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3552 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3553 * @command: Byte interpreted by slave, for protocols which use such bytes
3554 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3555 * @data: Data to be read or written
3556 *
3557 * This executes an SMBus protocol operation, and returns a negative
3558 * errno code else zero on success.
3559 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003560s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003561 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003562 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003564 unsigned long orig_jiffies;
3565 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
David Howells8a325992014-03-06 13:36:06 +00003568 /* If enabled, the following two tracepoints are conditional on
3569 * read_write and protocol.
3570 */
3571 trace_smbus_write(adapter, addr, flags, read_write,
3572 command, protocol, data);
3573 trace_smbus_read(adapter, addr, flags, read_write,
3574 command, protocol);
3575
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003576 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003579 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003580
3581 /* Retry automatically on arbitration loss */
3582 orig_jiffies = jiffies;
3583 for (res = 0, try = 0; try <= adapter->retries; try++) {
3584 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3585 read_write, command,
3586 protocol, data);
3587 if (res != -EAGAIN)
3588 break;
3589 if (time_after(jiffies,
3590 orig_jiffies + adapter->timeout))
3591 break;
3592 }
Peter Rosin8320f492016-05-04 22:15:27 +02003593 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003595 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003596 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003597 /*
3598 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3599 * implement native support for the SMBus operation.
3600 */
3601 }
3602
David Howells8a325992014-03-06 13:36:06 +00003603 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3604 command, protocol, data);
3605
3606trace:
3607 /* If enabled, the reply tracepoint is conditional on read_write. */
3608 trace_smbus_reply(adapter, addr, flags, read_write,
3609 command, protocol, data);
3610 trace_smbus_result(adapter, addr, flags, read_write,
3611 command, protocol, res);
3612
3613 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Irina Tirdea01eef962015-08-12 17:31:33 +03003617/**
3618 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3619 * @client: Handle to slave device
3620 * @command: Byte interpreted by slave
3621 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3622 * @values: Byte array into which data will be read; big enough to hold
3623 * the data returned by the slave. SMBus allows at most
3624 * I2C_SMBUS_BLOCK_MAX bytes.
3625 *
3626 * This executes the SMBus "block read" protocol if supported by the adapter.
3627 * If block read is not supported, it emulates it using either word or byte
3628 * read protocols depending on availability.
3629 *
3630 * The addresses of the I2C slave device that are accessed with this function
3631 * must be mapped to a linear region, so that a block read will have the same
3632 * effect as a byte read. Before using this function you must double-check
3633 * if the I2C slave does support exchanging a block transfer with a byte
3634 * transfer.
3635 */
3636s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3637 u8 command, u8 length, u8 *values)
3638{
3639 u8 i = 0;
3640 int status;
3641
3642 if (length > I2C_SMBUS_BLOCK_MAX)
3643 length = I2C_SMBUS_BLOCK_MAX;
3644
3645 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3646 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3647
3648 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3649 return -EOPNOTSUPP;
3650
3651 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3652 while ((i + 2) <= length) {
3653 status = i2c_smbus_read_word_data(client, command + i);
3654 if (status < 0)
3655 return status;
3656 values[i] = status & 0xff;
3657 values[i + 1] = status >> 8;
3658 i += 2;
3659 }
3660 }
3661
3662 while (i < length) {
3663 status = i2c_smbus_read_byte_data(client, command + i);
3664 if (status < 0)
3665 return status;
3666 values[i] = status;
3667 i++;
3668 }
3669
3670 return i;
3671}
3672EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3673
Jean Delvared5fd1202015-01-26 20:59:31 +01003674#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003675int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3676{
3677 int ret;
3678
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003679 if (!client || !slave_cb) {
Colin Ian King26591612016-12-29 22:27:33 +00003680 WARN(1, "insufficient data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003681 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003682 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003683
Wolfram Sangc6909d62015-08-05 21:12:54 +02003684 if (!(client->flags & I2C_CLIENT_SLAVE))
3685 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3686 __func__);
3687
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003688 if (!(client->flags & I2C_CLIENT_TEN)) {
3689 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003690 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003691 if (ret) {
3692 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003693 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003694 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003695 }
3696
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003697 if (!client->adapter->algo->reg_slave) {
3698 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003699 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003700 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003701
3702 client->slave_cb = slave_cb;
3703
3704 i2c_lock_adapter(client->adapter);
3705 ret = client->adapter->algo->reg_slave(client);
3706 i2c_unlock_adapter(client->adapter);
3707
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003708 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003709 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003710 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3711 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003712
3713 return ret;
3714}
3715EXPORT_SYMBOL_GPL(i2c_slave_register);
3716
3717int i2c_slave_unregister(struct i2c_client *client)
3718{
3719 int ret;
3720
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003721 if (!client->adapter->algo->unreg_slave) {
3722 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003723 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003724 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003725
3726 i2c_lock_adapter(client->adapter);
3727 ret = client->adapter->algo->unreg_slave(client);
3728 i2c_unlock_adapter(client->adapter);
3729
3730 if (ret == 0)
3731 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003732 else
3733 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003734
3735 return ret;
3736}
3737EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Luis Oliveiracefae802017-01-26 17:45:32 +00003738
3739/**
3740 * i2c_detect_slave_mode - detect operation mode
3741 * @dev: The device owning the bus
3742 *
3743 * This checks the device nodes for an I2C slave by checking the address
3744 * used in the reg property. If the address match the I2C_OWN_SLAVE_ADDRESS
3745 * flag this means the device is configured to act as a I2C slave and it will
3746 * be listening at that address.
3747 *
3748 * Returns true if an I2C own slave address is detected, otherwise returns
3749 * false.
3750 */
3751bool i2c_detect_slave_mode(struct device *dev)
3752{
3753 if (IS_BUILTIN(CONFIG_OF) && dev->of_node) {
3754 struct device_node *child;
3755 u32 reg;
3756
3757 for_each_child_of_node(dev->of_node, child) {
3758 of_property_read_u32(child, "reg", &reg);
3759 if (reg & I2C_OWN_SLAVE_ADDRESS) {
3760 of_node_put(child);
3761 return true;
3762 }
3763 }
3764 } else if (IS_BUILTIN(CONFIG_ACPI) && ACPI_HANDLE(dev)) {
3765 dev_dbg(dev, "ACPI slave is not supported yet\n");
3766 }
3767 return false;
3768}
3769EXPORT_SYMBOL_GPL(i2c_detect_slave_mode);
3770
Jean Delvared5fd1202015-01-26 20:59:31 +01003771#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3774MODULE_DESCRIPTION("I2C-Bus main module");
3775MODULE_LICENSE("GPL");