blob: a97f7a0b0d6e42473206d7b205e84efb4e01acd3 [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
80void i2c_transfer_trace_reg(void)
81{
82 static_key_slow_inc(&i2c_trace_msg);
83}
84
85void i2c_transfer_trace_unreg(void)
86{
87 static_key_slow_dec(&i2c_trace_msg);
88}
89
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020090#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030091struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020092 struct acpi_connection_info info;
93 struct i2c_adapter *adapter;
94};
95
96struct gsb_buffer {
97 u8 status;
98 u8 len;
99 union {
100 u16 wdata;
101 u8 bdata;
102 u8 data[0];
103 };
104} __packed;
105
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300106struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300107 struct i2c_board_info *info;
108 acpi_handle adapter_handle;
109 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300110 acpi_handle search_handle;
111 u32 speed;
112 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300113};
114
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300115static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200116{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300117 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300118 struct i2c_board_info *info = lookup->info;
119 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300120 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200121
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300122 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
123 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200124
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300125 sb = &ares->data.i2c_serial_bus;
126 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
127 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200128
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300129 status = acpi_get_handle(lookup->device_handle,
130 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300131 &lookup->adapter_handle);
132 if (!ACPI_SUCCESS(status))
133 return 1;
134
135 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300136 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300137 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
138 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200139
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140 return 1;
141}
142
Jarkko Nikula5853b222016-08-12 17:02:53 +0300143static int i2c_acpi_do_lookup(struct acpi_device *adev,
144 struct i2c_acpi_lookup *lookup)
145{
146 struct i2c_board_info *info = lookup->info;
147 struct list_head resource_list;
148 int ret;
149
150 if (acpi_bus_get_status(adev) || !adev->status.present ||
151 acpi_device_enumerated(adev))
152 return -EINVAL;
153
154 memset(info, 0, sizeof(*info));
155 lookup->device_handle = acpi_device_handle(adev);
156
157 /* Look up for I2cSerialBus resource */
158 INIT_LIST_HEAD(&resource_list);
159 ret = acpi_dev_get_resources(adev, &resource_list,
160 i2c_acpi_fill_info, lookup);
161 acpi_dev_free_resource_list(&resource_list);
162
163 if (ret < 0 || !info->addr)
164 return -EINVAL;
165
166 return 0;
167}
168
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300169static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300170 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300171 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300172 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200173{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200174 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300175 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300176 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200177 int ret;
178
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300179 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300180 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300181
Jarkko Nikula5853b222016-08-12 17:02:53 +0300182 ret = i2c_acpi_do_lookup(adev, &lookup);
183 if (ret)
184 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200185
Mika Westerberg318ce272016-09-20 16:59:25 +0300186 if (adapter) {
187 /* The adapter must match the one in I2cSerialBus() connector */
188 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
189 return -ENODEV;
190 } else {
191 struct acpi_device *adapter_adev;
192
193 /* The adapter must be present */
194 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
195 return -ENODEV;
196 if (acpi_bus_get_status(adapter_adev) ||
197 !adapter_adev->status.present)
198 return -ENODEV;
199 }
200
Jarkko Nikula5853b222016-08-12 17:02:53 +0300201 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300202 if (adapter_handle)
203 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200204
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300205 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300206 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300207 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
208 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300209 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300210
211 resource_list_for_each_entry(entry, &resource_list) {
212 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300213 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300214 break;
215 }
216 }
217
218 acpi_dev_free_resource_list(&resource_list);
219
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300220 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
221
222 return 0;
223}
224
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300225static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300226 struct acpi_device *adev,
227 struct i2c_board_info *info)
228{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200229 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300230 acpi_device_set_enumerated(adev);
231
232 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200233 adev->power.flags.ignore_parent = false;
234 dev_err(&adapter->dev,
235 "failed to add I2C device %s from ACPI\n",
236 dev_name(&adev->dev));
237 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300238}
239
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300240static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300241 void *data, void **return_value)
242{
243 struct i2c_adapter *adapter = data;
244 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300245 struct i2c_board_info info;
246
247 if (acpi_bus_get_device(handle, &adev))
248 return AE_OK;
249
Mika Westerberg318ce272016-09-20 16:59:25 +0300250 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300251 return AE_OK;
252
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300253 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200254
255 return AE_OK;
256}
257
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300258#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300259
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200260/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300261 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200262 * @adap: pointer to adapter
263 *
264 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
265 * namespace. When a device is found it will be added to the Linux device
266 * model and bound to the corresponding ACPI handle.
267 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300268static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200269{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200270 acpi_status status;
271
Dustin Byford8eb5c872015-10-23 12:27:07 -0700272 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200273 return;
274
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300275 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300276 I2C_ACPI_MAX_SCAN_DEPTH,
277 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200278 adap, NULL);
279 if (ACPI_FAILURE(status))
280 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
281}
282
Jarkko Nikula5853b222016-08-12 17:02:53 +0300283static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
284 void *data, void **return_value)
285{
286 struct i2c_acpi_lookup *lookup = data;
287 struct acpi_device *adev;
288
289 if (acpi_bus_get_device(handle, &adev))
290 return AE_OK;
291
292 if (i2c_acpi_do_lookup(adev, lookup))
293 return AE_OK;
294
295 if (lookup->search_handle != lookup->adapter_handle)
296 return AE_OK;
297
298 if (lookup->speed <= lookup->min_speed)
299 lookup->min_speed = lookup->speed;
300
301 return AE_OK;
302}
303
304/**
305 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
306 * @dev: The device owning the bus
307 *
308 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
309 * devices connected to this bus and use the speed of slowest device.
310 *
311 * Returns the speed in Hz or zero
312 */
313u32 i2c_acpi_find_bus_speed(struct device *dev)
314{
315 struct i2c_acpi_lookup lookup;
316 struct i2c_board_info dummy;
317 acpi_status status;
318
319 if (!has_acpi_companion(dev))
320 return 0;
321
322 memset(&lookup, 0, sizeof(lookup));
323 lookup.search_handle = ACPI_HANDLE(dev);
324 lookup.min_speed = UINT_MAX;
325 lookup.info = &dummy;
326
327 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
328 I2C_ACPI_MAX_SCAN_DEPTH,
329 i2c_acpi_lookup_speed, NULL,
330 &lookup, NULL);
331
332 if (ACPI_FAILURE(status)) {
333 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
334 return 0;
335 }
336
337 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
338}
339EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
340
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300341static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300342{
343 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
344
345 if (!adapter)
346 return 0;
347
348 return ACPI_HANDLE(dev) == (acpi_handle)data;
349}
350
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300351static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300352{
353 return ACPI_COMPANION(dev) == data;
354}
355
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300356static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300357{
358 struct device *dev;
359
360 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300361 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300362 return dev ? i2c_verify_adapter(dev) : NULL;
363}
364
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300365static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300366{
367 struct device *dev;
368
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300369 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300370 return dev ? i2c_verify_client(dev) : NULL;
371}
372
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300373static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300374 void *arg)
375{
376 struct acpi_device *adev = arg;
377 struct i2c_board_info info;
378 acpi_handle adapter_handle;
379 struct i2c_adapter *adapter;
380 struct i2c_client *client;
381
382 switch (value) {
383 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300384 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300385 break;
386
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300387 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300388 if (!adapter)
389 break;
390
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300391 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300392 break;
393 case ACPI_RECONFIG_DEVICE_REMOVE:
394 if (!acpi_device_enumerated(adev))
395 break;
396
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300397 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300398 if (!client)
399 break;
400
401 i2c_unregister_device(client);
402 put_device(&client->dev);
403 break;
404 }
405
406 return NOTIFY_OK;
407}
408
409static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300410 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300411};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200412#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300413static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300414extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415#endif /* CONFIG_ACPI */
416
417#ifdef CONFIG_ACPI_I2C_OPREGION
418static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
419 u8 cmd, u8 *data, u8 data_len)
420{
421
422 struct i2c_msg msgs[2];
423 int ret;
424 u8 *buffer;
425
426 buffer = kzalloc(data_len, GFP_KERNEL);
427 if (!buffer)
428 return AE_NO_MEMORY;
429
430 msgs[0].addr = client->addr;
431 msgs[0].flags = client->flags;
432 msgs[0].len = 1;
433 msgs[0].buf = &cmd;
434
435 msgs[1].addr = client->addr;
436 msgs[1].flags = client->flags | I2C_M_RD;
437 msgs[1].len = data_len;
438 msgs[1].buf = buffer;
439
440 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
441 if (ret < 0)
442 dev_err(&client->adapter->dev, "i2c read failed\n");
443 else
444 memcpy(data, buffer, data_len);
445
446 kfree(buffer);
447 return ret;
448}
449
450static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
451 u8 cmd, u8 *data, u8 data_len)
452{
453
454 struct i2c_msg msgs[1];
455 u8 *buffer;
456 int ret = AE_OK;
457
458 buffer = kzalloc(data_len + 1, GFP_KERNEL);
459 if (!buffer)
460 return AE_NO_MEMORY;
461
462 buffer[0] = cmd;
463 memcpy(buffer + 1, data, data_len);
464
465 msgs[0].addr = client->addr;
466 msgs[0].flags = client->flags;
467 msgs[0].len = data_len + 1;
468 msgs[0].buf = buffer;
469
470 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
471 if (ret < 0)
472 dev_err(&client->adapter->dev, "i2c write failed\n");
473
474 kfree(buffer);
475 return ret;
476}
477
478static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300479i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200480 u32 bits, u64 *value64,
481 void *handler_context, void *region_context)
482{
483 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300484 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200485 struct acpi_connection_info *info = &data->info;
486 struct acpi_resource_i2c_serialbus *sb;
487 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300488 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200489 struct acpi_resource *ares;
490 u32 accessor_type = function >> 16;
491 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100492 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200493 int status;
494
495 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
496 if (ACPI_FAILURE(ret))
497 return ret;
498
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300499 client = kzalloc(sizeof(*client), GFP_KERNEL);
500 if (!client) {
501 ret = AE_NO_MEMORY;
502 goto err;
503 }
504
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200505 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
506 ret = AE_BAD_PARAMETER;
507 goto err;
508 }
509
510 sb = &ares->data.i2c_serial_bus;
511 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
512 ret = AE_BAD_PARAMETER;
513 goto err;
514 }
515
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300516 client->adapter = adapter;
517 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200518
519 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300520 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200521
522 switch (accessor_type) {
523 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
524 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300525 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200526 if (status >= 0) {
527 gsb->bdata = status;
528 status = 0;
529 }
530 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300531 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200532 }
533 break;
534
535 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
536 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300537 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200538 if (status >= 0) {
539 gsb->bdata = status;
540 status = 0;
541 }
542 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300543 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200544 gsb->bdata);
545 }
546 break;
547
548 case ACPI_GSB_ACCESS_ATTRIB_WORD:
549 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300550 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200551 if (status >= 0) {
552 gsb->wdata = status;
553 status = 0;
554 }
555 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300556 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200557 gsb->wdata);
558 }
559 break;
560
561 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
562 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300563 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200564 gsb->data);
565 if (status >= 0) {
566 gsb->len = status;
567 status = 0;
568 }
569 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300570 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200571 gsb->len, gsb->data);
572 }
573 break;
574
575 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
576 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300577 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200578 gsb->data, info->access_length);
579 if (status > 0)
580 status = 0;
581 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300582 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200583 gsb->data, info->access_length);
584 }
585 break;
586
587 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900588 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
589 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200590 ret = AE_BAD_PARAMETER;
591 goto err;
592 }
593
594 gsb->status = status;
595
596 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300597 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200598 ACPI_FREE(ares);
599 return ret;
600}
601
602
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300603static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200604{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200605 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300606 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200607 acpi_status status;
608
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200609 if (!adapter->dev.parent)
610 return -ENODEV;
611
612 handle = ACPI_HANDLE(adapter->dev.parent);
613
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200614 if (!handle)
615 return -ENODEV;
616
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300617 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200618 GFP_KERNEL);
619 if (!data)
620 return -ENOMEM;
621
622 data->adapter = adapter;
623 status = acpi_bus_attach_private_data(handle, (void *)data);
624 if (ACPI_FAILURE(status)) {
625 kfree(data);
626 return -ENOMEM;
627 }
628
629 status = acpi_install_address_space_handler(handle,
630 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300631 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200632 NULL,
633 data);
634 if (ACPI_FAILURE(status)) {
635 dev_err(&adapter->dev, "Error installing i2c space handler\n");
636 acpi_bus_detach_private_data(handle);
637 kfree(data);
638 return -ENOMEM;
639 }
640
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800641 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200642 return 0;
643}
644
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300645static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200646{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200647 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300648 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200649 acpi_status status;
650
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200651 if (!adapter->dev.parent)
652 return;
653
654 handle = ACPI_HANDLE(adapter->dev.parent);
655
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200656 if (!handle)
657 return;
658
659 acpi_remove_address_space_handler(handle,
660 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300661 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200662
663 status = acpi_bus_get_private_data(handle, (void **)&data);
664 if (ACPI_SUCCESS(status))
665 kfree(data);
666
667 acpi_bus_detach_private_data(handle);
668}
669#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300670static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200671{ }
672
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300673static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200674{ return 0; }
675#endif /* CONFIG_ACPI_I2C_OPREGION */
676
David Brownellf37dd802007-02-13 22:09:00 +0100677/* ------------------------------------------------------------------------- */
678
Jean Delvared2653e92008-04-29 23:11:39 +0200679static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
680 const struct i2c_client *client)
681{
Lee Jones811073b2016-11-07 12:47:36 +0000682 if (!(id && client))
683 return NULL;
684
Jean Delvared2653e92008-04-29 23:11:39 +0200685 while (id->name[0]) {
686 if (strcmp(client->name, id->name) == 0)
687 return id;
688 id++;
689 }
690 return NULL;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static int i2c_device_match(struct device *dev, struct device_driver *drv)
694{
Jean Delvare51298d12009-09-18 22:45:45 +0200695 struct i2c_client *client = i2c_verify_client(dev);
696 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200697
Jean Delvare51298d12009-09-18 22:45:45 +0200698
Grant Likely959e85f2010-06-08 07:48:19 -0600699 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000700 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600701 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);
Lee Jones811073b2016-11-07 12:47:36 +0000708
709 /* Finally an I2C match */
710 if (i2c_match_id(driver->id_table, client))
711 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200712
Jean Delvareeb8a7902008-05-18 20:49:41 +0200713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Kay Sievers7eff2e72007-08-14 15:15:12 +0200716static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200717{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100718 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800719 int rc;
720
721 rc = acpi_device_uevent_modalias(dev, env);
722 if (rc != -ENODEV)
723 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200724
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100725 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200726}
727
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530728/* i2c bus recovery routines */
729static int get_scl_gpio_value(struct i2c_adapter *adap)
730{
731 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
732}
733
734static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
735{
736 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
737}
738
739static int get_sda_gpio_value(struct i2c_adapter *adap)
740{
741 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
742}
743
744static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
745{
746 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
747 struct device *dev = &adap->dev;
748 int ret = 0;
749
750 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
751 GPIOF_OUT_INIT_HIGH, "i2c-scl");
752 if (ret) {
753 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
754 return ret;
755 }
756
757 if (bri->get_sda) {
758 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
759 /* work without SDA polling */
760 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
761 bri->sda_gpio);
762 bri->get_sda = NULL;
763 }
764 }
765
766 return ret;
767}
768
769static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
770{
771 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
772
773 if (bri->get_sda)
774 gpio_free(bri->sda_gpio);
775
776 gpio_free(bri->scl_gpio);
777}
778
779/*
780 * We are generating clock pulses. ndelay() determines durating of clk pulses.
781 * We will generate clock with rate 100 KHz and so duration of both clock levels
782 * is: delay in ns = (10^6 / 100) / 2
783 */
784#define RECOVERY_NDELAY 5000
785#define RECOVERY_CLK_CNT 9
786
787static int i2c_generic_recovery(struct i2c_adapter *adap)
788{
789 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
790 int i = 0, val = 1, ret = 0;
791
792 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300793 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530794
Jan Luebbe8b062602015-07-08 16:35:06 +0200795 bri->set_scl(adap, val);
796 ndelay(RECOVERY_NDELAY);
797
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530798 /*
799 * By this time SCL is high, as we need to give 9 falling-rising edges
800 */
801 while (i++ < RECOVERY_CLK_CNT * 2) {
802 if (val) {
803 /* Break if SDA is high */
804 if (bri->get_sda && bri->get_sda(adap))
805 break;
806 /* SCL shouldn't be low here */
807 if (!bri->get_scl(adap)) {
808 dev_err(&adap->dev,
809 "SCL is stuck low, exit recovery\n");
810 ret = -EBUSY;
811 break;
812 }
813 }
814
815 val = !val;
816 bri->set_scl(adap, val);
817 ndelay(RECOVERY_NDELAY);
818 }
819
820 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300821 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530822
823 return ret;
824}
825
826int i2c_generic_scl_recovery(struct i2c_adapter *adap)
827{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530828 return i2c_generic_recovery(adap);
829}
Mark Brownc1c21f42015-04-15 19:18:39 +0100830EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530831
832int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
833{
834 int ret;
835
836 ret = i2c_get_gpios_for_recovery(adap);
837 if (ret)
838 return ret;
839
840 ret = i2c_generic_recovery(adap);
841 i2c_put_gpios_for_recovery(adap);
842
843 return ret;
844}
Mark Brownc1c21f42015-04-15 19:18:39 +0100845EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530846
847int i2c_recover_bus(struct i2c_adapter *adap)
848{
849 if (!adap->bus_recovery_info)
850 return -EOPNOTSUPP;
851
852 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
853 return adap->bus_recovery_info->recover_bus(adap);
854}
Mark Brownc1c21f42015-04-15 19:18:39 +0100855EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530856
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900857static void i2c_init_recovery(struct i2c_adapter *adap)
858{
859 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
860 char *err_str;
861
862 if (!bri)
863 return;
864
865 if (!bri->recover_bus) {
866 err_str = "no recover_bus() found";
867 goto err;
868 }
869
870 /* Generic GPIO recovery */
871 if (bri->recover_bus == i2c_generic_gpio_recovery) {
872 if (!gpio_is_valid(bri->scl_gpio)) {
873 err_str = "invalid SCL gpio";
874 goto err;
875 }
876
877 if (gpio_is_valid(bri->sda_gpio))
878 bri->get_sda = get_sda_gpio_value;
879 else
880 bri->get_sda = NULL;
881
882 bri->get_scl = get_scl_gpio_value;
883 bri->set_scl = set_scl_gpio_value;
884 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
885 /* Generic SCL recovery */
886 if (!bri->set_scl || !bri->get_scl) {
887 err_str = "no {get|set}_scl() found";
888 goto err;
889 }
890 }
891
892 return;
893 err:
894 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
895 adap->bus_recovery_info = NULL;
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898static int i2c_device_probe(struct device *dev)
899{
Jean Delvare51298d12009-09-18 22:45:45 +0200900 struct i2c_client *client = i2c_verify_client(dev);
901 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100902 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200903
Jean Delvare51298d12009-09-18 22:45:45 +0200904 if (!client)
905 return 0;
906
Mika Westerberg845c8772015-05-06 13:29:08 +0300907 if (!client->irq) {
908 int irq = -ENOENT;
909
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700910 if (dev->of_node) {
911 irq = of_irq_get_byname(dev->of_node, "irq");
912 if (irq == -EINVAL || irq == -ENODATA)
913 irq = of_irq_get(dev->of_node, 0);
914 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300915 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700916 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100917 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200918 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100919 if (irq < 0)
920 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200921
922 client->irq = irq;
923 }
924
Jean Delvare51298d12009-09-18 22:45:45 +0200925 driver = to_i2c_driver(dev->driver);
Lee Jonesda10c062016-11-07 12:47:39 +0000926 if (!driver->probe)
927 return -EINVAL;
928
929 /*
930 * An I2C ID table is not mandatory, if and only if, a suitable Device
931 * Tree match table entry is supplied for the probing device.
932 */
933 if (!driver->id_table &&
934 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200935 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200936
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700937 if (client->flags & I2C_CLIENT_WAKE) {
938 int wakeirq = -ENOENT;
939
940 if (dev->of_node) {
941 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
942 if (wakeirq == -EPROBE_DEFER)
943 return wakeirq;
944 }
945
946 device_init_wakeup(&client->dev, true);
947
948 if (wakeirq > 0 && wakeirq != client->irq)
949 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
950 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200951 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700952 else
953 status = 0;
954
955 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300956 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700957 }
958
David Brownell7b4fbc52007-05-01 23:26:30 +0200959 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200960
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200961 status = of_clk_set_defaults(dev->of_node, false);
962 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700963 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200964
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200965 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100966 if (status == -EPROBE_DEFER)
967 goto err_clear_wakeup_irq;
968
969 status = driver->probe(client, i2c_match_id(driver->id_table, client));
970 if (status)
971 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100972
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700973 return 0;
974
975err_detach_pm_domain:
976 dev_pm_domain_detach(&client->dev, true);
977err_clear_wakeup_irq:
978 dev_pm_clear_wake_irq(&client->dev);
979 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100980 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static int i2c_device_remove(struct device *dev)
984{
Jean Delvare51298d12009-09-18 22:45:45 +0200985 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200986 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100987 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200988
Jean Delvare51298d12009-09-18 22:45:45 +0200989 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200990 return 0;
991
992 driver = to_i2c_driver(dev->driver);
993 if (driver->remove) {
994 dev_dbg(dev, "remove\n");
995 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200996 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100997
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200998 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700999
1000 dev_pm_clear_wake_irq(&client->dev);
1001 device_init_wakeup(&client->dev, false);
1002
David Brownella1d9e6e2007-05-01 23:26:30 +02001003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
David Brownellf37dd802007-02-13 22:09:00 +01001006static void i2c_device_shutdown(struct device *dev)
1007{
Jean Delvare51298d12009-09-18 22:45:45 +02001008 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001009 struct i2c_driver *driver;
1010
Jean Delvare51298d12009-09-18 22:45:45 +02001011 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001012 return;
1013 driver = to_i2c_driver(dev->driver);
1014 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001015 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001016}
1017
David Brownell9c1600e2007-05-01 23:26:31 +02001018static void i2c_client_dev_release(struct device *dev)
1019{
1020 kfree(to_i2c_client(dev));
1021}
1022
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001023static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001024show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001025{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001026 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1027 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001028}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001029static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001030
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001031static ssize_t
1032show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001033{
1034 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001035 int len;
1036
1037 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1038 if (len != -ENODEV)
1039 return len;
1040
Jean Delvareeb8a7902008-05-18 20:49:41 +02001041 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001042}
Jean Delvare51298d12009-09-18 22:45:45 +02001043static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1044
1045static struct attribute *i2c_dev_attrs[] = {
1046 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001047 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001048 &dev_attr_modalias.attr,
1049 NULL
1050};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001051ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001052
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001053struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001054 .name = "i2c",
1055 .match = i2c_device_match,
1056 .probe = i2c_device_probe,
1057 .remove = i2c_device_remove,
1058 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001059};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001060EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001061
Jean Delvare51298d12009-09-18 22:45:45 +02001062static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001063 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001064 .uevent = i2c_device_uevent,
1065 .release = i2c_client_dev_release,
1066};
1067
David Brownell9b766b82008-01-27 18:14:51 +01001068
1069/**
1070 * i2c_verify_client - return parameter as i2c_client, or NULL
1071 * @dev: device, probably from some driver model iterator
1072 *
1073 * When traversing the driver model tree, perhaps using driver model
1074 * iterators like @device_for_each_child(), you can't assume very much
1075 * about the nodes you find. Use this function to avoid oopses caused
1076 * by wrongly treating some non-I2C device as an i2c_client.
1077 */
1078struct i2c_client *i2c_verify_client(struct device *dev)
1079{
Jean Delvare51298d12009-09-18 22:45:45 +02001080 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001081 ? to_i2c_client(dev)
1082 : NULL;
1083}
1084EXPORT_SYMBOL(i2c_verify_client);
1085
1086
Wolfram Sangda899f52015-05-18 21:09:12 +02001087/* Return a unique address which takes the flags of the client into account */
1088static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1089{
1090 unsigned short addr = client->addr;
1091
1092 /* For some client flags, add an arbitrary offset to avoid collisions */
1093 if (client->flags & I2C_CLIENT_TEN)
1094 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1095
1096 if (client->flags & I2C_CLIENT_SLAVE)
1097 addr |= I2C_ADDR_OFFSET_SLAVE;
1098
1099 return addr;
1100}
1101
Jean Delvare3a89db52010-06-03 11:33:52 +02001102/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001103 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001104static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001105{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001106 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001107 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001108 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001109 return -EINVAL;
1110 } else {
1111 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001112 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001113 return -EINVAL;
1114 }
1115 return 0;
1116}
1117
Jean Delvare656b8762010-06-03 11:33:53 +02001118/* And this is a strict address validity check, used when probing. If a
1119 * device uses a reserved address, then it shouldn't be probed. 7-bit
1120 * addressing is assumed, 10-bit address devices are rare and should be
1121 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001122static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001123{
1124 /*
1125 * Reserved addresses per I2C specification:
1126 * 0x00 General call address / START byte
1127 * 0x01 CBUS address
1128 * 0x02 Reserved for different bus format
1129 * 0x03 Reserved for future purposes
1130 * 0x04-0x07 Hs-mode master code
1131 * 0x78-0x7b 10-bit slave addressing
1132 * 0x7c-0x7f Reserved for future purposes
1133 */
1134 if (addr < 0x08 || addr > 0x77)
1135 return -EINVAL;
1136 return 0;
1137}
1138
Jean Delvare3b5f7942010-06-03 11:33:55 +02001139static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1140{
1141 struct i2c_client *client = i2c_verify_client(dev);
1142 int addr = *(int *)addrp;
1143
Wolfram Sang9bccc702015-07-17 14:48:56 +02001144 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001145 return -EBUSY;
1146 return 0;
1147}
1148
Michael Lawnick08263742010-08-11 18:21:02 +02001149/* walk up mux tree */
1150static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1151{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001152 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001153 int result;
1154
1155 result = device_for_each_child(&adapter->dev, &addr,
1156 __i2c_check_addr_busy);
1157
Jean Delvare97cc4d42010-10-24 18:16:57 +02001158 if (!result && parent)
1159 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001160
1161 return result;
1162}
1163
1164/* recurse down mux tree */
1165static int i2c_check_mux_children(struct device *dev, void *addrp)
1166{
1167 int result;
1168
1169 if (dev->type == &i2c_adapter_type)
1170 result = device_for_each_child(dev, addrp,
1171 i2c_check_mux_children);
1172 else
1173 result = __i2c_check_addr_busy(dev, addrp);
1174
1175 return result;
1176}
1177
Jean Delvare3b5f7942010-06-03 11:33:55 +02001178static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1179{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001180 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001181 int result = 0;
1182
Jean Delvare97cc4d42010-10-24 18:16:57 +02001183 if (parent)
1184 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001185
1186 if (!result)
1187 result = device_for_each_child(&adapter->dev, &addr,
1188 i2c_check_mux_children);
1189
1190 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001191}
1192
David Brownell9c1600e2007-05-01 23:26:31 +02001193/**
Peter Rosin8320f492016-05-04 22:15:27 +02001194 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001195 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001196 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1197 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001198 */
Peter Rosin8320f492016-05-04 22:15:27 +02001199static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1200 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001201{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001202 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001203}
Jean Delvarefe61e072010-08-11 18:20:58 +02001204
1205/**
Peter Rosin8320f492016-05-04 22:15:27 +02001206 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001207 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001208 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1209 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001210 */
Peter Rosin8320f492016-05-04 22:15:27 +02001211static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1212 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001213{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001214 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001215}
1216
1217/**
Peter Rosin8320f492016-05-04 22:15:27 +02001218 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001219 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001220 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1221 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001222 */
Peter Rosin8320f492016-05-04 22:15:27 +02001223static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1224 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001225{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001226 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001227}
Jean Delvarefe61e072010-08-11 18:20:58 +02001228
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001229static void i2c_dev_set_name(struct i2c_adapter *adap,
1230 struct i2c_client *client)
1231{
1232 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1233
1234 if (adev) {
1235 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1236 return;
1237 }
1238
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001239 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001240 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001241}
1242
Jean Delvarefe61e072010-08-11 18:20:58 +02001243/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001244 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001245 * @adap: the adapter managing the device
1246 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001247 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001248 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001249 * Create an i2c device. Binding is handled through driver model
1250 * probe()/remove() methods. A driver may be bound to this device when we
1251 * return from this function, or any later moment (e.g. maybe hotplugging will
1252 * load the driver module). This call is not appropriate for use by mainboard
1253 * initialization logic, which usually runs during an arch_initcall() long
1254 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001255 *
1256 * This returns the new i2c client, which may be saved for later use with
1257 * i2c_unregister_device(); or NULL to indicate an error.
1258 */
1259struct i2c_client *
1260i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1261{
1262 struct i2c_client *client;
1263 int status;
1264
1265 client = kzalloc(sizeof *client, GFP_KERNEL);
1266 if (!client)
1267 return NULL;
1268
1269 client->adapter = adap;
1270
1271 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001272
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001273 if (info->archdata)
1274 client->dev.archdata = *info->archdata;
1275
Marc Pignatee354252008-08-28 08:33:22 +02001276 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001277 client->addr = info->addr;
1278 client->irq = info->irq;
1279
David Brownell9c1600e2007-05-01 23:26:31 +02001280 strlcpy(client->name, info->type, sizeof(client->name));
1281
Wolfram Sangc4019b72015-07-17 12:50:06 +02001282 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001283 if (status) {
1284 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1285 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1286 goto out_err_silent;
1287 }
1288
Jean Delvaref8a227e2009-06-19 16:58:18 +02001289 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001290 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001291 if (status)
1292 goto out_err;
1293
1294 client->dev.parent = &client->adapter->dev;
1295 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001296 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001297 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001298 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001299
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001300 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001301 status = device_register(&client->dev);
1302 if (status)
1303 goto out_err;
1304
Jean Delvaref8a227e2009-06-19 16:58:18 +02001305 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1306 client->name, dev_name(&client->dev));
1307
David Brownell9c1600e2007-05-01 23:26:31 +02001308 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001309
1310out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001311 dev_err(&adap->dev,
1312 "Failed to register i2c client %s at 0x%02x (%d)\n",
1313 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001314out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001315 kfree(client);
1316 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001317}
1318EXPORT_SYMBOL_GPL(i2c_new_device);
1319
1320
1321/**
1322 * i2c_unregister_device - reverse effect of i2c_new_device()
1323 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001324 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001325 */
1326void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001327{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001328 if (client->dev.of_node)
1329 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001330 if (ACPI_COMPANION(&client->dev))
1331 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001332 device_unregister(&client->dev);
1333}
David Brownell9c1600e2007-05-01 23:26:31 +02001334EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001335
1336
Jean Delvare60b129d2008-05-11 20:37:06 +02001337static const struct i2c_device_id dummy_id[] = {
1338 { "dummy", 0 },
1339 { },
1340};
1341
Jean Delvared2653e92008-04-29 23:11:39 +02001342static int dummy_probe(struct i2c_client *client,
1343 const struct i2c_device_id *id)
1344{
1345 return 0;
1346}
1347
1348static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001349{
1350 return 0;
1351}
1352
1353static struct i2c_driver dummy_driver = {
1354 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001355 .probe = dummy_probe,
1356 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001357 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001358};
1359
1360/**
1361 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1362 * @adapter: the adapter managing the device
1363 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001364 * Context: can sleep
1365 *
1366 * This returns an I2C client bound to the "dummy" driver, intended for use
1367 * with devices that consume multiple addresses. Examples of such chips
1368 * include various EEPROMS (like 24c04 and 24c08 models).
1369 *
1370 * These dummy devices have two main uses. First, most I2C and SMBus calls
1371 * except i2c_transfer() need a client handle; the dummy will be that handle.
1372 * And second, this prevents the specified address from being bound to a
1373 * different driver.
1374 *
1375 * This returns the new i2c client, which should be saved for later use with
1376 * i2c_unregister_device(); or NULL to indicate an error.
1377 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001378struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001379{
1380 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001381 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001382 };
1383
David Brownelle9f13732008-01-27 18:14:52 +01001384 return i2c_new_device(adapter, &info);
1385}
1386EXPORT_SYMBOL_GPL(i2c_new_dummy);
1387
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001388/**
1389 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1390 * and create the associated device
1391 * @client: Handle to the primary client
1392 * @name: Handle to specify which secondary address to get
1393 * @default_addr: Used as a fallback if no secondary address was specified
1394 * Context: can sleep
1395 *
1396 * I2C clients can be composed of multiple I2C slaves bound together in a single
1397 * component. The I2C client driver then binds to the master I2C slave and needs
1398 * to create I2C dummy clients to communicate with all the other slaves.
1399 *
1400 * This function creates and returns an I2C dummy client whose I2C address is
1401 * retrieved from the platform firmware based on the given slave name. If no
1402 * address is specified by the firmware default_addr is used.
1403 *
1404 * On DT-based platforms the address is retrieved from the "reg" property entry
1405 * cell whose "reg-names" value matches the slave name.
1406 *
1407 * This returns the new i2c client, which should be saved for later use with
1408 * i2c_unregister_device(); or NULL to indicate an error.
1409 */
1410struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1411 const char *name,
1412 u16 default_addr)
1413{
1414 struct device_node *np = client->dev.of_node;
1415 u32 addr = default_addr;
1416 int i;
1417
1418 if (np) {
1419 i = of_property_match_string(np, "reg-names", name);
1420 if (i >= 0)
1421 of_property_read_u32_index(np, "reg", i, &addr);
1422 }
1423
1424 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1425 return i2c_new_dummy(client->adapter, addr);
1426}
1427EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1428
David Brownellf37dd802007-02-13 22:09:00 +01001429/* ------------------------------------------------------------------------- */
1430
David Brownell16ffadf2007-05-01 23:26:28 +02001431/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1432
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001433static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
David Brownellef2c83212007-05-01 23:26:28 +02001435 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 complete(&adap->dev_released);
1437}
1438
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001439unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001440{
1441 unsigned int depth = 0;
1442
1443 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1444 depth++;
1445
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001446 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1447 "adapter depth exceeds lockdep subclass limit\n");
1448
Jean Delvare390946b2012-09-10 10:14:02 +02001449 return depth;
1450}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001451EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001452
1453/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001454 * Let users instantiate I2C devices through sysfs. This can be used when
1455 * platform initialization code doesn't contain the proper data for
1456 * whatever reason. Also useful for drivers that do device detection and
1457 * detection fails, either because the device uses an unexpected address,
1458 * or this is a compatible device with different ID register values.
1459 *
1460 * Parameter checking may look overzealous, but we really don't want
1461 * the user to provide incorrect parameters.
1462 */
1463static ssize_t
1464i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1465 const char *buf, size_t count)
1466{
1467 struct i2c_adapter *adap = to_i2c_adapter(dev);
1468 struct i2c_board_info info;
1469 struct i2c_client *client;
1470 char *blank, end;
1471 int res;
1472
Jean Delvare99cd8e22009-06-19 16:58:20 +02001473 memset(&info, 0, sizeof(struct i2c_board_info));
1474
1475 blank = strchr(buf, ' ');
1476 if (!blank) {
1477 dev_err(dev, "%s: Missing parameters\n", "new_device");
1478 return -EINVAL;
1479 }
1480 if (blank - buf > I2C_NAME_SIZE - 1) {
1481 dev_err(dev, "%s: Invalid device name\n", "new_device");
1482 return -EINVAL;
1483 }
1484 memcpy(info.type, buf, blank - buf);
1485
1486 /* Parse remaining parameters, reject extra parameters */
1487 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1488 if (res < 1) {
1489 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1490 return -EINVAL;
1491 }
1492 if (res > 1 && end != '\n') {
1493 dev_err(dev, "%s: Extra parameters\n", "new_device");
1494 return -EINVAL;
1495 }
1496
Wolfram Sangcfa03272015-07-27 14:03:38 +02001497 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1498 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1499 info.flags |= I2C_CLIENT_TEN;
1500 }
1501
1502 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1503 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1504 info.flags |= I2C_CLIENT_SLAVE;
1505 }
1506
Jean Delvare99cd8e22009-06-19 16:58:20 +02001507 client = i2c_new_device(adap, &info);
1508 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001509 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001510
1511 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001512 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001513 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001514 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001515 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1516 info.type, info.addr);
1517
1518 return count;
1519}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001520static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001521
1522/*
1523 * And of course let the users delete the devices they instantiated, if
1524 * they got it wrong. This interface can only be used to delete devices
1525 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1526 * don't delete devices to which some kernel code still has references.
1527 *
1528 * Parameter checking may look overzealous, but we really don't want
1529 * the user to delete the wrong device.
1530 */
1531static ssize_t
1532i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1533 const char *buf, size_t count)
1534{
1535 struct i2c_adapter *adap = to_i2c_adapter(dev);
1536 struct i2c_client *client, *next;
1537 unsigned short addr;
1538 char end;
1539 int res;
1540
1541 /* Parse parameters, reject extra parameters */
1542 res = sscanf(buf, "%hi%c", &addr, &end);
1543 if (res < 1) {
1544 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1545 return -EINVAL;
1546 }
1547 if (res > 1 && end != '\n') {
1548 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1549 return -EINVAL;
1550 }
1551
1552 /* Make sure the device was added through sysfs */
1553 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001554 mutex_lock_nested(&adap->userspace_clients_lock,
1555 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001556 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1557 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001558 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001559 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1560 "delete_device", client->name, client->addr);
1561
1562 list_del(&client->detected);
1563 i2c_unregister_device(client);
1564 res = count;
1565 break;
1566 }
1567 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001568 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001569
1570 if (res < 0)
1571 dev_err(dev, "%s: Can't find device in list\n",
1572 "delete_device");
1573 return res;
1574}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001575static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1576 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001577
1578static struct attribute *i2c_adapter_attrs[] = {
1579 &dev_attr_name.attr,
1580 &dev_attr_new_device.attr,
1581 &dev_attr_delete_device.attr,
1582 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001583};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001584ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001585
Michael Lawnick08263742010-08-11 18:21:02 +02001586struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001587 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001588 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001589};
Michael Lawnick08263742010-08-11 18:21:02 +02001590EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Stephen Warren643dd092012-04-17 12:43:33 -06001592/**
1593 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1594 * @dev: device, probably from some driver model iterator
1595 *
1596 * When traversing the driver model tree, perhaps using driver model
1597 * iterators like @device_for_each_child(), you can't assume very much
1598 * about the nodes you find. Use this function to avoid oopses caused
1599 * by wrongly treating some non-I2C device as an i2c_adapter.
1600 */
1601struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1602{
1603 return (dev->type == &i2c_adapter_type)
1604 ? to_i2c_adapter(dev)
1605 : NULL;
1606}
1607EXPORT_SYMBOL(i2c_verify_adapter);
1608
Jean Delvare2bb50952009-09-18 22:45:46 +02001609#ifdef CONFIG_I2C_COMPAT
1610static struct class_compat *i2c_adapter_compat_class;
1611#endif
1612
David Brownell9c1600e2007-05-01 23:26:31 +02001613static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1614{
1615 struct i2c_devinfo *devinfo;
1616
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001617 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001618 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1619 if (devinfo->busnum == adapter->nr
1620 && !i2c_new_device(adapter,
1621 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001622 dev_err(&adapter->dev,
1623 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001624 devinfo->board_info.addr);
1625 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001626 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001627}
1628
Wolfram Sang687b81d2013-07-11 12:56:15 +01001629/* OF support code */
1630
1631#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001632static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1633 struct device_node *node)
1634{
1635 struct i2c_client *result;
1636 struct i2c_board_info info = {};
1637 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001638 const __be32 *addr_be;
1639 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001640 int len;
1641
1642 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1643
1644 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1645 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1646 node->full_name);
1647 return ERR_PTR(-EINVAL);
1648 }
1649
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001650 addr_be = of_get_property(node, "reg", &len);
1651 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001652 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1653 node->full_name);
1654 return ERR_PTR(-EINVAL);
1655 }
1656
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001657 addr = be32_to_cpup(addr_be);
1658 if (addr & I2C_TEN_BIT_ADDRESS) {
1659 addr &= ~I2C_TEN_BIT_ADDRESS;
1660 info.flags |= I2C_CLIENT_TEN;
1661 }
1662
1663 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1664 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1665 info.flags |= I2C_CLIENT_SLAVE;
1666 }
1667
1668 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001669 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1670 info.addr, node->full_name);
1671 return ERR_PTR(-EINVAL);
1672 }
1673
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001674 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001675 info.of_node = of_node_get(node);
1676 info.archdata = &dev_ad;
1677
1678 if (of_get_property(node, "wakeup-source", NULL))
1679 info.flags |= I2C_CLIENT_WAKE;
1680
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001681 result = i2c_new_device(adap, &info);
1682 if (result == NULL) {
1683 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1684 node->full_name);
1685 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001686 return ERR_PTR(-EINVAL);
1687 }
1688 return result;
1689}
1690
Wolfram Sang687b81d2013-07-11 12:56:15 +01001691static void of_i2c_register_devices(struct i2c_adapter *adap)
1692{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001693 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001694 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001695
1696 /* Only register child devices if the adapter has a node pointer set */
1697 if (!adap->dev.of_node)
1698 return;
1699
1700 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1701
Jon Hunter7e4c2242016-06-29 10:17:53 +01001702 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1703 if (!bus)
1704 bus = of_node_get(adap->dev.of_node);
1705
1706 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001707 if (of_node_test_and_set_flag(node, OF_POPULATED))
1708 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001709
1710 client = of_i2c_register_device(adap, node);
1711 if (IS_ERR(client)) {
1712 dev_warn(&adap->dev,
1713 "Failed to create I2C device for %s\n",
1714 node->full_name);
1715 of_node_clear_flag(node, OF_POPULATED);
1716 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001717 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001718
1719 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001720}
1721
1722static int of_dev_node_match(struct device *dev, void *data)
1723{
1724 return dev->of_node == data;
1725}
1726
1727/* must call put_device() when done with returned i2c_client device */
1728struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1729{
1730 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001731 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001732
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001733 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001734 if (!dev)
1735 return NULL;
1736
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001737 client = i2c_verify_client(dev);
1738 if (!client)
1739 put_device(dev);
1740
1741 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001742}
1743EXPORT_SYMBOL(of_find_i2c_device_by_node);
1744
1745/* must call put_device() when done with returned i2c_adapter device */
1746struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1747{
1748 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001749 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001750
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001751 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001752 if (!dev)
1753 return NULL;
1754
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001755 adapter = i2c_verify_adapter(dev);
1756 if (!adapter)
1757 put_device(dev);
1758
1759 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001760}
1761EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001762
1763/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1764struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1765{
1766 struct i2c_adapter *adapter;
1767
1768 adapter = of_find_i2c_adapter_by_node(node);
1769 if (!adapter)
1770 return NULL;
1771
1772 if (!try_module_get(adapter->owner)) {
1773 put_device(&adapter->dev);
1774 adapter = NULL;
1775 }
1776
1777 return adapter;
1778}
1779EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Lee Jonescabcf4f2016-11-07 12:47:37 +00001780
1781static const struct of_device_id*
1782i2c_of_match_device_sysfs(const struct of_device_id *matches,
1783 struct i2c_client *client)
1784{
1785 const char *name;
1786
1787 for (; matches->compatible[0]; matches++) {
1788 /*
1789 * Adding devices through the i2c sysfs interface provides us
1790 * a string to match which may be compatible with the device
1791 * tree compatible strings, however with no actual of_node the
1792 * of_match_device() will not match
1793 */
1794 if (sysfs_streq(client->name, matches->compatible))
1795 return matches;
1796
1797 name = strchr(matches->compatible, ',');
1798 if (!name)
1799 name = matches->compatible;
1800 else
1801 name++;
1802
1803 if (sysfs_streq(client->name, name))
1804 return matches;
1805 }
1806
1807 return NULL;
1808}
1809
Lee Jones298d4de2016-11-07 12:47:38 +00001810const struct of_device_id
1811*i2c_of_match_device(const struct of_device_id *matches,
1812 struct i2c_client *client)
1813{
1814 const struct of_device_id *match;
1815
1816 if (!(client && matches))
1817 return NULL;
1818
1819 match = of_match_device(matches, &client->dev);
1820 if (match)
1821 return match;
1822
1823 return i2c_of_match_device_sysfs(matches, client);
1824}
1825EXPORT_SYMBOL_GPL(i2c_of_match_device);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001826#else
1827static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1828#endif /* CONFIG_OF */
1829
Jean Delvare69b00892009-12-06 17:06:27 +01001830static int i2c_do_add_adapter(struct i2c_driver *driver,
1831 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001832{
Jean Delvare4735c982008-07-14 22:38:36 +02001833 /* Detect supported devices on that bus, and instantiate them */
1834 i2c_detect(adap, driver);
1835
1836 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001837 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001838 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1839 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001840 dev_warn(&adap->dev,
1841 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001842 /* We ignore the return code; if it fails, too bad */
1843 driver->attach_adapter(adap);
1844 }
1845 return 0;
1846}
1847
Jean Delvare69b00892009-12-06 17:06:27 +01001848static int __process_new_adapter(struct device_driver *d, void *data)
1849{
1850 return i2c_do_add_adapter(to_i2c_driver(d), data);
1851}
1852
Peter Rosind1ed7982016-08-25 23:07:01 +02001853static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1854 .lock_bus = i2c_adapter_lock_bus,
1855 .trylock_bus = i2c_adapter_trylock_bus,
1856 .unlock_bus = i2c_adapter_unlock_bus,
1857};
1858
David Brownell6e13e642007-05-01 23:26:31 +02001859static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001861 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
David Brownell1d0b19c2008-10-14 17:30:05 +02001863 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301864 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001865 res = -EAGAIN;
1866 goto out_list;
1867 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001868
Jean Delvare2236baa72010-11-15 22:40:38 +01001869 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001870 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001871 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001872
1873 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001874 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001875 goto out_list;
Jean Delvare2236baa72010-11-15 22:40:38 +01001876 }
1877
Peter Rosind1ed7982016-08-25 23:07:01 +02001878 if (!adap->lock_ops)
1879 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001880
Mika Kuoppala194684e2009-12-06 17:06:22 +01001881 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001882 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001883 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001884 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Jean Delvare8fcfef62009-03-28 21:34:43 +01001886 /* Set default timeout to 1 second if not already set */
1887 if (adap->timeout == 0)
1888 adap->timeout = HZ;
1889
Kay Sievers27d9c182009-01-07 14:29:16 +01001890 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001891 adap->dev.bus = &i2c_bus_type;
1892 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001893 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001894 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001895 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001896 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001899 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1900
Charles Keepax6ada5c12015-04-16 13:05:19 +01001901 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001902 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001903 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001904
Jean Delvare2bb50952009-09-18 22:45:46 +02001905#ifdef CONFIG_I2C_COMPAT
1906 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1907 adap->dev.parent);
1908 if (res)
1909 dev_warn(&adap->dev,
1910 "Failed to create compatibility class link\n");
1911#endif
1912
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001913 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301914
Jean Delvare729d6dd2009-06-19 16:58:18 +02001915 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001916 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001917 i2c_acpi_register_devices(adap);
1918 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001919
David Brownell6e13e642007-05-01 23:26:31 +02001920 if (adap->nr < __i2c_first_dynamic_bus_num)
1921 i2c_scan_static_board_info(adap);
1922
Jean Delvare4735c982008-07-14 22:38:36 +02001923 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001924 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001925 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001926 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001927
1928 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001929
Jean Delvareb119c6c2006-08-15 18:26:30 +02001930out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001931 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001932 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001933 mutex_unlock(&core_lock);
1934 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
David Brownell6e13e642007-05-01 23:26:31 +02001937/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001938 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1939 * @adap: the adapter to register (with adap->nr initialized)
1940 * Context: can sleep
1941 *
1942 * See i2c_add_numbered_adapter() for details.
1943 */
1944static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1945{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001946 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001947
1948 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001949 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001950 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001951 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001952 return id == -ENOSPC ? -EBUSY : id;
1953
1954 return i2c_register_adapter(adap);
1955}
1956
1957/**
David Brownell6e13e642007-05-01 23:26:31 +02001958 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1959 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001960 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001961 *
1962 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001963 * doesn't matter or when its bus number is specified by an dt alias.
1964 * Examples of bases when the bus number doesn't matter: I2C adapters
1965 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001966 *
1967 * When this returns zero, a new bus number was allocated and stored
1968 * in adap->nr, and the specified adapter became available for clients.
1969 * Otherwise, a negative errno value is returned.
1970 */
1971int i2c_add_adapter(struct i2c_adapter *adapter)
1972{
Doug Andersonee5c2742013-03-01 08:57:31 -08001973 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001974 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001975
Doug Andersonee5c2742013-03-01 08:57:31 -08001976 if (dev->of_node) {
1977 id = of_alias_get_id(dev->of_node, "i2c");
1978 if (id >= 0) {
1979 adapter->nr = id;
1980 return __i2c_add_numbered_adapter(adapter);
1981 }
1982 }
1983
Jean Delvarecaada322008-01-27 18:14:49 +01001984 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001985 id = idr_alloc(&i2c_adapter_idr, adapter,
1986 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001987 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001988 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001989 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001990
1991 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001992
David Brownell6e13e642007-05-01 23:26:31 +02001993 return i2c_register_adapter(adapter);
1994}
1995EXPORT_SYMBOL(i2c_add_adapter);
1996
1997/**
1998 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1999 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02002000 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002001 *
2002 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01002003 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
2004 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02002005 * is used to properly configure I2C devices.
2006 *
Grant Likely488bf312011-07-25 17:49:43 +02002007 * If the requested bus number is set to -1, then this function will behave
2008 * identically to i2c_add_adapter, and will dynamically assign a bus number.
2009 *
David Brownell6e13e642007-05-01 23:26:31 +02002010 * If no devices have pre-been declared for this bus, then be sure to
2011 * register the adapter before any dynamically allocated ones. Otherwise
2012 * the required bus ID may not be available.
2013 *
2014 * When this returns zero, the specified adapter became available for
2015 * clients using the bus number provided in adap->nr. Also, the table
2016 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
2017 * and the appropriate driver model device nodes are created. Otherwise, a
2018 * negative errno value is returned.
2019 */
2020int i2c_add_numbered_adapter(struct i2c_adapter *adap)
2021{
Grant Likely488bf312011-07-25 17:49:43 +02002022 if (adap->nr == -1) /* -1 means dynamically assign bus id */
2023 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002024
Doug Andersonee5c2742013-03-01 08:57:31 -08002025 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002026}
2027EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2028
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002029static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002030 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002031{
Jean Delvare4735c982008-07-14 22:38:36 +02002032 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002033
Jean Delvareacec2112009-03-28 21:34:40 +01002034 /* Remove the devices we created ourselves as the result of hardware
2035 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002036 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2037 if (client->adapter == adapter) {
2038 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2039 client->name, client->addr);
2040 list_del(&client->detected);
2041 i2c_unregister_device(client);
2042 }
2043 }
Jean Delvare026526f2008-01-27 18:14:49 +01002044}
2045
Jean Delvaree549c2b2009-06-19 16:58:19 +02002046static int __unregister_client(struct device *dev, void *dummy)
2047{
2048 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002049 if (client && strcmp(client->name, "dummy"))
2050 i2c_unregister_device(client);
2051 return 0;
2052}
2053
2054static int __unregister_dummy(struct device *dev, void *dummy)
2055{
2056 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002057 if (client)
2058 i2c_unregister_device(client);
2059 return 0;
2060}
2061
Jean Delvare69b00892009-12-06 17:06:27 +01002062static int __process_removed_adapter(struct device_driver *d, void *data)
2063{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002064 i2c_do_del_adapter(to_i2c_driver(d), data);
2065 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002066}
2067
David Brownelld64f73b2007-07-12 14:12:28 +02002068/**
2069 * i2c_del_adapter - unregister I2C adapter
2070 * @adap: the adapter being unregistered
2071 * Context: can sleep
2072 *
2073 * This unregisters an I2C adapter which was previously registered
2074 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2075 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002076void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002078 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002079 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002082 mutex_lock(&core_lock);
2083 found = idr_find(&i2c_adapter_idr, adap->nr);
2084 mutex_unlock(&core_lock);
2085 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002086 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002090 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002091 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002092 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002093 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002094 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002095 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002097 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002098 mutex_lock_nested(&adap->userspace_clients_lock,
2099 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002100 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2101 detected) {
2102 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2103 client->addr);
2104 list_del(&client->detected);
2105 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002106 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002107 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002108
Jean Delvaree549c2b2009-06-19 16:58:19 +02002109 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002110 * check the returned value. This is a two-pass process, because
2111 * we can't remove the dummy devices during the first pass: they
2112 * could have been instantiated by real devices wishing to clean
2113 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002114 device_for_each_child(&adap->dev, NULL, __unregister_client);
2115 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Jean Delvare2bb50952009-09-18 22:45:46 +02002117#ifdef CONFIG_I2C_COMPAT
2118 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2119 adap->dev.parent);
2120#endif
2121
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002122 /* device name is gone after device_unregister */
2123 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2124
Wolfram Sang9f924162015-12-23 18:19:28 +01002125 pm_runtime_disable(&adap->dev);
2126
Wolfram Sang26680ee2015-01-29 22:45:09 +01002127 /* wait until all references to the device are gone
2128 *
2129 * FIXME: This is old code and should ideally be replaced by an
2130 * alternative which results in decoupling the lifetime of the struct
2131 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302132 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
David Brownell6e13e642007-05-01 23:26:31 +02002138 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002139 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002141 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002143 /* Clear the device structure in case this adapter is ever going to be
2144 added again */
2145 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
David Brownellc0564602007-05-01 23:26:31 +02002147EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Wolfram Sang54177cc2015-12-17 13:32:36 +01002149/**
2150 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2151 * @dev: The device to scan for I2C timing properties
2152 * @t: the i2c_timings struct to be filled with values
2153 * @use_defaults: bool to use sane defaults derived from the I2C specification
2154 * when properties are not found, otherwise use 0
2155 *
2156 * Scan the device for the generic I2C properties describing timing parameters
2157 * for the signal and fill the given struct with the results. If a property was
2158 * not found and use_defaults was true, then maximum timings are assumed which
2159 * are derived from the I2C specification. If use_defaults is not used, the
2160 * results will be 0, so drivers can apply their own defaults later. The latter
2161 * is mainly intended for avoiding regressions of existing drivers which want
2162 * to switch to this function. New drivers almost always should use the defaults.
2163 */
2164
2165void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2166{
2167 int ret;
2168
2169 memset(t, 0, sizeof(*t));
2170
2171 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2172 if (ret && use_defaults)
2173 t->bus_freq_hz = 100000;
2174
2175 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2176 if (ret && use_defaults) {
2177 if (t->bus_freq_hz <= 100000)
2178 t->scl_rise_ns = 1000;
2179 else if (t->bus_freq_hz <= 400000)
2180 t->scl_rise_ns = 300;
2181 else
2182 t->scl_rise_ns = 120;
2183 }
2184
2185 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2186 if (ret && use_defaults) {
2187 if (t->bus_freq_hz <= 400000)
2188 t->scl_fall_ns = 300;
2189 else
2190 t->scl_fall_ns = 120;
2191 }
2192
2193 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2194
2195 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2196 if (ret && use_defaults)
2197 t->sda_fall_ns = t->scl_fall_ns;
2198}
2199EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2200
David Brownell7b4fbc52007-05-01 23:26:30 +02002201/* ------------------------------------------------------------------------- */
2202
Jean Delvare7ae31482011-03-20 14:50:52 +01002203int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2204{
2205 int res;
2206
2207 mutex_lock(&core_lock);
2208 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2209 mutex_unlock(&core_lock);
2210
2211 return res;
2212}
2213EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2214
Jean Delvare69b00892009-12-06 17:06:27 +01002215static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002216{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002217 if (dev->type != &i2c_adapter_type)
2218 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002219 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002220}
2221
David Brownell7b4fbc52007-05-01 23:26:30 +02002222/*
2223 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002224 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 */
2226
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002227int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002229 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
David Brownell1d0b19c2008-10-14 17:30:05 +02002231 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302232 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002233 return -EAGAIN;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002236 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002238 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Jean Delvare729d6dd2009-06-19 16:58:18 +02002240 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002241 * will have called probe() for all matching-but-unbound devices.
2242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 res = driver_register(&driver->driver);
2244 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002245 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002246
Wolfram Sang44239a52016-07-09 13:35:04 +09002247 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Jean Delvare4735c982008-07-14 22:38:36 +02002249 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002250 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002251
Jean Delvare7eebcb72006-02-05 23:28:21 +01002252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002254EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Jean Delvare69b00892009-12-06 17:06:27 +01002256static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002257{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002258 if (dev->type == &i2c_adapter_type)
2259 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2260 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002261}
2262
David Brownella1d9e6e2007-05-01 23:26:30 +02002263/**
2264 * i2c_del_driver - unregister I2C driver
2265 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002266 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002267 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002268void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Jean Delvare7ae31482011-03-20 14:50:52 +01002270 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002273 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
David Brownellc0564602007-05-01 23:26:31 +02002275EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
David Brownell7b4fbc52007-05-01 23:26:30 +02002277/* ------------------------------------------------------------------------- */
2278
Jean Delvaree48d3312008-01-27 18:14:48 +01002279/**
2280 * i2c_use_client - increments the reference count of the i2c client structure
2281 * @client: the client being referenced
2282 *
2283 * Each live reference to a client should be refcounted. The driver model does
2284 * that automatically as part of driver binding, so that most drivers don't
2285 * need to do this explicitly: they hold a reference until they're unbound
2286 * from the device.
2287 *
2288 * A pointer to the client with the incremented reference counter is returned.
2289 */
2290struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
David Brownell6ea438e2008-07-14 22:38:24 +02002292 if (client && get_device(&client->dev))
2293 return client;
2294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
David Brownellc0564602007-05-01 23:26:31 +02002296EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Jean Delvaree48d3312008-01-27 18:14:48 +01002298/**
2299 * i2c_release_client - release a use of the i2c client structure
2300 * @client: the client being no longer referenced
2301 *
2302 * Must be called when a user of a client is finished with it.
2303 */
2304void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
David Brownell6ea438e2008-07-14 22:38:24 +02002306 if (client)
2307 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
David Brownellc0564602007-05-01 23:26:31 +02002309EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
David Brownell9b766b82008-01-27 18:14:51 +01002311struct i2c_cmd_arg {
2312 unsigned cmd;
2313 void *arg;
2314};
2315
2316static int i2c_cmd(struct device *dev, void *_arg)
2317{
2318 struct i2c_client *client = i2c_verify_client(dev);
2319 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002320 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002321
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002322 if (!client || !client->dev.driver)
2323 return 0;
2324
2325 driver = to_i2c_driver(client->dev.driver);
2326 if (driver->command)
2327 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002328 return 0;
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2332{
David Brownell9b766b82008-01-27 18:14:51 +01002333 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
David Brownell9b766b82008-01-27 18:14:51 +01002335 cmd_arg.cmd = cmd;
2336 cmd_arg.arg = arg;
2337 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
David Brownellc0564602007-05-01 23:26:31 +02002339EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002341#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2342static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2343 void *arg)
2344{
2345 struct of_reconfig_data *rd = arg;
2346 struct i2c_adapter *adap;
2347 struct i2c_client *client;
2348
2349 switch (of_reconfig_get_state_change(action, rd)) {
2350 case OF_RECONFIG_CHANGE_ADD:
2351 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2352 if (adap == NULL)
2353 return NOTIFY_OK; /* not for us */
2354
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002355 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2356 put_device(&adap->dev);
2357 return NOTIFY_OK;
2358 }
2359
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002360 client = of_i2c_register_device(adap, rd->dn);
2361 put_device(&adap->dev);
2362
2363 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002364 dev_err(&adap->dev, "failed to create client for '%s'\n",
2365 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002366 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002367 return notifier_from_errno(PTR_ERR(client));
2368 }
2369 break;
2370 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002371 /* already depopulated? */
2372 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2373 return NOTIFY_OK;
2374
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002375 /* find our device by node */
2376 client = of_find_i2c_device_by_node(rd->dn);
2377 if (client == NULL)
2378 return NOTIFY_OK; /* no? not meant for us */
2379
2380 /* unregister takes one ref away */
2381 i2c_unregister_device(client);
2382
2383 /* and put the reference of the find */
2384 put_device(&client->dev);
2385 break;
2386 }
2387
2388 return NOTIFY_OK;
2389}
2390static struct notifier_block i2c_of_notifier = {
2391 .notifier_call = of_i2c_notify,
2392};
2393#else
2394extern struct notifier_block i2c_of_notifier;
2395#endif /* CONFIG_OF_DYNAMIC */
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397static int __init i2c_init(void)
2398{
2399 int retval;
2400
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002401 retval = of_alias_get_highest_id("i2c");
2402
2403 down_write(&__i2c_board_lock);
2404 if (retval >= __i2c_first_dynamic_bus_num)
2405 __i2c_first_dynamic_bus_num = retval + 1;
2406 up_write(&__i2c_board_lock);
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 retval = bus_register(&i2c_bus_type);
2409 if (retval)
2410 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002411
2412 is_registered = true;
2413
Jean Delvare2bb50952009-09-18 22:45:46 +02002414#ifdef CONFIG_I2C_COMPAT
2415 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2416 if (!i2c_adapter_compat_class) {
2417 retval = -ENOMEM;
2418 goto bus_err;
2419 }
2420#endif
David Brownelle9f13732008-01-27 18:14:52 +01002421 retval = i2c_add_driver(&dummy_driver);
2422 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002423 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002424
2425 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2426 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002427 if (IS_ENABLED(CONFIG_ACPI))
2428 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002429
David Brownelle9f13732008-01-27 18:14:52 +01002430 return 0;
2431
Jean Delvare2bb50952009-09-18 22:45:46 +02002432class_err:
2433#ifdef CONFIG_I2C_COMPAT
2434 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002435bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002436#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002437 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002438 bus_unregister(&i2c_bus_type);
2439 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442static void __exit i2c_exit(void)
2443{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002444 if (IS_ENABLED(CONFIG_ACPI))
2445 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002446 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2447 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002448 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002449#ifdef CONFIG_I2C_COMPAT
2450 class_compat_unregister(i2c_adapter_compat_class);
2451#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002453 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
David Brownella10f9e72008-10-14 17:30:06 +02002456/* We must initialize early, because some subsystems register i2c drivers
2457 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2458 */
2459postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460module_exit(i2c_exit);
2461
2462/* ----------------------------------------------------
2463 * the functional interface to the i2c busses.
2464 * ----------------------------------------------------
2465 */
2466
Wolfram Sangb7f62582015-01-05 23:45:59 +01002467/* Check if val is exceeding the quirk IFF quirk is non 0 */
2468#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2469
2470static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2471{
2472 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2473 err_msg, msg->addr, msg->len,
2474 msg->flags & I2C_M_RD ? "read" : "write");
2475 return -EOPNOTSUPP;
2476}
2477
2478static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2479{
2480 const struct i2c_adapter_quirks *q = adap->quirks;
2481 int max_num = q->max_num_msgs, i;
2482 bool do_len_check = true;
2483
2484 if (q->flags & I2C_AQ_COMB) {
2485 max_num = 2;
2486
2487 /* special checks for combined messages */
2488 if (num == 2) {
2489 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2490 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2491
2492 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2493 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2494
2495 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2496 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2497
2498 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2499 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2500
2501 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2502 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2503
2504 do_len_check = false;
2505 }
2506 }
2507
2508 if (i2c_quirk_exceeded(num, max_num))
2509 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2510
2511 for (i = 0; i < num; i++) {
2512 u16 len = msgs[i].len;
2513
2514 if (msgs[i].flags & I2C_M_RD) {
2515 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2516 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2517 } else {
2518 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2519 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2520 }
2521 }
2522
2523 return 0;
2524}
2525
David Brownella1cdeda2008-07-14 22:38:24 +02002526/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002527 * __i2c_transfer - unlocked flavor of i2c_transfer
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 * Adapter lock must be held when calling this function. No debug logging
2536 * takes place. adap->algo->master_xfer existence isn't checked.
2537 */
2538int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2539{
2540 unsigned long orig_jiffies;
2541 int ret, try;
2542
Wolfram Sangb7f62582015-01-05 23:45:59 +01002543 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2544 return -EOPNOTSUPP;
2545
David Howellsd9a83d62014-03-06 13:35:59 +00002546 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2547 * enabled. This is an efficient way of keeping the for-loop from
2548 * being executed when not needed.
2549 */
2550 if (static_key_false(&i2c_trace_msg)) {
2551 int i;
2552 for (i = 0; i < num; i++)
2553 if (msgs[i].flags & I2C_M_RD)
2554 trace_i2c_read(adap, &msgs[i], i);
2555 else
2556 trace_i2c_write(adap, &msgs[i], i);
2557 }
2558
Jean Delvareb37d2a32012-06-29 07:47:19 -03002559 /* Retry automatically on arbitration loss */
2560 orig_jiffies = jiffies;
2561 for (ret = 0, try = 0; try <= adap->retries; try++) {
2562 ret = adap->algo->master_xfer(adap, msgs, num);
2563 if (ret != -EAGAIN)
2564 break;
2565 if (time_after(jiffies, orig_jiffies + adap->timeout))
2566 break;
2567 }
2568
David Howellsd9a83d62014-03-06 13:35:59 +00002569 if (static_key_false(&i2c_trace_msg)) {
2570 int i;
2571 for (i = 0; i < ret; i++)
2572 if (msgs[i].flags & I2C_M_RD)
2573 trace_i2c_reply(adap, &msgs[i], i);
2574 trace_i2c_result(adap, i, ret);
2575 }
2576
Jean Delvareb37d2a32012-06-29 07:47:19 -03002577 return ret;
2578}
2579EXPORT_SYMBOL(__i2c_transfer);
2580
2581/**
David Brownella1cdeda2008-07-14 22:38:24 +02002582 * i2c_transfer - execute a single or combined I2C message
2583 * @adap: Handle to I2C bus
2584 * @msgs: One or more messages to execute before STOP is issued to
2585 * terminate the operation; each message begins with a START.
2586 * @num: Number of messages to be executed.
2587 *
2588 * Returns negative errno, else the number of messages executed.
2589 *
2590 * Note that there is no requirement that each message be sent to
2591 * the same slave address, although that is the most common model.
2592 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002593int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002595 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
David Brownella1cdeda2008-07-14 22:38:24 +02002597 /* REVISIT the fault reporting model here is weak:
2598 *
2599 * - When we get an error after receiving N bytes from a slave,
2600 * there is no way to report "N".
2601 *
2602 * - When we get a NAK after transmitting N bytes to a slave,
2603 * there is no way to report "N" ... or to let the master
2604 * continue executing the rest of this combined message, if
2605 * that's the appropriate response.
2606 *
2607 * - When for example "num" is two and we successfully complete
2608 * the first message but get an error part way through the
2609 * second, it's unclear whether that should be reported as
2610 * one (discarding status on the second message) or errno
2611 * (discarding status on the first one).
2612 */
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (adap->algo->master_xfer) {
2615#ifdef DEBUG
2616 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002617 dev_dbg(&adap->dev,
2618 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2619 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2620 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002621 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623#endif
2624
Mike Rapoportcea443a82008-01-27 18:14:50 +01002625 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002626 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002627 if (!ret)
2628 /* I2C activity is ongoing. */
2629 return -EAGAIN;
2630 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002631 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002632 }
2633
Jean Delvareb37d2a32012-06-29 07:47:19 -03002634 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002635 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 return ret;
2638 } else {
2639 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002640 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
2642}
David Brownellc0564602007-05-01 23:26:31 +02002643EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
David Brownella1cdeda2008-07-14 22:38:24 +02002645/**
2646 * i2c_master_send - issue a single I2C message in master transmit mode
2647 * @client: Handle to slave device
2648 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002649 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002650 *
2651 * Returns negative errno, or else the number of bytes written.
2652 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002653int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
2655 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002656 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 struct i2c_msg msg;
2658
Jean Delvare815f55f2005-05-07 22:58:46 +02002659 msg.addr = client->addr;
2660 msg.flags = client->flags & I2C_M_TEN;
2661 msg.len = count;
2662 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002663
Jean Delvare815f55f2005-05-07 22:58:46 +02002664 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002666 /*
2667 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2668 * transmitted, else error code.
2669 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002670 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
David Brownellc0564602007-05-01 23:26:31 +02002672EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
David Brownella1cdeda2008-07-14 22:38:24 +02002674/**
2675 * i2c_master_recv - issue a single I2C message in master receive mode
2676 * @client: Handle to slave device
2677 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002678 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002679 *
2680 * Returns negative errno, or else the number of bytes read.
2681 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002682int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Farid Hammane7225acf2010-05-21 18:40:58 +02002684 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct i2c_msg msg;
2686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Jean Delvare815f55f2005-05-07 22:58:46 +02002688 msg.addr = client->addr;
2689 msg.flags = client->flags & I2C_M_TEN;
2690 msg.flags |= I2C_M_RD;
2691 msg.len = count;
2692 msg.buf = buf;
2693
2694 ret = i2c_transfer(adap, &msg, 1);
2695
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002696 /*
2697 * If everything went ok (i.e. 1 msg received), return #bytes received,
2698 * else error code.
2699 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002700 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
David Brownellc0564602007-05-01 23:26:31 +02002702EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704/* ----------------------------------------------------
2705 * the i2c address scanning function
2706 * Will not work for 10-bit addresses!
2707 * ----------------------------------------------------
2708 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002709
Jean Delvare63e4e802010-06-03 11:33:51 +02002710/*
2711 * Legacy default probe function, mostly relevant for SMBus. The default
2712 * probe method is a quick write, but it is known to corrupt the 24RF08
2713 * EEPROMs due to a state machine bug, and could also irreversibly
2714 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2715 * we use a short byte read instead. Also, some bus drivers don't implement
2716 * quick write, so we fallback to a byte read in that case too.
2717 * On x86, there is another special case for FSC hardware monitoring chips,
2718 * which want regular byte reads (address 0x73.) Fortunately, these are the
2719 * only known chips using this I2C address on PC hardware.
2720 * Returns 1 if probe succeeded, 0 if not.
2721 */
2722static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2723{
2724 int err;
2725 union i2c_smbus_data dummy;
2726
2727#ifdef CONFIG_X86
2728 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2729 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2730 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2731 I2C_SMBUS_BYTE_DATA, &dummy);
2732 else
2733#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002734 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2735 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002736 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2737 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002738 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2739 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2740 I2C_SMBUS_BYTE, &dummy);
2741 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002742 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2743 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002744 err = -EOPNOTSUPP;
2745 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002746
2747 return err >= 0;
2748}
2749
Jean Delvareccfbbd02009-12-06 17:06:25 +01002750static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002751 struct i2c_driver *driver)
2752{
2753 struct i2c_board_info info;
2754 struct i2c_adapter *adapter = temp_client->adapter;
2755 int addr = temp_client->addr;
2756 int err;
2757
2758 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002759 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002760 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002761 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2762 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002763 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002764 }
2765
Wolfram Sang9bccc702015-07-17 14:48:56 +02002766 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002767 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002768 return 0;
2769
Jean Delvareccfbbd02009-12-06 17:06:25 +01002770 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002771 if (!i2c_default_probe(adapter, addr))
2772 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002773
2774 /* Finally call the custom detection function */
2775 memset(&info, 0, sizeof(struct i2c_board_info));
2776 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002777 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002778 if (err) {
2779 /* -ENODEV is returned if the detection fails. We catch it
2780 here as this isn't an error. */
2781 return err == -ENODEV ? 0 : err;
2782 }
2783
2784 /* Consistency check */
2785 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002786 dev_err(&adapter->dev,
2787 "%s detection function provided no name for 0x%x\n",
2788 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002789 } else {
2790 struct i2c_client *client;
2791
2792 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002793 if (adapter->class & I2C_CLASS_DEPRECATED)
2794 dev_warn(&adapter->dev,
2795 "This adapter will soon drop class based instantiation of devices. "
2796 "Please make sure client 0x%02x gets instantiated by other means. "
2797 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2798 info.addr);
2799
Jean Delvare4735c982008-07-14 22:38:36 +02002800 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2801 info.type, info.addr);
2802 client = i2c_new_device(adapter, &info);
2803 if (client)
2804 list_add_tail(&client->detected, &driver->clients);
2805 else
2806 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2807 info.type, info.addr);
2808 }
2809 return 0;
2810}
2811
2812static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2813{
Jean Delvarec3813d62009-12-14 21:17:25 +01002814 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002815 struct i2c_client *temp_client;
2816 int i, err = 0;
2817 int adap_id = i2c_adapter_id(adapter);
2818
Jean Delvarec3813d62009-12-14 21:17:25 +01002819 address_list = driver->address_list;
2820 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002821 return 0;
2822
Wolfram Sang45552272014-07-10 13:46:21 +02002823 /* Warn that the adapter lost class based instantiation */
2824 if (adapter->class == I2C_CLASS_DEPRECATED) {
2825 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002826 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2827 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002828 driver->driver.name);
2829 return 0;
2830 }
2831
Jean Delvare51b54ba2010-10-24 18:16:58 +02002832 /* Stop here if the classes do not match */
2833 if (!(adapter->class & driver->class))
2834 return 0;
2835
Jean Delvare4735c982008-07-14 22:38:36 +02002836 /* Set up a temporary client to help detect callback */
2837 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2838 if (!temp_client)
2839 return -ENOMEM;
2840 temp_client->adapter = adapter;
2841
Jean Delvarec3813d62009-12-14 21:17:25 +01002842 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002843 dev_dbg(&adapter->dev,
2844 "found normal entry for adapter %d, addr 0x%02x\n",
2845 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002846 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002847 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002848 if (unlikely(err))
2849 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002850 }
2851
Jean Delvare4735c982008-07-14 22:38:36 +02002852 kfree(temp_client);
2853 return err;
2854}
2855
Jean Delvared44f19d2010-08-11 18:20:57 +02002856int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2857{
2858 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2859 I2C_SMBUS_QUICK, NULL) >= 0;
2860}
2861EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2862
Jean Delvare12b5053a2007-05-01 23:26:31 +02002863struct i2c_client *
2864i2c_new_probed_device(struct i2c_adapter *adap,
2865 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002866 unsigned short const *addr_list,
2867 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002868{
2869 int i;
2870
Jean Delvare8031d792010-08-11 18:21:00 +02002871 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002872 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002873
Jean Delvare12b5053a2007-05-01 23:26:31 +02002874 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2875 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002876 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002877 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2878 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002879 continue;
2880 }
2881
Wolfram Sang9bccc702015-07-17 14:48:56 +02002882 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002883 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002884 dev_dbg(&adap->dev,
2885 "Address 0x%02x already in use, not probing\n",
2886 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002887 continue;
2888 }
2889
Jean Delvare63e4e802010-06-03 11:33:51 +02002890 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002891 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002892 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002893 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002894
2895 if (addr_list[i] == I2C_CLIENT_END) {
2896 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2897 return NULL;
2898 }
2899
2900 info->addr = addr_list[i];
2901 return i2c_new_device(adap, info);
2902}
2903EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2904
Jean Delvared735b342011-03-20 14:50:52 +01002905struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002908
Jean Delvarecaada322008-01-27 18:14:49 +01002909 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002910 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002911 if (!adapter)
2912 goto exit;
2913
2914 if (try_module_get(adapter->owner))
2915 get_device(&adapter->dev);
2916 else
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002917 adapter = NULL;
2918
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002919 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002920 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002921 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
David Brownellc0564602007-05-01 23:26:31 +02002923EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925void i2c_put_adapter(struct i2c_adapter *adap)
2926{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002927 if (!adap)
2928 return;
2929
2930 put_device(&adap->dev);
2931 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
David Brownellc0564602007-05-01 23:26:31 +02002933EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935/* The SMBus parts */
2936
David Brownell438d6c22006-12-10 21:21:31 +01002937#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002938static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002941
Farid Hammane7225acf2010-05-21 18:40:58 +02002942 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002943 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 data = data ^ POLY;
2945 data = data << 1;
2946 }
2947 return (u8)(data >> 8);
2948}
2949
Jean Delvare421ef472005-10-26 21:28:55 +02002950/* Incremental CRC8 over count bytes in the array pointed to by p */
2951static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 int i;
2954
Farid Hammane7225acf2010-05-21 18:40:58 +02002955 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002956 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return crc;
2958}
2959
Jean Delvare421ef472005-10-26 21:28:55 +02002960/* Assume a 7-bit address, which is reasonable for SMBus */
2961static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Jean Delvare421ef472005-10-26 21:28:55 +02002963 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002964 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002965 pec = i2c_smbus_pec(pec, &addr, 1);
2966
2967 /* The data buffer follows */
2968 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Jean Delvare421ef472005-10-26 21:28:55 +02002971/* Used for write only transactions */
2972static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Jean Delvare421ef472005-10-26 21:28:55 +02002974 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2975 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
Jean Delvare421ef472005-10-26 21:28:55 +02002978/* Return <0 on CRC error
2979 If there was a write before this read (most cases) we need to take the
2980 partial CRC from the write part into account.
2981 Note that this function does modify the message (we need to decrease the
2982 message length to hide the CRC byte from the caller). */
2983static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Jean Delvare421ef472005-10-26 21:28:55 +02002985 u8 rpec = msg->buf[--msg->len];
2986 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002989 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002991 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
David Brownell438d6c22006-12-10 21:21:31 +01002993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
2995
David Brownella1cdeda2008-07-14 22:38:24 +02002996/**
2997 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2998 * @client: Handle to slave device
2999 *
3000 * This executes the SMBus "receive byte" protocol, returning negative errno
3001 * else the byte received from the device.
3002 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003003s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003006 int status;
3007
3008 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3009 I2C_SMBUS_READ, 0,
3010 I2C_SMBUS_BYTE, &data);
3011 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
David Brownellc0564602007-05-01 23:26:31 +02003013EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
David Brownella1cdeda2008-07-14 22:38:24 +02003015/**
3016 * i2c_smbus_write_byte - SMBus "send byte" protocol
3017 * @client: Handle to slave device
3018 * @value: Byte to be sent
3019 *
3020 * This executes the SMBus "send byte" protocol, returning negative errno
3021 * else zero on success.
3022 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003023s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Farid Hammane7225acf2010-05-21 18:40:58 +02003025 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02003026 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
David Brownellc0564602007-05-01 23:26:31 +02003028EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
David Brownella1cdeda2008-07-14 22:38:24 +02003030/**
3031 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
3032 * @client: Handle to slave device
3033 * @command: Byte interpreted by slave
3034 *
3035 * This executes the SMBus "read byte" protocol, returning negative errno
3036 * else a data byte received from the device.
3037 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003038s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003041 int status;
3042
3043 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3044 I2C_SMBUS_READ, command,
3045 I2C_SMBUS_BYTE_DATA, &data);
3046 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
David Brownellc0564602007-05-01 23:26:31 +02003048EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
David Brownella1cdeda2008-07-14 22:38:24 +02003050/**
3051 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
3052 * @client: Handle to slave device
3053 * @command: Byte interpreted by slave
3054 * @value: Byte being written
3055 *
3056 * This executes the SMBus "write byte" protocol, returning negative errno
3057 * else zero on success.
3058 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003059s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3060 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 union i2c_smbus_data data;
3063 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003064 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3065 I2C_SMBUS_WRITE, command,
3066 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
David Brownellc0564602007-05-01 23:26:31 +02003068EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
David Brownella1cdeda2008-07-14 22:38:24 +02003070/**
3071 * i2c_smbus_read_word_data - SMBus "read word" protocol
3072 * @client: Handle to slave device
3073 * @command: Byte interpreted by slave
3074 *
3075 * This executes the SMBus "read word" protocol, returning negative errno
3076 * else a 16-bit unsigned "word" received from the device.
3077 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003078s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
3080 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003081 int status;
3082
3083 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3084 I2C_SMBUS_READ, command,
3085 I2C_SMBUS_WORD_DATA, &data);
3086 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
David Brownellc0564602007-05-01 23:26:31 +02003088EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
David Brownella1cdeda2008-07-14 22:38:24 +02003090/**
3091 * i2c_smbus_write_word_data - SMBus "write word" protocol
3092 * @client: Handle to slave device
3093 * @command: Byte interpreted by slave
3094 * @value: 16-bit "word" being written
3095 *
3096 * This executes the SMBus "write word" protocol, returning negative errno
3097 * else zero on success.
3098 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003099s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3100 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 union i2c_smbus_data data;
3103 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003104 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3105 I2C_SMBUS_WRITE, command,
3106 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
David Brownellc0564602007-05-01 23:26:31 +02003108EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
David Brownella64ec072007-10-13 23:56:31 +02003110/**
David Brownella1cdeda2008-07-14 22:38:24 +02003111 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003112 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003113 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003114 * @values: Byte array into which data will be read; big enough to hold
3115 * the data returned by the slave. SMBus allows at most 32 bytes.
3116 *
David Brownella1cdeda2008-07-14 22:38:24 +02003117 * This executes the SMBus "block read" protocol, returning negative errno
3118 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003119 *
3120 * Note that using this function requires that the client's adapter support
3121 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3122 * support this; its emulation through I2C messaging relies on a specific
3123 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3124 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003125s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003126 u8 *values)
3127{
3128 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003129 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003130
David Brownell24a5bb72008-07-14 22:38:23 +02003131 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3132 I2C_SMBUS_READ, command,
3133 I2C_SMBUS_BLOCK_DATA, &data);
3134 if (status)
3135 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003136
3137 memcpy(values, &data.block[1], data.block[0]);
3138 return data.block[0];
3139}
3140EXPORT_SYMBOL(i2c_smbus_read_block_data);
3141
David Brownella1cdeda2008-07-14 22:38:24 +02003142/**
3143 * i2c_smbus_write_block_data - SMBus "block write" protocol
3144 * @client: Handle to slave device
3145 * @command: Byte interpreted by slave
3146 * @length: Size of data block; SMBus allows at most 32 bytes
3147 * @values: Byte array which will be written.
3148 *
3149 * This executes the SMBus "block write" protocol, returning negative errno
3150 * else zero on success.
3151 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003152s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003153 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
3155 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (length > I2C_SMBUS_BLOCK_MAX)
3158 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003160 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003161 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3162 I2C_SMBUS_WRITE, command,
3163 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
David Brownellc0564602007-05-01 23:26:31 +02003165EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
3167/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003168s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003169 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003172 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003173
Jean Delvare4b2643d2007-07-12 14:12:29 +02003174 if (length > I2C_SMBUS_BLOCK_MAX)
3175 length = I2C_SMBUS_BLOCK_MAX;
3176 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003177 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3178 I2C_SMBUS_READ, command,
3179 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3180 if (status < 0)
3181 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003182
3183 memcpy(values, &data.block[1], data.block[0]);
3184 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
David Brownellc0564602007-05-01 23:26:31 +02003186EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Jean Delvare0cc43a12011-01-10 22:11:23 +01003188s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003189 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003190{
3191 union i2c_smbus_data data;
3192
3193 if (length > I2C_SMBUS_BLOCK_MAX)
3194 length = I2C_SMBUS_BLOCK_MAX;
3195 data.block[0] = length;
3196 memcpy(data.block + 1, values, length);
3197 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3198 I2C_SMBUS_WRITE, command,
3199 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3200}
David Brownellc0564602007-05-01 23:26:31 +02003201EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003202
David Brownell438d6c22006-12-10 21:21:31 +01003203/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003205static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3206 unsigned short flags,
3207 char read_write, u8 command, int size,
3208 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
3210 /* So we need to generate a series of msgs. In the case of writing, we
3211 need to use only one message; when reading, we need two. We initialize
3212 most things with sane defaults, to keep the code below somewhat
3213 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003214 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3215 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003216 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003218 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003219 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003220 struct i2c_msg msg[2] = {
3221 {
3222 .addr = addr,
3223 .flags = flags,
3224 .len = 1,
3225 .buf = msgbuf0,
3226 }, {
3227 .addr = addr,
3228 .flags = flags | I2C_M_RD,
3229 .len = 0,
3230 .buf = msgbuf1,
3231 },
3232 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003235 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 case I2C_SMBUS_QUICK:
3237 msg[0].len = 0;
3238 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003239 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3240 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 num = 1;
3242 break;
3243 case I2C_SMBUS_BYTE:
3244 if (read_write == I2C_SMBUS_READ) {
3245 /* Special case: only a read! */
3246 msg[0].flags = I2C_M_RD | flags;
3247 num = 1;
3248 }
3249 break;
3250 case I2C_SMBUS_BYTE_DATA:
3251 if (read_write == I2C_SMBUS_READ)
3252 msg[1].len = 1;
3253 else {
3254 msg[0].len = 2;
3255 msgbuf0[1] = data->byte;
3256 }
3257 break;
3258 case I2C_SMBUS_WORD_DATA:
3259 if (read_write == I2C_SMBUS_READ)
3260 msg[1].len = 2;
3261 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003262 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003264 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 break;
3267 case I2C_SMBUS_PROC_CALL:
3268 num = 2; /* Special case */
3269 read_write = I2C_SMBUS_READ;
3270 msg[0].len = 3;
3271 msg[1].len = 2;
3272 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003273 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 break;
3275 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003277 msg[1].flags |= I2C_M_RECV_LEN;
3278 msg[1].len = 1; /* block length will be added by
3279 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 } else {
3281 msg[0].len = data->block[0] + 2;
3282 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003283 dev_err(&adapter->dev,
3284 "Invalid block write size %d\n",
3285 data->block[0]);
3286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003288 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 msgbuf0[i] = data->block[i-1];
3290 }
3291 break;
3292 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003293 num = 2; /* Another special case */
3294 read_write = I2C_SMBUS_READ;
3295 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003296 dev_err(&adapter->dev,
3297 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003298 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003299 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003300 }
3301 msg[0].len = data->block[0] + 2;
3302 for (i = 1; i < msg[0].len; i++)
3303 msgbuf0[i] = data->block[i-1];
3304 msg[1].flags |= I2C_M_RECV_LEN;
3305 msg[1].len = 1; /* block length will be added by
3306 the underlying bus driver */
3307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 case I2C_SMBUS_I2C_BLOCK_DATA:
3309 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003310 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 } else {
3312 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003313 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003314 dev_err(&adapter->dev,
3315 "Invalid block write size %d\n",
3316 data->block[0]);
3317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
3319 for (i = 1; i <= data->block[0]; i++)
3320 msgbuf0[i] = data->block[i];
3321 }
3322 break;
3323 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003324 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3325 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327
Jean Delvare421ef472005-10-26 21:28:55 +02003328 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3329 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3330 if (i) {
3331 /* Compute PEC if first message is a write */
3332 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003333 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003334 i2c_smbus_add_pec(&msg[0]);
3335 else /* Write followed by read */
3336 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3337 }
3338 /* Ask for PEC if last message is a read */
3339 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003340 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003341 }
3342
David Brownell24a5bb72008-07-14 22:38:23 +02003343 status = i2c_transfer(adapter, msg, num);
3344 if (status < 0)
3345 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Jean Delvare421ef472005-10-26 21:28:55 +02003347 /* Check PEC if last message is a read */
3348 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003349 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3350 if (status < 0)
3351 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003352 }
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003355 switch (size) {
3356 case I2C_SMBUS_BYTE:
3357 data->byte = msgbuf0[0];
3358 break;
3359 case I2C_SMBUS_BYTE_DATA:
3360 data->byte = msgbuf1[0];
3361 break;
3362 case I2C_SMBUS_WORD_DATA:
3363 case I2C_SMBUS_PROC_CALL:
3364 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3365 break;
3366 case I2C_SMBUS_I2C_BLOCK_DATA:
3367 for (i = 0; i < data->block[0]; i++)
3368 data->block[i+1] = msgbuf1[i];
3369 break;
3370 case I2C_SMBUS_BLOCK_DATA:
3371 case I2C_SMBUS_BLOCK_PROC_CALL:
3372 for (i = 0; i < msgbuf1[0] + 1; i++)
3373 data->block[i] = msgbuf1[i];
3374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 return 0;
3377}
3378
David Brownella1cdeda2008-07-14 22:38:24 +02003379/**
3380 * i2c_smbus_xfer - execute SMBus protocol operations
3381 * @adapter: Handle to I2C bus
3382 * @addr: Address of SMBus slave on that bus
3383 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3384 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3385 * @command: Byte interpreted by slave, for protocols which use such bytes
3386 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3387 * @data: Data to be read or written
3388 *
3389 * This executes an SMBus protocol operation, and returns a negative
3390 * errno code else zero on success.
3391 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003392s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003393 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003394 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003396 unsigned long orig_jiffies;
3397 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
David Howells8a325992014-03-06 13:36:06 +00003400 /* If enabled, the following two tracepoints are conditional on
3401 * read_write and protocol.
3402 */
3403 trace_smbus_write(adapter, addr, flags, read_write,
3404 command, protocol, data);
3405 trace_smbus_read(adapter, addr, flags, read_write,
3406 command, protocol);
3407
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003408 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003411 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003412
3413 /* Retry automatically on arbitration loss */
3414 orig_jiffies = jiffies;
3415 for (res = 0, try = 0; try <= adapter->retries; try++) {
3416 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3417 read_write, command,
3418 protocol, data);
3419 if (res != -EAGAIN)
3420 break;
3421 if (time_after(jiffies,
3422 orig_jiffies + adapter->timeout))
3423 break;
3424 }
Peter Rosin8320f492016-05-04 22:15:27 +02003425 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003427 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003428 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003429 /*
3430 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3431 * implement native support for the SMBus operation.
3432 */
3433 }
3434
David Howells8a325992014-03-06 13:36:06 +00003435 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3436 command, protocol, data);
3437
3438trace:
3439 /* If enabled, the reply tracepoint is conditional on read_write. */
3440 trace_smbus_reply(adapter, addr, flags, read_write,
3441 command, protocol, data);
3442 trace_smbus_result(adapter, addr, flags, read_write,
3443 command, protocol, res);
3444
3445 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Irina Tirdea01eef962015-08-12 17:31:33 +03003449/**
3450 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3451 * @client: Handle to slave device
3452 * @command: Byte interpreted by slave
3453 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3454 * @values: Byte array into which data will be read; big enough to hold
3455 * the data returned by the slave. SMBus allows at most
3456 * I2C_SMBUS_BLOCK_MAX bytes.
3457 *
3458 * This executes the SMBus "block read" protocol if supported by the adapter.
3459 * If block read is not supported, it emulates it using either word or byte
3460 * read protocols depending on availability.
3461 *
3462 * The addresses of the I2C slave device that are accessed with this function
3463 * must be mapped to a linear region, so that a block read will have the same
3464 * effect as a byte read. Before using this function you must double-check
3465 * if the I2C slave does support exchanging a block transfer with a byte
3466 * transfer.
3467 */
3468s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3469 u8 command, u8 length, u8 *values)
3470{
3471 u8 i = 0;
3472 int status;
3473
3474 if (length > I2C_SMBUS_BLOCK_MAX)
3475 length = I2C_SMBUS_BLOCK_MAX;
3476
3477 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3478 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3479
3480 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3481 return -EOPNOTSUPP;
3482
3483 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3484 while ((i + 2) <= length) {
3485 status = i2c_smbus_read_word_data(client, command + i);
3486 if (status < 0)
3487 return status;
3488 values[i] = status & 0xff;
3489 values[i + 1] = status >> 8;
3490 i += 2;
3491 }
3492 }
3493
3494 while (i < length) {
3495 status = i2c_smbus_read_byte_data(client, command + i);
3496 if (status < 0)
3497 return status;
3498 values[i] = status;
3499 i++;
3500 }
3501
3502 return i;
3503}
3504EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3505
Jean Delvared5fd1202015-01-26 20:59:31 +01003506#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003507int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3508{
3509 int ret;
3510
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003511 if (!client || !slave_cb) {
3512 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003513 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003514 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003515
Wolfram Sangc6909d62015-08-05 21:12:54 +02003516 if (!(client->flags & I2C_CLIENT_SLAVE))
3517 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3518 __func__);
3519
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003520 if (!(client->flags & I2C_CLIENT_TEN)) {
3521 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003522 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003523 if (ret) {
3524 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003525 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003526 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003527 }
3528
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003529 if (!client->adapter->algo->reg_slave) {
3530 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003531 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003532 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003533
3534 client->slave_cb = slave_cb;
3535
3536 i2c_lock_adapter(client->adapter);
3537 ret = client->adapter->algo->reg_slave(client);
3538 i2c_unlock_adapter(client->adapter);
3539
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003540 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003541 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003542 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3543 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003544
3545 return ret;
3546}
3547EXPORT_SYMBOL_GPL(i2c_slave_register);
3548
3549int i2c_slave_unregister(struct i2c_client *client)
3550{
3551 int ret;
3552
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003553 if (!client->adapter->algo->unreg_slave) {
3554 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003555 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003556 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003557
3558 i2c_lock_adapter(client->adapter);
3559 ret = client->adapter->algo->unreg_slave(client);
3560 i2c_unlock_adapter(client->adapter);
3561
3562 if (ret == 0)
3563 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003564 else
3565 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003566
3567 return ret;
3568}
3569EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003570#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3573MODULE_DESCRIPTION("I2C-Bus main module");
3574MODULE_LICENSE("GPL");