blob: 6a2b995d7fc446672c177cd4ddda7c1ad868503a [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>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <asm/uaccess.h>
34#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
Jean Delvare6629dcf2010-05-04 11:09:28 +020068/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020069 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000070 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010071static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_IDR(i2c_adapter_idr);
73
Jean Delvare4f8cf822009-09-18 22:45:46 +020074static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020075static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010076
David Howellsd9a83d62014-03-06 13:35:59 +000077static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053078static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000079
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050080int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000081{
82 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050083 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000084}
85
86void i2c_transfer_trace_unreg(void)
87{
88 static_key_slow_dec(&i2c_trace_msg);
89}
90
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020091#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030092struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020093 struct acpi_connection_info info;
94 struct i2c_adapter *adapter;
95};
96
97struct gsb_buffer {
98 u8 status;
99 u8 len;
100 union {
101 u16 wdata;
102 u8 bdata;
103 u8 data[0];
104 };
105} __packed;
106
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300107struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300108 struct i2c_board_info *info;
109 acpi_handle adapter_handle;
110 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300111 acpi_handle search_handle;
112 u32 speed;
113 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300114};
115
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300116static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200117{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300118 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300119 struct i2c_board_info *info = lookup->info;
120 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300121 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200122
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300123 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
124 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200125
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300126 sb = &ares->data.i2c_serial_bus;
127 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
128 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200129
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300130 status = acpi_get_handle(lookup->device_handle,
131 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300132 &lookup->adapter_handle);
133 if (!ACPI_SUCCESS(status))
134 return 1;
135
136 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300137 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300138 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
139 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200141 return 1;
142}
143
Jarkko Nikula5853b222016-08-12 17:02:53 +0300144static int i2c_acpi_do_lookup(struct acpi_device *adev,
145 struct i2c_acpi_lookup *lookup)
146{
147 struct i2c_board_info *info = lookup->info;
148 struct list_head resource_list;
149 int ret;
150
151 if (acpi_bus_get_status(adev) || !adev->status.present ||
152 acpi_device_enumerated(adev))
153 return -EINVAL;
154
155 memset(info, 0, sizeof(*info));
156 lookup->device_handle = acpi_device_handle(adev);
157
158 /* Look up for I2cSerialBus resource */
159 INIT_LIST_HEAD(&resource_list);
160 ret = acpi_dev_get_resources(adev, &resource_list,
161 i2c_acpi_fill_info, lookup);
162 acpi_dev_free_resource_list(&resource_list);
163
164 if (ret < 0 || !info->addr)
165 return -EINVAL;
166
167 return 0;
168}
169
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300170static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300171 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300172 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300173 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200174{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200175 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300176 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300177 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200178 int ret;
179
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300180 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300181 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300182
Jarkko Nikula5853b222016-08-12 17:02:53 +0300183 ret = i2c_acpi_do_lookup(adev, &lookup);
184 if (ret)
185 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200186
Mika Westerberg318ce272016-09-20 16:59:25 +0300187 if (adapter) {
188 /* The adapter must match the one in I2cSerialBus() connector */
189 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
190 return -ENODEV;
191 } else {
192 struct acpi_device *adapter_adev;
193
194 /* The adapter must be present */
195 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
196 return -ENODEV;
197 if (acpi_bus_get_status(adapter_adev) ||
198 !adapter_adev->status.present)
199 return -ENODEV;
200 }
201
Jarkko Nikula5853b222016-08-12 17:02:53 +0300202 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300203 if (adapter_handle)
204 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200205
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300206 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300207 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300208 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
209 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300210 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300211
212 resource_list_for_each_entry(entry, &resource_list) {
213 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300214 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300215 break;
216 }
217 }
218
219 acpi_dev_free_resource_list(&resource_list);
220
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300221 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
222
223 return 0;
224}
225
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300226static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300227 struct acpi_device *adev,
228 struct i2c_board_info *info)
229{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200230 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300231 acpi_device_set_enumerated(adev);
232
233 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200234 adev->power.flags.ignore_parent = false;
235 dev_err(&adapter->dev,
236 "failed to add I2C device %s from ACPI\n",
237 dev_name(&adev->dev));
238 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300239}
240
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300241static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300242 void *data, void **return_value)
243{
244 struct i2c_adapter *adapter = data;
245 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300246 struct i2c_board_info info;
247
248 if (acpi_bus_get_device(handle, &adev))
249 return AE_OK;
250
Mika Westerberg318ce272016-09-20 16:59:25 +0300251 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300252 return AE_OK;
253
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300254 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200255
256 return AE_OK;
257}
258
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300259#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300260
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200261/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300262 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200263 * @adap: pointer to adapter
264 *
265 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
266 * namespace. When a device is found it will be added to the Linux device
267 * model and bound to the corresponding ACPI handle.
268 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300269static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200270{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200271 acpi_status status;
272
Dustin Byford8eb5c872015-10-23 12:27:07 -0700273 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200274 return;
275
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300276 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300277 I2C_ACPI_MAX_SCAN_DEPTH,
278 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200279 adap, NULL);
280 if (ACPI_FAILURE(status))
281 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
282}
283
Jarkko Nikula5853b222016-08-12 17:02:53 +0300284static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
285 void *data, void **return_value)
286{
287 struct i2c_acpi_lookup *lookup = data;
288 struct acpi_device *adev;
289
290 if (acpi_bus_get_device(handle, &adev))
291 return AE_OK;
292
293 if (i2c_acpi_do_lookup(adev, lookup))
294 return AE_OK;
295
296 if (lookup->search_handle != lookup->adapter_handle)
297 return AE_OK;
298
299 if (lookup->speed <= lookup->min_speed)
300 lookup->min_speed = lookup->speed;
301
302 return AE_OK;
303}
304
305/**
306 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
307 * @dev: The device owning the bus
308 *
309 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
310 * devices connected to this bus and use the speed of slowest device.
311 *
312 * Returns the speed in Hz or zero
313 */
314u32 i2c_acpi_find_bus_speed(struct device *dev)
315{
316 struct i2c_acpi_lookup lookup;
317 struct i2c_board_info dummy;
318 acpi_status status;
319
320 if (!has_acpi_companion(dev))
321 return 0;
322
323 memset(&lookup, 0, sizeof(lookup));
324 lookup.search_handle = ACPI_HANDLE(dev);
325 lookup.min_speed = UINT_MAX;
326 lookup.info = &dummy;
327
328 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
329 I2C_ACPI_MAX_SCAN_DEPTH,
330 i2c_acpi_lookup_speed, NULL,
331 &lookup, NULL);
332
333 if (ACPI_FAILURE(status)) {
334 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
335 return 0;
336 }
337
338 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
339}
340EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
341
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300342static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300343{
344 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
345
346 if (!adapter)
347 return 0;
348
349 return ACPI_HANDLE(dev) == (acpi_handle)data;
350}
351
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300352static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300353{
354 return ACPI_COMPANION(dev) == data;
355}
356
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300357static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300358{
359 struct device *dev;
360
361 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300362 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300363 return dev ? i2c_verify_adapter(dev) : NULL;
364}
365
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300366static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300367{
368 struct device *dev;
369
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300370 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300371 return dev ? i2c_verify_client(dev) : NULL;
372}
373
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300374static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300375 void *arg)
376{
377 struct acpi_device *adev = arg;
378 struct i2c_board_info info;
379 acpi_handle adapter_handle;
380 struct i2c_adapter *adapter;
381 struct i2c_client *client;
382
383 switch (value) {
384 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300385 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300386 break;
387
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300388 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300389 if (!adapter)
390 break;
391
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300392 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300393 break;
394 case ACPI_RECONFIG_DEVICE_REMOVE:
395 if (!acpi_device_enumerated(adev))
396 break;
397
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300398 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300399 if (!client)
400 break;
401
402 i2c_unregister_device(client);
403 put_device(&client->dev);
404 break;
405 }
406
407 return NOTIFY_OK;
408}
409
410static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300411 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300412};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200413#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300414static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300415extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200416#endif /* CONFIG_ACPI */
417
418#ifdef CONFIG_ACPI_I2C_OPREGION
419static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
420 u8 cmd, u8 *data, u8 data_len)
421{
422
423 struct i2c_msg msgs[2];
424 int ret;
425 u8 *buffer;
426
427 buffer = kzalloc(data_len, GFP_KERNEL);
428 if (!buffer)
429 return AE_NO_MEMORY;
430
431 msgs[0].addr = client->addr;
432 msgs[0].flags = client->flags;
433 msgs[0].len = 1;
434 msgs[0].buf = &cmd;
435
436 msgs[1].addr = client->addr;
437 msgs[1].flags = client->flags | I2C_M_RD;
438 msgs[1].len = data_len;
439 msgs[1].buf = buffer;
440
441 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
442 if (ret < 0)
443 dev_err(&client->adapter->dev, "i2c read failed\n");
444 else
445 memcpy(data, buffer, data_len);
446
447 kfree(buffer);
448 return ret;
449}
450
451static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
452 u8 cmd, u8 *data, u8 data_len)
453{
454
455 struct i2c_msg msgs[1];
456 u8 *buffer;
457 int ret = AE_OK;
458
459 buffer = kzalloc(data_len + 1, GFP_KERNEL);
460 if (!buffer)
461 return AE_NO_MEMORY;
462
463 buffer[0] = cmd;
464 memcpy(buffer + 1, data, data_len);
465
466 msgs[0].addr = client->addr;
467 msgs[0].flags = client->flags;
468 msgs[0].len = data_len + 1;
469 msgs[0].buf = buffer;
470
471 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
472 if (ret < 0)
473 dev_err(&client->adapter->dev, "i2c write failed\n");
474
475 kfree(buffer);
476 return ret;
477}
478
479static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300480i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200481 u32 bits, u64 *value64,
482 void *handler_context, void *region_context)
483{
484 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300485 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200486 struct acpi_connection_info *info = &data->info;
487 struct acpi_resource_i2c_serialbus *sb;
488 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300489 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200490 struct acpi_resource *ares;
491 u32 accessor_type = function >> 16;
492 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100493 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200494 int status;
495
496 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
497 if (ACPI_FAILURE(ret))
498 return ret;
499
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300500 client = kzalloc(sizeof(*client), GFP_KERNEL);
501 if (!client) {
502 ret = AE_NO_MEMORY;
503 goto err;
504 }
505
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200506 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
507 ret = AE_BAD_PARAMETER;
508 goto err;
509 }
510
511 sb = &ares->data.i2c_serial_bus;
512 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
513 ret = AE_BAD_PARAMETER;
514 goto err;
515 }
516
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300517 client->adapter = adapter;
518 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200519
520 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300521 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200522
523 switch (accessor_type) {
524 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
525 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300526 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200527 if (status >= 0) {
528 gsb->bdata = status;
529 status = 0;
530 }
531 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300532 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200533 }
534 break;
535
536 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
537 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300538 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200539 if (status >= 0) {
540 gsb->bdata = status;
541 status = 0;
542 }
543 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300544 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200545 gsb->bdata);
546 }
547 break;
548
549 case ACPI_GSB_ACCESS_ATTRIB_WORD:
550 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300551 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200552 if (status >= 0) {
553 gsb->wdata = status;
554 status = 0;
555 }
556 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300557 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200558 gsb->wdata);
559 }
560 break;
561
562 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
563 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300564 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200565 gsb->data);
566 if (status >= 0) {
567 gsb->len = status;
568 status = 0;
569 }
570 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300571 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200572 gsb->len, gsb->data);
573 }
574 break;
575
576 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
577 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300578 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200579 gsb->data, info->access_length);
580 if (status > 0)
581 status = 0;
582 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300583 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200584 gsb->data, info->access_length);
585 }
586 break;
587
588 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900589 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
590 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200591 ret = AE_BAD_PARAMETER;
592 goto err;
593 }
594
595 gsb->status = status;
596
597 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300598 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200599 ACPI_FREE(ares);
600 return ret;
601}
602
603
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300604static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200605{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200606 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300607 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200608 acpi_status status;
609
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200610 if (!adapter->dev.parent)
611 return -ENODEV;
612
613 handle = ACPI_HANDLE(adapter->dev.parent);
614
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200615 if (!handle)
616 return -ENODEV;
617
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300618 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200619 GFP_KERNEL);
620 if (!data)
621 return -ENOMEM;
622
623 data->adapter = adapter;
624 status = acpi_bus_attach_private_data(handle, (void *)data);
625 if (ACPI_FAILURE(status)) {
626 kfree(data);
627 return -ENOMEM;
628 }
629
630 status = acpi_install_address_space_handler(handle,
631 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300632 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200633 NULL,
634 data);
635 if (ACPI_FAILURE(status)) {
636 dev_err(&adapter->dev, "Error installing i2c space handler\n");
637 acpi_bus_detach_private_data(handle);
638 kfree(data);
639 return -ENOMEM;
640 }
641
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800642 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200643 return 0;
644}
645
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300646static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200647{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200648 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300649 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200650 acpi_status status;
651
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200652 if (!adapter->dev.parent)
653 return;
654
655 handle = ACPI_HANDLE(adapter->dev.parent);
656
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200657 if (!handle)
658 return;
659
660 acpi_remove_address_space_handler(handle,
661 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300662 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200663
664 status = acpi_bus_get_private_data(handle, (void **)&data);
665 if (ACPI_SUCCESS(status))
666 kfree(data);
667
668 acpi_bus_detach_private_data(handle);
669}
670#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300671static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200672{ }
673
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300674static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200675{ return 0; }
676#endif /* CONFIG_ACPI_I2C_OPREGION */
677
David Brownellf37dd802007-02-13 22:09:00 +0100678/* ------------------------------------------------------------------------- */
679
Jean Delvared2653e92008-04-29 23:11:39 +0200680static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
681 const struct i2c_client *client)
682{
683 while (id->name[0]) {
684 if (strcmp(client->name, id->name) == 0)
685 return id;
686 id++;
687 }
688 return NULL;
689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static int i2c_device_match(struct device *dev, struct device_driver *drv)
692{
Jean Delvare51298d12009-09-18 22:45:45 +0200693 struct i2c_client *client = i2c_verify_client(dev);
694 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200695
Jean Delvare51298d12009-09-18 22:45:45 +0200696 if (!client)
697 return 0;
698
Grant Likely959e85f2010-06-08 07:48:19 -0600699 /* Attempt an OF style match */
700 if (of_driver_match_device(dev, drv))
701 return 1;
702
Mika Westerberg907ddf82012-11-23 12:23:40 +0100703 /* Then ACPI style match */
704 if (acpi_driver_match_device(dev, drv))
705 return 1;
706
Jean Delvare51298d12009-09-18 22:45:45 +0200707 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200708 /* match on an id table if there is one */
709 if (driver->id_table)
710 return i2c_match_id(driver->id_table, client) != NULL;
711
Jean Delvareeb8a7902008-05-18 20:49:41 +0200712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Kay Sievers7eff2e72007-08-14 15:15:12 +0200715static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200716{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100717 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800718 int rc;
719
720 rc = acpi_device_uevent_modalias(dev, env);
721 if (rc != -ENODEV)
722 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200723
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100724 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200725}
726
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530727/* i2c bus recovery routines */
728static int get_scl_gpio_value(struct i2c_adapter *adap)
729{
730 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
731}
732
733static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
734{
735 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
736}
737
738static int get_sda_gpio_value(struct i2c_adapter *adap)
739{
740 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
741}
742
743static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
744{
745 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
746 struct device *dev = &adap->dev;
747 int ret = 0;
748
749 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
750 GPIOF_OUT_INIT_HIGH, "i2c-scl");
751 if (ret) {
752 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
753 return ret;
754 }
755
756 if (bri->get_sda) {
757 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
758 /* work without SDA polling */
759 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
760 bri->sda_gpio);
761 bri->get_sda = NULL;
762 }
763 }
764
765 return ret;
766}
767
768static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
769{
770 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
771
772 if (bri->get_sda)
773 gpio_free(bri->sda_gpio);
774
775 gpio_free(bri->scl_gpio);
776}
777
778/*
779 * We are generating clock pulses. ndelay() determines durating of clk pulses.
780 * We will generate clock with rate 100 KHz and so duration of both clock levels
781 * is: delay in ns = (10^6 / 100) / 2
782 */
783#define RECOVERY_NDELAY 5000
784#define RECOVERY_CLK_CNT 9
785
786static int i2c_generic_recovery(struct i2c_adapter *adap)
787{
788 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
789 int i = 0, val = 1, ret = 0;
790
791 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300792 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530793
Jan Luebbe8b062602015-07-08 16:35:06 +0200794 bri->set_scl(adap, val);
795 ndelay(RECOVERY_NDELAY);
796
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530797 /*
798 * By this time SCL is high, as we need to give 9 falling-rising edges
799 */
800 while (i++ < RECOVERY_CLK_CNT * 2) {
801 if (val) {
802 /* Break if SDA is high */
803 if (bri->get_sda && bri->get_sda(adap))
804 break;
805 /* SCL shouldn't be low here */
806 if (!bri->get_scl(adap)) {
807 dev_err(&adap->dev,
808 "SCL is stuck low, exit recovery\n");
809 ret = -EBUSY;
810 break;
811 }
812 }
813
814 val = !val;
815 bri->set_scl(adap, val);
816 ndelay(RECOVERY_NDELAY);
817 }
818
819 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300820 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530821
822 return ret;
823}
824
825int i2c_generic_scl_recovery(struct i2c_adapter *adap)
826{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530827 return i2c_generic_recovery(adap);
828}
Mark Brownc1c21f42015-04-15 19:18:39 +0100829EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530830
831int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
832{
833 int ret;
834
835 ret = i2c_get_gpios_for_recovery(adap);
836 if (ret)
837 return ret;
838
839 ret = i2c_generic_recovery(adap);
840 i2c_put_gpios_for_recovery(adap);
841
842 return ret;
843}
Mark Brownc1c21f42015-04-15 19:18:39 +0100844EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530845
846int i2c_recover_bus(struct i2c_adapter *adap)
847{
848 if (!adap->bus_recovery_info)
849 return -EOPNOTSUPP;
850
851 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
852 return adap->bus_recovery_info->recover_bus(adap);
853}
Mark Brownc1c21f42015-04-15 19:18:39 +0100854EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530855
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900856static void i2c_init_recovery(struct i2c_adapter *adap)
857{
858 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
859 char *err_str;
860
861 if (!bri)
862 return;
863
864 if (!bri->recover_bus) {
865 err_str = "no recover_bus() found";
866 goto err;
867 }
868
869 /* Generic GPIO recovery */
870 if (bri->recover_bus == i2c_generic_gpio_recovery) {
871 if (!gpio_is_valid(bri->scl_gpio)) {
872 err_str = "invalid SCL gpio";
873 goto err;
874 }
875
876 if (gpio_is_valid(bri->sda_gpio))
877 bri->get_sda = get_sda_gpio_value;
878 else
879 bri->get_sda = NULL;
880
881 bri->get_scl = get_scl_gpio_value;
882 bri->set_scl = set_scl_gpio_value;
883 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
884 /* Generic SCL recovery */
885 if (!bri->set_scl || !bri->get_scl) {
886 err_str = "no {get|set}_scl() found";
887 goto err;
888 }
889 }
890
891 return;
892 err:
893 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
894 adap->bus_recovery_info = NULL;
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static int i2c_device_probe(struct device *dev)
898{
Jean Delvare51298d12009-09-18 22:45:45 +0200899 struct i2c_client *client = i2c_verify_client(dev);
900 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100901 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200902
Jean Delvare51298d12009-09-18 22:45:45 +0200903 if (!client)
904 return 0;
905
Mika Westerberg845c8772015-05-06 13:29:08 +0300906 if (!client->irq) {
907 int irq = -ENOENT;
908
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700909 if (dev->of_node) {
910 irq = of_irq_get_byname(dev->of_node, "irq");
911 if (irq == -EINVAL || irq == -ENODATA)
912 irq = of_irq_get(dev->of_node, 0);
913 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300914 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700915 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100916 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200917 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100918 if (irq < 0)
919 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200920
921 client->irq = irq;
922 }
923
Jean Delvare51298d12009-09-18 22:45:45 +0200924 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200925 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200926 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200927
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700928 if (client->flags & I2C_CLIENT_WAKE) {
929 int wakeirq = -ENOENT;
930
931 if (dev->of_node) {
932 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
933 if (wakeirq == -EPROBE_DEFER)
934 return wakeirq;
935 }
936
937 device_init_wakeup(&client->dev, true);
938
939 if (wakeirq > 0 && wakeirq != client->irq)
940 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
941 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200942 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700943 else
944 status = 0;
945
946 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300947 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700948 }
949
David Brownell7b4fbc52007-05-01 23:26:30 +0200950 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200951
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200952 status = of_clk_set_defaults(dev->of_node, false);
953 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700954 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200955
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200956 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100957 if (status == -EPROBE_DEFER)
958 goto err_clear_wakeup_irq;
959
960 status = driver->probe(client, i2c_match_id(driver->id_table, client));
961 if (status)
962 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100963
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700964 return 0;
965
966err_detach_pm_domain:
967 dev_pm_domain_detach(&client->dev, true);
968err_clear_wakeup_irq:
969 dev_pm_clear_wake_irq(&client->dev);
970 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static int i2c_device_remove(struct device *dev)
975{
Jean Delvare51298d12009-09-18 22:45:45 +0200976 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200977 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100978 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200979
Jean Delvare51298d12009-09-18 22:45:45 +0200980 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200981 return 0;
982
983 driver = to_i2c_driver(dev->driver);
984 if (driver->remove) {
985 dev_dbg(dev, "remove\n");
986 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200987 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100988
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200989 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700990
991 dev_pm_clear_wake_irq(&client->dev);
992 device_init_wakeup(&client->dev, false);
993
David Brownella1d9e6e2007-05-01 23:26:30 +0200994 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
David Brownellf37dd802007-02-13 22:09:00 +0100997static void i2c_device_shutdown(struct device *dev)
998{
Jean Delvare51298d12009-09-18 22:45:45 +0200999 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001000 struct i2c_driver *driver;
1001
Jean Delvare51298d12009-09-18 22:45:45 +02001002 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001003 return;
1004 driver = to_i2c_driver(dev->driver);
1005 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001006 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001007}
1008
David Brownell9c1600e2007-05-01 23:26:31 +02001009static void i2c_client_dev_release(struct device *dev)
1010{
1011 kfree(to_i2c_client(dev));
1012}
1013
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001014static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001015show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001016{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001017 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1018 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001019}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001020static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001021
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001022static ssize_t
1023show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001024{
1025 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001026 int len;
1027
1028 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1029 if (len != -ENODEV)
1030 return len;
1031
Jean Delvareeb8a7902008-05-18 20:49:41 +02001032 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001033}
Jean Delvare51298d12009-09-18 22:45:45 +02001034static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1035
1036static struct attribute *i2c_dev_attrs[] = {
1037 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001038 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001039 &dev_attr_modalias.attr,
1040 NULL
1041};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001042ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001043
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001044struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001045 .name = "i2c",
1046 .match = i2c_device_match,
1047 .probe = i2c_device_probe,
1048 .remove = i2c_device_remove,
1049 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001050};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001051EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001052
Jean Delvare51298d12009-09-18 22:45:45 +02001053static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001054 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001055 .uevent = i2c_device_uevent,
1056 .release = i2c_client_dev_release,
1057};
1058
David Brownell9b766b82008-01-27 18:14:51 +01001059
1060/**
1061 * i2c_verify_client - return parameter as i2c_client, or NULL
1062 * @dev: device, probably from some driver model iterator
1063 *
1064 * When traversing the driver model tree, perhaps using driver model
1065 * iterators like @device_for_each_child(), you can't assume very much
1066 * about the nodes you find. Use this function to avoid oopses caused
1067 * by wrongly treating some non-I2C device as an i2c_client.
1068 */
1069struct i2c_client *i2c_verify_client(struct device *dev)
1070{
Jean Delvare51298d12009-09-18 22:45:45 +02001071 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001072 ? to_i2c_client(dev)
1073 : NULL;
1074}
1075EXPORT_SYMBOL(i2c_verify_client);
1076
1077
Wolfram Sangda899f52015-05-18 21:09:12 +02001078/* Return a unique address which takes the flags of the client into account */
1079static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1080{
1081 unsigned short addr = client->addr;
1082
1083 /* For some client flags, add an arbitrary offset to avoid collisions */
1084 if (client->flags & I2C_CLIENT_TEN)
1085 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1086
1087 if (client->flags & I2C_CLIENT_SLAVE)
1088 addr |= I2C_ADDR_OFFSET_SLAVE;
1089
1090 return addr;
1091}
1092
Jean Delvare3a89db52010-06-03 11:33:52 +02001093/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001094 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001095static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001096{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001097 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001098 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001099 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001100 return -EINVAL;
1101 } else {
1102 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001103 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001104 return -EINVAL;
1105 }
1106 return 0;
1107}
1108
Jean Delvare656b8762010-06-03 11:33:53 +02001109/* And this is a strict address validity check, used when probing. If a
1110 * device uses a reserved address, then it shouldn't be probed. 7-bit
1111 * addressing is assumed, 10-bit address devices are rare and should be
1112 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001113static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001114{
1115 /*
1116 * Reserved addresses per I2C specification:
1117 * 0x00 General call address / START byte
1118 * 0x01 CBUS address
1119 * 0x02 Reserved for different bus format
1120 * 0x03 Reserved for future purposes
1121 * 0x04-0x07 Hs-mode master code
1122 * 0x78-0x7b 10-bit slave addressing
1123 * 0x7c-0x7f Reserved for future purposes
1124 */
1125 if (addr < 0x08 || addr > 0x77)
1126 return -EINVAL;
1127 return 0;
1128}
1129
Jean Delvare3b5f7942010-06-03 11:33:55 +02001130static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1131{
1132 struct i2c_client *client = i2c_verify_client(dev);
1133 int addr = *(int *)addrp;
1134
Wolfram Sang9bccc702015-07-17 14:48:56 +02001135 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001136 return -EBUSY;
1137 return 0;
1138}
1139
Michael Lawnick08263742010-08-11 18:21:02 +02001140/* walk up mux tree */
1141static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1142{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001143 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001144 int result;
1145
1146 result = device_for_each_child(&adapter->dev, &addr,
1147 __i2c_check_addr_busy);
1148
Jean Delvare97cc4d42010-10-24 18:16:57 +02001149 if (!result && parent)
1150 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001151
1152 return result;
1153}
1154
1155/* recurse down mux tree */
1156static int i2c_check_mux_children(struct device *dev, void *addrp)
1157{
1158 int result;
1159
1160 if (dev->type == &i2c_adapter_type)
1161 result = device_for_each_child(dev, addrp,
1162 i2c_check_mux_children);
1163 else
1164 result = __i2c_check_addr_busy(dev, addrp);
1165
1166 return result;
1167}
1168
Jean Delvare3b5f7942010-06-03 11:33:55 +02001169static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1170{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001171 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001172 int result = 0;
1173
Jean Delvare97cc4d42010-10-24 18:16:57 +02001174 if (parent)
1175 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001176
1177 if (!result)
1178 result = device_for_each_child(&adapter->dev, &addr,
1179 i2c_check_mux_children);
1180
1181 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001182}
1183
David Brownell9c1600e2007-05-01 23:26:31 +02001184/**
Peter Rosin8320f492016-05-04 22:15:27 +02001185 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001186 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001187 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1188 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001189 */
Peter Rosin8320f492016-05-04 22:15:27 +02001190static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1191 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001192{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001193 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001194}
Jean Delvarefe61e072010-08-11 18:20:58 +02001195
1196/**
Peter Rosin8320f492016-05-04 22:15:27 +02001197 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001198 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001199 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1200 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001201 */
Peter Rosin8320f492016-05-04 22:15:27 +02001202static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1203 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001204{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001205 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001206}
1207
1208/**
Peter Rosin8320f492016-05-04 22:15:27 +02001209 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001210 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001211 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1212 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001213 */
Peter Rosin8320f492016-05-04 22:15:27 +02001214static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1215 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001216{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001217 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001218}
Jean Delvarefe61e072010-08-11 18:20:58 +02001219
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001220static void i2c_dev_set_name(struct i2c_adapter *adap,
1221 struct i2c_client *client)
1222{
1223 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1224
1225 if (adev) {
1226 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1227 return;
1228 }
1229
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001230 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001231 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001232}
1233
Jean Delvarefe61e072010-08-11 18:20:58 +02001234/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001235 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001236 * @adap: the adapter managing the device
1237 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001238 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001239 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001240 * Create an i2c device. Binding is handled through driver model
1241 * probe()/remove() methods. A driver may be bound to this device when we
1242 * return from this function, or any later moment (e.g. maybe hotplugging will
1243 * load the driver module). This call is not appropriate for use by mainboard
1244 * initialization logic, which usually runs during an arch_initcall() long
1245 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001246 *
1247 * This returns the new i2c client, which may be saved for later use with
1248 * i2c_unregister_device(); or NULL to indicate an error.
1249 */
1250struct i2c_client *
1251i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1252{
1253 struct i2c_client *client;
1254 int status;
1255
1256 client = kzalloc(sizeof *client, GFP_KERNEL);
1257 if (!client)
1258 return NULL;
1259
1260 client->adapter = adap;
1261
1262 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001263
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001264 if (info->archdata)
1265 client->dev.archdata = *info->archdata;
1266
Marc Pignatee354252008-08-28 08:33:22 +02001267 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001268 client->addr = info->addr;
1269 client->irq = info->irq;
1270
David Brownell9c1600e2007-05-01 23:26:31 +02001271 strlcpy(client->name, info->type, sizeof(client->name));
1272
Wolfram Sangc4019b72015-07-17 12:50:06 +02001273 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001274 if (status) {
1275 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1276 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1277 goto out_err_silent;
1278 }
1279
Jean Delvaref8a227e2009-06-19 16:58:18 +02001280 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001281 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001282 if (status)
1283 goto out_err;
1284
1285 client->dev.parent = &client->adapter->dev;
1286 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001287 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001288 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001289 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001290
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001291 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001292 status = device_register(&client->dev);
1293 if (status)
1294 goto out_err;
1295
Jean Delvaref8a227e2009-06-19 16:58:18 +02001296 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1297 client->name, dev_name(&client->dev));
1298
David Brownell9c1600e2007-05-01 23:26:31 +02001299 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001300
1301out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001302 dev_err(&adap->dev,
1303 "Failed to register i2c client %s at 0x%02x (%d)\n",
1304 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001305out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001306 kfree(client);
1307 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001308}
1309EXPORT_SYMBOL_GPL(i2c_new_device);
1310
1311
1312/**
1313 * i2c_unregister_device - reverse effect of i2c_new_device()
1314 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001315 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001316 */
1317void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001318{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001319 if (client->dev.of_node)
1320 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001321 if (ACPI_COMPANION(&client->dev))
1322 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001323 device_unregister(&client->dev);
1324}
David Brownell9c1600e2007-05-01 23:26:31 +02001325EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001326
1327
Jean Delvare60b129d2008-05-11 20:37:06 +02001328static const struct i2c_device_id dummy_id[] = {
1329 { "dummy", 0 },
1330 { },
1331};
1332
Jean Delvared2653e92008-04-29 23:11:39 +02001333static int dummy_probe(struct i2c_client *client,
1334 const struct i2c_device_id *id)
1335{
1336 return 0;
1337}
1338
1339static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001340{
1341 return 0;
1342}
1343
1344static struct i2c_driver dummy_driver = {
1345 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001346 .probe = dummy_probe,
1347 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001348 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001349};
1350
1351/**
1352 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1353 * @adapter: the adapter managing the device
1354 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001355 * Context: can sleep
1356 *
1357 * This returns an I2C client bound to the "dummy" driver, intended for use
1358 * with devices that consume multiple addresses. Examples of such chips
1359 * include various EEPROMS (like 24c04 and 24c08 models).
1360 *
1361 * These dummy devices have two main uses. First, most I2C and SMBus calls
1362 * except i2c_transfer() need a client handle; the dummy will be that handle.
1363 * And second, this prevents the specified address from being bound to a
1364 * different driver.
1365 *
1366 * This returns the new i2c client, which should be saved for later use with
1367 * i2c_unregister_device(); or NULL to indicate an error.
1368 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001369struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001370{
1371 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001372 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001373 };
1374
David Brownelle9f13732008-01-27 18:14:52 +01001375 return i2c_new_device(adapter, &info);
1376}
1377EXPORT_SYMBOL_GPL(i2c_new_dummy);
1378
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001379/**
1380 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1381 * and create the associated device
1382 * @client: Handle to the primary client
1383 * @name: Handle to specify which secondary address to get
1384 * @default_addr: Used as a fallback if no secondary address was specified
1385 * Context: can sleep
1386 *
1387 * I2C clients can be composed of multiple I2C slaves bound together in a single
1388 * component. The I2C client driver then binds to the master I2C slave and needs
1389 * to create I2C dummy clients to communicate with all the other slaves.
1390 *
1391 * This function creates and returns an I2C dummy client whose I2C address is
1392 * retrieved from the platform firmware based on the given slave name. If no
1393 * address is specified by the firmware default_addr is used.
1394 *
1395 * On DT-based platforms the address is retrieved from the "reg" property entry
1396 * cell whose "reg-names" value matches the slave name.
1397 *
1398 * This returns the new i2c client, which should be saved for later use with
1399 * i2c_unregister_device(); or NULL to indicate an error.
1400 */
1401struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1402 const char *name,
1403 u16 default_addr)
1404{
1405 struct device_node *np = client->dev.of_node;
1406 u32 addr = default_addr;
1407 int i;
1408
1409 if (np) {
1410 i = of_property_match_string(np, "reg-names", name);
1411 if (i >= 0)
1412 of_property_read_u32_index(np, "reg", i, &addr);
1413 }
1414
1415 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1416 return i2c_new_dummy(client->adapter, addr);
1417}
1418EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1419
David Brownellf37dd802007-02-13 22:09:00 +01001420/* ------------------------------------------------------------------------- */
1421
David Brownell16ffadf2007-05-01 23:26:28 +02001422/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1423
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001424static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
David Brownellef2c83212007-05-01 23:26:28 +02001426 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 complete(&adap->dev_released);
1428}
1429
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001430unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001431{
1432 unsigned int depth = 0;
1433
1434 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1435 depth++;
1436
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001437 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1438 "adapter depth exceeds lockdep subclass limit\n");
1439
Jean Delvare390946b2012-09-10 10:14:02 +02001440 return depth;
1441}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001442EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001443
1444/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001445 * Let users instantiate I2C devices through sysfs. This can be used when
1446 * platform initialization code doesn't contain the proper data for
1447 * whatever reason. Also useful for drivers that do device detection and
1448 * detection fails, either because the device uses an unexpected address,
1449 * or this is a compatible device with different ID register values.
1450 *
1451 * Parameter checking may look overzealous, but we really don't want
1452 * the user to provide incorrect parameters.
1453 */
1454static ssize_t
1455i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1456 const char *buf, size_t count)
1457{
1458 struct i2c_adapter *adap = to_i2c_adapter(dev);
1459 struct i2c_board_info info;
1460 struct i2c_client *client;
1461 char *blank, end;
1462 int res;
1463
Jean Delvare99cd8e22009-06-19 16:58:20 +02001464 memset(&info, 0, sizeof(struct i2c_board_info));
1465
1466 blank = strchr(buf, ' ');
1467 if (!blank) {
1468 dev_err(dev, "%s: Missing parameters\n", "new_device");
1469 return -EINVAL;
1470 }
1471 if (blank - buf > I2C_NAME_SIZE - 1) {
1472 dev_err(dev, "%s: Invalid device name\n", "new_device");
1473 return -EINVAL;
1474 }
1475 memcpy(info.type, buf, blank - buf);
1476
1477 /* Parse remaining parameters, reject extra parameters */
1478 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1479 if (res < 1) {
1480 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1481 return -EINVAL;
1482 }
1483 if (res > 1 && end != '\n') {
1484 dev_err(dev, "%s: Extra parameters\n", "new_device");
1485 return -EINVAL;
1486 }
1487
Wolfram Sangcfa03272015-07-27 14:03:38 +02001488 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1489 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1490 info.flags |= I2C_CLIENT_TEN;
1491 }
1492
1493 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1494 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1495 info.flags |= I2C_CLIENT_SLAVE;
1496 }
1497
Jean Delvare99cd8e22009-06-19 16:58:20 +02001498 client = i2c_new_device(adap, &info);
1499 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001500 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001501
1502 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001503 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001504 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001505 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001506 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1507 info.type, info.addr);
1508
1509 return count;
1510}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001511static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001512
1513/*
1514 * And of course let the users delete the devices they instantiated, if
1515 * they got it wrong. This interface can only be used to delete devices
1516 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1517 * don't delete devices to which some kernel code still has references.
1518 *
1519 * Parameter checking may look overzealous, but we really don't want
1520 * the user to delete the wrong device.
1521 */
1522static ssize_t
1523i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1524 const char *buf, size_t count)
1525{
1526 struct i2c_adapter *adap = to_i2c_adapter(dev);
1527 struct i2c_client *client, *next;
1528 unsigned short addr;
1529 char end;
1530 int res;
1531
1532 /* Parse parameters, reject extra parameters */
1533 res = sscanf(buf, "%hi%c", &addr, &end);
1534 if (res < 1) {
1535 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1536 return -EINVAL;
1537 }
1538 if (res > 1 && end != '\n') {
1539 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1540 return -EINVAL;
1541 }
1542
1543 /* Make sure the device was added through sysfs */
1544 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001545 mutex_lock_nested(&adap->userspace_clients_lock,
1546 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001547 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1548 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001549 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001550 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1551 "delete_device", client->name, client->addr);
1552
1553 list_del(&client->detected);
1554 i2c_unregister_device(client);
1555 res = count;
1556 break;
1557 }
1558 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001559 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001560
1561 if (res < 0)
1562 dev_err(dev, "%s: Can't find device in list\n",
1563 "delete_device");
1564 return res;
1565}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001566static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1567 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001568
1569static struct attribute *i2c_adapter_attrs[] = {
1570 &dev_attr_name.attr,
1571 &dev_attr_new_device.attr,
1572 &dev_attr_delete_device.attr,
1573 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001574};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001575ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001576
Michael Lawnick08263742010-08-11 18:21:02 +02001577struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001578 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001579 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001580};
Michael Lawnick08263742010-08-11 18:21:02 +02001581EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Stephen Warren643dd092012-04-17 12:43:33 -06001583/**
1584 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1585 * @dev: device, probably from some driver model iterator
1586 *
1587 * When traversing the driver model tree, perhaps using driver model
1588 * iterators like @device_for_each_child(), you can't assume very much
1589 * about the nodes you find. Use this function to avoid oopses caused
1590 * by wrongly treating some non-I2C device as an i2c_adapter.
1591 */
1592struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1593{
1594 return (dev->type == &i2c_adapter_type)
1595 ? to_i2c_adapter(dev)
1596 : NULL;
1597}
1598EXPORT_SYMBOL(i2c_verify_adapter);
1599
Jean Delvare2bb50952009-09-18 22:45:46 +02001600#ifdef CONFIG_I2C_COMPAT
1601static struct class_compat *i2c_adapter_compat_class;
1602#endif
1603
David Brownell9c1600e2007-05-01 23:26:31 +02001604static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1605{
1606 struct i2c_devinfo *devinfo;
1607
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001608 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001609 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1610 if (devinfo->busnum == adapter->nr
1611 && !i2c_new_device(adapter,
1612 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001613 dev_err(&adapter->dev,
1614 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001615 devinfo->board_info.addr);
1616 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001617 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001618}
1619
Wolfram Sang687b81d2013-07-11 12:56:15 +01001620/* OF support code */
1621
1622#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001623static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1624 struct device_node *node)
1625{
1626 struct i2c_client *result;
1627 struct i2c_board_info info = {};
1628 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001629 const __be32 *addr_be;
1630 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001631 int len;
1632
1633 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1634
1635 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1636 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1637 node->full_name);
1638 return ERR_PTR(-EINVAL);
1639 }
1640
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001641 addr_be = of_get_property(node, "reg", &len);
1642 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001643 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1644 node->full_name);
1645 return ERR_PTR(-EINVAL);
1646 }
1647
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001648 addr = be32_to_cpup(addr_be);
1649 if (addr & I2C_TEN_BIT_ADDRESS) {
1650 addr &= ~I2C_TEN_BIT_ADDRESS;
1651 info.flags |= I2C_CLIENT_TEN;
1652 }
1653
1654 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1655 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1656 info.flags |= I2C_CLIENT_SLAVE;
1657 }
1658
1659 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001660 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1661 info.addr, node->full_name);
1662 return ERR_PTR(-EINVAL);
1663 }
1664
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001665 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001666 info.of_node = of_node_get(node);
1667 info.archdata = &dev_ad;
1668
1669 if (of_get_property(node, "wakeup-source", NULL))
1670 info.flags |= I2C_CLIENT_WAKE;
1671
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001672 result = i2c_new_device(adap, &info);
1673 if (result == NULL) {
1674 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1675 node->full_name);
1676 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001677 return ERR_PTR(-EINVAL);
1678 }
1679 return result;
1680}
1681
Wolfram Sang687b81d2013-07-11 12:56:15 +01001682static void of_i2c_register_devices(struct i2c_adapter *adap)
1683{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001684 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001685 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001686
1687 /* Only register child devices if the adapter has a node pointer set */
1688 if (!adap->dev.of_node)
1689 return;
1690
1691 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1692
Jon Hunter7e4c2242016-06-29 10:17:53 +01001693 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1694 if (!bus)
1695 bus = of_node_get(adap->dev.of_node);
1696
1697 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001698 if (of_node_test_and_set_flag(node, OF_POPULATED))
1699 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001700
1701 client = of_i2c_register_device(adap, node);
1702 if (IS_ERR(client)) {
1703 dev_warn(&adap->dev,
1704 "Failed to create I2C device for %s\n",
1705 node->full_name);
1706 of_node_clear_flag(node, OF_POPULATED);
1707 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001708 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001709
1710 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001711}
1712
1713static int of_dev_node_match(struct device *dev, void *data)
1714{
1715 return dev->of_node == data;
1716}
1717
1718/* must call put_device() when done with returned i2c_client device */
1719struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1720{
1721 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001722 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001723
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001724 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001725 if (!dev)
1726 return NULL;
1727
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001728 client = i2c_verify_client(dev);
1729 if (!client)
1730 put_device(dev);
1731
1732 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001733}
1734EXPORT_SYMBOL(of_find_i2c_device_by_node);
1735
1736/* must call put_device() when done with returned i2c_adapter device */
1737struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1738{
1739 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001740 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001741
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001742 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001743 if (!dev)
1744 return NULL;
1745
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001746 adapter = i2c_verify_adapter(dev);
1747 if (!adapter)
1748 put_device(dev);
1749
1750 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001751}
1752EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001753
1754/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1755struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1756{
1757 struct i2c_adapter *adapter;
1758
1759 adapter = of_find_i2c_adapter_by_node(node);
1760 if (!adapter)
1761 return NULL;
1762
1763 if (!try_module_get(adapter->owner)) {
1764 put_device(&adapter->dev);
1765 adapter = NULL;
1766 }
1767
1768 return adapter;
1769}
1770EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001771#else
1772static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1773#endif /* CONFIG_OF */
1774
Jean Delvare69b00892009-12-06 17:06:27 +01001775static int i2c_do_add_adapter(struct i2c_driver *driver,
1776 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001777{
Jean Delvare4735c982008-07-14 22:38:36 +02001778 /* Detect supported devices on that bus, and instantiate them */
1779 i2c_detect(adap, driver);
1780
1781 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001782 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001783 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1784 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001785 dev_warn(&adap->dev,
1786 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001787 /* We ignore the return code; if it fails, too bad */
1788 driver->attach_adapter(adap);
1789 }
1790 return 0;
1791}
1792
Jean Delvare69b00892009-12-06 17:06:27 +01001793static int __process_new_adapter(struct device_driver *d, void *data)
1794{
1795 return i2c_do_add_adapter(to_i2c_driver(d), data);
1796}
1797
Peter Rosind1ed7982016-08-25 23:07:01 +02001798static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1799 .lock_bus = i2c_adapter_lock_bus,
1800 .trylock_bus = i2c_adapter_trylock_bus,
1801 .unlock_bus = i2c_adapter_unlock_bus,
1802};
1803
David Brownell6e13e642007-05-01 23:26:31 +02001804static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001806 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
David Brownell1d0b19c2008-10-14 17:30:05 +02001808 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301809 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001810 res = -EAGAIN;
1811 goto out_list;
1812 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001813
Jean Delvare2236baa2010-11-15 22:40:38 +01001814 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001815 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001816 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001817
1818 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001819 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001820 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001821 }
1822
Peter Rosind1ed7982016-08-25 23:07:01 +02001823 if (!adap->lock_ops)
1824 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001825
Mika Kuoppala194684e2009-12-06 17:06:22 +01001826 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001827 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001828 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001829 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Jean Delvare8fcfef62009-03-28 21:34:43 +01001831 /* Set default timeout to 1 second if not already set */
1832 if (adap->timeout == 0)
1833 adap->timeout = HZ;
1834
Kay Sievers27d9c182009-01-07 14:29:16 +01001835 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001836 adap->dev.bus = &i2c_bus_type;
1837 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001838 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001839 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001840 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001841 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001844 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1845
Charles Keepax6ada5c12015-04-16 13:05:19 +01001846 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001847 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001848 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001849
Jean Delvare2bb50952009-09-18 22:45:46 +02001850#ifdef CONFIG_I2C_COMPAT
1851 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1852 adap->dev.parent);
1853 if (res)
1854 dev_warn(&adap->dev,
1855 "Failed to create compatibility class link\n");
1856#endif
1857
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001858 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301859
Jean Delvare729d6dd2009-06-19 16:58:18 +02001860 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001861 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001862 i2c_acpi_register_devices(adap);
1863 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001864
David Brownell6e13e642007-05-01 23:26:31 +02001865 if (adap->nr < __i2c_first_dynamic_bus_num)
1866 i2c_scan_static_board_info(adap);
1867
Jean Delvare4735c982008-07-14 22:38:36 +02001868 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001869 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001870 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001871 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001872
1873 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001874
Jean Delvareb119c6c2006-08-15 18:26:30 +02001875out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001876 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001877 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001878 mutex_unlock(&core_lock);
1879 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
David Brownell6e13e642007-05-01 23:26:31 +02001882/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001883 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1884 * @adap: the adapter to register (with adap->nr initialized)
1885 * Context: can sleep
1886 *
1887 * See i2c_add_numbered_adapter() for details.
1888 */
1889static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1890{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001891 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001892
1893 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001894 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001895 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001896 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001897 return id == -ENOSPC ? -EBUSY : id;
1898
1899 return i2c_register_adapter(adap);
1900}
1901
1902/**
David Brownell6e13e642007-05-01 23:26:31 +02001903 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1904 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001905 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001906 *
1907 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001908 * doesn't matter or when its bus number is specified by an dt alias.
1909 * Examples of bases when the bus number doesn't matter: I2C adapters
1910 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001911 *
1912 * When this returns zero, a new bus number was allocated and stored
1913 * in adap->nr, and the specified adapter became available for clients.
1914 * Otherwise, a negative errno value is returned.
1915 */
1916int i2c_add_adapter(struct i2c_adapter *adapter)
1917{
Doug Andersonee5c2742013-03-01 08:57:31 -08001918 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001919 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001920
Doug Andersonee5c2742013-03-01 08:57:31 -08001921 if (dev->of_node) {
1922 id = of_alias_get_id(dev->of_node, "i2c");
1923 if (id >= 0) {
1924 adapter->nr = id;
1925 return __i2c_add_numbered_adapter(adapter);
1926 }
1927 }
1928
Jean Delvarecaada322008-01-27 18:14:49 +01001929 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001930 id = idr_alloc(&i2c_adapter_idr, adapter,
1931 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001932 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001933 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001934 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001935
1936 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001937
David Brownell6e13e642007-05-01 23:26:31 +02001938 return i2c_register_adapter(adapter);
1939}
1940EXPORT_SYMBOL(i2c_add_adapter);
1941
1942/**
1943 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1944 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001945 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001946 *
1947 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001948 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1949 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001950 * is used to properly configure I2C devices.
1951 *
Grant Likely488bf312011-07-25 17:49:43 +02001952 * If the requested bus number is set to -1, then this function will behave
1953 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1954 *
David Brownell6e13e642007-05-01 23:26:31 +02001955 * If no devices have pre-been declared for this bus, then be sure to
1956 * register the adapter before any dynamically allocated ones. Otherwise
1957 * the required bus ID may not be available.
1958 *
1959 * When this returns zero, the specified adapter became available for
1960 * clients using the bus number provided in adap->nr. Also, the table
1961 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1962 * and the appropriate driver model device nodes are created. Otherwise, a
1963 * negative errno value is returned.
1964 */
1965int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1966{
Grant Likely488bf312011-07-25 17:49:43 +02001967 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1968 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001969
Doug Andersonee5c2742013-03-01 08:57:31 -08001970 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001971}
1972EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1973
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001974static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001975 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001976{
Jean Delvare4735c982008-07-14 22:38:36 +02001977 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001978
Jean Delvareacec2112009-03-28 21:34:40 +01001979 /* Remove the devices we created ourselves as the result of hardware
1980 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001981 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1982 if (client->adapter == adapter) {
1983 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1984 client->name, client->addr);
1985 list_del(&client->detected);
1986 i2c_unregister_device(client);
1987 }
1988 }
Jean Delvare026526f2008-01-27 18:14:49 +01001989}
1990
Jean Delvaree549c2b2009-06-19 16:58:19 +02001991static int __unregister_client(struct device *dev, void *dummy)
1992{
1993 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001994 if (client && strcmp(client->name, "dummy"))
1995 i2c_unregister_device(client);
1996 return 0;
1997}
1998
1999static int __unregister_dummy(struct device *dev, void *dummy)
2000{
2001 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002002 if (client)
2003 i2c_unregister_device(client);
2004 return 0;
2005}
2006
Jean Delvare69b00892009-12-06 17:06:27 +01002007static int __process_removed_adapter(struct device_driver *d, void *data)
2008{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002009 i2c_do_del_adapter(to_i2c_driver(d), data);
2010 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002011}
2012
David Brownelld64f73b2007-07-12 14:12:28 +02002013/**
2014 * i2c_del_adapter - unregister I2C adapter
2015 * @adap: the adapter being unregistered
2016 * Context: can sleep
2017 *
2018 * This unregisters an I2C adapter which was previously registered
2019 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2020 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002021void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002023 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002024 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002027 mutex_lock(&core_lock);
2028 found = idr_find(&i2c_adapter_idr, adap->nr);
2029 mutex_unlock(&core_lock);
2030 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002031 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002035 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002036 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002037 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002038 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002039 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002040 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002042 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002043 mutex_lock_nested(&adap->userspace_clients_lock,
2044 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002045 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2046 detected) {
2047 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2048 client->addr);
2049 list_del(&client->detected);
2050 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002051 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002052 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002053
Jean Delvaree549c2b2009-06-19 16:58:19 +02002054 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002055 * check the returned value. This is a two-pass process, because
2056 * we can't remove the dummy devices during the first pass: they
2057 * could have been instantiated by real devices wishing to clean
2058 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002059 device_for_each_child(&adap->dev, NULL, __unregister_client);
2060 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Jean Delvare2bb50952009-09-18 22:45:46 +02002062#ifdef CONFIG_I2C_COMPAT
2063 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2064 adap->dev.parent);
2065#endif
2066
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002067 /* device name is gone after device_unregister */
2068 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2069
Wolfram Sang9f924162015-12-23 18:19:28 +01002070 pm_runtime_disable(&adap->dev);
2071
Wolfram Sang26680ee2015-01-29 22:45:09 +01002072 /* wait until all references to the device are gone
2073 *
2074 * FIXME: This is old code and should ideally be replaced by an
2075 * alternative which results in decoupling the lifetime of the struct
2076 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302077 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
David Brownell6e13e642007-05-01 23:26:31 +02002083 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002084 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002086 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002088 /* Clear the device structure in case this adapter is ever going to be
2089 added again */
2090 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
David Brownellc0564602007-05-01 23:26:31 +02002092EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Wolfram Sang54177cc2015-12-17 13:32:36 +01002094/**
2095 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2096 * @dev: The device to scan for I2C timing properties
2097 * @t: the i2c_timings struct to be filled with values
2098 * @use_defaults: bool to use sane defaults derived from the I2C specification
2099 * when properties are not found, otherwise use 0
2100 *
2101 * Scan the device for the generic I2C properties describing timing parameters
2102 * for the signal and fill the given struct with the results. If a property was
2103 * not found and use_defaults was true, then maximum timings are assumed which
2104 * are derived from the I2C specification. If use_defaults is not used, the
2105 * results will be 0, so drivers can apply their own defaults later. The latter
2106 * is mainly intended for avoiding regressions of existing drivers which want
2107 * to switch to this function. New drivers almost always should use the defaults.
2108 */
2109
2110void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2111{
2112 int ret;
2113
2114 memset(t, 0, sizeof(*t));
2115
2116 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2117 if (ret && use_defaults)
2118 t->bus_freq_hz = 100000;
2119
2120 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2121 if (ret && use_defaults) {
2122 if (t->bus_freq_hz <= 100000)
2123 t->scl_rise_ns = 1000;
2124 else if (t->bus_freq_hz <= 400000)
2125 t->scl_rise_ns = 300;
2126 else
2127 t->scl_rise_ns = 120;
2128 }
2129
2130 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2131 if (ret && use_defaults) {
2132 if (t->bus_freq_hz <= 400000)
2133 t->scl_fall_ns = 300;
2134 else
2135 t->scl_fall_ns = 120;
2136 }
2137
2138 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2139
2140 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2141 if (ret && use_defaults)
2142 t->sda_fall_ns = t->scl_fall_ns;
2143}
2144EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2145
David Brownell7b4fbc52007-05-01 23:26:30 +02002146/* ------------------------------------------------------------------------- */
2147
Jean Delvare7ae31482011-03-20 14:50:52 +01002148int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2149{
2150 int res;
2151
2152 mutex_lock(&core_lock);
2153 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2154 mutex_unlock(&core_lock);
2155
2156 return res;
2157}
2158EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2159
Jean Delvare69b00892009-12-06 17:06:27 +01002160static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002161{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002162 if (dev->type != &i2c_adapter_type)
2163 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002164 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002165}
2166
David Brownell7b4fbc52007-05-01 23:26:30 +02002167/*
2168 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002169 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
2171
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002172int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002174 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
David Brownell1d0b19c2008-10-14 17:30:05 +02002176 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302177 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002178 return -EAGAIN;
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002181 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002183 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Jean Delvare729d6dd2009-06-19 16:58:18 +02002185 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002186 * will have called probe() for all matching-but-unbound devices.
2187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 res = driver_register(&driver->driver);
2189 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002190 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002191
Wolfram Sang44239a52016-07-09 13:35:04 +09002192 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Jean Delvare4735c982008-07-14 22:38:36 +02002194 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002195 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002196
Jean Delvare7eebcb72006-02-05 23:28:21 +01002197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002199EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jean Delvare69b00892009-12-06 17:06:27 +01002201static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002202{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002203 if (dev->type == &i2c_adapter_type)
2204 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2205 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002206}
2207
David Brownella1d9e6e2007-05-01 23:26:30 +02002208/**
2209 * i2c_del_driver - unregister I2C driver
2210 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002211 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002212 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002213void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Jean Delvare7ae31482011-03-20 14:50:52 +01002215 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002218 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
David Brownellc0564602007-05-01 23:26:31 +02002220EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
David Brownell7b4fbc52007-05-01 23:26:30 +02002222/* ------------------------------------------------------------------------- */
2223
Jean Delvaree48d3312008-01-27 18:14:48 +01002224/**
2225 * i2c_use_client - increments the reference count of the i2c client structure
2226 * @client: the client being referenced
2227 *
2228 * Each live reference to a client should be refcounted. The driver model does
2229 * that automatically as part of driver binding, so that most drivers don't
2230 * need to do this explicitly: they hold a reference until they're unbound
2231 * from the device.
2232 *
2233 * A pointer to the client with the incremented reference counter is returned.
2234 */
2235struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
David Brownell6ea438e2008-07-14 22:38:24 +02002237 if (client && get_device(&client->dev))
2238 return client;
2239 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
David Brownellc0564602007-05-01 23:26:31 +02002241EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Jean Delvaree48d3312008-01-27 18:14:48 +01002243/**
2244 * i2c_release_client - release a use of the i2c client structure
2245 * @client: the client being no longer referenced
2246 *
2247 * Must be called when a user of a client is finished with it.
2248 */
2249void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
David Brownell6ea438e2008-07-14 22:38:24 +02002251 if (client)
2252 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
David Brownellc0564602007-05-01 23:26:31 +02002254EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
David Brownell9b766b82008-01-27 18:14:51 +01002256struct i2c_cmd_arg {
2257 unsigned cmd;
2258 void *arg;
2259};
2260
2261static int i2c_cmd(struct device *dev, void *_arg)
2262{
2263 struct i2c_client *client = i2c_verify_client(dev);
2264 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002265 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002266
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002267 if (!client || !client->dev.driver)
2268 return 0;
2269
2270 driver = to_i2c_driver(client->dev.driver);
2271 if (driver->command)
2272 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002273 return 0;
2274}
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2277{
David Brownell9b766b82008-01-27 18:14:51 +01002278 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
David Brownell9b766b82008-01-27 18:14:51 +01002280 cmd_arg.cmd = cmd;
2281 cmd_arg.arg = arg;
2282 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
David Brownellc0564602007-05-01 23:26:31 +02002284EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002286#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2287static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2288 void *arg)
2289{
2290 struct of_reconfig_data *rd = arg;
2291 struct i2c_adapter *adap;
2292 struct i2c_client *client;
2293
2294 switch (of_reconfig_get_state_change(action, rd)) {
2295 case OF_RECONFIG_CHANGE_ADD:
2296 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2297 if (adap == NULL)
2298 return NOTIFY_OK; /* not for us */
2299
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002300 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2301 put_device(&adap->dev);
2302 return NOTIFY_OK;
2303 }
2304
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002305 client = of_i2c_register_device(adap, rd->dn);
2306 put_device(&adap->dev);
2307
2308 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002309 dev_err(&adap->dev, "failed to create client for '%s'\n",
2310 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002311 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002312 return notifier_from_errno(PTR_ERR(client));
2313 }
2314 break;
2315 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002316 /* already depopulated? */
2317 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2318 return NOTIFY_OK;
2319
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002320 /* find our device by node */
2321 client = of_find_i2c_device_by_node(rd->dn);
2322 if (client == NULL)
2323 return NOTIFY_OK; /* no? not meant for us */
2324
2325 /* unregister takes one ref away */
2326 i2c_unregister_device(client);
2327
2328 /* and put the reference of the find */
2329 put_device(&client->dev);
2330 break;
2331 }
2332
2333 return NOTIFY_OK;
2334}
2335static struct notifier_block i2c_of_notifier = {
2336 .notifier_call = of_i2c_notify,
2337};
2338#else
2339extern struct notifier_block i2c_of_notifier;
2340#endif /* CONFIG_OF_DYNAMIC */
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342static int __init i2c_init(void)
2343{
2344 int retval;
2345
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002346 retval = of_alias_get_highest_id("i2c");
2347
2348 down_write(&__i2c_board_lock);
2349 if (retval >= __i2c_first_dynamic_bus_num)
2350 __i2c_first_dynamic_bus_num = retval + 1;
2351 up_write(&__i2c_board_lock);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 retval = bus_register(&i2c_bus_type);
2354 if (retval)
2355 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002356
2357 is_registered = true;
2358
Jean Delvare2bb50952009-09-18 22:45:46 +02002359#ifdef CONFIG_I2C_COMPAT
2360 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2361 if (!i2c_adapter_compat_class) {
2362 retval = -ENOMEM;
2363 goto bus_err;
2364 }
2365#endif
David Brownelle9f13732008-01-27 18:14:52 +01002366 retval = i2c_add_driver(&dummy_driver);
2367 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002368 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002369
2370 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2371 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002372 if (IS_ENABLED(CONFIG_ACPI))
2373 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002374
David Brownelle9f13732008-01-27 18:14:52 +01002375 return 0;
2376
Jean Delvare2bb50952009-09-18 22:45:46 +02002377class_err:
2378#ifdef CONFIG_I2C_COMPAT
2379 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002380bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002381#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002382 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002383 bus_unregister(&i2c_bus_type);
2384 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
2387static void __exit i2c_exit(void)
2388{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002389 if (IS_ENABLED(CONFIG_ACPI))
2390 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002391 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2392 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002393 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002394#ifdef CONFIG_I2C_COMPAT
2395 class_compat_unregister(i2c_adapter_compat_class);
2396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002398 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
David Brownella10f9e72008-10-14 17:30:06 +02002401/* We must initialize early, because some subsystems register i2c drivers
2402 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2403 */
2404postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405module_exit(i2c_exit);
2406
2407/* ----------------------------------------------------
2408 * the functional interface to the i2c busses.
2409 * ----------------------------------------------------
2410 */
2411
Wolfram Sangb7f62582015-01-05 23:45:59 +01002412/* Check if val is exceeding the quirk IFF quirk is non 0 */
2413#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2414
2415static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2416{
2417 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2418 err_msg, msg->addr, msg->len,
2419 msg->flags & I2C_M_RD ? "read" : "write");
2420 return -EOPNOTSUPP;
2421}
2422
2423static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2424{
2425 const struct i2c_adapter_quirks *q = adap->quirks;
2426 int max_num = q->max_num_msgs, i;
2427 bool do_len_check = true;
2428
2429 if (q->flags & I2C_AQ_COMB) {
2430 max_num = 2;
2431
2432 /* special checks for combined messages */
2433 if (num == 2) {
2434 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2435 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2436
2437 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2438 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2439
2440 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2441 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2442
2443 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2444 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2445
2446 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2447 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2448
2449 do_len_check = false;
2450 }
2451 }
2452
2453 if (i2c_quirk_exceeded(num, max_num))
2454 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2455
2456 for (i = 0; i < num; i++) {
2457 u16 len = msgs[i].len;
2458
2459 if (msgs[i].flags & I2C_M_RD) {
2460 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2461 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2462 } else {
2463 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2464 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2465 }
2466 }
2467
2468 return 0;
2469}
2470
David Brownella1cdeda2008-07-14 22:38:24 +02002471/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002472 * __i2c_transfer - unlocked flavor of i2c_transfer
2473 * @adap: Handle to I2C bus
2474 * @msgs: One or more messages to execute before STOP is issued to
2475 * terminate the operation; each message begins with a START.
2476 * @num: Number of messages to be executed.
2477 *
2478 * Returns negative errno, else the number of messages executed.
2479 *
2480 * Adapter lock must be held when calling this function. No debug logging
2481 * takes place. adap->algo->master_xfer existence isn't checked.
2482 */
2483int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2484{
2485 unsigned long orig_jiffies;
2486 int ret, try;
2487
Wolfram Sangb7f62582015-01-05 23:45:59 +01002488 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2489 return -EOPNOTSUPP;
2490
David Howellsd9a83d62014-03-06 13:35:59 +00002491 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2492 * enabled. This is an efficient way of keeping the for-loop from
2493 * being executed when not needed.
2494 */
2495 if (static_key_false(&i2c_trace_msg)) {
2496 int i;
2497 for (i = 0; i < num; i++)
2498 if (msgs[i].flags & I2C_M_RD)
2499 trace_i2c_read(adap, &msgs[i], i);
2500 else
2501 trace_i2c_write(adap, &msgs[i], i);
2502 }
2503
Jean Delvareb37d2a32012-06-29 07:47:19 -03002504 /* Retry automatically on arbitration loss */
2505 orig_jiffies = jiffies;
2506 for (ret = 0, try = 0; try <= adap->retries; try++) {
2507 ret = adap->algo->master_xfer(adap, msgs, num);
2508 if (ret != -EAGAIN)
2509 break;
2510 if (time_after(jiffies, orig_jiffies + adap->timeout))
2511 break;
2512 }
2513
David Howellsd9a83d62014-03-06 13:35:59 +00002514 if (static_key_false(&i2c_trace_msg)) {
2515 int i;
2516 for (i = 0; i < ret; i++)
2517 if (msgs[i].flags & I2C_M_RD)
2518 trace_i2c_reply(adap, &msgs[i], i);
2519 trace_i2c_result(adap, i, ret);
2520 }
2521
Jean Delvareb37d2a32012-06-29 07:47:19 -03002522 return ret;
2523}
2524EXPORT_SYMBOL(__i2c_transfer);
2525
2526/**
David Brownella1cdeda2008-07-14 22:38:24 +02002527 * i2c_transfer - execute a single or combined I2C message
2528 * @adap: Handle to I2C bus
2529 * @msgs: One or more messages to execute before STOP is issued to
2530 * terminate the operation; each message begins with a START.
2531 * @num: Number of messages to be executed.
2532 *
2533 * Returns negative errno, else the number of messages executed.
2534 *
2535 * Note that there is no requirement that each message be sent to
2536 * the same slave address, although that is the most common model.
2537 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002538int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002540 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
David Brownella1cdeda2008-07-14 22:38:24 +02002542 /* REVISIT the fault reporting model here is weak:
2543 *
2544 * - When we get an error after receiving N bytes from a slave,
2545 * there is no way to report "N".
2546 *
2547 * - When we get a NAK after transmitting N bytes to a slave,
2548 * there is no way to report "N" ... or to let the master
2549 * continue executing the rest of this combined message, if
2550 * that's the appropriate response.
2551 *
2552 * - When for example "num" is two and we successfully complete
2553 * the first message but get an error part way through the
2554 * second, it's unclear whether that should be reported as
2555 * one (discarding status on the second message) or errno
2556 * (discarding status on the first one).
2557 */
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (adap->algo->master_xfer) {
2560#ifdef DEBUG
2561 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002562 dev_dbg(&adap->dev,
2563 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2564 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2565 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002566 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568#endif
2569
Mike Rapoportcea443a82008-01-27 18:14:50 +01002570 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002571 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002572 if (!ret)
2573 /* I2C activity is ongoing. */
2574 return -EAGAIN;
2575 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002576 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002577 }
2578
Jean Delvareb37d2a32012-06-29 07:47:19 -03002579 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002580 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 return ret;
2583 } else {
2584 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002585 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587}
David Brownellc0564602007-05-01 23:26:31 +02002588EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
David Brownella1cdeda2008-07-14 22:38:24 +02002590/**
2591 * i2c_master_send - issue a single I2C message in master transmit mode
2592 * @client: Handle to slave device
2593 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002594 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002595 *
2596 * Returns negative errno, or else the number of bytes written.
2597 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002598int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002601 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct i2c_msg msg;
2603
Jean Delvare815f55f2005-05-07 22:58:46 +02002604 msg.addr = client->addr;
2605 msg.flags = client->flags & I2C_M_TEN;
2606 msg.len = count;
2607 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002608
Jean Delvare815f55f2005-05-07 22:58:46 +02002609 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002611 /*
2612 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2613 * transmitted, else error code.
2614 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002615 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
David Brownellc0564602007-05-01 23:26:31 +02002617EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
David Brownella1cdeda2008-07-14 22:38:24 +02002619/**
2620 * i2c_master_recv - issue a single I2C message in master receive mode
2621 * @client: Handle to slave device
2622 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002623 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002624 *
2625 * Returns negative errno, or else the number of bytes read.
2626 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002627int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Farid Hammane7225acf2010-05-21 18:40:58 +02002629 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct i2c_msg msg;
2631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Jean Delvare815f55f2005-05-07 22:58:46 +02002633 msg.addr = client->addr;
2634 msg.flags = client->flags & I2C_M_TEN;
2635 msg.flags |= I2C_M_RD;
2636 msg.len = count;
2637 msg.buf = buf;
2638
2639 ret = i2c_transfer(adap, &msg, 1);
2640
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002641 /*
2642 * If everything went ok (i.e. 1 msg received), return #bytes received,
2643 * else error code.
2644 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002645 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
David Brownellc0564602007-05-01 23:26:31 +02002647EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649/* ----------------------------------------------------
2650 * the i2c address scanning function
2651 * Will not work for 10-bit addresses!
2652 * ----------------------------------------------------
2653 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002654
Jean Delvare63e4e802010-06-03 11:33:51 +02002655/*
2656 * Legacy default probe function, mostly relevant for SMBus. The default
2657 * probe method is a quick write, but it is known to corrupt the 24RF08
2658 * EEPROMs due to a state machine bug, and could also irreversibly
2659 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2660 * we use a short byte read instead. Also, some bus drivers don't implement
2661 * quick write, so we fallback to a byte read in that case too.
2662 * On x86, there is another special case for FSC hardware monitoring chips,
2663 * which want regular byte reads (address 0x73.) Fortunately, these are the
2664 * only known chips using this I2C address on PC hardware.
2665 * Returns 1 if probe succeeded, 0 if not.
2666 */
2667static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2668{
2669 int err;
2670 union i2c_smbus_data dummy;
2671
2672#ifdef CONFIG_X86
2673 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2674 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2675 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2676 I2C_SMBUS_BYTE_DATA, &dummy);
2677 else
2678#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002679 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2680 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002681 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2682 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002683 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2684 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2685 I2C_SMBUS_BYTE, &dummy);
2686 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002687 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2688 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002689 err = -EOPNOTSUPP;
2690 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002691
2692 return err >= 0;
2693}
2694
Jean Delvareccfbbd02009-12-06 17:06:25 +01002695static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002696 struct i2c_driver *driver)
2697{
2698 struct i2c_board_info info;
2699 struct i2c_adapter *adapter = temp_client->adapter;
2700 int addr = temp_client->addr;
2701 int err;
2702
2703 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002704 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002705 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002706 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2707 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002708 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002709 }
2710
Wolfram Sang9bccc702015-07-17 14:48:56 +02002711 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002712 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002713 return 0;
2714
Jean Delvareccfbbd02009-12-06 17:06:25 +01002715 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002716 if (!i2c_default_probe(adapter, addr))
2717 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002718
2719 /* Finally call the custom detection function */
2720 memset(&info, 0, sizeof(struct i2c_board_info));
2721 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002722 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002723 if (err) {
2724 /* -ENODEV is returned if the detection fails. We catch it
2725 here as this isn't an error. */
2726 return err == -ENODEV ? 0 : err;
2727 }
2728
2729 /* Consistency check */
2730 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002731 dev_err(&adapter->dev,
2732 "%s detection function provided no name for 0x%x\n",
2733 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002734 } else {
2735 struct i2c_client *client;
2736
2737 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002738 if (adapter->class & I2C_CLASS_DEPRECATED)
2739 dev_warn(&adapter->dev,
2740 "This adapter will soon drop class based instantiation of devices. "
2741 "Please make sure client 0x%02x gets instantiated by other means. "
2742 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2743 info.addr);
2744
Jean Delvare4735c982008-07-14 22:38:36 +02002745 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2746 info.type, info.addr);
2747 client = i2c_new_device(adapter, &info);
2748 if (client)
2749 list_add_tail(&client->detected, &driver->clients);
2750 else
2751 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2752 info.type, info.addr);
2753 }
2754 return 0;
2755}
2756
2757static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2758{
Jean Delvarec3813d62009-12-14 21:17:25 +01002759 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002760 struct i2c_client *temp_client;
2761 int i, err = 0;
2762 int adap_id = i2c_adapter_id(adapter);
2763
Jean Delvarec3813d62009-12-14 21:17:25 +01002764 address_list = driver->address_list;
2765 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002766 return 0;
2767
Wolfram Sang45552272014-07-10 13:46:21 +02002768 /* Warn that the adapter lost class based instantiation */
2769 if (adapter->class == I2C_CLASS_DEPRECATED) {
2770 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002771 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2772 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002773 driver->driver.name);
2774 return 0;
2775 }
2776
Jean Delvare51b54ba2010-10-24 18:16:58 +02002777 /* Stop here if the classes do not match */
2778 if (!(adapter->class & driver->class))
2779 return 0;
2780
Jean Delvare4735c982008-07-14 22:38:36 +02002781 /* Set up a temporary client to help detect callback */
2782 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2783 if (!temp_client)
2784 return -ENOMEM;
2785 temp_client->adapter = adapter;
2786
Jean Delvarec3813d62009-12-14 21:17:25 +01002787 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002788 dev_dbg(&adapter->dev,
2789 "found normal entry for adapter %d, addr 0x%02x\n",
2790 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002791 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002792 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002793 if (unlikely(err))
2794 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002795 }
2796
Jean Delvare4735c982008-07-14 22:38:36 +02002797 kfree(temp_client);
2798 return err;
2799}
2800
Jean Delvared44f19d2010-08-11 18:20:57 +02002801int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2802{
2803 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2804 I2C_SMBUS_QUICK, NULL) >= 0;
2805}
2806EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2807
Jean Delvare12b5053a2007-05-01 23:26:31 +02002808struct i2c_client *
2809i2c_new_probed_device(struct i2c_adapter *adap,
2810 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002811 unsigned short const *addr_list,
2812 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002813{
2814 int i;
2815
Jean Delvare8031d792010-08-11 18:21:00 +02002816 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002817 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002818
Jean Delvare12b5053a2007-05-01 23:26:31 +02002819 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2820 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002821 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002822 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2823 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002824 continue;
2825 }
2826
Wolfram Sang9bccc702015-07-17 14:48:56 +02002827 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002828 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002829 dev_dbg(&adap->dev,
2830 "Address 0x%02x already in use, not probing\n",
2831 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002832 continue;
2833 }
2834
Jean Delvare63e4e802010-06-03 11:33:51 +02002835 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002836 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002837 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002838 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002839
2840 if (addr_list[i] == I2C_CLIENT_END) {
2841 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2842 return NULL;
2843 }
2844
2845 info->addr = addr_list[i];
2846 return i2c_new_device(adap, info);
2847}
2848EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2849
Jean Delvared735b342011-03-20 14:50:52 +01002850struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002853
Jean Delvarecaada322008-01-27 18:14:49 +01002854 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002855 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002856 if (!adapter)
2857 goto exit;
2858
2859 if (try_module_get(adapter->owner))
2860 get_device(&adapter->dev);
2861 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002862 adapter = NULL;
2863
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002864 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002865 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002866 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
David Brownellc0564602007-05-01 23:26:31 +02002868EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870void i2c_put_adapter(struct i2c_adapter *adap)
2871{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002872 if (!adap)
2873 return;
2874
2875 put_device(&adap->dev);
2876 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
David Brownellc0564602007-05-01 23:26:31 +02002878EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880/* The SMBus parts */
2881
David Brownell438d6c22006-12-10 21:21:31 +01002882#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002883static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002886
Farid Hammane7225acf2010-05-21 18:40:58 +02002887 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002888 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 data = data ^ POLY;
2890 data = data << 1;
2891 }
2892 return (u8)(data >> 8);
2893}
2894
Jean Delvare421ef472005-10-26 21:28:55 +02002895/* Incremental CRC8 over count bytes in the array pointed to by p */
2896static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 int i;
2899
Farid Hammane7225acf2010-05-21 18:40:58 +02002900 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002901 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return crc;
2903}
2904
Jean Delvare421ef472005-10-26 21:28:55 +02002905/* Assume a 7-bit address, which is reasonable for SMBus */
2906static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Jean Delvare421ef472005-10-26 21:28:55 +02002908 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002909 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002910 pec = i2c_smbus_pec(pec, &addr, 1);
2911
2912 /* The data buffer follows */
2913 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
Jean Delvare421ef472005-10-26 21:28:55 +02002916/* Used for write only transactions */
2917static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Jean Delvare421ef472005-10-26 21:28:55 +02002919 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2920 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Jean Delvare421ef472005-10-26 21:28:55 +02002923/* Return <0 on CRC error
2924 If there was a write before this read (most cases) we need to take the
2925 partial CRC from the write part into account.
2926 Note that this function does modify the message (we need to decrease the
2927 message length to hide the CRC byte from the caller). */
2928static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Jean Delvare421ef472005-10-26 21:28:55 +02002930 u8 rpec = msg->buf[--msg->len];
2931 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002934 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002936 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
David Brownell438d6c22006-12-10 21:21:31 +01002938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
David Brownella1cdeda2008-07-14 22:38:24 +02002941/**
2942 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2943 * @client: Handle to slave device
2944 *
2945 * This executes the SMBus "receive byte" protocol, returning negative errno
2946 * else the byte received from the device.
2947 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002948s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002951 int status;
2952
2953 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2954 I2C_SMBUS_READ, 0,
2955 I2C_SMBUS_BYTE, &data);
2956 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
David Brownellc0564602007-05-01 23:26:31 +02002958EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
David Brownella1cdeda2008-07-14 22:38:24 +02002960/**
2961 * i2c_smbus_write_byte - SMBus "send byte" protocol
2962 * @client: Handle to slave device
2963 * @value: Byte to be sent
2964 *
2965 * This executes the SMBus "send byte" protocol, returning negative errno
2966 * else zero on success.
2967 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002968s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Farid Hammane7225acf2010-05-21 18:40:58 +02002970 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002971 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
David Brownellc0564602007-05-01 23:26:31 +02002973EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
David Brownella1cdeda2008-07-14 22:38:24 +02002975/**
2976 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2977 * @client: Handle to slave device
2978 * @command: Byte interpreted by slave
2979 *
2980 * This executes the SMBus "read byte" protocol, returning negative errno
2981 * else a data byte received from the device.
2982 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002983s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
2985 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002986 int status;
2987
2988 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2989 I2C_SMBUS_READ, command,
2990 I2C_SMBUS_BYTE_DATA, &data);
2991 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
David Brownellc0564602007-05-01 23:26:31 +02002993EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
David Brownella1cdeda2008-07-14 22:38:24 +02002995/**
2996 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2997 * @client: Handle to slave device
2998 * @command: Byte interpreted by slave
2999 * @value: Byte being written
3000 *
3001 * This executes the SMBus "write byte" protocol, returning negative errno
3002 * else zero on success.
3003 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003004s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3005 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 union i2c_smbus_data data;
3008 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003009 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3010 I2C_SMBUS_WRITE, command,
3011 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
David Brownellc0564602007-05-01 23:26:31 +02003013EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
David Brownella1cdeda2008-07-14 22:38:24 +02003015/**
3016 * i2c_smbus_read_word_data - SMBus "read word" protocol
3017 * @client: Handle to slave device
3018 * @command: Byte interpreted by slave
3019 *
3020 * This executes the SMBus "read word" protocol, returning negative errno
3021 * else a 16-bit unsigned "word" received from the device.
3022 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003023s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
3025 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003026 int status;
3027
3028 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3029 I2C_SMBUS_READ, command,
3030 I2C_SMBUS_WORD_DATA, &data);
3031 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
David Brownellc0564602007-05-01 23:26:31 +02003033EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
David Brownella1cdeda2008-07-14 22:38:24 +02003035/**
3036 * i2c_smbus_write_word_data - SMBus "write word" protocol
3037 * @client: Handle to slave device
3038 * @command: Byte interpreted by slave
3039 * @value: 16-bit "word" being written
3040 *
3041 * This executes the SMBus "write word" protocol, returning negative errno
3042 * else zero on success.
3043 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003044s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3045 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
3047 union i2c_smbus_data data;
3048 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003049 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3050 I2C_SMBUS_WRITE, command,
3051 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
David Brownellc0564602007-05-01 23:26:31 +02003053EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
David Brownella64ec072007-10-13 23:56:31 +02003055/**
David Brownella1cdeda2008-07-14 22:38:24 +02003056 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003057 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003058 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003059 * @values: Byte array into which data will be read; big enough to hold
3060 * the data returned by the slave. SMBus allows at most 32 bytes.
3061 *
David Brownella1cdeda2008-07-14 22:38:24 +02003062 * This executes the SMBus "block read" protocol, returning negative errno
3063 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003064 *
3065 * Note that using this function requires that the client's adapter support
3066 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3067 * support this; its emulation through I2C messaging relies on a specific
3068 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3069 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003070s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003071 u8 *values)
3072{
3073 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003074 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003075
David Brownell24a5bb72008-07-14 22:38:23 +02003076 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3077 I2C_SMBUS_READ, command,
3078 I2C_SMBUS_BLOCK_DATA, &data);
3079 if (status)
3080 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003081
3082 memcpy(values, &data.block[1], data.block[0]);
3083 return data.block[0];
3084}
3085EXPORT_SYMBOL(i2c_smbus_read_block_data);
3086
David Brownella1cdeda2008-07-14 22:38:24 +02003087/**
3088 * i2c_smbus_write_block_data - SMBus "block write" protocol
3089 * @client: Handle to slave device
3090 * @command: Byte interpreted by slave
3091 * @length: Size of data block; SMBus allows at most 32 bytes
3092 * @values: Byte array which will be written.
3093 *
3094 * This executes the SMBus "block write" protocol, returning negative errno
3095 * else zero on success.
3096 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003097s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003098 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
3100 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (length > I2C_SMBUS_BLOCK_MAX)
3103 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003105 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003106 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3107 I2C_SMBUS_WRITE, command,
3108 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
David Brownellc0564602007-05-01 23:26:31 +02003110EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003113s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003114 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
3116 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003117 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003118
Jean Delvare4b2643d2007-07-12 14:12:29 +02003119 if (length > I2C_SMBUS_BLOCK_MAX)
3120 length = I2C_SMBUS_BLOCK_MAX;
3121 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003122 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3123 I2C_SMBUS_READ, command,
3124 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3125 if (status < 0)
3126 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003127
3128 memcpy(values, &data.block[1], data.block[0]);
3129 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
David Brownellc0564602007-05-01 23:26:31 +02003131EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jean Delvare0cc43a12011-01-10 22:11:23 +01003133s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003134 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003135{
3136 union i2c_smbus_data data;
3137
3138 if (length > I2C_SMBUS_BLOCK_MAX)
3139 length = I2C_SMBUS_BLOCK_MAX;
3140 data.block[0] = length;
3141 memcpy(data.block + 1, values, length);
3142 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3143 I2C_SMBUS_WRITE, command,
3144 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3145}
David Brownellc0564602007-05-01 23:26:31 +02003146EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003147
David Brownell438d6c22006-12-10 21:21:31 +01003148/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003150static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3151 unsigned short flags,
3152 char read_write, u8 command, int size,
3153 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 /* So we need to generate a series of msgs. In the case of writing, we
3156 need to use only one message; when reading, we need two. We initialize
3157 most things with sane defaults, to keep the code below somewhat
3158 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003159 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3160 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003161 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003163 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003164 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003165 struct i2c_msg msg[2] = {
3166 {
3167 .addr = addr,
3168 .flags = flags,
3169 .len = 1,
3170 .buf = msgbuf0,
3171 }, {
3172 .addr = addr,
3173 .flags = flags | I2C_M_RD,
3174 .len = 0,
3175 .buf = msgbuf1,
3176 },
3177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003180 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 case I2C_SMBUS_QUICK:
3182 msg[0].len = 0;
3183 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003184 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3185 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 num = 1;
3187 break;
3188 case I2C_SMBUS_BYTE:
3189 if (read_write == I2C_SMBUS_READ) {
3190 /* Special case: only a read! */
3191 msg[0].flags = I2C_M_RD | flags;
3192 num = 1;
3193 }
3194 break;
3195 case I2C_SMBUS_BYTE_DATA:
3196 if (read_write == I2C_SMBUS_READ)
3197 msg[1].len = 1;
3198 else {
3199 msg[0].len = 2;
3200 msgbuf0[1] = data->byte;
3201 }
3202 break;
3203 case I2C_SMBUS_WORD_DATA:
3204 if (read_write == I2C_SMBUS_READ)
3205 msg[1].len = 2;
3206 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003207 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003209 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211 break;
3212 case I2C_SMBUS_PROC_CALL:
3213 num = 2; /* Special case */
3214 read_write = I2C_SMBUS_READ;
3215 msg[0].len = 3;
3216 msg[1].len = 2;
3217 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003218 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 break;
3220 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003222 msg[1].flags |= I2C_M_RECV_LEN;
3223 msg[1].len = 1; /* block length will be added by
3224 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 } else {
3226 msg[0].len = data->block[0] + 2;
3227 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003228 dev_err(&adapter->dev,
3229 "Invalid block write size %d\n",
3230 data->block[0]);
3231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003233 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 msgbuf0[i] = data->block[i-1];
3235 }
3236 break;
3237 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003238 num = 2; /* Another special case */
3239 read_write = I2C_SMBUS_READ;
3240 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003241 dev_err(&adapter->dev,
3242 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003243 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003244 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003245 }
3246 msg[0].len = data->block[0] + 2;
3247 for (i = 1; i < msg[0].len; i++)
3248 msgbuf0[i] = data->block[i-1];
3249 msg[1].flags |= I2C_M_RECV_LEN;
3250 msg[1].len = 1; /* block length will be added by
3251 the underlying bus driver */
3252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 case I2C_SMBUS_I2C_BLOCK_DATA:
3254 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003255 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 } else {
3257 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003258 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003259 dev_err(&adapter->dev,
3260 "Invalid block write size %d\n",
3261 data->block[0]);
3262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
3264 for (i = 1; i <= data->block[0]; i++)
3265 msgbuf0[i] = data->block[i];
3266 }
3267 break;
3268 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003269 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3270 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272
Jean Delvare421ef472005-10-26 21:28:55 +02003273 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3274 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3275 if (i) {
3276 /* Compute PEC if first message is a write */
3277 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003278 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003279 i2c_smbus_add_pec(&msg[0]);
3280 else /* Write followed by read */
3281 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3282 }
3283 /* Ask for PEC if last message is a read */
3284 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003285 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003286 }
3287
David Brownell24a5bb72008-07-14 22:38:23 +02003288 status = i2c_transfer(adapter, msg, num);
3289 if (status < 0)
3290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Jean Delvare421ef472005-10-26 21:28:55 +02003292 /* Check PEC if last message is a read */
3293 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003294 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3295 if (status < 0)
3296 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003297 }
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003300 switch (size) {
3301 case I2C_SMBUS_BYTE:
3302 data->byte = msgbuf0[0];
3303 break;
3304 case I2C_SMBUS_BYTE_DATA:
3305 data->byte = msgbuf1[0];
3306 break;
3307 case I2C_SMBUS_WORD_DATA:
3308 case I2C_SMBUS_PROC_CALL:
3309 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3310 break;
3311 case I2C_SMBUS_I2C_BLOCK_DATA:
3312 for (i = 0; i < data->block[0]; i++)
3313 data->block[i+1] = msgbuf1[i];
3314 break;
3315 case I2C_SMBUS_BLOCK_DATA:
3316 case I2C_SMBUS_BLOCK_PROC_CALL:
3317 for (i = 0; i < msgbuf1[0] + 1; i++)
3318 data->block[i] = msgbuf1[i];
3319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321 return 0;
3322}
3323
David Brownella1cdeda2008-07-14 22:38:24 +02003324/**
3325 * i2c_smbus_xfer - execute SMBus protocol operations
3326 * @adapter: Handle to I2C bus
3327 * @addr: Address of SMBus slave on that bus
3328 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3329 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3330 * @command: Byte interpreted by slave, for protocols which use such bytes
3331 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3332 * @data: Data to be read or written
3333 *
3334 * This executes an SMBus protocol operation, and returns a negative
3335 * errno code else zero on success.
3336 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003337s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003338 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003339 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003341 unsigned long orig_jiffies;
3342 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
David Howells8a325992014-03-06 13:36:06 +00003345 /* If enabled, the following two tracepoints are conditional on
3346 * read_write and protocol.
3347 */
3348 trace_smbus_write(adapter, addr, flags, read_write,
3349 command, protocol, data);
3350 trace_smbus_read(adapter, addr, flags, read_write,
3351 command, protocol);
3352
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003353 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003356 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003357
3358 /* Retry automatically on arbitration loss */
3359 orig_jiffies = jiffies;
3360 for (res = 0, try = 0; try <= adapter->retries; try++) {
3361 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3362 read_write, command,
3363 protocol, data);
3364 if (res != -EAGAIN)
3365 break;
3366 if (time_after(jiffies,
3367 orig_jiffies + adapter->timeout))
3368 break;
3369 }
Peter Rosin8320f492016-05-04 22:15:27 +02003370 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003372 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003373 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003374 /*
3375 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3376 * implement native support for the SMBus operation.
3377 */
3378 }
3379
David Howells8a325992014-03-06 13:36:06 +00003380 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3381 command, protocol, data);
3382
3383trace:
3384 /* If enabled, the reply tracepoint is conditional on read_write. */
3385 trace_smbus_reply(adapter, addr, flags, read_write,
3386 command, protocol, data);
3387 trace_smbus_result(adapter, addr, flags, read_write,
3388 command, protocol, res);
3389
3390 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Irina Tirdea01eef962015-08-12 17:31:33 +03003394/**
3395 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3396 * @client: Handle to slave device
3397 * @command: Byte interpreted by slave
3398 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3399 * @values: Byte array into which data will be read; big enough to hold
3400 * the data returned by the slave. SMBus allows at most
3401 * I2C_SMBUS_BLOCK_MAX bytes.
3402 *
3403 * This executes the SMBus "block read" protocol if supported by the adapter.
3404 * If block read is not supported, it emulates it using either word or byte
3405 * read protocols depending on availability.
3406 *
3407 * The addresses of the I2C slave device that are accessed with this function
3408 * must be mapped to a linear region, so that a block read will have the same
3409 * effect as a byte read. Before using this function you must double-check
3410 * if the I2C slave does support exchanging a block transfer with a byte
3411 * transfer.
3412 */
3413s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3414 u8 command, u8 length, u8 *values)
3415{
3416 u8 i = 0;
3417 int status;
3418
3419 if (length > I2C_SMBUS_BLOCK_MAX)
3420 length = I2C_SMBUS_BLOCK_MAX;
3421
3422 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3423 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3424
3425 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3426 return -EOPNOTSUPP;
3427
3428 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3429 while ((i + 2) <= length) {
3430 status = i2c_smbus_read_word_data(client, command + i);
3431 if (status < 0)
3432 return status;
3433 values[i] = status & 0xff;
3434 values[i + 1] = status >> 8;
3435 i += 2;
3436 }
3437 }
3438
3439 while (i < length) {
3440 status = i2c_smbus_read_byte_data(client, command + i);
3441 if (status < 0)
3442 return status;
3443 values[i] = status;
3444 i++;
3445 }
3446
3447 return i;
3448}
3449EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3450
Jean Delvared5fd1202015-01-26 20:59:31 +01003451#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003452int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3453{
3454 int ret;
3455
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003456 if (!client || !slave_cb) {
3457 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003458 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003459 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003460
Wolfram Sangc6909d62015-08-05 21:12:54 +02003461 if (!(client->flags & I2C_CLIENT_SLAVE))
3462 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3463 __func__);
3464
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003465 if (!(client->flags & I2C_CLIENT_TEN)) {
3466 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003467 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003468 if (ret) {
3469 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003470 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003471 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003472 }
3473
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003474 if (!client->adapter->algo->reg_slave) {
3475 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003476 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003477 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003478
3479 client->slave_cb = slave_cb;
3480
3481 i2c_lock_adapter(client->adapter);
3482 ret = client->adapter->algo->reg_slave(client);
3483 i2c_unlock_adapter(client->adapter);
3484
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003485 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003486 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003487 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3488 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003489
3490 return ret;
3491}
3492EXPORT_SYMBOL_GPL(i2c_slave_register);
3493
3494int i2c_slave_unregister(struct i2c_client *client)
3495{
3496 int ret;
3497
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003498 if (!client->adapter->algo->unreg_slave) {
3499 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003500 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003501 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003502
3503 i2c_lock_adapter(client->adapter);
3504 ret = client->adapter->algo->unreg_slave(client);
3505 i2c_unlock_adapter(client->adapter);
3506
3507 if (ret == 0)
3508 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003509 else
3510 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003511
3512 return ret;
3513}
3514EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003515#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3518MODULE_DESCRIPTION("I2C-Bus main module");
3519MODULE_LICENSE("GPL");