blob: 52b2cd4ac8b203fba70715094db3a0231f35e55c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram Sang44239a52016-07-09 13:35:04 +090030#define pr_fmt(fmt) "i2c-core: " fmt
31
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020032#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <asm/uaccess.h>
34#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020035#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010036#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010037#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020038#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010039#include <linux/errno.h>
40#include <linux/gpio.h>
41#include <linux/hardirq.h>
42#include <linux/i2c.h>
43#include <linux/idr.h>
44#include <linux/init.h>
45#include <linux/irqflags.h>
46#include <linux/jump_label.h>
47#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/mutex.h>
50#include <linux/of_device.h>
51#include <linux/of.h>
52#include <linux/of_irq.h>
53#include <linux/pm_domain.h>
54#include <linux/pm_runtime.h>
55#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010056#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010057#include <linux/rwsem.h>
58#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David Brownell9c1600e2007-05-01 23:26:31 +020060#include "i2c-core.h"
61
David Howellsd9a83d62014-03-06 13:35:59 +000062#define CREATE_TRACE_POINTS
63#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Wolfram Sangda899f52015-05-18 21:09:12 +020065#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
66#define I2C_ADDR_OFFSET_SLAVE 0x1000
67
Jean Delvare6629dcf2010-05-04 11:09:28 +020068/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020069 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000070 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010071static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_IDR(i2c_adapter_idr);
73
Jean Delvare4f8cf822009-09-18 22:45:46 +020074static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020075static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010076
David Howellsd9a83d62014-03-06 13:35:59 +000077static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053078static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000079
80void i2c_transfer_trace_reg(void)
81{
82 static_key_slow_inc(&i2c_trace_msg);
83}
84
85void i2c_transfer_trace_unreg(void)
86{
87 static_key_slow_dec(&i2c_trace_msg);
88}
89
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020090#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030091struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020092 struct acpi_connection_info info;
93 struct i2c_adapter *adapter;
94};
95
96struct gsb_buffer {
97 u8 status;
98 u8 len;
99 union {
100 u16 wdata;
101 u8 bdata;
102 u8 data[0];
103 };
104} __packed;
105
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300106struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300107 struct i2c_board_info *info;
108 acpi_handle adapter_handle;
109 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300110 acpi_handle search_handle;
111 u32 speed;
112 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300113};
114
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300115static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200116{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300117 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300118 struct i2c_board_info *info = lookup->info;
119 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300120 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200121
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300122 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
123 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200124
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300125 sb = &ares->data.i2c_serial_bus;
126 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
127 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200128
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300129 status = acpi_get_handle(lookup->device_handle,
130 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300131 &lookup->adapter_handle);
132 if (!ACPI_SUCCESS(status))
133 return 1;
134
135 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300136 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300137 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
138 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200139
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140 return 1;
141}
142
Jarkko Nikula5853b222016-08-12 17:02:53 +0300143static int i2c_acpi_do_lookup(struct acpi_device *adev,
144 struct i2c_acpi_lookup *lookup)
145{
146 struct i2c_board_info *info = lookup->info;
147 struct list_head resource_list;
148 int ret;
149
150 if (acpi_bus_get_status(adev) || !adev->status.present ||
151 acpi_device_enumerated(adev))
152 return -EINVAL;
153
154 memset(info, 0, sizeof(*info));
155 lookup->device_handle = acpi_device_handle(adev);
156
157 /* Look up for I2cSerialBus resource */
158 INIT_LIST_HEAD(&resource_list);
159 ret = acpi_dev_get_resources(adev, &resource_list,
160 i2c_acpi_fill_info, lookup);
161 acpi_dev_free_resource_list(&resource_list);
162
163 if (ret < 0 || !info->addr)
164 return -EINVAL;
165
166 return 0;
167}
168
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300169static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300170 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300171 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300172 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200173{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200174 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300175 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300176 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200177 int ret;
178
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300179 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300180 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300181
Jarkko Nikula5853b222016-08-12 17:02:53 +0300182 ret = i2c_acpi_do_lookup(adev, &lookup);
183 if (ret)
184 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200185
Mika Westerberg318ce272016-09-20 16:59:25 +0300186 if (adapter) {
187 /* The adapter must match the one in I2cSerialBus() connector */
188 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
189 return -ENODEV;
190 } else {
191 struct acpi_device *adapter_adev;
192
193 /* The adapter must be present */
194 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
195 return -ENODEV;
196 if (acpi_bus_get_status(adapter_adev) ||
197 !adapter_adev->status.present)
198 return -ENODEV;
199 }
200
Jarkko Nikula5853b222016-08-12 17:02:53 +0300201 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300202 if (adapter_handle)
203 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200204
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300205 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300206 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300207 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
208 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300209 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300210
211 resource_list_for_each_entry(entry, &resource_list) {
212 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300213 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300214 break;
215 }
216 }
217
218 acpi_dev_free_resource_list(&resource_list);
219
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300220 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
221
222 return 0;
223}
224
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300225static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300226 struct acpi_device *adev,
227 struct i2c_board_info *info)
228{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200229 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300230 acpi_device_set_enumerated(adev);
231
232 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200233 adev->power.flags.ignore_parent = false;
234 dev_err(&adapter->dev,
235 "failed to add I2C device %s from ACPI\n",
236 dev_name(&adev->dev));
237 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300238}
239
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300240static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300241 void *data, void **return_value)
242{
243 struct i2c_adapter *adapter = data;
244 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300245 struct i2c_board_info info;
246
247 if (acpi_bus_get_device(handle, &adev))
248 return AE_OK;
249
Mika Westerberg318ce272016-09-20 16:59:25 +0300250 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300251 return AE_OK;
252
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300253 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200254
255 return AE_OK;
256}
257
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300258#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300259
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200260/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300261 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200262 * @adap: pointer to adapter
263 *
264 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
265 * namespace. When a device is found it will be added to the Linux device
266 * model and bound to the corresponding ACPI handle.
267 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300268static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200269{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200270 acpi_status status;
271
Dustin Byford8eb5c872015-10-23 12:27:07 -0700272 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200273 return;
274
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300275 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300276 I2C_ACPI_MAX_SCAN_DEPTH,
277 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200278 adap, NULL);
279 if (ACPI_FAILURE(status))
280 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
281}
282
Jarkko Nikula5853b222016-08-12 17:02:53 +0300283static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
284 void *data, void **return_value)
285{
286 struct i2c_acpi_lookup *lookup = data;
287 struct acpi_device *adev;
288
289 if (acpi_bus_get_device(handle, &adev))
290 return AE_OK;
291
292 if (i2c_acpi_do_lookup(adev, lookup))
293 return AE_OK;
294
295 if (lookup->search_handle != lookup->adapter_handle)
296 return AE_OK;
297
298 if (lookup->speed <= lookup->min_speed)
299 lookup->min_speed = lookup->speed;
300
301 return AE_OK;
302}
303
304/**
305 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
306 * @dev: The device owning the bus
307 *
308 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
309 * devices connected to this bus and use the speed of slowest device.
310 *
311 * Returns the speed in Hz or zero
312 */
313u32 i2c_acpi_find_bus_speed(struct device *dev)
314{
315 struct i2c_acpi_lookup lookup;
316 struct i2c_board_info dummy;
317 acpi_status status;
318
319 if (!has_acpi_companion(dev))
320 return 0;
321
322 memset(&lookup, 0, sizeof(lookup));
323 lookup.search_handle = ACPI_HANDLE(dev);
324 lookup.min_speed = UINT_MAX;
325 lookup.info = &dummy;
326
327 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
328 I2C_ACPI_MAX_SCAN_DEPTH,
329 i2c_acpi_lookup_speed, NULL,
330 &lookup, NULL);
331
332 if (ACPI_FAILURE(status)) {
333 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
334 return 0;
335 }
336
337 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
338}
339EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
340
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300341static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300342{
343 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
344
345 if (!adapter)
346 return 0;
347
348 return ACPI_HANDLE(dev) == (acpi_handle)data;
349}
350
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300351static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300352{
353 return ACPI_COMPANION(dev) == data;
354}
355
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300356static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300357{
358 struct device *dev;
359
360 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300361 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300362 return dev ? i2c_verify_adapter(dev) : NULL;
363}
364
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300365static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300366{
367 struct device *dev;
368
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300369 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300370 return dev ? i2c_verify_client(dev) : NULL;
371}
372
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300373static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300374 void *arg)
375{
376 struct acpi_device *adev = arg;
377 struct i2c_board_info info;
378 acpi_handle adapter_handle;
379 struct i2c_adapter *adapter;
380 struct i2c_client *client;
381
382 switch (value) {
383 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300384 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300385 break;
386
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300387 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300388 if (!adapter)
389 break;
390
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300391 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300392 break;
393 case ACPI_RECONFIG_DEVICE_REMOVE:
394 if (!acpi_device_enumerated(adev))
395 break;
396
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300397 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300398 if (!client)
399 break;
400
401 i2c_unregister_device(client);
402 put_device(&client->dev);
403 break;
404 }
405
406 return NOTIFY_OK;
407}
408
409static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300410 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300411};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200412#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300413static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300414extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415#endif /* CONFIG_ACPI */
416
417#ifdef CONFIG_ACPI_I2C_OPREGION
418static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
419 u8 cmd, u8 *data, u8 data_len)
420{
421
422 struct i2c_msg msgs[2];
423 int ret;
424 u8 *buffer;
425
426 buffer = kzalloc(data_len, GFP_KERNEL);
427 if (!buffer)
428 return AE_NO_MEMORY;
429
430 msgs[0].addr = client->addr;
431 msgs[0].flags = client->flags;
432 msgs[0].len = 1;
433 msgs[0].buf = &cmd;
434
435 msgs[1].addr = client->addr;
436 msgs[1].flags = client->flags | I2C_M_RD;
437 msgs[1].len = data_len;
438 msgs[1].buf = buffer;
439
440 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
441 if (ret < 0)
442 dev_err(&client->adapter->dev, "i2c read failed\n");
443 else
444 memcpy(data, buffer, data_len);
445
446 kfree(buffer);
447 return ret;
448}
449
450static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
451 u8 cmd, u8 *data, u8 data_len)
452{
453
454 struct i2c_msg msgs[1];
455 u8 *buffer;
456 int ret = AE_OK;
457
458 buffer = kzalloc(data_len + 1, GFP_KERNEL);
459 if (!buffer)
460 return AE_NO_MEMORY;
461
462 buffer[0] = cmd;
463 memcpy(buffer + 1, data, data_len);
464
465 msgs[0].addr = client->addr;
466 msgs[0].flags = client->flags;
467 msgs[0].len = data_len + 1;
468 msgs[0].buf = buffer;
469
470 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
471 if (ret < 0)
472 dev_err(&client->adapter->dev, "i2c write failed\n");
473
474 kfree(buffer);
475 return ret;
476}
477
478static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300479i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200480 u32 bits, u64 *value64,
481 void *handler_context, void *region_context)
482{
483 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300484 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200485 struct acpi_connection_info *info = &data->info;
486 struct acpi_resource_i2c_serialbus *sb;
487 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300488 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200489 struct acpi_resource *ares;
490 u32 accessor_type = function >> 16;
491 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100492 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200493 int status;
494
495 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
496 if (ACPI_FAILURE(ret))
497 return ret;
498
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300499 client = kzalloc(sizeof(*client), GFP_KERNEL);
500 if (!client) {
501 ret = AE_NO_MEMORY;
502 goto err;
503 }
504
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200505 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
506 ret = AE_BAD_PARAMETER;
507 goto err;
508 }
509
510 sb = &ares->data.i2c_serial_bus;
511 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
512 ret = AE_BAD_PARAMETER;
513 goto err;
514 }
515
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300516 client->adapter = adapter;
517 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200518
519 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300520 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200521
522 switch (accessor_type) {
523 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
524 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300525 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200526 if (status >= 0) {
527 gsb->bdata = status;
528 status = 0;
529 }
530 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300531 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200532 }
533 break;
534
535 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
536 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300537 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200538 if (status >= 0) {
539 gsb->bdata = status;
540 status = 0;
541 }
542 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300543 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200544 gsb->bdata);
545 }
546 break;
547
548 case ACPI_GSB_ACCESS_ATTRIB_WORD:
549 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300550 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200551 if (status >= 0) {
552 gsb->wdata = status;
553 status = 0;
554 }
555 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300556 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200557 gsb->wdata);
558 }
559 break;
560
561 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
562 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300563 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200564 gsb->data);
565 if (status >= 0) {
566 gsb->len = status;
567 status = 0;
568 }
569 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300570 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200571 gsb->len, gsb->data);
572 }
573 break;
574
575 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
576 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300577 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200578 gsb->data, info->access_length);
579 if (status > 0)
580 status = 0;
581 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300582 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200583 gsb->data, info->access_length);
584 }
585 break;
586
587 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900588 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
589 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200590 ret = AE_BAD_PARAMETER;
591 goto err;
592 }
593
594 gsb->status = status;
595
596 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300597 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200598 ACPI_FREE(ares);
599 return ret;
600}
601
602
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300603static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200604{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200605 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300606 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200607 acpi_status status;
608
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200609 if (!adapter->dev.parent)
610 return -ENODEV;
611
612 handle = ACPI_HANDLE(adapter->dev.parent);
613
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200614 if (!handle)
615 return -ENODEV;
616
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300617 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200618 GFP_KERNEL);
619 if (!data)
620 return -ENOMEM;
621
622 data->adapter = adapter;
623 status = acpi_bus_attach_private_data(handle, (void *)data);
624 if (ACPI_FAILURE(status)) {
625 kfree(data);
626 return -ENOMEM;
627 }
628
629 status = acpi_install_address_space_handler(handle,
630 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300631 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200632 NULL,
633 data);
634 if (ACPI_FAILURE(status)) {
635 dev_err(&adapter->dev, "Error installing i2c space handler\n");
636 acpi_bus_detach_private_data(handle);
637 kfree(data);
638 return -ENOMEM;
639 }
640
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800641 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200642 return 0;
643}
644
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300645static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200646{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200647 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300648 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200649 acpi_status status;
650
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200651 if (!adapter->dev.parent)
652 return;
653
654 handle = ACPI_HANDLE(adapter->dev.parent);
655
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200656 if (!handle)
657 return;
658
659 acpi_remove_address_space_handler(handle,
660 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300661 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200662
663 status = acpi_bus_get_private_data(handle, (void **)&data);
664 if (ACPI_SUCCESS(status))
665 kfree(data);
666
667 acpi_bus_detach_private_data(handle);
668}
669#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300670static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200671{ }
672
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300673static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200674{ return 0; }
675#endif /* CONFIG_ACPI_I2C_OPREGION */
676
David Brownellf37dd802007-02-13 22:09:00 +0100677/* ------------------------------------------------------------------------- */
678
Lee Jones5f441fc2016-11-07 12:47:40 +0000679const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +0200680 const struct i2c_client *client)
681{
Lee Jones811073b2016-11-07 12:47:36 +0000682 if (!(id && client))
683 return NULL;
684
Jean Delvared2653e92008-04-29 23:11:39 +0200685 while (id->name[0]) {
686 if (strcmp(client->name, id->name) == 0)
687 return id;
688 id++;
689 }
690 return NULL;
691}
Lee Jones5f441fc2016-11-07 12:47:40 +0000692EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static int i2c_device_match(struct device *dev, struct device_driver *drv)
695{
Jean Delvare51298d12009-09-18 22:45:45 +0200696 struct i2c_client *client = i2c_verify_client(dev);
697 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200698
Jean Delvare51298d12009-09-18 22:45:45 +0200699
Grant Likely959e85f2010-06-08 07:48:19 -0600700 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000701 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600702 return 1;
703
Mika Westerberg907ddf82012-11-23 12:23:40 +0100704 /* Then ACPI style match */
705 if (acpi_driver_match_device(dev, drv))
706 return 1;
707
Jean Delvare51298d12009-09-18 22:45:45 +0200708 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000709
710 /* Finally an I2C match */
711 if (i2c_match_id(driver->id_table, client))
712 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200713
Jean Delvareeb8a7902008-05-18 20:49:41 +0200714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Kay Sievers7eff2e72007-08-14 15:15:12 +0200717static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200718{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100719 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800720 int rc;
721
722 rc = acpi_device_uevent_modalias(dev, env);
723 if (rc != -ENODEV)
724 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200725
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100726 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200727}
728
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530729/* i2c bus recovery routines */
730static int get_scl_gpio_value(struct i2c_adapter *adap)
731{
732 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
733}
734
735static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
736{
737 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
738}
739
740static int get_sda_gpio_value(struct i2c_adapter *adap)
741{
742 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
743}
744
745static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
746{
747 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
748 struct device *dev = &adap->dev;
749 int ret = 0;
750
751 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
752 GPIOF_OUT_INIT_HIGH, "i2c-scl");
753 if (ret) {
754 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
755 return ret;
756 }
757
758 if (bri->get_sda) {
759 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
760 /* work without SDA polling */
761 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
762 bri->sda_gpio);
763 bri->get_sda = NULL;
764 }
765 }
766
767 return ret;
768}
769
770static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
771{
772 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
773
774 if (bri->get_sda)
775 gpio_free(bri->sda_gpio);
776
777 gpio_free(bri->scl_gpio);
778}
779
780/*
781 * We are generating clock pulses. ndelay() determines durating of clk pulses.
782 * We will generate clock with rate 100 KHz and so duration of both clock levels
783 * is: delay in ns = (10^6 / 100) / 2
784 */
785#define RECOVERY_NDELAY 5000
786#define RECOVERY_CLK_CNT 9
787
788static int i2c_generic_recovery(struct i2c_adapter *adap)
789{
790 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
791 int i = 0, val = 1, ret = 0;
792
793 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300794 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530795
Jan Luebbe8b062602015-07-08 16:35:06 +0200796 bri->set_scl(adap, val);
797 ndelay(RECOVERY_NDELAY);
798
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530799 /*
800 * By this time SCL is high, as we need to give 9 falling-rising edges
801 */
802 while (i++ < RECOVERY_CLK_CNT * 2) {
803 if (val) {
804 /* Break if SDA is high */
805 if (bri->get_sda && bri->get_sda(adap))
806 break;
807 /* SCL shouldn't be low here */
808 if (!bri->get_scl(adap)) {
809 dev_err(&adap->dev,
810 "SCL is stuck low, exit recovery\n");
811 ret = -EBUSY;
812 break;
813 }
814 }
815
816 val = !val;
817 bri->set_scl(adap, val);
818 ndelay(RECOVERY_NDELAY);
819 }
820
821 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300822 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530823
824 return ret;
825}
826
827int i2c_generic_scl_recovery(struct i2c_adapter *adap)
828{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530829 return i2c_generic_recovery(adap);
830}
Mark Brownc1c21f42015-04-15 19:18:39 +0100831EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530832
833int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
834{
835 int ret;
836
837 ret = i2c_get_gpios_for_recovery(adap);
838 if (ret)
839 return ret;
840
841 ret = i2c_generic_recovery(adap);
842 i2c_put_gpios_for_recovery(adap);
843
844 return ret;
845}
Mark Brownc1c21f42015-04-15 19:18:39 +0100846EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530847
848int i2c_recover_bus(struct i2c_adapter *adap)
849{
850 if (!adap->bus_recovery_info)
851 return -EOPNOTSUPP;
852
853 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
854 return adap->bus_recovery_info->recover_bus(adap);
855}
Mark Brownc1c21f42015-04-15 19:18:39 +0100856EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530857
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900858static void i2c_init_recovery(struct i2c_adapter *adap)
859{
860 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
861 char *err_str;
862
863 if (!bri)
864 return;
865
866 if (!bri->recover_bus) {
867 err_str = "no recover_bus() found";
868 goto err;
869 }
870
871 /* Generic GPIO recovery */
872 if (bri->recover_bus == i2c_generic_gpio_recovery) {
873 if (!gpio_is_valid(bri->scl_gpio)) {
874 err_str = "invalid SCL gpio";
875 goto err;
876 }
877
878 if (gpio_is_valid(bri->sda_gpio))
879 bri->get_sda = get_sda_gpio_value;
880 else
881 bri->get_sda = NULL;
882
883 bri->get_scl = get_scl_gpio_value;
884 bri->set_scl = set_scl_gpio_value;
885 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
886 /* Generic SCL recovery */
887 if (!bri->set_scl || !bri->get_scl) {
888 err_str = "no {get|set}_scl() found";
889 goto err;
890 }
891 }
892
893 return;
894 err:
895 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
896 adap->bus_recovery_info = NULL;
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static int i2c_device_probe(struct device *dev)
900{
Jean Delvare51298d12009-09-18 22:45:45 +0200901 struct i2c_client *client = i2c_verify_client(dev);
902 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100903 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200904
Jean Delvare51298d12009-09-18 22:45:45 +0200905 if (!client)
906 return 0;
907
Mika Westerberg845c8772015-05-06 13:29:08 +0300908 if (!client->irq) {
909 int irq = -ENOENT;
910
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700911 if (dev->of_node) {
912 irq = of_irq_get_byname(dev->of_node, "irq");
913 if (irq == -EINVAL || irq == -ENODATA)
914 irq = of_irq_get(dev->of_node, 0);
915 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300916 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700917 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100918 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200919 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100920 if (irq < 0)
921 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200922
923 client->irq = irq;
924 }
925
Jean Delvare51298d12009-09-18 22:45:45 +0200926 driver = to_i2c_driver(dev->driver);
Lee Jonesda10c062016-11-07 12:47:39 +0000927 if (!driver->probe)
928 return -EINVAL;
929
930 /*
931 * An I2C ID table is not mandatory, if and only if, a suitable Device
932 * Tree match table entry is supplied for the probing device.
933 */
934 if (!driver->id_table &&
935 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200936 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200937
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700938 if (client->flags & I2C_CLIENT_WAKE) {
939 int wakeirq = -ENOENT;
940
941 if (dev->of_node) {
942 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
943 if (wakeirq == -EPROBE_DEFER)
944 return wakeirq;
945 }
946
947 device_init_wakeup(&client->dev, true);
948
949 if (wakeirq > 0 && wakeirq != client->irq)
950 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
951 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200952 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700953 else
954 status = 0;
955
956 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300957 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700958 }
959
David Brownell7b4fbc52007-05-01 23:26:30 +0200960 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200961
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200962 status = of_clk_set_defaults(dev->of_node, false);
963 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700964 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200965
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200966 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100967 if (status == -EPROBE_DEFER)
968 goto err_clear_wakeup_irq;
969
970 status = driver->probe(client, i2c_match_id(driver->id_table, client));
971 if (status)
972 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100973
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700974 return 0;
975
976err_detach_pm_domain:
977 dev_pm_domain_detach(&client->dev, true);
978err_clear_wakeup_irq:
979 dev_pm_clear_wake_irq(&client->dev);
980 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100981 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984static int i2c_device_remove(struct device *dev)
985{
Jean Delvare51298d12009-09-18 22:45:45 +0200986 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200987 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100988 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200989
Jean Delvare51298d12009-09-18 22:45:45 +0200990 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200991 return 0;
992
993 driver = to_i2c_driver(dev->driver);
994 if (driver->remove) {
995 dev_dbg(dev, "remove\n");
996 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200997 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100998
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200999 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001000
1001 dev_pm_clear_wake_irq(&client->dev);
1002 device_init_wakeup(&client->dev, false);
1003
David Brownella1d9e6e2007-05-01 23:26:30 +02001004 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
David Brownellf37dd802007-02-13 22:09:00 +01001007static void i2c_device_shutdown(struct device *dev)
1008{
Jean Delvare51298d12009-09-18 22:45:45 +02001009 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001010 struct i2c_driver *driver;
1011
Jean Delvare51298d12009-09-18 22:45:45 +02001012 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001013 return;
1014 driver = to_i2c_driver(dev->driver);
1015 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001016 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001017}
1018
David Brownell9c1600e2007-05-01 23:26:31 +02001019static void i2c_client_dev_release(struct device *dev)
1020{
1021 kfree(to_i2c_client(dev));
1022}
1023
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001024static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001025show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001026{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001027 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1028 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001029}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001030static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001031
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001032static ssize_t
1033show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001034{
1035 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001036 int len;
1037
1038 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1039 if (len != -ENODEV)
1040 return len;
1041
Jean Delvareeb8a7902008-05-18 20:49:41 +02001042 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001043}
Jean Delvare51298d12009-09-18 22:45:45 +02001044static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1045
1046static struct attribute *i2c_dev_attrs[] = {
1047 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001048 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001049 &dev_attr_modalias.attr,
1050 NULL
1051};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001052ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001053
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001054struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001055 .name = "i2c",
1056 .match = i2c_device_match,
1057 .probe = i2c_device_probe,
1058 .remove = i2c_device_remove,
1059 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001060};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001061EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001062
Jean Delvare51298d12009-09-18 22:45:45 +02001063static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001064 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001065 .uevent = i2c_device_uevent,
1066 .release = i2c_client_dev_release,
1067};
1068
David Brownell9b766b82008-01-27 18:14:51 +01001069
1070/**
1071 * i2c_verify_client - return parameter as i2c_client, or NULL
1072 * @dev: device, probably from some driver model iterator
1073 *
1074 * When traversing the driver model tree, perhaps using driver model
1075 * iterators like @device_for_each_child(), you can't assume very much
1076 * about the nodes you find. Use this function to avoid oopses caused
1077 * by wrongly treating some non-I2C device as an i2c_client.
1078 */
1079struct i2c_client *i2c_verify_client(struct device *dev)
1080{
Jean Delvare51298d12009-09-18 22:45:45 +02001081 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001082 ? to_i2c_client(dev)
1083 : NULL;
1084}
1085EXPORT_SYMBOL(i2c_verify_client);
1086
1087
Wolfram Sangda899f52015-05-18 21:09:12 +02001088/* Return a unique address which takes the flags of the client into account */
1089static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1090{
1091 unsigned short addr = client->addr;
1092
1093 /* For some client flags, add an arbitrary offset to avoid collisions */
1094 if (client->flags & I2C_CLIENT_TEN)
1095 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1096
1097 if (client->flags & I2C_CLIENT_SLAVE)
1098 addr |= I2C_ADDR_OFFSET_SLAVE;
1099
1100 return addr;
1101}
1102
Jean Delvare3a89db52010-06-03 11:33:52 +02001103/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001104 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001105static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001106{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001107 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001108 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001109 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001110 return -EINVAL;
1111 } else {
1112 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001113 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001114 return -EINVAL;
1115 }
1116 return 0;
1117}
1118
Jean Delvare656b8762010-06-03 11:33:53 +02001119/* And this is a strict address validity check, used when probing. If a
1120 * device uses a reserved address, then it shouldn't be probed. 7-bit
1121 * addressing is assumed, 10-bit address devices are rare and should be
1122 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001123static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001124{
1125 /*
1126 * Reserved addresses per I2C specification:
1127 * 0x00 General call address / START byte
1128 * 0x01 CBUS address
1129 * 0x02 Reserved for different bus format
1130 * 0x03 Reserved for future purposes
1131 * 0x04-0x07 Hs-mode master code
1132 * 0x78-0x7b 10-bit slave addressing
1133 * 0x7c-0x7f Reserved for future purposes
1134 */
1135 if (addr < 0x08 || addr > 0x77)
1136 return -EINVAL;
1137 return 0;
1138}
1139
Jean Delvare3b5f7942010-06-03 11:33:55 +02001140static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1141{
1142 struct i2c_client *client = i2c_verify_client(dev);
1143 int addr = *(int *)addrp;
1144
Wolfram Sang9bccc702015-07-17 14:48:56 +02001145 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001146 return -EBUSY;
1147 return 0;
1148}
1149
Michael Lawnick08263742010-08-11 18:21:02 +02001150/* walk up mux tree */
1151static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1152{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001153 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001154 int result;
1155
1156 result = device_for_each_child(&adapter->dev, &addr,
1157 __i2c_check_addr_busy);
1158
Jean Delvare97cc4d42010-10-24 18:16:57 +02001159 if (!result && parent)
1160 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001161
1162 return result;
1163}
1164
1165/* recurse down mux tree */
1166static int i2c_check_mux_children(struct device *dev, void *addrp)
1167{
1168 int result;
1169
1170 if (dev->type == &i2c_adapter_type)
1171 result = device_for_each_child(dev, addrp,
1172 i2c_check_mux_children);
1173 else
1174 result = __i2c_check_addr_busy(dev, addrp);
1175
1176 return result;
1177}
1178
Jean Delvare3b5f7942010-06-03 11:33:55 +02001179static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1180{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001181 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001182 int result = 0;
1183
Jean Delvare97cc4d42010-10-24 18:16:57 +02001184 if (parent)
1185 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001186
1187 if (!result)
1188 result = device_for_each_child(&adapter->dev, &addr,
1189 i2c_check_mux_children);
1190
1191 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001192}
1193
David Brownell9c1600e2007-05-01 23:26:31 +02001194/**
Peter Rosin8320f492016-05-04 22:15:27 +02001195 * i2c_adapter_lock_bus - Get 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 locks the root i2c adapter, I2C_LOCK_SEGMENT
1198 * locks 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_lock_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_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001204}
Jean Delvarefe61e072010-08-11 18:20:58 +02001205
1206/**
Peter Rosin8320f492016-05-04 22:15:27 +02001207 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001208 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001209 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1210 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001211 */
Peter Rosin8320f492016-05-04 22:15:27 +02001212static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1213 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001214{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001215 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001216}
1217
1218/**
Peter Rosin8320f492016-05-04 22:15:27 +02001219 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001220 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001221 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1222 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001223 */
Peter Rosin8320f492016-05-04 22:15:27 +02001224static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1225 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001226{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001227 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001228}
Jean Delvarefe61e072010-08-11 18:20:58 +02001229
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001230static void i2c_dev_set_name(struct i2c_adapter *adap,
1231 struct i2c_client *client)
1232{
1233 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1234
1235 if (adev) {
1236 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1237 return;
1238 }
1239
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001240 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001241 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001242}
1243
Jean Delvarefe61e072010-08-11 18:20:58 +02001244/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001245 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001246 * @adap: the adapter managing the device
1247 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001248 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001249 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001250 * Create an i2c device. Binding is handled through driver model
1251 * probe()/remove() methods. A driver may be bound to this device when we
1252 * return from this function, or any later moment (e.g. maybe hotplugging will
1253 * load the driver module). This call is not appropriate for use by mainboard
1254 * initialization logic, which usually runs during an arch_initcall() long
1255 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001256 *
1257 * This returns the new i2c client, which may be saved for later use with
1258 * i2c_unregister_device(); or NULL to indicate an error.
1259 */
1260struct i2c_client *
1261i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1262{
1263 struct i2c_client *client;
1264 int status;
1265
1266 client = kzalloc(sizeof *client, GFP_KERNEL);
1267 if (!client)
1268 return NULL;
1269
1270 client->adapter = adap;
1271
1272 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001273
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001274 if (info->archdata)
1275 client->dev.archdata = *info->archdata;
1276
Marc Pignatee354252008-08-28 08:33:22 +02001277 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001278 client->addr = info->addr;
1279 client->irq = info->irq;
1280
David Brownell9c1600e2007-05-01 23:26:31 +02001281 strlcpy(client->name, info->type, sizeof(client->name));
1282
Wolfram Sangc4019b72015-07-17 12:50:06 +02001283 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001284 if (status) {
1285 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1286 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1287 goto out_err_silent;
1288 }
1289
Jean Delvaref8a227e2009-06-19 16:58:18 +02001290 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001291 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001292 if (status)
1293 goto out_err;
1294
1295 client->dev.parent = &client->adapter->dev;
1296 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001297 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001298 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001299 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001300
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001301 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001302 status = device_register(&client->dev);
1303 if (status)
1304 goto out_err;
1305
Jean Delvaref8a227e2009-06-19 16:58:18 +02001306 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1307 client->name, dev_name(&client->dev));
1308
David Brownell9c1600e2007-05-01 23:26:31 +02001309 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001310
1311out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001312 dev_err(&adap->dev,
1313 "Failed to register i2c client %s at 0x%02x (%d)\n",
1314 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001315out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001316 kfree(client);
1317 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001318}
1319EXPORT_SYMBOL_GPL(i2c_new_device);
1320
1321
1322/**
1323 * i2c_unregister_device - reverse effect of i2c_new_device()
1324 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001325 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001326 */
1327void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001328{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001329 if (client->dev.of_node)
1330 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001331 if (ACPI_COMPANION(&client->dev))
1332 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001333 device_unregister(&client->dev);
1334}
David Brownell9c1600e2007-05-01 23:26:31 +02001335EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001336
1337
Jean Delvare60b129d2008-05-11 20:37:06 +02001338static const struct i2c_device_id dummy_id[] = {
1339 { "dummy", 0 },
1340 { },
1341};
1342
Jean Delvared2653e92008-04-29 23:11:39 +02001343static int dummy_probe(struct i2c_client *client,
1344 const struct i2c_device_id *id)
1345{
1346 return 0;
1347}
1348
1349static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001350{
1351 return 0;
1352}
1353
1354static struct i2c_driver dummy_driver = {
1355 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001356 .probe = dummy_probe,
1357 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001358 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001359};
1360
1361/**
1362 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1363 * @adapter: the adapter managing the device
1364 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001365 * Context: can sleep
1366 *
1367 * This returns an I2C client bound to the "dummy" driver, intended for use
1368 * with devices that consume multiple addresses. Examples of such chips
1369 * include various EEPROMS (like 24c04 and 24c08 models).
1370 *
1371 * These dummy devices have two main uses. First, most I2C and SMBus calls
1372 * except i2c_transfer() need a client handle; the dummy will be that handle.
1373 * And second, this prevents the specified address from being bound to a
1374 * different driver.
1375 *
1376 * This returns the new i2c client, which should be saved for later use with
1377 * i2c_unregister_device(); or NULL to indicate an error.
1378 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001379struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001380{
1381 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001382 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001383 };
1384
David Brownelle9f13732008-01-27 18:14:52 +01001385 return i2c_new_device(adapter, &info);
1386}
1387EXPORT_SYMBOL_GPL(i2c_new_dummy);
1388
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001389/**
1390 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1391 * and create the associated device
1392 * @client: Handle to the primary client
1393 * @name: Handle to specify which secondary address to get
1394 * @default_addr: Used as a fallback if no secondary address was specified
1395 * Context: can sleep
1396 *
1397 * I2C clients can be composed of multiple I2C slaves bound together in a single
1398 * component. The I2C client driver then binds to the master I2C slave and needs
1399 * to create I2C dummy clients to communicate with all the other slaves.
1400 *
1401 * This function creates and returns an I2C dummy client whose I2C address is
1402 * retrieved from the platform firmware based on the given slave name. If no
1403 * address is specified by the firmware default_addr is used.
1404 *
1405 * On DT-based platforms the address is retrieved from the "reg" property entry
1406 * cell whose "reg-names" value matches the slave name.
1407 *
1408 * This returns the new i2c client, which should be saved for later use with
1409 * i2c_unregister_device(); or NULL to indicate an error.
1410 */
1411struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1412 const char *name,
1413 u16 default_addr)
1414{
1415 struct device_node *np = client->dev.of_node;
1416 u32 addr = default_addr;
1417 int i;
1418
1419 if (np) {
1420 i = of_property_match_string(np, "reg-names", name);
1421 if (i >= 0)
1422 of_property_read_u32_index(np, "reg", i, &addr);
1423 }
1424
1425 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1426 return i2c_new_dummy(client->adapter, addr);
1427}
1428EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1429
David Brownellf37dd802007-02-13 22:09:00 +01001430/* ------------------------------------------------------------------------- */
1431
David Brownell16ffadf2007-05-01 23:26:28 +02001432/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1433
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001434static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
David Brownellef2c83212007-05-01 23:26:28 +02001436 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 complete(&adap->dev_released);
1438}
1439
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001440unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001441{
1442 unsigned int depth = 0;
1443
1444 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1445 depth++;
1446
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001447 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1448 "adapter depth exceeds lockdep subclass limit\n");
1449
Jean Delvare390946b2012-09-10 10:14:02 +02001450 return depth;
1451}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001452EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001453
1454/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001455 * Let users instantiate I2C devices through sysfs. This can be used when
1456 * platform initialization code doesn't contain the proper data for
1457 * whatever reason. Also useful for drivers that do device detection and
1458 * detection fails, either because the device uses an unexpected address,
1459 * or this is a compatible device with different ID register values.
1460 *
1461 * Parameter checking may look overzealous, but we really don't want
1462 * the user to provide incorrect parameters.
1463 */
1464static ssize_t
1465i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1466 const char *buf, size_t count)
1467{
1468 struct i2c_adapter *adap = to_i2c_adapter(dev);
1469 struct i2c_board_info info;
1470 struct i2c_client *client;
1471 char *blank, end;
1472 int res;
1473
Jean Delvare99cd8e22009-06-19 16:58:20 +02001474 memset(&info, 0, sizeof(struct i2c_board_info));
1475
1476 blank = strchr(buf, ' ');
1477 if (!blank) {
1478 dev_err(dev, "%s: Missing parameters\n", "new_device");
1479 return -EINVAL;
1480 }
1481 if (blank - buf > I2C_NAME_SIZE - 1) {
1482 dev_err(dev, "%s: Invalid device name\n", "new_device");
1483 return -EINVAL;
1484 }
1485 memcpy(info.type, buf, blank - buf);
1486
1487 /* Parse remaining parameters, reject extra parameters */
1488 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1489 if (res < 1) {
1490 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1491 return -EINVAL;
1492 }
1493 if (res > 1 && end != '\n') {
1494 dev_err(dev, "%s: Extra parameters\n", "new_device");
1495 return -EINVAL;
1496 }
1497
Wolfram Sangcfa03272015-07-27 14:03:38 +02001498 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1499 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1500 info.flags |= I2C_CLIENT_TEN;
1501 }
1502
1503 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1504 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1505 info.flags |= I2C_CLIENT_SLAVE;
1506 }
1507
Jean Delvare99cd8e22009-06-19 16:58:20 +02001508 client = i2c_new_device(adap, &info);
1509 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001510 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001511
1512 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001513 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001514 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001515 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001516 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1517 info.type, info.addr);
1518
1519 return count;
1520}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001521static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001522
1523/*
1524 * And of course let the users delete the devices they instantiated, if
1525 * they got it wrong. This interface can only be used to delete devices
1526 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1527 * don't delete devices to which some kernel code still has references.
1528 *
1529 * Parameter checking may look overzealous, but we really don't want
1530 * the user to delete the wrong device.
1531 */
1532static ssize_t
1533i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1534 const char *buf, size_t count)
1535{
1536 struct i2c_adapter *adap = to_i2c_adapter(dev);
1537 struct i2c_client *client, *next;
1538 unsigned short addr;
1539 char end;
1540 int res;
1541
1542 /* Parse parameters, reject extra parameters */
1543 res = sscanf(buf, "%hi%c", &addr, &end);
1544 if (res < 1) {
1545 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1546 return -EINVAL;
1547 }
1548 if (res > 1 && end != '\n') {
1549 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1550 return -EINVAL;
1551 }
1552
1553 /* Make sure the device was added through sysfs */
1554 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001555 mutex_lock_nested(&adap->userspace_clients_lock,
1556 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001557 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1558 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001559 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001560 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1561 "delete_device", client->name, client->addr);
1562
1563 list_del(&client->detected);
1564 i2c_unregister_device(client);
1565 res = count;
1566 break;
1567 }
1568 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001569 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001570
1571 if (res < 0)
1572 dev_err(dev, "%s: Can't find device in list\n",
1573 "delete_device");
1574 return res;
1575}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001576static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1577 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001578
1579static struct attribute *i2c_adapter_attrs[] = {
1580 &dev_attr_name.attr,
1581 &dev_attr_new_device.attr,
1582 &dev_attr_delete_device.attr,
1583 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001584};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001585ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001586
Michael Lawnick08263742010-08-11 18:21:02 +02001587struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001588 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001589 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001590};
Michael Lawnick08263742010-08-11 18:21:02 +02001591EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Stephen Warren643dd092012-04-17 12:43:33 -06001593/**
1594 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1595 * @dev: device, probably from some driver model iterator
1596 *
1597 * When traversing the driver model tree, perhaps using driver model
1598 * iterators like @device_for_each_child(), you can't assume very much
1599 * about the nodes you find. Use this function to avoid oopses caused
1600 * by wrongly treating some non-I2C device as an i2c_adapter.
1601 */
1602struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1603{
1604 return (dev->type == &i2c_adapter_type)
1605 ? to_i2c_adapter(dev)
1606 : NULL;
1607}
1608EXPORT_SYMBOL(i2c_verify_adapter);
1609
Jean Delvare2bb50952009-09-18 22:45:46 +02001610#ifdef CONFIG_I2C_COMPAT
1611static struct class_compat *i2c_adapter_compat_class;
1612#endif
1613
David Brownell9c1600e2007-05-01 23:26:31 +02001614static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1615{
1616 struct i2c_devinfo *devinfo;
1617
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001618 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001619 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1620 if (devinfo->busnum == adapter->nr
1621 && !i2c_new_device(adapter,
1622 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001623 dev_err(&adapter->dev,
1624 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001625 devinfo->board_info.addr);
1626 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001627 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001628}
1629
Wolfram Sang687b81d2013-07-11 12:56:15 +01001630/* OF support code */
1631
1632#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001633static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1634 struct device_node *node)
1635{
1636 struct i2c_client *result;
1637 struct i2c_board_info info = {};
1638 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001639 const __be32 *addr_be;
1640 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001641 int len;
1642
1643 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1644
1645 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1646 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1647 node->full_name);
1648 return ERR_PTR(-EINVAL);
1649 }
1650
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001651 addr_be = of_get_property(node, "reg", &len);
1652 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001653 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1654 node->full_name);
1655 return ERR_PTR(-EINVAL);
1656 }
1657
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001658 addr = be32_to_cpup(addr_be);
1659 if (addr & I2C_TEN_BIT_ADDRESS) {
1660 addr &= ~I2C_TEN_BIT_ADDRESS;
1661 info.flags |= I2C_CLIENT_TEN;
1662 }
1663
1664 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1665 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1666 info.flags |= I2C_CLIENT_SLAVE;
1667 }
1668
1669 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001670 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1671 info.addr, node->full_name);
1672 return ERR_PTR(-EINVAL);
1673 }
1674
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001675 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001676 info.of_node = of_node_get(node);
1677 info.archdata = &dev_ad;
1678
1679 if (of_get_property(node, "wakeup-source", NULL))
1680 info.flags |= I2C_CLIENT_WAKE;
1681
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001682 result = i2c_new_device(adap, &info);
1683 if (result == NULL) {
1684 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1685 node->full_name);
1686 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001687 return ERR_PTR(-EINVAL);
1688 }
1689 return result;
1690}
1691
Wolfram Sang687b81d2013-07-11 12:56:15 +01001692static void of_i2c_register_devices(struct i2c_adapter *adap)
1693{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001694 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001695 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001696
1697 /* Only register child devices if the adapter has a node pointer set */
1698 if (!adap->dev.of_node)
1699 return;
1700
1701 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1702
Jon Hunter7e4c2242016-06-29 10:17:53 +01001703 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1704 if (!bus)
1705 bus = of_node_get(adap->dev.of_node);
1706
1707 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001708 if (of_node_test_and_set_flag(node, OF_POPULATED))
1709 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001710
1711 client = of_i2c_register_device(adap, node);
1712 if (IS_ERR(client)) {
1713 dev_warn(&adap->dev,
1714 "Failed to create I2C device for %s\n",
1715 node->full_name);
1716 of_node_clear_flag(node, OF_POPULATED);
1717 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001718 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001719
1720 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001721}
1722
1723static int of_dev_node_match(struct device *dev, void *data)
1724{
1725 return dev->of_node == data;
1726}
1727
1728/* must call put_device() when done with returned i2c_client device */
1729struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1730{
1731 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001732 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001733
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001734 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001735 if (!dev)
1736 return NULL;
1737
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001738 client = i2c_verify_client(dev);
1739 if (!client)
1740 put_device(dev);
1741
1742 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001743}
1744EXPORT_SYMBOL(of_find_i2c_device_by_node);
1745
1746/* must call put_device() when done with returned i2c_adapter device */
1747struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1748{
1749 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001750 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001751
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001752 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001753 if (!dev)
1754 return NULL;
1755
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001756 adapter = i2c_verify_adapter(dev);
1757 if (!adapter)
1758 put_device(dev);
1759
1760 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001761}
1762EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001763
1764/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1765struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1766{
1767 struct i2c_adapter *adapter;
1768
1769 adapter = of_find_i2c_adapter_by_node(node);
1770 if (!adapter)
1771 return NULL;
1772
1773 if (!try_module_get(adapter->owner)) {
1774 put_device(&adapter->dev);
1775 adapter = NULL;
1776 }
1777
1778 return adapter;
1779}
1780EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Lee Jonescabcf4f2016-11-07 12:47:37 +00001781
1782static const struct of_device_id*
1783i2c_of_match_device_sysfs(const struct of_device_id *matches,
1784 struct i2c_client *client)
1785{
1786 const char *name;
1787
1788 for (; matches->compatible[0]; matches++) {
1789 /*
1790 * Adding devices through the i2c sysfs interface provides us
1791 * a string to match which may be compatible with the device
1792 * tree compatible strings, however with no actual of_node the
1793 * of_match_device() will not match
1794 */
1795 if (sysfs_streq(client->name, matches->compatible))
1796 return matches;
1797
1798 name = strchr(matches->compatible, ',');
1799 if (!name)
1800 name = matches->compatible;
1801 else
1802 name++;
1803
1804 if (sysfs_streq(client->name, name))
1805 return matches;
1806 }
1807
1808 return NULL;
1809}
1810
Lee Jones298d4de2016-11-07 12:47:38 +00001811const struct of_device_id
1812*i2c_of_match_device(const struct of_device_id *matches,
1813 struct i2c_client *client)
1814{
1815 const struct of_device_id *match;
1816
1817 if (!(client && matches))
1818 return NULL;
1819
1820 match = of_match_device(matches, &client->dev);
1821 if (match)
1822 return match;
1823
1824 return i2c_of_match_device_sysfs(matches, client);
1825}
1826EXPORT_SYMBOL_GPL(i2c_of_match_device);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001827#else
1828static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1829#endif /* CONFIG_OF */
1830
Jean Delvare69b00892009-12-06 17:06:27 +01001831static int i2c_do_add_adapter(struct i2c_driver *driver,
1832 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001833{
Jean Delvare4735c982008-07-14 22:38:36 +02001834 /* Detect supported devices on that bus, and instantiate them */
1835 i2c_detect(adap, driver);
1836
1837 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001838 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001839 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1840 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001841 dev_warn(&adap->dev,
1842 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001843 /* We ignore the return code; if it fails, too bad */
1844 driver->attach_adapter(adap);
1845 }
1846 return 0;
1847}
1848
Jean Delvare69b00892009-12-06 17:06:27 +01001849static int __process_new_adapter(struct device_driver *d, void *data)
1850{
1851 return i2c_do_add_adapter(to_i2c_driver(d), data);
1852}
1853
Peter Rosind1ed7982016-08-25 23:07:01 +02001854static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1855 .lock_bus = i2c_adapter_lock_bus,
1856 .trylock_bus = i2c_adapter_trylock_bus,
1857 .unlock_bus = i2c_adapter_unlock_bus,
1858};
1859
David Brownell6e13e642007-05-01 23:26:31 +02001860static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001862 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
David Brownell1d0b19c2008-10-14 17:30:05 +02001864 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301865 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001866 res = -EAGAIN;
1867 goto out_list;
1868 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001869
Jean Delvare2236baa2010-11-15 22:40:38 +01001870 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001871 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001872 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001873
1874 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001875 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001876 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001877 }
1878
Peter Rosind1ed7982016-08-25 23:07:01 +02001879 if (!adap->lock_ops)
1880 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001881
Mika Kuoppala194684e2009-12-06 17:06:22 +01001882 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001883 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001884 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001885 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Jean Delvare8fcfef62009-03-28 21:34:43 +01001887 /* Set default timeout to 1 second if not already set */
1888 if (adap->timeout == 0)
1889 adap->timeout = HZ;
1890
Kay Sievers27d9c182009-01-07 14:29:16 +01001891 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001892 adap->dev.bus = &i2c_bus_type;
1893 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001894 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001895 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001896 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001897 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001900 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1901
Charles Keepax6ada5c12015-04-16 13:05:19 +01001902 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001903 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001904 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001905
Jean Delvare2bb50952009-09-18 22:45:46 +02001906#ifdef CONFIG_I2C_COMPAT
1907 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1908 adap->dev.parent);
1909 if (res)
1910 dev_warn(&adap->dev,
1911 "Failed to create compatibility class link\n");
1912#endif
1913
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001914 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301915
Jean Delvare729d6dd2009-06-19 16:58:18 +02001916 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001917 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001918 i2c_acpi_register_devices(adap);
1919 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001920
David Brownell6e13e642007-05-01 23:26:31 +02001921 if (adap->nr < __i2c_first_dynamic_bus_num)
1922 i2c_scan_static_board_info(adap);
1923
Jean Delvare4735c982008-07-14 22:38:36 +02001924 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001925 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001926 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001927 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001928
1929 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001930
Jean Delvareb119c6c2006-08-15 18:26:30 +02001931out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001932 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001933 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001934 mutex_unlock(&core_lock);
1935 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
David Brownell6e13e642007-05-01 23:26:31 +02001938/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001939 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1940 * @adap: the adapter to register (with adap->nr initialized)
1941 * Context: can sleep
1942 *
1943 * See i2c_add_numbered_adapter() for details.
1944 */
1945static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1946{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001947 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001948
1949 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001950 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001951 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001952 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001953 return id == -ENOSPC ? -EBUSY : id;
1954
1955 return i2c_register_adapter(adap);
1956}
1957
1958/**
David Brownell6e13e642007-05-01 23:26:31 +02001959 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1960 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001961 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001962 *
1963 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001964 * doesn't matter or when its bus number is specified by an dt alias.
1965 * Examples of bases when the bus number doesn't matter: I2C adapters
1966 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001967 *
1968 * When this returns zero, a new bus number was allocated and stored
1969 * in adap->nr, and the specified adapter became available for clients.
1970 * Otherwise, a negative errno value is returned.
1971 */
1972int i2c_add_adapter(struct i2c_adapter *adapter)
1973{
Doug Andersonee5c2742013-03-01 08:57:31 -08001974 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001975 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001976
Doug Andersonee5c2742013-03-01 08:57:31 -08001977 if (dev->of_node) {
1978 id = of_alias_get_id(dev->of_node, "i2c");
1979 if (id >= 0) {
1980 adapter->nr = id;
1981 return __i2c_add_numbered_adapter(adapter);
1982 }
1983 }
1984
Jean Delvarecaada322008-01-27 18:14:49 +01001985 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001986 id = idr_alloc(&i2c_adapter_idr, adapter,
1987 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001988 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001989 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001990 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001991
1992 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001993
David Brownell6e13e642007-05-01 23:26:31 +02001994 return i2c_register_adapter(adapter);
1995}
1996EXPORT_SYMBOL(i2c_add_adapter);
1997
1998/**
1999 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
2000 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02002001 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002002 *
2003 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01002004 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
2005 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02002006 * is used to properly configure I2C devices.
2007 *
Grant Likely488bf312011-07-25 17:49:43 +02002008 * If the requested bus number is set to -1, then this function will behave
2009 * identically to i2c_add_adapter, and will dynamically assign a bus number.
2010 *
David Brownell6e13e642007-05-01 23:26:31 +02002011 * If no devices have pre-been declared for this bus, then be sure to
2012 * register the adapter before any dynamically allocated ones. Otherwise
2013 * the required bus ID may not be available.
2014 *
2015 * When this returns zero, the specified adapter became available for
2016 * clients using the bus number provided in adap->nr. Also, the table
2017 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
2018 * and the appropriate driver model device nodes are created. Otherwise, a
2019 * negative errno value is returned.
2020 */
2021int i2c_add_numbered_adapter(struct i2c_adapter *adap)
2022{
Grant Likely488bf312011-07-25 17:49:43 +02002023 if (adap->nr == -1) /* -1 means dynamically assign bus id */
2024 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002025
Doug Andersonee5c2742013-03-01 08:57:31 -08002026 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002027}
2028EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2029
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002030static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002031 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002032{
Jean Delvare4735c982008-07-14 22:38:36 +02002033 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002034
Jean Delvareacec2112009-03-28 21:34:40 +01002035 /* Remove the devices we created ourselves as the result of hardware
2036 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002037 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2038 if (client->adapter == adapter) {
2039 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2040 client->name, client->addr);
2041 list_del(&client->detected);
2042 i2c_unregister_device(client);
2043 }
2044 }
Jean Delvare026526f2008-01-27 18:14:49 +01002045}
2046
Jean Delvaree549c2b2009-06-19 16:58:19 +02002047static int __unregister_client(struct device *dev, void *dummy)
2048{
2049 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002050 if (client && strcmp(client->name, "dummy"))
2051 i2c_unregister_device(client);
2052 return 0;
2053}
2054
2055static int __unregister_dummy(struct device *dev, void *dummy)
2056{
2057 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002058 if (client)
2059 i2c_unregister_device(client);
2060 return 0;
2061}
2062
Jean Delvare69b00892009-12-06 17:06:27 +01002063static int __process_removed_adapter(struct device_driver *d, void *data)
2064{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002065 i2c_do_del_adapter(to_i2c_driver(d), data);
2066 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002067}
2068
David Brownelld64f73b2007-07-12 14:12:28 +02002069/**
2070 * i2c_del_adapter - unregister I2C adapter
2071 * @adap: the adapter being unregistered
2072 * Context: can sleep
2073 *
2074 * This unregisters an I2C adapter which was previously registered
2075 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2076 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002077void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002079 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002080 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002083 mutex_lock(&core_lock);
2084 found = idr_find(&i2c_adapter_idr, adap->nr);
2085 mutex_unlock(&core_lock);
2086 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002087 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002091 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002092 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002093 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002094 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002095 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002096 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002098 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002099 mutex_lock_nested(&adap->userspace_clients_lock,
2100 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002101 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2102 detected) {
2103 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2104 client->addr);
2105 list_del(&client->detected);
2106 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002107 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002108 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002109
Jean Delvaree549c2b2009-06-19 16:58:19 +02002110 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002111 * check the returned value. This is a two-pass process, because
2112 * we can't remove the dummy devices during the first pass: they
2113 * could have been instantiated by real devices wishing to clean
2114 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002115 device_for_each_child(&adap->dev, NULL, __unregister_client);
2116 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Jean Delvare2bb50952009-09-18 22:45:46 +02002118#ifdef CONFIG_I2C_COMPAT
2119 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2120 adap->dev.parent);
2121#endif
2122
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002123 /* device name is gone after device_unregister */
2124 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2125
Wolfram Sang9f924162015-12-23 18:19:28 +01002126 pm_runtime_disable(&adap->dev);
2127
Wolfram Sang26680ee2015-01-29 22:45:09 +01002128 /* wait until all references to the device are gone
2129 *
2130 * FIXME: This is old code and should ideally be replaced by an
2131 * alternative which results in decoupling the lifetime of the struct
2132 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302133 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
David Brownell6e13e642007-05-01 23:26:31 +02002139 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002140 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002142 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002144 /* Clear the device structure in case this adapter is ever going to be
2145 added again */
2146 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
David Brownellc0564602007-05-01 23:26:31 +02002148EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Wolfram Sang54177cc2015-12-17 13:32:36 +01002150/**
2151 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2152 * @dev: The device to scan for I2C timing properties
2153 * @t: the i2c_timings struct to be filled with values
2154 * @use_defaults: bool to use sane defaults derived from the I2C specification
2155 * when properties are not found, otherwise use 0
2156 *
2157 * Scan the device for the generic I2C properties describing timing parameters
2158 * for the signal and fill the given struct with the results. If a property was
2159 * not found and use_defaults was true, then maximum timings are assumed which
2160 * are derived from the I2C specification. If use_defaults is not used, the
2161 * results will be 0, so drivers can apply their own defaults later. The latter
2162 * is mainly intended for avoiding regressions of existing drivers which want
2163 * to switch to this function. New drivers almost always should use the defaults.
2164 */
2165
2166void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2167{
2168 int ret;
2169
2170 memset(t, 0, sizeof(*t));
2171
2172 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2173 if (ret && use_defaults)
2174 t->bus_freq_hz = 100000;
2175
2176 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2177 if (ret && use_defaults) {
2178 if (t->bus_freq_hz <= 100000)
2179 t->scl_rise_ns = 1000;
2180 else if (t->bus_freq_hz <= 400000)
2181 t->scl_rise_ns = 300;
2182 else
2183 t->scl_rise_ns = 120;
2184 }
2185
2186 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2187 if (ret && use_defaults) {
2188 if (t->bus_freq_hz <= 400000)
2189 t->scl_fall_ns = 300;
2190 else
2191 t->scl_fall_ns = 120;
2192 }
2193
2194 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2195
2196 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2197 if (ret && use_defaults)
2198 t->sda_fall_ns = t->scl_fall_ns;
2199}
2200EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2201
David Brownell7b4fbc52007-05-01 23:26:30 +02002202/* ------------------------------------------------------------------------- */
2203
Jean Delvare7ae31482011-03-20 14:50:52 +01002204int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2205{
2206 int res;
2207
2208 mutex_lock(&core_lock);
2209 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2210 mutex_unlock(&core_lock);
2211
2212 return res;
2213}
2214EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2215
Jean Delvare69b00892009-12-06 17:06:27 +01002216static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002217{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002218 if (dev->type != &i2c_adapter_type)
2219 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002220 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002221}
2222
David Brownell7b4fbc52007-05-01 23:26:30 +02002223/*
2224 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002225 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 */
2227
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002228int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002230 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
David Brownell1d0b19c2008-10-14 17:30:05 +02002232 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302233 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002234 return -EAGAIN;
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002237 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002239 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Jean Delvare729d6dd2009-06-19 16:58:18 +02002241 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002242 * will have called probe() for all matching-but-unbound devices.
2243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 res = driver_register(&driver->driver);
2245 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002246 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002247
Wolfram Sang44239a52016-07-09 13:35:04 +09002248 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jean Delvare4735c982008-07-14 22:38:36 +02002250 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002251 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002252
Jean Delvare7eebcb72006-02-05 23:28:21 +01002253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002255EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Jean Delvare69b00892009-12-06 17:06:27 +01002257static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002258{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002259 if (dev->type == &i2c_adapter_type)
2260 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2261 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002262}
2263
David Brownella1d9e6e2007-05-01 23:26:30 +02002264/**
2265 * i2c_del_driver - unregister I2C driver
2266 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002267 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002268 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002269void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Jean Delvare7ae31482011-03-20 14:50:52 +01002271 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002274 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
David Brownellc0564602007-05-01 23:26:31 +02002276EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
David Brownell7b4fbc52007-05-01 23:26:30 +02002278/* ------------------------------------------------------------------------- */
2279
Jean Delvaree48d3312008-01-27 18:14:48 +01002280/**
2281 * i2c_use_client - increments the reference count of the i2c client structure
2282 * @client: the client being referenced
2283 *
2284 * Each live reference to a client should be refcounted. The driver model does
2285 * that automatically as part of driver binding, so that most drivers don't
2286 * need to do this explicitly: they hold a reference until they're unbound
2287 * from the device.
2288 *
2289 * A pointer to the client with the incremented reference counter is returned.
2290 */
2291struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
David Brownell6ea438e2008-07-14 22:38:24 +02002293 if (client && get_device(&client->dev))
2294 return client;
2295 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
David Brownellc0564602007-05-01 23:26:31 +02002297EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Jean Delvaree48d3312008-01-27 18:14:48 +01002299/**
2300 * i2c_release_client - release a use of the i2c client structure
2301 * @client: the client being no longer referenced
2302 *
2303 * Must be called when a user of a client is finished with it.
2304 */
2305void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
David Brownell6ea438e2008-07-14 22:38:24 +02002307 if (client)
2308 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
David Brownellc0564602007-05-01 23:26:31 +02002310EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
David Brownell9b766b82008-01-27 18:14:51 +01002312struct i2c_cmd_arg {
2313 unsigned cmd;
2314 void *arg;
2315};
2316
2317static int i2c_cmd(struct device *dev, void *_arg)
2318{
2319 struct i2c_client *client = i2c_verify_client(dev);
2320 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002321 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002322
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002323 if (!client || !client->dev.driver)
2324 return 0;
2325
2326 driver = to_i2c_driver(client->dev.driver);
2327 if (driver->command)
2328 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002329 return 0;
2330}
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2333{
David Brownell9b766b82008-01-27 18:14:51 +01002334 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
David Brownell9b766b82008-01-27 18:14:51 +01002336 cmd_arg.cmd = cmd;
2337 cmd_arg.arg = arg;
2338 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
David Brownellc0564602007-05-01 23:26:31 +02002340EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002342#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2343static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2344 void *arg)
2345{
2346 struct of_reconfig_data *rd = arg;
2347 struct i2c_adapter *adap;
2348 struct i2c_client *client;
2349
2350 switch (of_reconfig_get_state_change(action, rd)) {
2351 case OF_RECONFIG_CHANGE_ADD:
2352 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2353 if (adap == NULL)
2354 return NOTIFY_OK; /* not for us */
2355
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002356 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2357 put_device(&adap->dev);
2358 return NOTIFY_OK;
2359 }
2360
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002361 client = of_i2c_register_device(adap, rd->dn);
2362 put_device(&adap->dev);
2363
2364 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002365 dev_err(&adap->dev, "failed to create client for '%s'\n",
2366 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002367 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002368 return notifier_from_errno(PTR_ERR(client));
2369 }
2370 break;
2371 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002372 /* already depopulated? */
2373 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2374 return NOTIFY_OK;
2375
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002376 /* find our device by node */
2377 client = of_find_i2c_device_by_node(rd->dn);
2378 if (client == NULL)
2379 return NOTIFY_OK; /* no? not meant for us */
2380
2381 /* unregister takes one ref away */
2382 i2c_unregister_device(client);
2383
2384 /* and put the reference of the find */
2385 put_device(&client->dev);
2386 break;
2387 }
2388
2389 return NOTIFY_OK;
2390}
2391static struct notifier_block i2c_of_notifier = {
2392 .notifier_call = of_i2c_notify,
2393};
2394#else
2395extern struct notifier_block i2c_of_notifier;
2396#endif /* CONFIG_OF_DYNAMIC */
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398static int __init i2c_init(void)
2399{
2400 int retval;
2401
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002402 retval = of_alias_get_highest_id("i2c");
2403
2404 down_write(&__i2c_board_lock);
2405 if (retval >= __i2c_first_dynamic_bus_num)
2406 __i2c_first_dynamic_bus_num = retval + 1;
2407 up_write(&__i2c_board_lock);
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 retval = bus_register(&i2c_bus_type);
2410 if (retval)
2411 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002412
2413 is_registered = true;
2414
Jean Delvare2bb50952009-09-18 22:45:46 +02002415#ifdef CONFIG_I2C_COMPAT
2416 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2417 if (!i2c_adapter_compat_class) {
2418 retval = -ENOMEM;
2419 goto bus_err;
2420 }
2421#endif
David Brownelle9f13732008-01-27 18:14:52 +01002422 retval = i2c_add_driver(&dummy_driver);
2423 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002424 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002425
2426 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2427 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002428 if (IS_ENABLED(CONFIG_ACPI))
2429 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002430
David Brownelle9f13732008-01-27 18:14:52 +01002431 return 0;
2432
Jean Delvare2bb50952009-09-18 22:45:46 +02002433class_err:
2434#ifdef CONFIG_I2C_COMPAT
2435 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002436bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002437#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002438 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002439 bus_unregister(&i2c_bus_type);
2440 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
2443static void __exit i2c_exit(void)
2444{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002445 if (IS_ENABLED(CONFIG_ACPI))
2446 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002447 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2448 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002449 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002450#ifdef CONFIG_I2C_COMPAT
2451 class_compat_unregister(i2c_adapter_compat_class);
2452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002454 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
David Brownella10f9e72008-10-14 17:30:06 +02002457/* We must initialize early, because some subsystems register i2c drivers
2458 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2459 */
2460postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461module_exit(i2c_exit);
2462
2463/* ----------------------------------------------------
2464 * the functional interface to the i2c busses.
2465 * ----------------------------------------------------
2466 */
2467
Wolfram Sangb7f62582015-01-05 23:45:59 +01002468/* Check if val is exceeding the quirk IFF quirk is non 0 */
2469#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2470
2471static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2472{
2473 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2474 err_msg, msg->addr, msg->len,
2475 msg->flags & I2C_M_RD ? "read" : "write");
2476 return -EOPNOTSUPP;
2477}
2478
2479static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2480{
2481 const struct i2c_adapter_quirks *q = adap->quirks;
2482 int max_num = q->max_num_msgs, i;
2483 bool do_len_check = true;
2484
2485 if (q->flags & I2C_AQ_COMB) {
2486 max_num = 2;
2487
2488 /* special checks for combined messages */
2489 if (num == 2) {
2490 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2491 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2492
2493 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2494 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2495
2496 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2497 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2498
2499 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2500 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2501
2502 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2503 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2504
2505 do_len_check = false;
2506 }
2507 }
2508
2509 if (i2c_quirk_exceeded(num, max_num))
2510 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2511
2512 for (i = 0; i < num; i++) {
2513 u16 len = msgs[i].len;
2514
2515 if (msgs[i].flags & I2C_M_RD) {
2516 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2517 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2518 } else {
2519 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2520 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2521 }
2522 }
2523
2524 return 0;
2525}
2526
David Brownella1cdeda2008-07-14 22:38:24 +02002527/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002528 * __i2c_transfer - unlocked flavor of i2c_transfer
2529 * @adap: Handle to I2C bus
2530 * @msgs: One or more messages to execute before STOP is issued to
2531 * terminate the operation; each message begins with a START.
2532 * @num: Number of messages to be executed.
2533 *
2534 * Returns negative errno, else the number of messages executed.
2535 *
2536 * Adapter lock must be held when calling this function. No debug logging
2537 * takes place. adap->algo->master_xfer existence isn't checked.
2538 */
2539int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2540{
2541 unsigned long orig_jiffies;
2542 int ret, try;
2543
Wolfram Sangb7f62582015-01-05 23:45:59 +01002544 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2545 return -EOPNOTSUPP;
2546
David Howellsd9a83d62014-03-06 13:35:59 +00002547 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2548 * enabled. This is an efficient way of keeping the for-loop from
2549 * being executed when not needed.
2550 */
2551 if (static_key_false(&i2c_trace_msg)) {
2552 int i;
2553 for (i = 0; i < num; i++)
2554 if (msgs[i].flags & I2C_M_RD)
2555 trace_i2c_read(adap, &msgs[i], i);
2556 else
2557 trace_i2c_write(adap, &msgs[i], i);
2558 }
2559
Jean Delvareb37d2a32012-06-29 07:47:19 -03002560 /* Retry automatically on arbitration loss */
2561 orig_jiffies = jiffies;
2562 for (ret = 0, try = 0; try <= adap->retries; try++) {
2563 ret = adap->algo->master_xfer(adap, msgs, num);
2564 if (ret != -EAGAIN)
2565 break;
2566 if (time_after(jiffies, orig_jiffies + adap->timeout))
2567 break;
2568 }
2569
David Howellsd9a83d62014-03-06 13:35:59 +00002570 if (static_key_false(&i2c_trace_msg)) {
2571 int i;
2572 for (i = 0; i < ret; i++)
2573 if (msgs[i].flags & I2C_M_RD)
2574 trace_i2c_reply(adap, &msgs[i], i);
2575 trace_i2c_result(adap, i, ret);
2576 }
2577
Jean Delvareb37d2a32012-06-29 07:47:19 -03002578 return ret;
2579}
2580EXPORT_SYMBOL(__i2c_transfer);
2581
2582/**
David Brownella1cdeda2008-07-14 22:38:24 +02002583 * i2c_transfer - execute a single or combined I2C message
2584 * @adap: Handle to I2C bus
2585 * @msgs: One or more messages to execute before STOP is issued to
2586 * terminate the operation; each message begins with a START.
2587 * @num: Number of messages to be executed.
2588 *
2589 * Returns negative errno, else the number of messages executed.
2590 *
2591 * Note that there is no requirement that each message be sent to
2592 * the same slave address, although that is the most common model.
2593 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002594int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002596 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
David Brownella1cdeda2008-07-14 22:38:24 +02002598 /* REVISIT the fault reporting model here is weak:
2599 *
2600 * - When we get an error after receiving N bytes from a slave,
2601 * there is no way to report "N".
2602 *
2603 * - When we get a NAK after transmitting N bytes to a slave,
2604 * there is no way to report "N" ... or to let the master
2605 * continue executing the rest of this combined message, if
2606 * that's the appropriate response.
2607 *
2608 * - When for example "num" is two and we successfully complete
2609 * the first message but get an error part way through the
2610 * second, it's unclear whether that should be reported as
2611 * one (discarding status on the second message) or errno
2612 * (discarding status on the first one).
2613 */
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (adap->algo->master_xfer) {
2616#ifdef DEBUG
2617 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002618 dev_dbg(&adap->dev,
2619 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2620 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2621 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002622 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624#endif
2625
Mike Rapoportcea443a82008-01-27 18:14:50 +01002626 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002627 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002628 if (!ret)
2629 /* I2C activity is ongoing. */
2630 return -EAGAIN;
2631 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002632 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002633 }
2634
Jean Delvareb37d2a32012-06-29 07:47:19 -03002635 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002636 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 return ret;
2639 } else {
2640 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002641 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643}
David Brownellc0564602007-05-01 23:26:31 +02002644EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
David Brownella1cdeda2008-07-14 22:38:24 +02002646/**
2647 * i2c_master_send - issue a single I2C message in master transmit mode
2648 * @client: Handle to slave device
2649 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002650 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002651 *
2652 * Returns negative errno, or else the number of bytes written.
2653 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002654int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002657 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 struct i2c_msg msg;
2659
Jean Delvare815f55f2005-05-07 22:58:46 +02002660 msg.addr = client->addr;
2661 msg.flags = client->flags & I2C_M_TEN;
2662 msg.len = count;
2663 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002664
Jean Delvare815f55f2005-05-07 22:58:46 +02002665 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002667 /*
2668 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2669 * transmitted, else error code.
2670 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002671 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
David Brownellc0564602007-05-01 23:26:31 +02002673EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
David Brownella1cdeda2008-07-14 22:38:24 +02002675/**
2676 * i2c_master_recv - issue a single I2C message in master receive mode
2677 * @client: Handle to slave device
2678 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002679 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002680 *
2681 * Returns negative errno, or else the number of bytes read.
2682 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002683int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Farid Hammane7225acf2010-05-21 18:40:58 +02002685 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct i2c_msg msg;
2687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Jean Delvare815f55f2005-05-07 22:58:46 +02002689 msg.addr = client->addr;
2690 msg.flags = client->flags & I2C_M_TEN;
2691 msg.flags |= I2C_M_RD;
2692 msg.len = count;
2693 msg.buf = buf;
2694
2695 ret = i2c_transfer(adap, &msg, 1);
2696
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002697 /*
2698 * If everything went ok (i.e. 1 msg received), return #bytes received,
2699 * else error code.
2700 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002701 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
David Brownellc0564602007-05-01 23:26:31 +02002703EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705/* ----------------------------------------------------
2706 * the i2c address scanning function
2707 * Will not work for 10-bit addresses!
2708 * ----------------------------------------------------
2709 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002710
Jean Delvare63e4e802010-06-03 11:33:51 +02002711/*
2712 * Legacy default probe function, mostly relevant for SMBus. The default
2713 * probe method is a quick write, but it is known to corrupt the 24RF08
2714 * EEPROMs due to a state machine bug, and could also irreversibly
2715 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2716 * we use a short byte read instead. Also, some bus drivers don't implement
2717 * quick write, so we fallback to a byte read in that case too.
2718 * On x86, there is another special case for FSC hardware monitoring chips,
2719 * which want regular byte reads (address 0x73.) Fortunately, these are the
2720 * only known chips using this I2C address on PC hardware.
2721 * Returns 1 if probe succeeded, 0 if not.
2722 */
2723static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2724{
2725 int err;
2726 union i2c_smbus_data dummy;
2727
2728#ifdef CONFIG_X86
2729 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2730 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2731 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2732 I2C_SMBUS_BYTE_DATA, &dummy);
2733 else
2734#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002735 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2736 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002737 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2738 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002739 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2740 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2741 I2C_SMBUS_BYTE, &dummy);
2742 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002743 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2744 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002745 err = -EOPNOTSUPP;
2746 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002747
2748 return err >= 0;
2749}
2750
Jean Delvareccfbbd02009-12-06 17:06:25 +01002751static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002752 struct i2c_driver *driver)
2753{
2754 struct i2c_board_info info;
2755 struct i2c_adapter *adapter = temp_client->adapter;
2756 int addr = temp_client->addr;
2757 int err;
2758
2759 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002760 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002761 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002762 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2763 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002764 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002765 }
2766
Wolfram Sang9bccc702015-07-17 14:48:56 +02002767 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002768 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002769 return 0;
2770
Jean Delvareccfbbd02009-12-06 17:06:25 +01002771 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002772 if (!i2c_default_probe(adapter, addr))
2773 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002774
2775 /* Finally call the custom detection function */
2776 memset(&info, 0, sizeof(struct i2c_board_info));
2777 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002778 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002779 if (err) {
2780 /* -ENODEV is returned if the detection fails. We catch it
2781 here as this isn't an error. */
2782 return err == -ENODEV ? 0 : err;
2783 }
2784
2785 /* Consistency check */
2786 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002787 dev_err(&adapter->dev,
2788 "%s detection function provided no name for 0x%x\n",
2789 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002790 } else {
2791 struct i2c_client *client;
2792
2793 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002794 if (adapter->class & I2C_CLASS_DEPRECATED)
2795 dev_warn(&adapter->dev,
2796 "This adapter will soon drop class based instantiation of devices. "
2797 "Please make sure client 0x%02x gets instantiated by other means. "
2798 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2799 info.addr);
2800
Jean Delvare4735c982008-07-14 22:38:36 +02002801 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2802 info.type, info.addr);
2803 client = i2c_new_device(adapter, &info);
2804 if (client)
2805 list_add_tail(&client->detected, &driver->clients);
2806 else
2807 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2808 info.type, info.addr);
2809 }
2810 return 0;
2811}
2812
2813static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2814{
Jean Delvarec3813d62009-12-14 21:17:25 +01002815 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002816 struct i2c_client *temp_client;
2817 int i, err = 0;
2818 int adap_id = i2c_adapter_id(adapter);
2819
Jean Delvarec3813d62009-12-14 21:17:25 +01002820 address_list = driver->address_list;
2821 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002822 return 0;
2823
Wolfram Sang45552272014-07-10 13:46:21 +02002824 /* Warn that the adapter lost class based instantiation */
2825 if (adapter->class == I2C_CLASS_DEPRECATED) {
2826 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002827 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2828 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002829 driver->driver.name);
2830 return 0;
2831 }
2832
Jean Delvare51b54ba2010-10-24 18:16:58 +02002833 /* Stop here if the classes do not match */
2834 if (!(adapter->class & driver->class))
2835 return 0;
2836
Jean Delvare4735c982008-07-14 22:38:36 +02002837 /* Set up a temporary client to help detect callback */
2838 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2839 if (!temp_client)
2840 return -ENOMEM;
2841 temp_client->adapter = adapter;
2842
Jean Delvarec3813d62009-12-14 21:17:25 +01002843 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002844 dev_dbg(&adapter->dev,
2845 "found normal entry for adapter %d, addr 0x%02x\n",
2846 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002847 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002848 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002849 if (unlikely(err))
2850 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002851 }
2852
Jean Delvare4735c982008-07-14 22:38:36 +02002853 kfree(temp_client);
2854 return err;
2855}
2856
Jean Delvared44f19d2010-08-11 18:20:57 +02002857int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2858{
2859 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2860 I2C_SMBUS_QUICK, NULL) >= 0;
2861}
2862EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2863
Jean Delvare12b5053a2007-05-01 23:26:31 +02002864struct i2c_client *
2865i2c_new_probed_device(struct i2c_adapter *adap,
2866 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002867 unsigned short const *addr_list,
2868 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002869{
2870 int i;
2871
Jean Delvare8031d792010-08-11 18:21:00 +02002872 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002873 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002874
Jean Delvare12b5053a2007-05-01 23:26:31 +02002875 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2876 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002877 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002878 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2879 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002880 continue;
2881 }
2882
Wolfram Sang9bccc702015-07-17 14:48:56 +02002883 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002884 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002885 dev_dbg(&adap->dev,
2886 "Address 0x%02x already in use, not probing\n",
2887 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002888 continue;
2889 }
2890
Jean Delvare63e4e802010-06-03 11:33:51 +02002891 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002892 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002893 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002894 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002895
2896 if (addr_list[i] == I2C_CLIENT_END) {
2897 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2898 return NULL;
2899 }
2900
2901 info->addr = addr_list[i];
2902 return i2c_new_device(adap, info);
2903}
2904EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2905
Jean Delvared735b342011-03-20 14:50:52 +01002906struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002909
Jean Delvarecaada322008-01-27 18:14:49 +01002910 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002911 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002912 if (!adapter)
2913 goto exit;
2914
2915 if (try_module_get(adapter->owner))
2916 get_device(&adapter->dev);
2917 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002918 adapter = NULL;
2919
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002920 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002921 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002922 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
David Brownellc0564602007-05-01 23:26:31 +02002924EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926void i2c_put_adapter(struct i2c_adapter *adap)
2927{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002928 if (!adap)
2929 return;
2930
2931 put_device(&adap->dev);
2932 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
David Brownellc0564602007-05-01 23:26:31 +02002934EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936/* The SMBus parts */
2937
David Brownell438d6c22006-12-10 21:21:31 +01002938#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002939static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002942
Farid Hammane7225acf2010-05-21 18:40:58 +02002943 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002944 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 data = data ^ POLY;
2946 data = data << 1;
2947 }
2948 return (u8)(data >> 8);
2949}
2950
Jean Delvare421ef472005-10-26 21:28:55 +02002951/* Incremental CRC8 over count bytes in the array pointed to by p */
2952static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 int i;
2955
Farid Hammane7225acf2010-05-21 18:40:58 +02002956 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002957 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return crc;
2959}
2960
Jean Delvare421ef472005-10-26 21:28:55 +02002961/* Assume a 7-bit address, which is reasonable for SMBus */
2962static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Jean Delvare421ef472005-10-26 21:28:55 +02002964 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002965 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002966 pec = i2c_smbus_pec(pec, &addr, 1);
2967
2968 /* The data buffer follows */
2969 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Jean Delvare421ef472005-10-26 21:28:55 +02002972/* Used for write only transactions */
2973static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Jean Delvare421ef472005-10-26 21:28:55 +02002975 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2976 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
Jean Delvare421ef472005-10-26 21:28:55 +02002979/* Return <0 on CRC error
2980 If there was a write before this read (most cases) we need to take the
2981 partial CRC from the write part into account.
2982 Note that this function does modify the message (we need to decrease the
2983 message length to hide the CRC byte from the caller). */
2984static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
Jean Delvare421ef472005-10-26 21:28:55 +02002986 u8 rpec = msg->buf[--msg->len];
2987 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002990 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002992 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
David Brownell438d6c22006-12-10 21:21:31 +01002994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
David Brownella1cdeda2008-07-14 22:38:24 +02002997/**
2998 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2999 * @client: Handle to slave device
3000 *
3001 * This executes the SMBus "receive byte" protocol, returning negative errno
3002 * else the byte received from the device.
3003 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003004s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003007 int status;
3008
3009 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3010 I2C_SMBUS_READ, 0,
3011 I2C_SMBUS_BYTE, &data);
3012 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
David Brownellc0564602007-05-01 23:26:31 +02003014EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
David Brownella1cdeda2008-07-14 22:38:24 +02003016/**
3017 * i2c_smbus_write_byte - SMBus "send byte" protocol
3018 * @client: Handle to slave device
3019 * @value: Byte to be sent
3020 *
3021 * This executes the SMBus "send byte" protocol, returning negative errno
3022 * else zero on success.
3023 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003024s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Farid Hammane7225acf2010-05-21 18:40:58 +02003026 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02003027 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
David Brownellc0564602007-05-01 23:26:31 +02003029EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
David Brownella1cdeda2008-07-14 22:38:24 +02003031/**
3032 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
3033 * @client: Handle to slave device
3034 * @command: Byte interpreted by slave
3035 *
3036 * This executes the SMBus "read byte" protocol, returning negative errno
3037 * else a data byte received from the device.
3038 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003039s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003042 int status;
3043
3044 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3045 I2C_SMBUS_READ, command,
3046 I2C_SMBUS_BYTE_DATA, &data);
3047 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
David Brownellc0564602007-05-01 23:26:31 +02003049EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
David Brownella1cdeda2008-07-14 22:38:24 +02003051/**
3052 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
3053 * @client: Handle to slave device
3054 * @command: Byte interpreted by slave
3055 * @value: Byte being written
3056 *
3057 * This executes the SMBus "write byte" protocol, returning negative errno
3058 * else zero on success.
3059 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003060s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3061 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 union i2c_smbus_data data;
3064 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003065 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3066 I2C_SMBUS_WRITE, command,
3067 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
David Brownellc0564602007-05-01 23:26:31 +02003069EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
David Brownella1cdeda2008-07-14 22:38:24 +02003071/**
3072 * i2c_smbus_read_word_data - SMBus "read word" protocol
3073 * @client: Handle to slave device
3074 * @command: Byte interpreted by slave
3075 *
3076 * This executes the SMBus "read word" protocol, returning negative errno
3077 * else a 16-bit unsigned "word" received from the device.
3078 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003079s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003082 int status;
3083
3084 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3085 I2C_SMBUS_READ, command,
3086 I2C_SMBUS_WORD_DATA, &data);
3087 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
David Brownellc0564602007-05-01 23:26:31 +02003089EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
David Brownella1cdeda2008-07-14 22:38:24 +02003091/**
3092 * i2c_smbus_write_word_data - SMBus "write word" protocol
3093 * @client: Handle to slave device
3094 * @command: Byte interpreted by slave
3095 * @value: 16-bit "word" being written
3096 *
3097 * This executes the SMBus "write word" protocol, returning negative errno
3098 * else zero on success.
3099 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003100s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3101 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103 union i2c_smbus_data data;
3104 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003105 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3106 I2C_SMBUS_WRITE, command,
3107 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
David Brownellc0564602007-05-01 23:26:31 +02003109EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
David Brownella64ec072007-10-13 23:56:31 +02003111/**
David Brownella1cdeda2008-07-14 22:38:24 +02003112 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003113 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003114 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003115 * @values: Byte array into which data will be read; big enough to hold
3116 * the data returned by the slave. SMBus allows at most 32 bytes.
3117 *
David Brownella1cdeda2008-07-14 22:38:24 +02003118 * This executes the SMBus "block read" protocol, returning negative errno
3119 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003120 *
3121 * Note that using this function requires that the client's adapter support
3122 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3123 * support this; its emulation through I2C messaging relies on a specific
3124 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3125 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003126s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003127 u8 *values)
3128{
3129 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003130 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003131
David Brownell24a5bb72008-07-14 22:38:23 +02003132 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3133 I2C_SMBUS_READ, command,
3134 I2C_SMBUS_BLOCK_DATA, &data);
3135 if (status)
3136 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003137
3138 memcpy(values, &data.block[1], data.block[0]);
3139 return data.block[0];
3140}
3141EXPORT_SYMBOL(i2c_smbus_read_block_data);
3142
David Brownella1cdeda2008-07-14 22:38:24 +02003143/**
3144 * i2c_smbus_write_block_data - SMBus "block write" protocol
3145 * @client: Handle to slave device
3146 * @command: Byte interpreted by slave
3147 * @length: Size of data block; SMBus allows at most 32 bytes
3148 * @values: Byte array which will be written.
3149 *
3150 * This executes the SMBus "block write" protocol, returning negative errno
3151 * else zero on success.
3152 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003153s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003154 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (length > I2C_SMBUS_BLOCK_MAX)
3159 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003161 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003162 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3163 I2C_SMBUS_WRITE, command,
3164 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
David Brownellc0564602007-05-01 23:26:31 +02003166EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003169s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003170 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003173 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003174
Jean Delvare4b2643d2007-07-12 14:12:29 +02003175 if (length > I2C_SMBUS_BLOCK_MAX)
3176 length = I2C_SMBUS_BLOCK_MAX;
3177 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003178 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3179 I2C_SMBUS_READ, command,
3180 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3181 if (status < 0)
3182 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003183
3184 memcpy(values, &data.block[1], data.block[0]);
3185 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
David Brownellc0564602007-05-01 23:26:31 +02003187EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Jean Delvare0cc43a12011-01-10 22:11:23 +01003189s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003190 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003191{
3192 union i2c_smbus_data data;
3193
3194 if (length > I2C_SMBUS_BLOCK_MAX)
3195 length = I2C_SMBUS_BLOCK_MAX;
3196 data.block[0] = length;
3197 memcpy(data.block + 1, values, length);
3198 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3199 I2C_SMBUS_WRITE, command,
3200 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3201}
David Brownellc0564602007-05-01 23:26:31 +02003202EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003203
David Brownell438d6c22006-12-10 21:21:31 +01003204/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003206static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3207 unsigned short flags,
3208 char read_write, u8 command, int size,
3209 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
3211 /* So we need to generate a series of msgs. In the case of writing, we
3212 need to use only one message; when reading, we need two. We initialize
3213 most things with sane defaults, to keep the code below somewhat
3214 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003215 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3216 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003217 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003219 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003220 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003221 struct i2c_msg msg[2] = {
3222 {
3223 .addr = addr,
3224 .flags = flags,
3225 .len = 1,
3226 .buf = msgbuf0,
3227 }, {
3228 .addr = addr,
3229 .flags = flags | I2C_M_RD,
3230 .len = 0,
3231 .buf = msgbuf1,
3232 },
3233 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003236 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 case I2C_SMBUS_QUICK:
3238 msg[0].len = 0;
3239 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003240 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3241 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 num = 1;
3243 break;
3244 case I2C_SMBUS_BYTE:
3245 if (read_write == I2C_SMBUS_READ) {
3246 /* Special case: only a read! */
3247 msg[0].flags = I2C_M_RD | flags;
3248 num = 1;
3249 }
3250 break;
3251 case I2C_SMBUS_BYTE_DATA:
3252 if (read_write == I2C_SMBUS_READ)
3253 msg[1].len = 1;
3254 else {
3255 msg[0].len = 2;
3256 msgbuf0[1] = data->byte;
3257 }
3258 break;
3259 case I2C_SMBUS_WORD_DATA:
3260 if (read_write == I2C_SMBUS_READ)
3261 msg[1].len = 2;
3262 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003263 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003265 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267 break;
3268 case I2C_SMBUS_PROC_CALL:
3269 num = 2; /* Special case */
3270 read_write = I2C_SMBUS_READ;
3271 msg[0].len = 3;
3272 msg[1].len = 2;
3273 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003274 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 break;
3276 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003278 msg[1].flags |= I2C_M_RECV_LEN;
3279 msg[1].len = 1; /* block length will be added by
3280 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 } else {
3282 msg[0].len = data->block[0] + 2;
3283 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003284 dev_err(&adapter->dev,
3285 "Invalid block write size %d\n",
3286 data->block[0]);
3287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003289 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 msgbuf0[i] = data->block[i-1];
3291 }
3292 break;
3293 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003294 num = 2; /* Another special case */
3295 read_write = I2C_SMBUS_READ;
3296 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003297 dev_err(&adapter->dev,
3298 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003299 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003300 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003301 }
3302 msg[0].len = data->block[0] + 2;
3303 for (i = 1; i < msg[0].len; i++)
3304 msgbuf0[i] = data->block[i-1];
3305 msg[1].flags |= I2C_M_RECV_LEN;
3306 msg[1].len = 1; /* block length will be added by
3307 the underlying bus driver */
3308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 case I2C_SMBUS_I2C_BLOCK_DATA:
3310 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003311 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 } else {
3313 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003314 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003315 dev_err(&adapter->dev,
3316 "Invalid block write size %d\n",
3317 data->block[0]);
3318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 for (i = 1; i <= data->block[0]; i++)
3321 msgbuf0[i] = data->block[i];
3322 }
3323 break;
3324 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003325 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3326 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328
Jean Delvare421ef472005-10-26 21:28:55 +02003329 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3330 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3331 if (i) {
3332 /* Compute PEC if first message is a write */
3333 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003334 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003335 i2c_smbus_add_pec(&msg[0]);
3336 else /* Write followed by read */
3337 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3338 }
3339 /* Ask for PEC if last message is a read */
3340 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003341 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003342 }
3343
David Brownell24a5bb72008-07-14 22:38:23 +02003344 status = i2c_transfer(adapter, msg, num);
3345 if (status < 0)
3346 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Jean Delvare421ef472005-10-26 21:28:55 +02003348 /* Check PEC if last message is a read */
3349 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003350 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3351 if (status < 0)
3352 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003353 }
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003356 switch (size) {
3357 case I2C_SMBUS_BYTE:
3358 data->byte = msgbuf0[0];
3359 break;
3360 case I2C_SMBUS_BYTE_DATA:
3361 data->byte = msgbuf1[0];
3362 break;
3363 case I2C_SMBUS_WORD_DATA:
3364 case I2C_SMBUS_PROC_CALL:
3365 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3366 break;
3367 case I2C_SMBUS_I2C_BLOCK_DATA:
3368 for (i = 0; i < data->block[0]; i++)
3369 data->block[i+1] = msgbuf1[i];
3370 break;
3371 case I2C_SMBUS_BLOCK_DATA:
3372 case I2C_SMBUS_BLOCK_PROC_CALL:
3373 for (i = 0; i < msgbuf1[0] + 1; i++)
3374 data->block[i] = msgbuf1[i];
3375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 }
3377 return 0;
3378}
3379
David Brownella1cdeda2008-07-14 22:38:24 +02003380/**
3381 * i2c_smbus_xfer - execute SMBus protocol operations
3382 * @adapter: Handle to I2C bus
3383 * @addr: Address of SMBus slave on that bus
3384 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3385 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3386 * @command: Byte interpreted by slave, for protocols which use such bytes
3387 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3388 * @data: Data to be read or written
3389 *
3390 * This executes an SMBus protocol operation, and returns a negative
3391 * errno code else zero on success.
3392 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003393s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003394 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003395 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003397 unsigned long orig_jiffies;
3398 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
David Howells8a325992014-03-06 13:36:06 +00003401 /* If enabled, the following two tracepoints are conditional on
3402 * read_write and protocol.
3403 */
3404 trace_smbus_write(adapter, addr, flags, read_write,
3405 command, protocol, data);
3406 trace_smbus_read(adapter, addr, flags, read_write,
3407 command, protocol);
3408
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003409 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003412 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003413
3414 /* Retry automatically on arbitration loss */
3415 orig_jiffies = jiffies;
3416 for (res = 0, try = 0; try <= adapter->retries; try++) {
3417 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3418 read_write, command,
3419 protocol, data);
3420 if (res != -EAGAIN)
3421 break;
3422 if (time_after(jiffies,
3423 orig_jiffies + adapter->timeout))
3424 break;
3425 }
Peter Rosin8320f492016-05-04 22:15:27 +02003426 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003428 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003429 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003430 /*
3431 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3432 * implement native support for the SMBus operation.
3433 */
3434 }
3435
David Howells8a325992014-03-06 13:36:06 +00003436 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3437 command, protocol, data);
3438
3439trace:
3440 /* If enabled, the reply tracepoint is conditional on read_write. */
3441 trace_smbus_reply(adapter, addr, flags, read_write,
3442 command, protocol, data);
3443 trace_smbus_result(adapter, addr, flags, read_write,
3444 command, protocol, res);
3445
3446 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Irina Tirdea01eef962015-08-12 17:31:33 +03003450/**
3451 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3452 * @client: Handle to slave device
3453 * @command: Byte interpreted by slave
3454 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3455 * @values: Byte array into which data will be read; big enough to hold
3456 * the data returned by the slave. SMBus allows at most
3457 * I2C_SMBUS_BLOCK_MAX bytes.
3458 *
3459 * This executes the SMBus "block read" protocol if supported by the adapter.
3460 * If block read is not supported, it emulates it using either word or byte
3461 * read protocols depending on availability.
3462 *
3463 * The addresses of the I2C slave device that are accessed with this function
3464 * must be mapped to a linear region, so that a block read will have the same
3465 * effect as a byte read. Before using this function you must double-check
3466 * if the I2C slave does support exchanging a block transfer with a byte
3467 * transfer.
3468 */
3469s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3470 u8 command, u8 length, u8 *values)
3471{
3472 u8 i = 0;
3473 int status;
3474
3475 if (length > I2C_SMBUS_BLOCK_MAX)
3476 length = I2C_SMBUS_BLOCK_MAX;
3477
3478 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3479 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3480
3481 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3482 return -EOPNOTSUPP;
3483
3484 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3485 while ((i + 2) <= length) {
3486 status = i2c_smbus_read_word_data(client, command + i);
3487 if (status < 0)
3488 return status;
3489 values[i] = status & 0xff;
3490 values[i + 1] = status >> 8;
3491 i += 2;
3492 }
3493 }
3494
3495 while (i < length) {
3496 status = i2c_smbus_read_byte_data(client, command + i);
3497 if (status < 0)
3498 return status;
3499 values[i] = status;
3500 i++;
3501 }
3502
3503 return i;
3504}
3505EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3506
Jean Delvared5fd1202015-01-26 20:59:31 +01003507#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003508int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3509{
3510 int ret;
3511
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003512 if (!client || !slave_cb) {
3513 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003514 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003515 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003516
Wolfram Sangc6909d62015-08-05 21:12:54 +02003517 if (!(client->flags & I2C_CLIENT_SLAVE))
3518 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3519 __func__);
3520
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003521 if (!(client->flags & I2C_CLIENT_TEN)) {
3522 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003523 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003524 if (ret) {
3525 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003526 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003527 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003528 }
3529
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003530 if (!client->adapter->algo->reg_slave) {
3531 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003532 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003533 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003534
3535 client->slave_cb = slave_cb;
3536
3537 i2c_lock_adapter(client->adapter);
3538 ret = client->adapter->algo->reg_slave(client);
3539 i2c_unlock_adapter(client->adapter);
3540
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003541 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003542 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003543 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3544 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003545
3546 return ret;
3547}
3548EXPORT_SYMBOL_GPL(i2c_slave_register);
3549
3550int i2c_slave_unregister(struct i2c_client *client)
3551{
3552 int ret;
3553
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003554 if (!client->adapter->algo->unreg_slave) {
3555 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003556 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003557 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003558
3559 i2c_lock_adapter(client->adapter);
3560 ret = client->adapter->algo->unreg_slave(client);
3561 i2c_unlock_adapter(client->adapter);
3562
3563 if (ret == 0)
3564 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003565 else
3566 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003567
3568 return ret;
3569}
3570EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003571#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3574MODULE_DESCRIPTION("I2C-Bus main module");
3575MODULE_LICENSE("GPL");