blob: 65242666901ec741b4a9bb16d33d959a93d739d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram 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,
171 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200172{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200173 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300174 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300175 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200176 int ret;
177
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300178 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300179 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300180
Jarkko Nikula5853b222016-08-12 17:02:53 +0300181 ret = i2c_acpi_do_lookup(adev, &lookup);
182 if (ret)
183 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200184
Jarkko Nikula5853b222016-08-12 17:02:53 +0300185 info->fwnode = acpi_fwnode_handle(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300186 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200187
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300188 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300189 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300190 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
191 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300192 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300193
194 resource_list_for_each_entry(entry, &resource_list) {
195 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300196 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300197 break;
198 }
199 }
200
201 acpi_dev_free_resource_list(&resource_list);
202
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300203 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
204
205 return 0;
206}
207
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300208static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300209 struct acpi_device *adev,
210 struct i2c_board_info *info)
211{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200212 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300213 acpi_device_set_enumerated(adev);
214
215 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200216 adev->power.flags.ignore_parent = false;
217 dev_err(&adapter->dev,
218 "failed to add I2C device %s from ACPI\n",
219 dev_name(&adev->dev));
220 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300221}
222
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300223static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300224 void *data, void **return_value)
225{
226 struct i2c_adapter *adapter = data;
227 struct acpi_device *adev;
228 acpi_handle adapter_handle;
229 struct i2c_board_info info;
230
231 if (acpi_bus_get_device(handle, &adev))
232 return AE_OK;
233
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300234 if (i2c_acpi_get_info(adev, &info, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300235 return AE_OK;
236
237 if (adapter_handle != ACPI_HANDLE(&adapter->dev))
238 return AE_OK;
239
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300240 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200241
242 return AE_OK;
243}
244
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300245#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300246
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200247/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300248 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200249 * @adap: pointer to adapter
250 *
251 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
252 * namespace. When a device is found it will be added to the Linux device
253 * model and bound to the corresponding ACPI handle.
254 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300255static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200256{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200257 acpi_status status;
258
Dustin Byford8eb5c872015-10-23 12:27:07 -0700259 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200260 return;
261
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300262 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300263 I2C_ACPI_MAX_SCAN_DEPTH,
264 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 adap, NULL);
266 if (ACPI_FAILURE(status))
267 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
268}
269
Jarkko Nikula5853b222016-08-12 17:02:53 +0300270static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
271 void *data, void **return_value)
272{
273 struct i2c_acpi_lookup *lookup = data;
274 struct acpi_device *adev;
275
276 if (acpi_bus_get_device(handle, &adev))
277 return AE_OK;
278
279 if (i2c_acpi_do_lookup(adev, lookup))
280 return AE_OK;
281
282 if (lookup->search_handle != lookup->adapter_handle)
283 return AE_OK;
284
285 if (lookup->speed <= lookup->min_speed)
286 lookup->min_speed = lookup->speed;
287
288 return AE_OK;
289}
290
291/**
292 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
293 * @dev: The device owning the bus
294 *
295 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
296 * devices connected to this bus and use the speed of slowest device.
297 *
298 * Returns the speed in Hz or zero
299 */
300u32 i2c_acpi_find_bus_speed(struct device *dev)
301{
302 struct i2c_acpi_lookup lookup;
303 struct i2c_board_info dummy;
304 acpi_status status;
305
306 if (!has_acpi_companion(dev))
307 return 0;
308
309 memset(&lookup, 0, sizeof(lookup));
310 lookup.search_handle = ACPI_HANDLE(dev);
311 lookup.min_speed = UINT_MAX;
312 lookup.info = &dummy;
313
314 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
315 I2C_ACPI_MAX_SCAN_DEPTH,
316 i2c_acpi_lookup_speed, NULL,
317 &lookup, NULL);
318
319 if (ACPI_FAILURE(status)) {
320 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
321 return 0;
322 }
323
324 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
325}
326EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
327
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300328static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300329{
330 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
331
332 if (!adapter)
333 return 0;
334
335 return ACPI_HANDLE(dev) == (acpi_handle)data;
336}
337
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300338static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300339{
340 return ACPI_COMPANION(dev) == data;
341}
342
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300343static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300344{
345 struct device *dev;
346
347 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300348 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300349 return dev ? i2c_verify_adapter(dev) : NULL;
350}
351
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300352static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300353{
354 struct device *dev;
355
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300356 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300357 return dev ? i2c_verify_client(dev) : NULL;
358}
359
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300360static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300361 void *arg)
362{
363 struct acpi_device *adev = arg;
364 struct i2c_board_info info;
365 acpi_handle adapter_handle;
366 struct i2c_adapter *adapter;
367 struct i2c_client *client;
368
369 switch (value) {
370 case ACPI_RECONFIG_DEVICE_ADD:
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300371 if (i2c_acpi_get_info(adev, &info, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300372 break;
373
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300374 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300375 if (!adapter)
376 break;
377
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300378 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300379 break;
380 case ACPI_RECONFIG_DEVICE_REMOVE:
381 if (!acpi_device_enumerated(adev))
382 break;
383
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300384 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300385 if (!client)
386 break;
387
388 i2c_unregister_device(client);
389 put_device(&client->dev);
390 break;
391 }
392
393 return NOTIFY_OK;
394}
395
396static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300397 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300398};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200399#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300400static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300401extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200402#endif /* CONFIG_ACPI */
403
404#ifdef CONFIG_ACPI_I2C_OPREGION
405static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
406 u8 cmd, u8 *data, u8 data_len)
407{
408
409 struct i2c_msg msgs[2];
410 int ret;
411 u8 *buffer;
412
413 buffer = kzalloc(data_len, GFP_KERNEL);
414 if (!buffer)
415 return AE_NO_MEMORY;
416
417 msgs[0].addr = client->addr;
418 msgs[0].flags = client->flags;
419 msgs[0].len = 1;
420 msgs[0].buf = &cmd;
421
422 msgs[1].addr = client->addr;
423 msgs[1].flags = client->flags | I2C_M_RD;
424 msgs[1].len = data_len;
425 msgs[1].buf = buffer;
426
427 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
428 if (ret < 0)
429 dev_err(&client->adapter->dev, "i2c read failed\n");
430 else
431 memcpy(data, buffer, data_len);
432
433 kfree(buffer);
434 return ret;
435}
436
437static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
438 u8 cmd, u8 *data, u8 data_len)
439{
440
441 struct i2c_msg msgs[1];
442 u8 *buffer;
443 int ret = AE_OK;
444
445 buffer = kzalloc(data_len + 1, GFP_KERNEL);
446 if (!buffer)
447 return AE_NO_MEMORY;
448
449 buffer[0] = cmd;
450 memcpy(buffer + 1, data, data_len);
451
452 msgs[0].addr = client->addr;
453 msgs[0].flags = client->flags;
454 msgs[0].len = data_len + 1;
455 msgs[0].buf = buffer;
456
457 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
458 if (ret < 0)
459 dev_err(&client->adapter->dev, "i2c write failed\n");
460
461 kfree(buffer);
462 return ret;
463}
464
465static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300466i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200467 u32 bits, u64 *value64,
468 void *handler_context, void *region_context)
469{
470 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300471 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200472 struct acpi_connection_info *info = &data->info;
473 struct acpi_resource_i2c_serialbus *sb;
474 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300475 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200476 struct acpi_resource *ares;
477 u32 accessor_type = function >> 16;
478 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100479 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200480 int status;
481
482 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
483 if (ACPI_FAILURE(ret))
484 return ret;
485
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300486 client = kzalloc(sizeof(*client), GFP_KERNEL);
487 if (!client) {
488 ret = AE_NO_MEMORY;
489 goto err;
490 }
491
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200492 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
493 ret = AE_BAD_PARAMETER;
494 goto err;
495 }
496
497 sb = &ares->data.i2c_serial_bus;
498 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
499 ret = AE_BAD_PARAMETER;
500 goto err;
501 }
502
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300503 client->adapter = adapter;
504 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200505
506 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300507 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200508
509 switch (accessor_type) {
510 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
511 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300512 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200513 if (status >= 0) {
514 gsb->bdata = status;
515 status = 0;
516 }
517 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300518 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200519 }
520 break;
521
522 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
523 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300524 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200525 if (status >= 0) {
526 gsb->bdata = status;
527 status = 0;
528 }
529 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300530 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200531 gsb->bdata);
532 }
533 break;
534
535 case ACPI_GSB_ACCESS_ATTRIB_WORD:
536 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300537 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200538 if (status >= 0) {
539 gsb->wdata = status;
540 status = 0;
541 }
542 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300543 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200544 gsb->wdata);
545 }
546 break;
547
548 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
549 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300550 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200551 gsb->data);
552 if (status >= 0) {
553 gsb->len = status;
554 status = 0;
555 }
556 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300557 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200558 gsb->len, gsb->data);
559 }
560 break;
561
562 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
563 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300564 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200565 gsb->data, info->access_length);
566 if (status > 0)
567 status = 0;
568 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300569 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200570 gsb->data, info->access_length);
571 }
572 break;
573
574 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900575 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
576 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200577 ret = AE_BAD_PARAMETER;
578 goto err;
579 }
580
581 gsb->status = status;
582
583 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300584 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200585 ACPI_FREE(ares);
586 return ret;
587}
588
589
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300590static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200591{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200592 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300593 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200594 acpi_status status;
595
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200596 if (!adapter->dev.parent)
597 return -ENODEV;
598
599 handle = ACPI_HANDLE(adapter->dev.parent);
600
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200601 if (!handle)
602 return -ENODEV;
603
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300604 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200605 GFP_KERNEL);
606 if (!data)
607 return -ENOMEM;
608
609 data->adapter = adapter;
610 status = acpi_bus_attach_private_data(handle, (void *)data);
611 if (ACPI_FAILURE(status)) {
612 kfree(data);
613 return -ENOMEM;
614 }
615
616 status = acpi_install_address_space_handler(handle,
617 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300618 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200619 NULL,
620 data);
621 if (ACPI_FAILURE(status)) {
622 dev_err(&adapter->dev, "Error installing i2c space handler\n");
623 acpi_bus_detach_private_data(handle);
624 kfree(data);
625 return -ENOMEM;
626 }
627
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800628 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200629 return 0;
630}
631
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300632static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200633{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200634 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300635 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200636 acpi_status status;
637
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200638 if (!adapter->dev.parent)
639 return;
640
641 handle = ACPI_HANDLE(adapter->dev.parent);
642
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200643 if (!handle)
644 return;
645
646 acpi_remove_address_space_handler(handle,
647 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300648 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200649
650 status = acpi_bus_get_private_data(handle, (void **)&data);
651 if (ACPI_SUCCESS(status))
652 kfree(data);
653
654 acpi_bus_detach_private_data(handle);
655}
656#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300657static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200658{ }
659
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300660static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200661{ return 0; }
662#endif /* CONFIG_ACPI_I2C_OPREGION */
663
David Brownellf37dd802007-02-13 22:09:00 +0100664/* ------------------------------------------------------------------------- */
665
Jean Delvared2653e92008-04-29 23:11:39 +0200666static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
667 const struct i2c_client *client)
668{
669 while (id->name[0]) {
670 if (strcmp(client->name, id->name) == 0)
671 return id;
672 id++;
673 }
674 return NULL;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static int i2c_device_match(struct device *dev, struct device_driver *drv)
678{
Jean Delvare51298d12009-09-18 22:45:45 +0200679 struct i2c_client *client = i2c_verify_client(dev);
680 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200681
Jean Delvare51298d12009-09-18 22:45:45 +0200682 if (!client)
683 return 0;
684
Grant Likely959e85f2010-06-08 07:48:19 -0600685 /* Attempt an OF style match */
686 if (of_driver_match_device(dev, drv))
687 return 1;
688
Mika Westerberg907ddf82012-11-23 12:23:40 +0100689 /* Then ACPI style match */
690 if (acpi_driver_match_device(dev, drv))
691 return 1;
692
Jean Delvare51298d12009-09-18 22:45:45 +0200693 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200694 /* match on an id table if there is one */
695 if (driver->id_table)
696 return i2c_match_id(driver->id_table, client) != NULL;
697
Jean Delvareeb8a7902008-05-18 20:49:41 +0200698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Kay Sievers7eff2e72007-08-14 15:15:12 +0200701static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200702{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100703 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800704 int rc;
705
706 rc = acpi_device_uevent_modalias(dev, env);
707 if (rc != -ENODEV)
708 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200709
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100710 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200711}
712
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530713/* i2c bus recovery routines */
714static int get_scl_gpio_value(struct i2c_adapter *adap)
715{
716 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
717}
718
719static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
720{
721 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
722}
723
724static int get_sda_gpio_value(struct i2c_adapter *adap)
725{
726 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
727}
728
729static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
730{
731 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
732 struct device *dev = &adap->dev;
733 int ret = 0;
734
735 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
736 GPIOF_OUT_INIT_HIGH, "i2c-scl");
737 if (ret) {
738 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
739 return ret;
740 }
741
742 if (bri->get_sda) {
743 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
744 /* work without SDA polling */
745 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
746 bri->sda_gpio);
747 bri->get_sda = NULL;
748 }
749 }
750
751 return ret;
752}
753
754static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
755{
756 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
757
758 if (bri->get_sda)
759 gpio_free(bri->sda_gpio);
760
761 gpio_free(bri->scl_gpio);
762}
763
764/*
765 * We are generating clock pulses. ndelay() determines durating of clk pulses.
766 * We will generate clock with rate 100 KHz and so duration of both clock levels
767 * is: delay in ns = (10^6 / 100) / 2
768 */
769#define RECOVERY_NDELAY 5000
770#define RECOVERY_CLK_CNT 9
771
772static int i2c_generic_recovery(struct i2c_adapter *adap)
773{
774 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
775 int i = 0, val = 1, ret = 0;
776
777 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300778 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530779
Jan Luebbe8b062602015-07-08 16:35:06 +0200780 bri->set_scl(adap, val);
781 ndelay(RECOVERY_NDELAY);
782
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530783 /*
784 * By this time SCL is high, as we need to give 9 falling-rising edges
785 */
786 while (i++ < RECOVERY_CLK_CNT * 2) {
787 if (val) {
788 /* Break if SDA is high */
789 if (bri->get_sda && bri->get_sda(adap))
790 break;
791 /* SCL shouldn't be low here */
792 if (!bri->get_scl(adap)) {
793 dev_err(&adap->dev,
794 "SCL is stuck low, exit recovery\n");
795 ret = -EBUSY;
796 break;
797 }
798 }
799
800 val = !val;
801 bri->set_scl(adap, val);
802 ndelay(RECOVERY_NDELAY);
803 }
804
805 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300806 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530807
808 return ret;
809}
810
811int i2c_generic_scl_recovery(struct i2c_adapter *adap)
812{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530813 return i2c_generic_recovery(adap);
814}
Mark Brownc1c21f42015-04-15 19:18:39 +0100815EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530816
817int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
818{
819 int ret;
820
821 ret = i2c_get_gpios_for_recovery(adap);
822 if (ret)
823 return ret;
824
825 ret = i2c_generic_recovery(adap);
826 i2c_put_gpios_for_recovery(adap);
827
828 return ret;
829}
Mark Brownc1c21f42015-04-15 19:18:39 +0100830EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530831
832int i2c_recover_bus(struct i2c_adapter *adap)
833{
834 if (!adap->bus_recovery_info)
835 return -EOPNOTSUPP;
836
837 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
838 return adap->bus_recovery_info->recover_bus(adap);
839}
Mark Brownc1c21f42015-04-15 19:18:39 +0100840EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530841
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900842static void i2c_init_recovery(struct i2c_adapter *adap)
843{
844 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
845 char *err_str;
846
847 if (!bri)
848 return;
849
850 if (!bri->recover_bus) {
851 err_str = "no recover_bus() found";
852 goto err;
853 }
854
855 /* Generic GPIO recovery */
856 if (bri->recover_bus == i2c_generic_gpio_recovery) {
857 if (!gpio_is_valid(bri->scl_gpio)) {
858 err_str = "invalid SCL gpio";
859 goto err;
860 }
861
862 if (gpio_is_valid(bri->sda_gpio))
863 bri->get_sda = get_sda_gpio_value;
864 else
865 bri->get_sda = NULL;
866
867 bri->get_scl = get_scl_gpio_value;
868 bri->set_scl = set_scl_gpio_value;
869 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
870 /* Generic SCL recovery */
871 if (!bri->set_scl || !bri->get_scl) {
872 err_str = "no {get|set}_scl() found";
873 goto err;
874 }
875 }
876
877 return;
878 err:
879 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
880 adap->bus_recovery_info = NULL;
881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static int i2c_device_probe(struct device *dev)
884{
Jean Delvare51298d12009-09-18 22:45:45 +0200885 struct i2c_client *client = i2c_verify_client(dev);
886 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100887 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200888
Jean Delvare51298d12009-09-18 22:45:45 +0200889 if (!client)
890 return 0;
891
Mika Westerberg845c8772015-05-06 13:29:08 +0300892 if (!client->irq) {
893 int irq = -ENOENT;
894
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700895 if (dev->of_node) {
896 irq = of_irq_get_byname(dev->of_node, "irq");
897 if (irq == -EINVAL || irq == -ENODATA)
898 irq = of_irq_get(dev->of_node, 0);
899 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300900 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700901 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100902 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200903 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100904 if (irq < 0)
905 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200906
907 client->irq = irq;
908 }
909
Jean Delvare51298d12009-09-18 22:45:45 +0200910 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200911 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200912 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200913
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700914 if (client->flags & I2C_CLIENT_WAKE) {
915 int wakeirq = -ENOENT;
916
917 if (dev->of_node) {
918 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
919 if (wakeirq == -EPROBE_DEFER)
920 return wakeirq;
921 }
922
923 device_init_wakeup(&client->dev, true);
924
925 if (wakeirq > 0 && wakeirq != client->irq)
926 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
927 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200928 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700929 else
930 status = 0;
931
932 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300933 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700934 }
935
David Brownell7b4fbc52007-05-01 23:26:30 +0200936 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200937
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200938 status = of_clk_set_defaults(dev->of_node, false);
939 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700940 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200941
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200942 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100943 if (status == -EPROBE_DEFER)
944 goto err_clear_wakeup_irq;
945
946 status = driver->probe(client, i2c_match_id(driver->id_table, client));
947 if (status)
948 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100949
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700950 return 0;
951
952err_detach_pm_domain:
953 dev_pm_domain_detach(&client->dev, true);
954err_clear_wakeup_irq:
955 dev_pm_clear_wake_irq(&client->dev);
956 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100957 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static int i2c_device_remove(struct device *dev)
961{
Jean Delvare51298d12009-09-18 22:45:45 +0200962 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200963 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100964 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200965
Jean Delvare51298d12009-09-18 22:45:45 +0200966 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200967 return 0;
968
969 driver = to_i2c_driver(dev->driver);
970 if (driver->remove) {
971 dev_dbg(dev, "remove\n");
972 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200973 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100974
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200975 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700976
977 dev_pm_clear_wake_irq(&client->dev);
978 device_init_wakeup(&client->dev, false);
979
David Brownella1d9e6e2007-05-01 23:26:30 +0200980 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
David Brownellf37dd802007-02-13 22:09:00 +0100983static void i2c_device_shutdown(struct device *dev)
984{
Jean Delvare51298d12009-09-18 22:45:45 +0200985 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100986 struct i2c_driver *driver;
987
Jean Delvare51298d12009-09-18 22:45:45 +0200988 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100989 return;
990 driver = to_i2c_driver(dev->driver);
991 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200992 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100993}
994
David Brownell9c1600e2007-05-01 23:26:31 +0200995static void i2c_client_dev_release(struct device *dev)
996{
997 kfree(to_i2c_client(dev));
998}
999
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001000static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001001show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001002{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001003 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1004 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001005}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001006static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001007
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001008static ssize_t
1009show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001010{
1011 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001012 int len;
1013
1014 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1015 if (len != -ENODEV)
1016 return len;
1017
Jean Delvareeb8a7902008-05-18 20:49:41 +02001018 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001019}
Jean Delvare51298d12009-09-18 22:45:45 +02001020static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1021
1022static struct attribute *i2c_dev_attrs[] = {
1023 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001024 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001025 &dev_attr_modalias.attr,
1026 NULL
1027};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001028ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001029
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001030struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001031 .name = "i2c",
1032 .match = i2c_device_match,
1033 .probe = i2c_device_probe,
1034 .remove = i2c_device_remove,
1035 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001036};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001037EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001038
Jean Delvare51298d12009-09-18 22:45:45 +02001039static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001040 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001041 .uevent = i2c_device_uevent,
1042 .release = i2c_client_dev_release,
1043};
1044
David Brownell9b766b82008-01-27 18:14:51 +01001045
1046/**
1047 * i2c_verify_client - return parameter as i2c_client, or NULL
1048 * @dev: device, probably from some driver model iterator
1049 *
1050 * When traversing the driver model tree, perhaps using driver model
1051 * iterators like @device_for_each_child(), you can't assume very much
1052 * about the nodes you find. Use this function to avoid oopses caused
1053 * by wrongly treating some non-I2C device as an i2c_client.
1054 */
1055struct i2c_client *i2c_verify_client(struct device *dev)
1056{
Jean Delvare51298d12009-09-18 22:45:45 +02001057 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001058 ? to_i2c_client(dev)
1059 : NULL;
1060}
1061EXPORT_SYMBOL(i2c_verify_client);
1062
1063
Wolfram Sangda899f52015-05-18 21:09:12 +02001064/* Return a unique address which takes the flags of the client into account */
1065static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1066{
1067 unsigned short addr = client->addr;
1068
1069 /* For some client flags, add an arbitrary offset to avoid collisions */
1070 if (client->flags & I2C_CLIENT_TEN)
1071 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1072
1073 if (client->flags & I2C_CLIENT_SLAVE)
1074 addr |= I2C_ADDR_OFFSET_SLAVE;
1075
1076 return addr;
1077}
1078
Jean Delvare3a89db52010-06-03 11:33:52 +02001079/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001080 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001081static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001082{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001083 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001084 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001085 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001086 return -EINVAL;
1087 } else {
1088 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001089 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001090 return -EINVAL;
1091 }
1092 return 0;
1093}
1094
Jean Delvare656b8762010-06-03 11:33:53 +02001095/* And this is a strict address validity check, used when probing. If a
1096 * device uses a reserved address, then it shouldn't be probed. 7-bit
1097 * addressing is assumed, 10-bit address devices are rare and should be
1098 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001099static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001100{
1101 /*
1102 * Reserved addresses per I2C specification:
1103 * 0x00 General call address / START byte
1104 * 0x01 CBUS address
1105 * 0x02 Reserved for different bus format
1106 * 0x03 Reserved for future purposes
1107 * 0x04-0x07 Hs-mode master code
1108 * 0x78-0x7b 10-bit slave addressing
1109 * 0x7c-0x7f Reserved for future purposes
1110 */
1111 if (addr < 0x08 || addr > 0x77)
1112 return -EINVAL;
1113 return 0;
1114}
1115
Jean Delvare3b5f7942010-06-03 11:33:55 +02001116static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1117{
1118 struct i2c_client *client = i2c_verify_client(dev);
1119 int addr = *(int *)addrp;
1120
Wolfram Sang9bccc702015-07-17 14:48:56 +02001121 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001122 return -EBUSY;
1123 return 0;
1124}
1125
Michael Lawnick08263742010-08-11 18:21:02 +02001126/* walk up mux tree */
1127static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1128{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001129 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001130 int result;
1131
1132 result = device_for_each_child(&adapter->dev, &addr,
1133 __i2c_check_addr_busy);
1134
Jean Delvare97cc4d42010-10-24 18:16:57 +02001135 if (!result && parent)
1136 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001137
1138 return result;
1139}
1140
1141/* recurse down mux tree */
1142static int i2c_check_mux_children(struct device *dev, void *addrp)
1143{
1144 int result;
1145
1146 if (dev->type == &i2c_adapter_type)
1147 result = device_for_each_child(dev, addrp,
1148 i2c_check_mux_children);
1149 else
1150 result = __i2c_check_addr_busy(dev, addrp);
1151
1152 return result;
1153}
1154
Jean Delvare3b5f7942010-06-03 11:33:55 +02001155static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1156{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001157 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001158 int result = 0;
1159
Jean Delvare97cc4d42010-10-24 18:16:57 +02001160 if (parent)
1161 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001162
1163 if (!result)
1164 result = device_for_each_child(&adapter->dev, &addr,
1165 i2c_check_mux_children);
1166
1167 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001168}
1169
David Brownell9c1600e2007-05-01 23:26:31 +02001170/**
Peter Rosin8320f492016-05-04 22:15:27 +02001171 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001172 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001173 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1174 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001175 */
Peter Rosin8320f492016-05-04 22:15:27 +02001176static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1177 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001178{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001179 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001180}
Jean Delvarefe61e072010-08-11 18:20:58 +02001181
1182/**
Peter Rosin8320f492016-05-04 22:15:27 +02001183 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001184 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001185 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1186 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001187 */
Peter Rosin8320f492016-05-04 22:15:27 +02001188static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1189 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001190{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001191 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001192}
1193
1194/**
Peter Rosin8320f492016-05-04 22:15:27 +02001195 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001196 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001197 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1198 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001199 */
Peter Rosin8320f492016-05-04 22:15:27 +02001200static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1201 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001202{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001203 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001204}
Jean Delvarefe61e072010-08-11 18:20:58 +02001205
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001206static void i2c_dev_set_name(struct i2c_adapter *adap,
1207 struct i2c_client *client)
1208{
1209 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1210
1211 if (adev) {
1212 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1213 return;
1214 }
1215
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001216 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001217 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001218}
1219
Jean Delvarefe61e072010-08-11 18:20:58 +02001220/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001221 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001222 * @adap: the adapter managing the device
1223 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001224 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001225 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001226 * Create an i2c device. Binding is handled through driver model
1227 * probe()/remove() methods. A driver may be bound to this device when we
1228 * return from this function, or any later moment (e.g. maybe hotplugging will
1229 * load the driver module). This call is not appropriate for use by mainboard
1230 * initialization logic, which usually runs during an arch_initcall() long
1231 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001232 *
1233 * This returns the new i2c client, which may be saved for later use with
1234 * i2c_unregister_device(); or NULL to indicate an error.
1235 */
1236struct i2c_client *
1237i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1238{
1239 struct i2c_client *client;
1240 int status;
1241
1242 client = kzalloc(sizeof *client, GFP_KERNEL);
1243 if (!client)
1244 return NULL;
1245
1246 client->adapter = adap;
1247
1248 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001249
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001250 if (info->archdata)
1251 client->dev.archdata = *info->archdata;
1252
Marc Pignatee354252008-08-28 08:33:22 +02001253 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001254 client->addr = info->addr;
1255 client->irq = info->irq;
1256
David Brownell9c1600e2007-05-01 23:26:31 +02001257 strlcpy(client->name, info->type, sizeof(client->name));
1258
Wolfram Sangc4019b72015-07-17 12:50:06 +02001259 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001260 if (status) {
1261 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1262 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1263 goto out_err_silent;
1264 }
1265
Jean Delvaref8a227e2009-06-19 16:58:18 +02001266 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001267 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001268 if (status)
1269 goto out_err;
1270
1271 client->dev.parent = &client->adapter->dev;
1272 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001273 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001274 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001275 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001276
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001277 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001278 status = device_register(&client->dev);
1279 if (status)
1280 goto out_err;
1281
Jean Delvaref8a227e2009-06-19 16:58:18 +02001282 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1283 client->name, dev_name(&client->dev));
1284
David Brownell9c1600e2007-05-01 23:26:31 +02001285 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001286
1287out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001288 dev_err(&adap->dev,
1289 "Failed to register i2c client %s at 0x%02x (%d)\n",
1290 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001291out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001292 kfree(client);
1293 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001294}
1295EXPORT_SYMBOL_GPL(i2c_new_device);
1296
1297
1298/**
1299 * i2c_unregister_device - reverse effect of i2c_new_device()
1300 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001301 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001302 */
1303void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001304{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001305 if (client->dev.of_node)
1306 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001307 if (ACPI_COMPANION(&client->dev))
1308 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001309 device_unregister(&client->dev);
1310}
David Brownell9c1600e2007-05-01 23:26:31 +02001311EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001312
1313
Jean Delvare60b129d2008-05-11 20:37:06 +02001314static const struct i2c_device_id dummy_id[] = {
1315 { "dummy", 0 },
1316 { },
1317};
1318
Jean Delvared2653e92008-04-29 23:11:39 +02001319static int dummy_probe(struct i2c_client *client,
1320 const struct i2c_device_id *id)
1321{
1322 return 0;
1323}
1324
1325static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001326{
1327 return 0;
1328}
1329
1330static struct i2c_driver dummy_driver = {
1331 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001332 .probe = dummy_probe,
1333 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001334 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001335};
1336
1337/**
1338 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1339 * @adapter: the adapter managing the device
1340 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001341 * Context: can sleep
1342 *
1343 * This returns an I2C client bound to the "dummy" driver, intended for use
1344 * with devices that consume multiple addresses. Examples of such chips
1345 * include various EEPROMS (like 24c04 and 24c08 models).
1346 *
1347 * These dummy devices have two main uses. First, most I2C and SMBus calls
1348 * except i2c_transfer() need a client handle; the dummy will be that handle.
1349 * And second, this prevents the specified address from being bound to a
1350 * different driver.
1351 *
1352 * This returns the new i2c client, which should be saved for later use with
1353 * i2c_unregister_device(); or NULL to indicate an error.
1354 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001355struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001356{
1357 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001358 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001359 };
1360
David Brownelle9f13732008-01-27 18:14:52 +01001361 return i2c_new_device(adapter, &info);
1362}
1363EXPORT_SYMBOL_GPL(i2c_new_dummy);
1364
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001365/**
1366 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1367 * and create the associated device
1368 * @client: Handle to the primary client
1369 * @name: Handle to specify which secondary address to get
1370 * @default_addr: Used as a fallback if no secondary address was specified
1371 * Context: can sleep
1372 *
1373 * I2C clients can be composed of multiple I2C slaves bound together in a single
1374 * component. The I2C client driver then binds to the master I2C slave and needs
1375 * to create I2C dummy clients to communicate with all the other slaves.
1376 *
1377 * This function creates and returns an I2C dummy client whose I2C address is
1378 * retrieved from the platform firmware based on the given slave name. If no
1379 * address is specified by the firmware default_addr is used.
1380 *
1381 * On DT-based platforms the address is retrieved from the "reg" property entry
1382 * cell whose "reg-names" value matches the slave name.
1383 *
1384 * This returns the new i2c client, which should be saved for later use with
1385 * i2c_unregister_device(); or NULL to indicate an error.
1386 */
1387struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1388 const char *name,
1389 u16 default_addr)
1390{
1391 struct device_node *np = client->dev.of_node;
1392 u32 addr = default_addr;
1393 int i;
1394
1395 if (np) {
1396 i = of_property_match_string(np, "reg-names", name);
1397 if (i >= 0)
1398 of_property_read_u32_index(np, "reg", i, &addr);
1399 }
1400
1401 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1402 return i2c_new_dummy(client->adapter, addr);
1403}
1404EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1405
David Brownellf37dd802007-02-13 22:09:00 +01001406/* ------------------------------------------------------------------------- */
1407
David Brownell16ffadf2007-05-01 23:26:28 +02001408/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1409
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001410static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
David Brownellef2c83212007-05-01 23:26:28 +02001412 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 complete(&adap->dev_released);
1414}
1415
Jean Delvare99cd8e22009-06-19 16:58:20 +02001416/*
Jean Delvare390946b2012-09-10 10:14:02 +02001417 * This function is only needed for mutex_lock_nested, so it is never
1418 * called unless locking correctness checking is enabled. Thus we
1419 * make it inline to avoid a compiler warning. That's what gcc ends up
1420 * doing anyway.
1421 */
1422static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1423{
1424 unsigned int depth = 0;
1425
1426 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1427 depth++;
1428
1429 return depth;
1430}
1431
1432/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001433 * Let users instantiate I2C devices through sysfs. This can be used when
1434 * platform initialization code doesn't contain the proper data for
1435 * whatever reason. Also useful for drivers that do device detection and
1436 * detection fails, either because the device uses an unexpected address,
1437 * or this is a compatible device with different ID register values.
1438 *
1439 * Parameter checking may look overzealous, but we really don't want
1440 * the user to provide incorrect parameters.
1441 */
1442static ssize_t
1443i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1444 const char *buf, size_t count)
1445{
1446 struct i2c_adapter *adap = to_i2c_adapter(dev);
1447 struct i2c_board_info info;
1448 struct i2c_client *client;
1449 char *blank, end;
1450 int res;
1451
Jean Delvare99cd8e22009-06-19 16:58:20 +02001452 memset(&info, 0, sizeof(struct i2c_board_info));
1453
1454 blank = strchr(buf, ' ');
1455 if (!blank) {
1456 dev_err(dev, "%s: Missing parameters\n", "new_device");
1457 return -EINVAL;
1458 }
1459 if (blank - buf > I2C_NAME_SIZE - 1) {
1460 dev_err(dev, "%s: Invalid device name\n", "new_device");
1461 return -EINVAL;
1462 }
1463 memcpy(info.type, buf, blank - buf);
1464
1465 /* Parse remaining parameters, reject extra parameters */
1466 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1467 if (res < 1) {
1468 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1469 return -EINVAL;
1470 }
1471 if (res > 1 && end != '\n') {
1472 dev_err(dev, "%s: Extra parameters\n", "new_device");
1473 return -EINVAL;
1474 }
1475
Wolfram Sangcfa03272015-07-27 14:03:38 +02001476 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1477 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1478 info.flags |= I2C_CLIENT_TEN;
1479 }
1480
1481 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1482 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1483 info.flags |= I2C_CLIENT_SLAVE;
1484 }
1485
Jean Delvare99cd8e22009-06-19 16:58:20 +02001486 client = i2c_new_device(adap, &info);
1487 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001488 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001489
1490 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001491 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001492 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001493 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001494 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1495 info.type, info.addr);
1496
1497 return count;
1498}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001499static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001500
1501/*
1502 * And of course let the users delete the devices they instantiated, if
1503 * they got it wrong. This interface can only be used to delete devices
1504 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1505 * don't delete devices to which some kernel code still has references.
1506 *
1507 * Parameter checking may look overzealous, but we really don't want
1508 * the user to delete the wrong device.
1509 */
1510static ssize_t
1511i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1512 const char *buf, size_t count)
1513{
1514 struct i2c_adapter *adap = to_i2c_adapter(dev);
1515 struct i2c_client *client, *next;
1516 unsigned short addr;
1517 char end;
1518 int res;
1519
1520 /* Parse parameters, reject extra parameters */
1521 res = sscanf(buf, "%hi%c", &addr, &end);
1522 if (res < 1) {
1523 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1524 return -EINVAL;
1525 }
1526 if (res > 1 && end != '\n') {
1527 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1528 return -EINVAL;
1529 }
1530
1531 /* Make sure the device was added through sysfs */
1532 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001533 mutex_lock_nested(&adap->userspace_clients_lock,
1534 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001535 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1536 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001537 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001538 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1539 "delete_device", client->name, client->addr);
1540
1541 list_del(&client->detected);
1542 i2c_unregister_device(client);
1543 res = count;
1544 break;
1545 }
1546 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001547 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001548
1549 if (res < 0)
1550 dev_err(dev, "%s: Can't find device in list\n",
1551 "delete_device");
1552 return res;
1553}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001554static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1555 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001556
1557static struct attribute *i2c_adapter_attrs[] = {
1558 &dev_attr_name.attr,
1559 &dev_attr_new_device.attr,
1560 &dev_attr_delete_device.attr,
1561 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001562};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001563ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001564
Michael Lawnick08263742010-08-11 18:21:02 +02001565struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001566 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001567 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001568};
Michael Lawnick08263742010-08-11 18:21:02 +02001569EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Stephen Warren643dd092012-04-17 12:43:33 -06001571/**
1572 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1573 * @dev: device, probably from some driver model iterator
1574 *
1575 * When traversing the driver model tree, perhaps using driver model
1576 * iterators like @device_for_each_child(), you can't assume very much
1577 * about the nodes you find. Use this function to avoid oopses caused
1578 * by wrongly treating some non-I2C device as an i2c_adapter.
1579 */
1580struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1581{
1582 return (dev->type == &i2c_adapter_type)
1583 ? to_i2c_adapter(dev)
1584 : NULL;
1585}
1586EXPORT_SYMBOL(i2c_verify_adapter);
1587
Jean Delvare2bb50952009-09-18 22:45:46 +02001588#ifdef CONFIG_I2C_COMPAT
1589static struct class_compat *i2c_adapter_compat_class;
1590#endif
1591
David Brownell9c1600e2007-05-01 23:26:31 +02001592static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1593{
1594 struct i2c_devinfo *devinfo;
1595
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001596 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001597 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1598 if (devinfo->busnum == adapter->nr
1599 && !i2c_new_device(adapter,
1600 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001601 dev_err(&adapter->dev,
1602 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001603 devinfo->board_info.addr);
1604 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001605 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001606}
1607
Wolfram Sang687b81d2013-07-11 12:56:15 +01001608/* OF support code */
1609
1610#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001611static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1612 struct device_node *node)
1613{
1614 struct i2c_client *result;
1615 struct i2c_board_info info = {};
1616 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001617 const __be32 *addr_be;
1618 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001619 int len;
1620
1621 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1622
1623 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1624 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1625 node->full_name);
1626 return ERR_PTR(-EINVAL);
1627 }
1628
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001629 addr_be = of_get_property(node, "reg", &len);
1630 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001631 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1632 node->full_name);
1633 return ERR_PTR(-EINVAL);
1634 }
1635
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001636 addr = be32_to_cpup(addr_be);
1637 if (addr & I2C_TEN_BIT_ADDRESS) {
1638 addr &= ~I2C_TEN_BIT_ADDRESS;
1639 info.flags |= I2C_CLIENT_TEN;
1640 }
1641
1642 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1643 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1644 info.flags |= I2C_CLIENT_SLAVE;
1645 }
1646
1647 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001648 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1649 info.addr, node->full_name);
1650 return ERR_PTR(-EINVAL);
1651 }
1652
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001653 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001654 info.of_node = of_node_get(node);
1655 info.archdata = &dev_ad;
1656
1657 if (of_get_property(node, "wakeup-source", NULL))
1658 info.flags |= I2C_CLIENT_WAKE;
1659
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001660 result = i2c_new_device(adap, &info);
1661 if (result == NULL) {
1662 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1663 node->full_name);
1664 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001665 return ERR_PTR(-EINVAL);
1666 }
1667 return result;
1668}
1669
Wolfram Sang687b81d2013-07-11 12:56:15 +01001670static void of_i2c_register_devices(struct i2c_adapter *adap)
1671{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001672 struct device_node *node;
1673
1674 /* Only register child devices if the adapter has a node pointer set */
1675 if (!adap->dev.of_node)
1676 return;
1677
1678 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1679
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001680 for_each_available_child_of_node(adap->dev.of_node, node) {
1681 if (of_node_test_and_set_flag(node, OF_POPULATED))
1682 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001683 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001684 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001685}
1686
1687static int of_dev_node_match(struct device *dev, void *data)
1688{
1689 return dev->of_node == data;
1690}
1691
1692/* must call put_device() when done with returned i2c_client device */
1693struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1694{
1695 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001696 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001697
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001698 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001699 if (!dev)
1700 return NULL;
1701
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001702 client = i2c_verify_client(dev);
1703 if (!client)
1704 put_device(dev);
1705
1706 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001707}
1708EXPORT_SYMBOL(of_find_i2c_device_by_node);
1709
1710/* must call put_device() when done with returned i2c_adapter device */
1711struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1712{
1713 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001714 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001715
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001716 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001717 if (!dev)
1718 return NULL;
1719
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001720 adapter = i2c_verify_adapter(dev);
1721 if (!adapter)
1722 put_device(dev);
1723
1724 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001725}
1726EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001727
1728/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1729struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1730{
1731 struct i2c_adapter *adapter;
1732
1733 adapter = of_find_i2c_adapter_by_node(node);
1734 if (!adapter)
1735 return NULL;
1736
1737 if (!try_module_get(adapter->owner)) {
1738 put_device(&adapter->dev);
1739 adapter = NULL;
1740 }
1741
1742 return adapter;
1743}
1744EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001745#else
1746static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1747#endif /* CONFIG_OF */
1748
Jean Delvare69b00892009-12-06 17:06:27 +01001749static int i2c_do_add_adapter(struct i2c_driver *driver,
1750 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001751{
Jean Delvare4735c982008-07-14 22:38:36 +02001752 /* Detect supported devices on that bus, and instantiate them */
1753 i2c_detect(adap, driver);
1754
1755 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001756 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001757 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1758 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001759 dev_warn(&adap->dev,
1760 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001761 /* We ignore the return code; if it fails, too bad */
1762 driver->attach_adapter(adap);
1763 }
1764 return 0;
1765}
1766
Jean Delvare69b00892009-12-06 17:06:27 +01001767static int __process_new_adapter(struct device_driver *d, void *data)
1768{
1769 return i2c_do_add_adapter(to_i2c_driver(d), data);
1770}
1771
Peter Rosind1ed7982016-08-25 23:07:01 +02001772static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1773 .lock_bus = i2c_adapter_lock_bus,
1774 .trylock_bus = i2c_adapter_trylock_bus,
1775 .unlock_bus = i2c_adapter_unlock_bus,
1776};
1777
David Brownell6e13e642007-05-01 23:26:31 +02001778static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001780 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
David Brownell1d0b19c2008-10-14 17:30:05 +02001782 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301783 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001784 res = -EAGAIN;
1785 goto out_list;
1786 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001787
Jean Delvare2236baa2010-11-15 22:40:38 +01001788 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001789 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001790 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001791
1792 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001793 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001794 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001795 }
1796
Peter Rosind1ed7982016-08-25 23:07:01 +02001797 if (!adap->lock_ops)
1798 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001799
Mika Kuoppala194684e2009-12-06 17:06:22 +01001800 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001801 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001802 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001803 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Jean Delvare8fcfef62009-03-28 21:34:43 +01001805 /* Set default timeout to 1 second if not already set */
1806 if (adap->timeout == 0)
1807 adap->timeout = HZ;
1808
Kay Sievers27d9c182009-01-07 14:29:16 +01001809 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001810 adap->dev.bus = &i2c_bus_type;
1811 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001812 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001813 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001814 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001815 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001818 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1819
Charles Keepax6ada5c12015-04-16 13:05:19 +01001820 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001821 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001822 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001823
Jean Delvare2bb50952009-09-18 22:45:46 +02001824#ifdef CONFIG_I2C_COMPAT
1825 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1826 adap->dev.parent);
1827 if (res)
1828 dev_warn(&adap->dev,
1829 "Failed to create compatibility class link\n");
1830#endif
1831
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001832 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301833
Jean Delvare729d6dd2009-06-19 16:58:18 +02001834 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001835 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001836 i2c_acpi_register_devices(adap);
1837 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001838
David Brownell6e13e642007-05-01 23:26:31 +02001839 if (adap->nr < __i2c_first_dynamic_bus_num)
1840 i2c_scan_static_board_info(adap);
1841
Jean Delvare4735c982008-07-14 22:38:36 +02001842 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001843 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001844 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001845 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001846
1847 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001848
Jean Delvareb119c6c2006-08-15 18:26:30 +02001849out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001850 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001851 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001852 mutex_unlock(&core_lock);
1853 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
David Brownell6e13e642007-05-01 23:26:31 +02001856/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001857 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1858 * @adap: the adapter to register (with adap->nr initialized)
1859 * Context: can sleep
1860 *
1861 * See i2c_add_numbered_adapter() for details.
1862 */
1863static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1864{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001865 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001866
1867 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001868 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001869 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001870 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001871 return id == -ENOSPC ? -EBUSY : id;
1872
1873 return i2c_register_adapter(adap);
1874}
1875
1876/**
David Brownell6e13e642007-05-01 23:26:31 +02001877 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1878 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001879 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001880 *
1881 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001882 * doesn't matter or when its bus number is specified by an dt alias.
1883 * Examples of bases when the bus number doesn't matter: I2C adapters
1884 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001885 *
1886 * When this returns zero, a new bus number was allocated and stored
1887 * in adap->nr, and the specified adapter became available for clients.
1888 * Otherwise, a negative errno value is returned.
1889 */
1890int i2c_add_adapter(struct i2c_adapter *adapter)
1891{
Doug Andersonee5c2742013-03-01 08:57:31 -08001892 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001893 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001894
Doug Andersonee5c2742013-03-01 08:57:31 -08001895 if (dev->of_node) {
1896 id = of_alias_get_id(dev->of_node, "i2c");
1897 if (id >= 0) {
1898 adapter->nr = id;
1899 return __i2c_add_numbered_adapter(adapter);
1900 }
1901 }
1902
Jean Delvarecaada322008-01-27 18:14:49 +01001903 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001904 id = idr_alloc(&i2c_adapter_idr, adapter,
1905 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001906 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001907 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001908 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001909
1910 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001911
David Brownell6e13e642007-05-01 23:26:31 +02001912 return i2c_register_adapter(adapter);
1913}
1914EXPORT_SYMBOL(i2c_add_adapter);
1915
1916/**
1917 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1918 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001919 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001920 *
1921 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001922 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1923 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001924 * is used to properly configure I2C devices.
1925 *
Grant Likely488bf312011-07-25 17:49:43 +02001926 * If the requested bus number is set to -1, then this function will behave
1927 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1928 *
David Brownell6e13e642007-05-01 23:26:31 +02001929 * If no devices have pre-been declared for this bus, then be sure to
1930 * register the adapter before any dynamically allocated ones. Otherwise
1931 * the required bus ID may not be available.
1932 *
1933 * When this returns zero, the specified adapter became available for
1934 * clients using the bus number provided in adap->nr. Also, the table
1935 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1936 * and the appropriate driver model device nodes are created. Otherwise, a
1937 * negative errno value is returned.
1938 */
1939int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1940{
Grant Likely488bf312011-07-25 17:49:43 +02001941 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1942 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001943
Doug Andersonee5c2742013-03-01 08:57:31 -08001944 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001945}
1946EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1947
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001948static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001949 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001950{
Jean Delvare4735c982008-07-14 22:38:36 +02001951 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001952
Jean Delvareacec2112009-03-28 21:34:40 +01001953 /* Remove the devices we created ourselves as the result of hardware
1954 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001955 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1956 if (client->adapter == adapter) {
1957 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1958 client->name, client->addr);
1959 list_del(&client->detected);
1960 i2c_unregister_device(client);
1961 }
1962 }
Jean Delvare026526f2008-01-27 18:14:49 +01001963}
1964
Jean Delvaree549c2b2009-06-19 16:58:19 +02001965static int __unregister_client(struct device *dev, void *dummy)
1966{
1967 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001968 if (client && strcmp(client->name, "dummy"))
1969 i2c_unregister_device(client);
1970 return 0;
1971}
1972
1973static int __unregister_dummy(struct device *dev, void *dummy)
1974{
1975 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001976 if (client)
1977 i2c_unregister_device(client);
1978 return 0;
1979}
1980
Jean Delvare69b00892009-12-06 17:06:27 +01001981static int __process_removed_adapter(struct device_driver *d, void *data)
1982{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001983 i2c_do_del_adapter(to_i2c_driver(d), data);
1984 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001985}
1986
David Brownelld64f73b2007-07-12 14:12:28 +02001987/**
1988 * i2c_del_adapter - unregister I2C adapter
1989 * @adap: the adapter being unregistered
1990 * Context: can sleep
1991 *
1992 * This unregisters an I2C adapter which was previously registered
1993 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1994 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001995void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001997 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001998 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002001 mutex_lock(&core_lock);
2002 found = idr_find(&i2c_adapter_idr, adap->nr);
2003 mutex_unlock(&core_lock);
2004 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002005 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002006 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002009 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002010 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002011 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002012 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002013 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002014 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002016 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002017 mutex_lock_nested(&adap->userspace_clients_lock,
2018 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002019 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2020 detected) {
2021 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2022 client->addr);
2023 list_del(&client->detected);
2024 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002025 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002026 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002027
Jean Delvaree549c2b2009-06-19 16:58:19 +02002028 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002029 * check the returned value. This is a two-pass process, because
2030 * we can't remove the dummy devices during the first pass: they
2031 * could have been instantiated by real devices wishing to clean
2032 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002033 device_for_each_child(&adap->dev, NULL, __unregister_client);
2034 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Jean Delvare2bb50952009-09-18 22:45:46 +02002036#ifdef CONFIG_I2C_COMPAT
2037 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2038 adap->dev.parent);
2039#endif
2040
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002041 /* device name is gone after device_unregister */
2042 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2043
Wolfram Sang9f924162015-12-23 18:19:28 +01002044 pm_runtime_disable(&adap->dev);
2045
Wolfram Sang26680ee2015-01-29 22:45:09 +01002046 /* wait until all references to the device are gone
2047 *
2048 * FIXME: This is old code and should ideally be replaced by an
2049 * alternative which results in decoupling the lifetime of the struct
2050 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302051 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002052 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
David Brownell6e13e642007-05-01 23:26:31 +02002057 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002058 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002060 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002062 /* Clear the device structure in case this adapter is ever going to be
2063 added again */
2064 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
David Brownellc0564602007-05-01 23:26:31 +02002066EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Wolfram Sang54177cc2015-12-17 13:32:36 +01002068/**
2069 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2070 * @dev: The device to scan for I2C timing properties
2071 * @t: the i2c_timings struct to be filled with values
2072 * @use_defaults: bool to use sane defaults derived from the I2C specification
2073 * when properties are not found, otherwise use 0
2074 *
2075 * Scan the device for the generic I2C properties describing timing parameters
2076 * for the signal and fill the given struct with the results. If a property was
2077 * not found and use_defaults was true, then maximum timings are assumed which
2078 * are derived from the I2C specification. If use_defaults is not used, the
2079 * results will be 0, so drivers can apply their own defaults later. The latter
2080 * is mainly intended for avoiding regressions of existing drivers which want
2081 * to switch to this function. New drivers almost always should use the defaults.
2082 */
2083
2084void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2085{
2086 int ret;
2087
2088 memset(t, 0, sizeof(*t));
2089
2090 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2091 if (ret && use_defaults)
2092 t->bus_freq_hz = 100000;
2093
2094 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2095 if (ret && use_defaults) {
2096 if (t->bus_freq_hz <= 100000)
2097 t->scl_rise_ns = 1000;
2098 else if (t->bus_freq_hz <= 400000)
2099 t->scl_rise_ns = 300;
2100 else
2101 t->scl_rise_ns = 120;
2102 }
2103
2104 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2105 if (ret && use_defaults) {
2106 if (t->bus_freq_hz <= 400000)
2107 t->scl_fall_ns = 300;
2108 else
2109 t->scl_fall_ns = 120;
2110 }
2111
2112 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2113
2114 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2115 if (ret && use_defaults)
2116 t->sda_fall_ns = t->scl_fall_ns;
2117}
2118EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2119
David Brownell7b4fbc52007-05-01 23:26:30 +02002120/* ------------------------------------------------------------------------- */
2121
Jean Delvare7ae31482011-03-20 14:50:52 +01002122int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2123{
2124 int res;
2125
2126 mutex_lock(&core_lock);
2127 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2128 mutex_unlock(&core_lock);
2129
2130 return res;
2131}
2132EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2133
Jean Delvare69b00892009-12-06 17:06:27 +01002134static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002135{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002136 if (dev->type != &i2c_adapter_type)
2137 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002138 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002139}
2140
David Brownell7b4fbc52007-05-01 23:26:30 +02002141/*
2142 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002143 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
2145
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002146int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002148 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Brownell1d0b19c2008-10-14 17:30:05 +02002150 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302151 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002152 return -EAGAIN;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002155 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Jean Delvare729d6dd2009-06-19 16:58:18 +02002158 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002159 * will have called probe() for all matching-but-unbound devices.
2160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 res = driver_register(&driver->driver);
2162 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002163 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002164
Wolfram Sang44239a52016-07-09 13:35:04 +09002165 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jean Delvare4735c982008-07-14 22:38:36 +02002167 INIT_LIST_HEAD(&driver->clients);
2168 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002169 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002170
Jean Delvare7eebcb72006-02-05 23:28:21 +01002171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002173EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jean Delvare69b00892009-12-06 17:06:27 +01002175static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002176{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002177 if (dev->type == &i2c_adapter_type)
2178 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2179 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002180}
2181
David Brownella1d9e6e2007-05-01 23:26:30 +02002182/**
2183 * i2c_del_driver - unregister I2C driver
2184 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002185 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002186 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002187void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Jean Delvare7ae31482011-03-20 14:50:52 +01002189 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002192 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
David Brownellc0564602007-05-01 23:26:31 +02002194EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
David Brownell7b4fbc52007-05-01 23:26:30 +02002196/* ------------------------------------------------------------------------- */
2197
Jean Delvaree48d3312008-01-27 18:14:48 +01002198/**
2199 * i2c_use_client - increments the reference count of the i2c client structure
2200 * @client: the client being referenced
2201 *
2202 * Each live reference to a client should be refcounted. The driver model does
2203 * that automatically as part of driver binding, so that most drivers don't
2204 * need to do this explicitly: they hold a reference until they're unbound
2205 * from the device.
2206 *
2207 * A pointer to the client with the incremented reference counter is returned.
2208 */
2209struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
David Brownell6ea438e2008-07-14 22:38:24 +02002211 if (client && get_device(&client->dev))
2212 return client;
2213 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
David Brownellc0564602007-05-01 23:26:31 +02002215EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Jean Delvaree48d3312008-01-27 18:14:48 +01002217/**
2218 * i2c_release_client - release a use of the i2c client structure
2219 * @client: the client being no longer referenced
2220 *
2221 * Must be called when a user of a client is finished with it.
2222 */
2223void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
David Brownell6ea438e2008-07-14 22:38:24 +02002225 if (client)
2226 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
David Brownellc0564602007-05-01 23:26:31 +02002228EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
David Brownell9b766b82008-01-27 18:14:51 +01002230struct i2c_cmd_arg {
2231 unsigned cmd;
2232 void *arg;
2233};
2234
2235static int i2c_cmd(struct device *dev, void *_arg)
2236{
2237 struct i2c_client *client = i2c_verify_client(dev);
2238 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002239 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002240
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002241 if (!client || !client->dev.driver)
2242 return 0;
2243
2244 driver = to_i2c_driver(client->dev.driver);
2245 if (driver->command)
2246 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002247 return 0;
2248}
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2251{
David Brownell9b766b82008-01-27 18:14:51 +01002252 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
David Brownell9b766b82008-01-27 18:14:51 +01002254 cmd_arg.cmd = cmd;
2255 cmd_arg.arg = arg;
2256 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
David Brownellc0564602007-05-01 23:26:31 +02002258EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002260#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2261static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2262 void *arg)
2263{
2264 struct of_reconfig_data *rd = arg;
2265 struct i2c_adapter *adap;
2266 struct i2c_client *client;
2267
2268 switch (of_reconfig_get_state_change(action, rd)) {
2269 case OF_RECONFIG_CHANGE_ADD:
2270 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2271 if (adap == NULL)
2272 return NOTIFY_OK; /* not for us */
2273
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002274 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2275 put_device(&adap->dev);
2276 return NOTIFY_OK;
2277 }
2278
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002279 client = of_i2c_register_device(adap, rd->dn);
2280 put_device(&adap->dev);
2281
2282 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002283 dev_err(&adap->dev, "failed to create client for '%s'\n",
2284 rd->dn->full_name);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002285 return notifier_from_errno(PTR_ERR(client));
2286 }
2287 break;
2288 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002289 /* already depopulated? */
2290 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2291 return NOTIFY_OK;
2292
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002293 /* find our device by node */
2294 client = of_find_i2c_device_by_node(rd->dn);
2295 if (client == NULL)
2296 return NOTIFY_OK; /* no? not meant for us */
2297
2298 /* unregister takes one ref away */
2299 i2c_unregister_device(client);
2300
2301 /* and put the reference of the find */
2302 put_device(&client->dev);
2303 break;
2304 }
2305
2306 return NOTIFY_OK;
2307}
2308static struct notifier_block i2c_of_notifier = {
2309 .notifier_call = of_i2c_notify,
2310};
2311#else
2312extern struct notifier_block i2c_of_notifier;
2313#endif /* CONFIG_OF_DYNAMIC */
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315static int __init i2c_init(void)
2316{
2317 int retval;
2318
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002319 retval = of_alias_get_highest_id("i2c");
2320
2321 down_write(&__i2c_board_lock);
2322 if (retval >= __i2c_first_dynamic_bus_num)
2323 __i2c_first_dynamic_bus_num = retval + 1;
2324 up_write(&__i2c_board_lock);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 retval = bus_register(&i2c_bus_type);
2327 if (retval)
2328 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002329
2330 is_registered = true;
2331
Jean Delvare2bb50952009-09-18 22:45:46 +02002332#ifdef CONFIG_I2C_COMPAT
2333 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2334 if (!i2c_adapter_compat_class) {
2335 retval = -ENOMEM;
2336 goto bus_err;
2337 }
2338#endif
David Brownelle9f13732008-01-27 18:14:52 +01002339 retval = i2c_add_driver(&dummy_driver);
2340 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002341 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002342
2343 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2344 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002345 if (IS_ENABLED(CONFIG_ACPI))
2346 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002347
David Brownelle9f13732008-01-27 18:14:52 +01002348 return 0;
2349
Jean Delvare2bb50952009-09-18 22:45:46 +02002350class_err:
2351#ifdef CONFIG_I2C_COMPAT
2352 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002353bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002354#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002355 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002356 bus_unregister(&i2c_bus_type);
2357 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360static void __exit i2c_exit(void)
2361{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002362 if (IS_ENABLED(CONFIG_ACPI))
2363 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002364 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2365 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002366 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002367#ifdef CONFIG_I2C_COMPAT
2368 class_compat_unregister(i2c_adapter_compat_class);
2369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002371 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
David Brownella10f9e72008-10-14 17:30:06 +02002374/* We must initialize early, because some subsystems register i2c drivers
2375 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2376 */
2377postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378module_exit(i2c_exit);
2379
2380/* ----------------------------------------------------
2381 * the functional interface to the i2c busses.
2382 * ----------------------------------------------------
2383 */
2384
Wolfram Sangb7f62582015-01-05 23:45:59 +01002385/* Check if val is exceeding the quirk IFF quirk is non 0 */
2386#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2387
2388static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2389{
2390 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2391 err_msg, msg->addr, msg->len,
2392 msg->flags & I2C_M_RD ? "read" : "write");
2393 return -EOPNOTSUPP;
2394}
2395
2396static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2397{
2398 const struct i2c_adapter_quirks *q = adap->quirks;
2399 int max_num = q->max_num_msgs, i;
2400 bool do_len_check = true;
2401
2402 if (q->flags & I2C_AQ_COMB) {
2403 max_num = 2;
2404
2405 /* special checks for combined messages */
2406 if (num == 2) {
2407 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2408 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2409
2410 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2411 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2412
2413 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2414 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2415
2416 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2417 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2418
2419 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2420 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2421
2422 do_len_check = false;
2423 }
2424 }
2425
2426 if (i2c_quirk_exceeded(num, max_num))
2427 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2428
2429 for (i = 0; i < num; i++) {
2430 u16 len = msgs[i].len;
2431
2432 if (msgs[i].flags & I2C_M_RD) {
2433 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2434 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2435 } else {
2436 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2437 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2438 }
2439 }
2440
2441 return 0;
2442}
2443
David Brownella1cdeda2008-07-14 22:38:24 +02002444/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002445 * __i2c_transfer - unlocked flavor of i2c_transfer
2446 * @adap: Handle to I2C bus
2447 * @msgs: One or more messages to execute before STOP is issued to
2448 * terminate the operation; each message begins with a START.
2449 * @num: Number of messages to be executed.
2450 *
2451 * Returns negative errno, else the number of messages executed.
2452 *
2453 * Adapter lock must be held when calling this function. No debug logging
2454 * takes place. adap->algo->master_xfer existence isn't checked.
2455 */
2456int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2457{
2458 unsigned long orig_jiffies;
2459 int ret, try;
2460
Wolfram Sangb7f62582015-01-05 23:45:59 +01002461 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2462 return -EOPNOTSUPP;
2463
David Howellsd9a83d62014-03-06 13:35:59 +00002464 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2465 * enabled. This is an efficient way of keeping the for-loop from
2466 * being executed when not needed.
2467 */
2468 if (static_key_false(&i2c_trace_msg)) {
2469 int i;
2470 for (i = 0; i < num; i++)
2471 if (msgs[i].flags & I2C_M_RD)
2472 trace_i2c_read(adap, &msgs[i], i);
2473 else
2474 trace_i2c_write(adap, &msgs[i], i);
2475 }
2476
Jean Delvareb37d2a32012-06-29 07:47:19 -03002477 /* Retry automatically on arbitration loss */
2478 orig_jiffies = jiffies;
2479 for (ret = 0, try = 0; try <= adap->retries; try++) {
2480 ret = adap->algo->master_xfer(adap, msgs, num);
2481 if (ret != -EAGAIN)
2482 break;
2483 if (time_after(jiffies, orig_jiffies + adap->timeout))
2484 break;
2485 }
2486
David Howellsd9a83d62014-03-06 13:35:59 +00002487 if (static_key_false(&i2c_trace_msg)) {
2488 int i;
2489 for (i = 0; i < ret; i++)
2490 if (msgs[i].flags & I2C_M_RD)
2491 trace_i2c_reply(adap, &msgs[i], i);
2492 trace_i2c_result(adap, i, ret);
2493 }
2494
Jean Delvareb37d2a32012-06-29 07:47:19 -03002495 return ret;
2496}
2497EXPORT_SYMBOL(__i2c_transfer);
2498
2499/**
David Brownella1cdeda2008-07-14 22:38:24 +02002500 * i2c_transfer - execute a single or combined I2C message
2501 * @adap: Handle to I2C bus
2502 * @msgs: One or more messages to execute before STOP is issued to
2503 * terminate the operation; each message begins with a START.
2504 * @num: Number of messages to be executed.
2505 *
2506 * Returns negative errno, else the number of messages executed.
2507 *
2508 * Note that there is no requirement that each message be sent to
2509 * the same slave address, although that is the most common model.
2510 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002511int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002513 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
David Brownella1cdeda2008-07-14 22:38:24 +02002515 /* REVISIT the fault reporting model here is weak:
2516 *
2517 * - When we get an error after receiving N bytes from a slave,
2518 * there is no way to report "N".
2519 *
2520 * - When we get a NAK after transmitting N bytes to a slave,
2521 * there is no way to report "N" ... or to let the master
2522 * continue executing the rest of this combined message, if
2523 * that's the appropriate response.
2524 *
2525 * - When for example "num" is two and we successfully complete
2526 * the first message but get an error part way through the
2527 * second, it's unclear whether that should be reported as
2528 * one (discarding status on the second message) or errno
2529 * (discarding status on the first one).
2530 */
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (adap->algo->master_xfer) {
2533#ifdef DEBUG
2534 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002535 dev_dbg(&adap->dev,
2536 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2537 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2538 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002539 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541#endif
2542
Mike Rapoportcea443a2008-01-27 18:14:50 +01002543 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002544 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002545 if (!ret)
2546 /* I2C activity is ongoing. */
2547 return -EAGAIN;
2548 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002549 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002550 }
2551
Jean Delvareb37d2a32012-06-29 07:47:19 -03002552 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002553 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 return ret;
2556 } else {
2557 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002558 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560}
David Brownellc0564602007-05-01 23:26:31 +02002561EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
David Brownella1cdeda2008-07-14 22:38:24 +02002563/**
2564 * i2c_master_send - issue a single I2C message in master transmit mode
2565 * @client: Handle to slave device
2566 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002567 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002568 *
2569 * Returns negative errno, or else the number of bytes written.
2570 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002571int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002574 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 struct i2c_msg msg;
2576
Jean Delvare815f55f2005-05-07 22:58:46 +02002577 msg.addr = client->addr;
2578 msg.flags = client->flags & I2C_M_TEN;
2579 msg.len = count;
2580 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002581
Jean Delvare815f55f2005-05-07 22:58:46 +02002582 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002584 /*
2585 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2586 * transmitted, else error code.
2587 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002588 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
David Brownellc0564602007-05-01 23:26:31 +02002590EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
David Brownella1cdeda2008-07-14 22:38:24 +02002592/**
2593 * i2c_master_recv - issue a single I2C message in master receive mode
2594 * @client: Handle to slave device
2595 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002596 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002597 *
2598 * Returns negative errno, or else the number of bytes read.
2599 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002600int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Farid Hammane7225acf2010-05-21 18:40:58 +02002602 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct i2c_msg msg;
2604 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Jean Delvare815f55f2005-05-07 22:58:46 +02002606 msg.addr = client->addr;
2607 msg.flags = client->flags & I2C_M_TEN;
2608 msg.flags |= I2C_M_RD;
2609 msg.len = count;
2610 msg.buf = buf;
2611
2612 ret = i2c_transfer(adap, &msg, 1);
2613
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002614 /*
2615 * If everything went ok (i.e. 1 msg received), return #bytes received,
2616 * else error code.
2617 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002618 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
David Brownellc0564602007-05-01 23:26:31 +02002620EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622/* ----------------------------------------------------
2623 * the i2c address scanning function
2624 * Will not work for 10-bit addresses!
2625 * ----------------------------------------------------
2626 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002627
Jean Delvare63e4e802010-06-03 11:33:51 +02002628/*
2629 * Legacy default probe function, mostly relevant for SMBus. The default
2630 * probe method is a quick write, but it is known to corrupt the 24RF08
2631 * EEPROMs due to a state machine bug, and could also irreversibly
2632 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2633 * we use a short byte read instead. Also, some bus drivers don't implement
2634 * quick write, so we fallback to a byte read in that case too.
2635 * On x86, there is another special case for FSC hardware monitoring chips,
2636 * which want regular byte reads (address 0x73.) Fortunately, these are the
2637 * only known chips using this I2C address on PC hardware.
2638 * Returns 1 if probe succeeded, 0 if not.
2639 */
2640static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2641{
2642 int err;
2643 union i2c_smbus_data dummy;
2644
2645#ifdef CONFIG_X86
2646 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2647 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2648 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2649 I2C_SMBUS_BYTE_DATA, &dummy);
2650 else
2651#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002652 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2653 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002654 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2655 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002656 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2657 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2658 I2C_SMBUS_BYTE, &dummy);
2659 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002660 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2661 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002662 err = -EOPNOTSUPP;
2663 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002664
2665 return err >= 0;
2666}
2667
Jean Delvareccfbbd02009-12-06 17:06:25 +01002668static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002669 struct i2c_driver *driver)
2670{
2671 struct i2c_board_info info;
2672 struct i2c_adapter *adapter = temp_client->adapter;
2673 int addr = temp_client->addr;
2674 int err;
2675
2676 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002677 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002678 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002679 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2680 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002681 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002682 }
2683
Wolfram Sang9bccc702015-07-17 14:48:56 +02002684 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002685 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002686 return 0;
2687
Jean Delvareccfbbd02009-12-06 17:06:25 +01002688 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002689 if (!i2c_default_probe(adapter, addr))
2690 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002691
2692 /* Finally call the custom detection function */
2693 memset(&info, 0, sizeof(struct i2c_board_info));
2694 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002695 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002696 if (err) {
2697 /* -ENODEV is returned if the detection fails. We catch it
2698 here as this isn't an error. */
2699 return err == -ENODEV ? 0 : err;
2700 }
2701
2702 /* Consistency check */
2703 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002704 dev_err(&adapter->dev,
2705 "%s detection function provided no name for 0x%x\n",
2706 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002707 } else {
2708 struct i2c_client *client;
2709
2710 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002711 if (adapter->class & I2C_CLASS_DEPRECATED)
2712 dev_warn(&adapter->dev,
2713 "This adapter will soon drop class based instantiation of devices. "
2714 "Please make sure client 0x%02x gets instantiated by other means. "
2715 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2716 info.addr);
2717
Jean Delvare4735c982008-07-14 22:38:36 +02002718 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2719 info.type, info.addr);
2720 client = i2c_new_device(adapter, &info);
2721 if (client)
2722 list_add_tail(&client->detected, &driver->clients);
2723 else
2724 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2725 info.type, info.addr);
2726 }
2727 return 0;
2728}
2729
2730static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2731{
Jean Delvarec3813d62009-12-14 21:17:25 +01002732 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002733 struct i2c_client *temp_client;
2734 int i, err = 0;
2735 int adap_id = i2c_adapter_id(adapter);
2736
Jean Delvarec3813d62009-12-14 21:17:25 +01002737 address_list = driver->address_list;
2738 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002739 return 0;
2740
Wolfram Sang45552272014-07-10 13:46:21 +02002741 /* Warn that the adapter lost class based instantiation */
2742 if (adapter->class == I2C_CLASS_DEPRECATED) {
2743 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002744 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2745 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002746 driver->driver.name);
2747 return 0;
2748 }
2749
Jean Delvare51b54ba2010-10-24 18:16:58 +02002750 /* Stop here if the classes do not match */
2751 if (!(adapter->class & driver->class))
2752 return 0;
2753
Jean Delvare4735c982008-07-14 22:38:36 +02002754 /* Set up a temporary client to help detect callback */
2755 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2756 if (!temp_client)
2757 return -ENOMEM;
2758 temp_client->adapter = adapter;
2759
Jean Delvarec3813d62009-12-14 21:17:25 +01002760 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002761 dev_dbg(&adapter->dev,
2762 "found normal entry for adapter %d, addr 0x%02x\n",
2763 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002764 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002765 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002766 if (unlikely(err))
2767 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002768 }
2769
Jean Delvare4735c982008-07-14 22:38:36 +02002770 kfree(temp_client);
2771 return err;
2772}
2773
Jean Delvared44f19d2010-08-11 18:20:57 +02002774int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2775{
2776 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2777 I2C_SMBUS_QUICK, NULL) >= 0;
2778}
2779EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2780
Jean Delvare12b5053a2007-05-01 23:26:31 +02002781struct i2c_client *
2782i2c_new_probed_device(struct i2c_adapter *adap,
2783 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002784 unsigned short const *addr_list,
2785 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002786{
2787 int i;
2788
Jean Delvare8031d792010-08-11 18:21:00 +02002789 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002790 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002791
Jean Delvare12b5053a2007-05-01 23:26:31 +02002792 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2793 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002794 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002795 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2796 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002797 continue;
2798 }
2799
Wolfram Sang9bccc702015-07-17 14:48:56 +02002800 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002801 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002802 dev_dbg(&adap->dev,
2803 "Address 0x%02x already in use, not probing\n",
2804 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002805 continue;
2806 }
2807
Jean Delvare63e4e802010-06-03 11:33:51 +02002808 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002809 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002810 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002811 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002812
2813 if (addr_list[i] == I2C_CLIENT_END) {
2814 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2815 return NULL;
2816 }
2817
2818 info->addr = addr_list[i];
2819 return i2c_new_device(adap, info);
2820}
2821EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2822
Jean Delvared735b342011-03-20 14:50:52 +01002823struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002826
Jean Delvarecaada322008-01-27 18:14:49 +01002827 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002828 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002829 if (!adapter)
2830 goto exit;
2831
2832 if (try_module_get(adapter->owner))
2833 get_device(&adapter->dev);
2834 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002835 adapter = NULL;
2836
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002837 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002838 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002839 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
David Brownellc0564602007-05-01 23:26:31 +02002841EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843void i2c_put_adapter(struct i2c_adapter *adap)
2844{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002845 if (!adap)
2846 return;
2847
2848 put_device(&adap->dev);
2849 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
David Brownellc0564602007-05-01 23:26:31 +02002851EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853/* The SMBus parts */
2854
David Brownell438d6c22006-12-10 21:21:31 +01002855#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002856static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002859
Farid Hammane7225acf2010-05-21 18:40:58 +02002860 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002861 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 data = data ^ POLY;
2863 data = data << 1;
2864 }
2865 return (u8)(data >> 8);
2866}
2867
Jean Delvare421ef472005-10-26 21:28:55 +02002868/* Incremental CRC8 over count bytes in the array pointed to by p */
2869static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 int i;
2872
Farid Hammane7225acf2010-05-21 18:40:58 +02002873 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002874 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return crc;
2876}
2877
Jean Delvare421ef472005-10-26 21:28:55 +02002878/* Assume a 7-bit address, which is reasonable for SMBus */
2879static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Jean Delvare421ef472005-10-26 21:28:55 +02002881 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002882 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002883 pec = i2c_smbus_pec(pec, &addr, 1);
2884
2885 /* The data buffer follows */
2886 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
Jean Delvare421ef472005-10-26 21:28:55 +02002889/* Used for write only transactions */
2890static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Jean Delvare421ef472005-10-26 21:28:55 +02002892 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2893 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Jean Delvare421ef472005-10-26 21:28:55 +02002896/* Return <0 on CRC error
2897 If there was a write before this read (most cases) we need to take the
2898 partial CRC from the write part into account.
2899 Note that this function does modify the message (we need to decrease the
2900 message length to hide the CRC byte from the caller). */
2901static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Jean Delvare421ef472005-10-26 21:28:55 +02002903 u8 rpec = msg->buf[--msg->len];
2904 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002907 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002909 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
David Brownell438d6c22006-12-10 21:21:31 +01002911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
David Brownella1cdeda2008-07-14 22:38:24 +02002914/**
2915 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2916 * @client: Handle to slave device
2917 *
2918 * This executes the SMBus "receive byte" protocol, returning negative errno
2919 * else the byte received from the device.
2920 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002921s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002924 int status;
2925
2926 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2927 I2C_SMBUS_READ, 0,
2928 I2C_SMBUS_BYTE, &data);
2929 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
David Brownellc0564602007-05-01 23:26:31 +02002931EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
David Brownella1cdeda2008-07-14 22:38:24 +02002933/**
2934 * i2c_smbus_write_byte - SMBus "send byte" protocol
2935 * @client: Handle to slave device
2936 * @value: Byte to be sent
2937 *
2938 * This executes the SMBus "send byte" protocol, returning negative errno
2939 * else zero on success.
2940 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002941s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Farid Hammane7225acf2010-05-21 18:40:58 +02002943 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002944 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
David Brownellc0564602007-05-01 23:26:31 +02002946EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
David Brownella1cdeda2008-07-14 22:38:24 +02002948/**
2949 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2950 * @client: Handle to slave device
2951 * @command: Byte interpreted by slave
2952 *
2953 * This executes the SMBus "read byte" protocol, returning negative errno
2954 * else a data byte received from the device.
2955 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002956s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002959 int status;
2960
2961 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2962 I2C_SMBUS_READ, command,
2963 I2C_SMBUS_BYTE_DATA, &data);
2964 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
David Brownellc0564602007-05-01 23:26:31 +02002966EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
David Brownella1cdeda2008-07-14 22:38:24 +02002968/**
2969 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2970 * @client: Handle to slave device
2971 * @command: Byte interpreted by slave
2972 * @value: Byte being written
2973 *
2974 * This executes the SMBus "write byte" protocol, returning negative errno
2975 * else zero on success.
2976 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002977s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2978 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
2980 union i2c_smbus_data data;
2981 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002982 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2983 I2C_SMBUS_WRITE, command,
2984 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
David Brownellc0564602007-05-01 23:26:31 +02002986EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
David Brownella1cdeda2008-07-14 22:38:24 +02002988/**
2989 * i2c_smbus_read_word_data - SMBus "read word" protocol
2990 * @client: Handle to slave device
2991 * @command: Byte interpreted by slave
2992 *
2993 * This executes the SMBus "read word" protocol, returning negative errno
2994 * else a 16-bit unsigned "word" received from the device.
2995 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002996s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
2998 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002999 int status;
3000
3001 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3002 I2C_SMBUS_READ, command,
3003 I2C_SMBUS_WORD_DATA, &data);
3004 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
David Brownellc0564602007-05-01 23:26:31 +02003006EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
David Brownella1cdeda2008-07-14 22:38:24 +02003008/**
3009 * i2c_smbus_write_word_data - SMBus "write word" protocol
3010 * @client: Handle to slave device
3011 * @command: Byte interpreted by slave
3012 * @value: 16-bit "word" being written
3013 *
3014 * This executes the SMBus "write word" protocol, returning negative errno
3015 * else zero on success.
3016 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003017s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3018 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 union i2c_smbus_data data;
3021 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003022 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3023 I2C_SMBUS_WRITE, command,
3024 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
David Brownellc0564602007-05-01 23:26:31 +02003026EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
David Brownella64ec072007-10-13 23:56:31 +02003028/**
David Brownella1cdeda2008-07-14 22:38:24 +02003029 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003030 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003031 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003032 * @values: Byte array into which data will be read; big enough to hold
3033 * the data returned by the slave. SMBus allows at most 32 bytes.
3034 *
David Brownella1cdeda2008-07-14 22:38:24 +02003035 * This executes the SMBus "block read" protocol, returning negative errno
3036 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003037 *
3038 * Note that using this function requires that the client's adapter support
3039 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3040 * support this; its emulation through I2C messaging relies on a specific
3041 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3042 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003043s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003044 u8 *values)
3045{
3046 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003047 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003048
David Brownell24a5bb72008-07-14 22:38:23 +02003049 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3050 I2C_SMBUS_READ, command,
3051 I2C_SMBUS_BLOCK_DATA, &data);
3052 if (status)
3053 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003054
3055 memcpy(values, &data.block[1], data.block[0]);
3056 return data.block[0];
3057}
3058EXPORT_SYMBOL(i2c_smbus_read_block_data);
3059
David Brownella1cdeda2008-07-14 22:38:24 +02003060/**
3061 * i2c_smbus_write_block_data - SMBus "block write" protocol
3062 * @client: Handle to slave device
3063 * @command: Byte interpreted by slave
3064 * @length: Size of data block; SMBus allows at most 32 bytes
3065 * @values: Byte array which will be written.
3066 *
3067 * This executes the SMBus "block write" protocol, returning negative errno
3068 * else zero on success.
3069 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003070s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003071 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (length > I2C_SMBUS_BLOCK_MAX)
3076 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003078 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003079 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3080 I2C_SMBUS_WRITE, command,
3081 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
David Brownellc0564602007-05-01 23:26:31 +02003083EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003086s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003087 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
3089 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003090 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003091
Jean Delvare4b2643d2007-07-12 14:12:29 +02003092 if (length > I2C_SMBUS_BLOCK_MAX)
3093 length = I2C_SMBUS_BLOCK_MAX;
3094 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003095 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3096 I2C_SMBUS_READ, command,
3097 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3098 if (status < 0)
3099 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003100
3101 memcpy(values, &data.block[1], data.block[0]);
3102 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
David Brownellc0564602007-05-01 23:26:31 +02003104EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Jean Delvare0cc43a12011-01-10 22:11:23 +01003106s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003107 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003108{
3109 union i2c_smbus_data data;
3110
3111 if (length > I2C_SMBUS_BLOCK_MAX)
3112 length = I2C_SMBUS_BLOCK_MAX;
3113 data.block[0] = length;
3114 memcpy(data.block + 1, values, length);
3115 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3116 I2C_SMBUS_WRITE, command,
3117 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3118}
David Brownellc0564602007-05-01 23:26:31 +02003119EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003120
David Brownell438d6c22006-12-10 21:21:31 +01003121/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003123static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3124 unsigned short flags,
3125 char read_write, u8 command, int size,
3126 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 /* So we need to generate a series of msgs. In the case of writing, we
3129 need to use only one message; when reading, we need two. We initialize
3130 most things with sane defaults, to keep the code below somewhat
3131 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003132 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3133 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003134 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003136 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003137 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003138 struct i2c_msg msg[2] = {
3139 {
3140 .addr = addr,
3141 .flags = flags,
3142 .len = 1,
3143 .buf = msgbuf0,
3144 }, {
3145 .addr = addr,
3146 .flags = flags | I2C_M_RD,
3147 .len = 0,
3148 .buf = msgbuf1,
3149 },
3150 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003153 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 case I2C_SMBUS_QUICK:
3155 msg[0].len = 0;
3156 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003157 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3158 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 num = 1;
3160 break;
3161 case I2C_SMBUS_BYTE:
3162 if (read_write == I2C_SMBUS_READ) {
3163 /* Special case: only a read! */
3164 msg[0].flags = I2C_M_RD | flags;
3165 num = 1;
3166 }
3167 break;
3168 case I2C_SMBUS_BYTE_DATA:
3169 if (read_write == I2C_SMBUS_READ)
3170 msg[1].len = 1;
3171 else {
3172 msg[0].len = 2;
3173 msgbuf0[1] = data->byte;
3174 }
3175 break;
3176 case I2C_SMBUS_WORD_DATA:
3177 if (read_write == I2C_SMBUS_READ)
3178 msg[1].len = 2;
3179 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003180 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003182 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184 break;
3185 case I2C_SMBUS_PROC_CALL:
3186 num = 2; /* Special case */
3187 read_write = I2C_SMBUS_READ;
3188 msg[0].len = 3;
3189 msg[1].len = 2;
3190 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003191 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 break;
3193 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003195 msg[1].flags |= I2C_M_RECV_LEN;
3196 msg[1].len = 1; /* block length will be added by
3197 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 } else {
3199 msg[0].len = data->block[0] + 2;
3200 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003201 dev_err(&adapter->dev,
3202 "Invalid block write size %d\n",
3203 data->block[0]);
3204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003206 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 msgbuf0[i] = data->block[i-1];
3208 }
3209 break;
3210 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003211 num = 2; /* Another special case */
3212 read_write = I2C_SMBUS_READ;
3213 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003214 dev_err(&adapter->dev,
3215 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003216 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003217 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003218 }
3219 msg[0].len = data->block[0] + 2;
3220 for (i = 1; i < msg[0].len; i++)
3221 msgbuf0[i] = data->block[i-1];
3222 msg[1].flags |= I2C_M_RECV_LEN;
3223 msg[1].len = 1; /* block length will be added by
3224 the underlying bus driver */
3225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 case I2C_SMBUS_I2C_BLOCK_DATA:
3227 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003228 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 } else {
3230 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003231 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003232 dev_err(&adapter->dev,
3233 "Invalid block write size %d\n",
3234 data->block[0]);
3235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
3237 for (i = 1; i <= data->block[0]; i++)
3238 msgbuf0[i] = data->block[i];
3239 }
3240 break;
3241 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003242 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3243 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245
Jean Delvare421ef472005-10-26 21:28:55 +02003246 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3247 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3248 if (i) {
3249 /* Compute PEC if first message is a write */
3250 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003251 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003252 i2c_smbus_add_pec(&msg[0]);
3253 else /* Write followed by read */
3254 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3255 }
3256 /* Ask for PEC if last message is a read */
3257 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003258 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003259 }
3260
David Brownell24a5bb72008-07-14 22:38:23 +02003261 status = i2c_transfer(adapter, msg, num);
3262 if (status < 0)
3263 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Jean Delvare421ef472005-10-26 21:28:55 +02003265 /* Check PEC if last message is a read */
3266 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003267 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3268 if (status < 0)
3269 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003270 }
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003273 switch (size) {
3274 case I2C_SMBUS_BYTE:
3275 data->byte = msgbuf0[0];
3276 break;
3277 case I2C_SMBUS_BYTE_DATA:
3278 data->byte = msgbuf1[0];
3279 break;
3280 case I2C_SMBUS_WORD_DATA:
3281 case I2C_SMBUS_PROC_CALL:
3282 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3283 break;
3284 case I2C_SMBUS_I2C_BLOCK_DATA:
3285 for (i = 0; i < data->block[0]; i++)
3286 data->block[i+1] = msgbuf1[i];
3287 break;
3288 case I2C_SMBUS_BLOCK_DATA:
3289 case I2C_SMBUS_BLOCK_PROC_CALL:
3290 for (i = 0; i < msgbuf1[0] + 1; i++)
3291 data->block[i] = msgbuf1[i];
3292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294 return 0;
3295}
3296
David Brownella1cdeda2008-07-14 22:38:24 +02003297/**
3298 * i2c_smbus_xfer - execute SMBus protocol operations
3299 * @adapter: Handle to I2C bus
3300 * @addr: Address of SMBus slave on that bus
3301 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3302 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3303 * @command: Byte interpreted by slave, for protocols which use such bytes
3304 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3305 * @data: Data to be read or written
3306 *
3307 * This executes an SMBus protocol operation, and returns a negative
3308 * errno code else zero on success.
3309 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003310s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003311 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003312 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003314 unsigned long orig_jiffies;
3315 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
David Howells8a325992014-03-06 13:36:06 +00003318 /* If enabled, the following two tracepoints are conditional on
3319 * read_write and protocol.
3320 */
3321 trace_smbus_write(adapter, addr, flags, read_write,
3322 command, protocol, data);
3323 trace_smbus_read(adapter, addr, flags, read_write,
3324 command, protocol);
3325
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003326 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003329 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003330
3331 /* Retry automatically on arbitration loss */
3332 orig_jiffies = jiffies;
3333 for (res = 0, try = 0; try <= adapter->retries; try++) {
3334 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3335 read_write, command,
3336 protocol, data);
3337 if (res != -EAGAIN)
3338 break;
3339 if (time_after(jiffies,
3340 orig_jiffies + adapter->timeout))
3341 break;
3342 }
Peter Rosin8320f492016-05-04 22:15:27 +02003343 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003345 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003346 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003347 /*
3348 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3349 * implement native support for the SMBus operation.
3350 */
3351 }
3352
David Howells8a325992014-03-06 13:36:06 +00003353 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3354 command, protocol, data);
3355
3356trace:
3357 /* If enabled, the reply tracepoint is conditional on read_write. */
3358 trace_smbus_reply(adapter, addr, flags, read_write,
3359 command, protocol, data);
3360 trace_smbus_result(adapter, addr, flags, read_write,
3361 command, protocol, res);
3362
3363 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Irina Tirdea01eef962015-08-12 17:31:33 +03003367/**
3368 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3369 * @client: Handle to slave device
3370 * @command: Byte interpreted by slave
3371 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3372 * @values: Byte array into which data will be read; big enough to hold
3373 * the data returned by the slave. SMBus allows at most
3374 * I2C_SMBUS_BLOCK_MAX bytes.
3375 *
3376 * This executes the SMBus "block read" protocol if supported by the adapter.
3377 * If block read is not supported, it emulates it using either word or byte
3378 * read protocols depending on availability.
3379 *
3380 * The addresses of the I2C slave device that are accessed with this function
3381 * must be mapped to a linear region, so that a block read will have the same
3382 * effect as a byte read. Before using this function you must double-check
3383 * if the I2C slave does support exchanging a block transfer with a byte
3384 * transfer.
3385 */
3386s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3387 u8 command, u8 length, u8 *values)
3388{
3389 u8 i = 0;
3390 int status;
3391
3392 if (length > I2C_SMBUS_BLOCK_MAX)
3393 length = I2C_SMBUS_BLOCK_MAX;
3394
3395 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3396 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3397
3398 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3399 return -EOPNOTSUPP;
3400
3401 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3402 while ((i + 2) <= length) {
3403 status = i2c_smbus_read_word_data(client, command + i);
3404 if (status < 0)
3405 return status;
3406 values[i] = status & 0xff;
3407 values[i + 1] = status >> 8;
3408 i += 2;
3409 }
3410 }
3411
3412 while (i < length) {
3413 status = i2c_smbus_read_byte_data(client, command + i);
3414 if (status < 0)
3415 return status;
3416 values[i] = status;
3417 i++;
3418 }
3419
3420 return i;
3421}
3422EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3423
Jean Delvared5fd1202015-01-26 20:59:31 +01003424#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003425int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3426{
3427 int ret;
3428
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003429 if (!client || !slave_cb) {
3430 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003431 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003432 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003433
Wolfram Sangc6909d62015-08-05 21:12:54 +02003434 if (!(client->flags & I2C_CLIENT_SLAVE))
3435 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3436 __func__);
3437
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003438 if (!(client->flags & I2C_CLIENT_TEN)) {
3439 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003440 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003441 if (ret) {
3442 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003443 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003444 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003445 }
3446
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003447 if (!client->adapter->algo->reg_slave) {
3448 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003449 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003450 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003451
3452 client->slave_cb = slave_cb;
3453
3454 i2c_lock_adapter(client->adapter);
3455 ret = client->adapter->algo->reg_slave(client);
3456 i2c_unlock_adapter(client->adapter);
3457
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003458 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003459 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003460 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3461 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003462
3463 return ret;
3464}
3465EXPORT_SYMBOL_GPL(i2c_slave_register);
3466
3467int i2c_slave_unregister(struct i2c_client *client)
3468{
3469 int ret;
3470
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003471 if (!client->adapter->algo->unreg_slave) {
3472 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003473 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003474 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003475
3476 i2c_lock_adapter(client->adapter);
3477 ret = client->adapter->algo->unreg_slave(client);
3478 i2c_unlock_adapter(client->adapter);
3479
3480 if (ret == 0)
3481 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003482 else
3483 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003484
3485 return ret;
3486}
3487EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003488#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3491MODULE_DESCRIPTION("I2C-Bus main module");
3492MODULE_LICENSE("GPL");