blob: c588a8504c99f56769fae732b0f56cefc47b1ce5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram Sang44239a52016-07-09 13:35:04 +090030#define pr_fmt(fmt) "i2c-core: " fmt
31
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020032#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <asm/uaccess.h>
34#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020035#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010036#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010037#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020038#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010039#include <linux/errno.h>
40#include <linux/gpio.h>
41#include <linux/hardirq.h>
42#include <linux/i2c.h>
43#include <linux/idr.h>
44#include <linux/init.h>
45#include <linux/irqflags.h>
46#include <linux/jump_label.h>
47#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/mutex.h>
50#include <linux/of_device.h>
51#include <linux/of.h>
52#include <linux/of_irq.h>
53#include <linux/pm_domain.h>
54#include <linux/pm_runtime.h>
55#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010056#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010057#include <linux/rwsem.h>
58#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David Brownell9c1600e2007-05-01 23:26:31 +020060#include "i2c-core.h"
61
David Howellsd9a83d62014-03-06 13:35:59 +000062#define CREATE_TRACE_POINTS
63#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Wolfram Sangda899f52015-05-18 21:09:12 +020065#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
66#define I2C_ADDR_OFFSET_SLAVE 0x1000
67
Jean Delvare6629dcf2010-05-04 11:09:28 +020068/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020069 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000070 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010071static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_IDR(i2c_adapter_idr);
73
Jean Delvare4f8cf822009-09-18 22:45:46 +020074static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020075static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010076
David Howellsd9a83d62014-03-06 13:35:59 +000077static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053078static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000079
80void i2c_transfer_trace_reg(void)
81{
82 static_key_slow_inc(&i2c_trace_msg);
83}
84
85void i2c_transfer_trace_unreg(void)
86{
87 static_key_slow_dec(&i2c_trace_msg);
88}
89
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020090#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030091struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020092 struct acpi_connection_info info;
93 struct i2c_adapter *adapter;
94};
95
96struct gsb_buffer {
97 u8 status;
98 u8 len;
99 union {
100 u16 wdata;
101 u8 bdata;
102 u8 data[0];
103 };
104} __packed;
105
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300106struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300107 struct i2c_board_info *info;
108 acpi_handle adapter_handle;
109 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300110 acpi_handle search_handle;
111 u32 speed;
112 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300113};
114
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300115static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200116{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300117 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300118 struct i2c_board_info *info = lookup->info;
119 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300120 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200121
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300122 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
123 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200124
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300125 sb = &ares->data.i2c_serial_bus;
126 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
127 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200128
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300129 status = acpi_get_handle(lookup->device_handle,
130 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300131 &lookup->adapter_handle);
132 if (!ACPI_SUCCESS(status))
133 return 1;
134
135 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300136 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300137 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
138 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200139
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140 return 1;
141}
142
Jarkko Nikula5853b222016-08-12 17:02:53 +0300143static int i2c_acpi_do_lookup(struct acpi_device *adev,
144 struct i2c_acpi_lookup *lookup)
145{
146 struct i2c_board_info *info = lookup->info;
147 struct list_head resource_list;
148 int ret;
149
150 if (acpi_bus_get_status(adev) || !adev->status.present ||
151 acpi_device_enumerated(adev))
152 return -EINVAL;
153
154 memset(info, 0, sizeof(*info));
155 lookup->device_handle = acpi_device_handle(adev);
156
157 /* Look up for I2cSerialBus resource */
158 INIT_LIST_HEAD(&resource_list);
159 ret = acpi_dev_get_resources(adev, &resource_list,
160 i2c_acpi_fill_info, lookup);
161 acpi_dev_free_resource_list(&resource_list);
162
163 if (ret < 0 || !info->addr)
164 return -EINVAL;
165
166 return 0;
167}
168
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300169static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300170 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300171 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300172 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200173{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200174 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300175 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300176 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200177 int ret;
178
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300179 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300180 lookup.info = info;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300181
Jarkko Nikula5853b222016-08-12 17:02:53 +0300182 ret = i2c_acpi_do_lookup(adev, &lookup);
183 if (ret)
184 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200185
Mika Westerberg318ce272016-09-20 16:59:25 +0300186 if (adapter) {
187 /* The adapter must match the one in I2cSerialBus() connector */
188 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
189 return -ENODEV;
190 } else {
191 struct acpi_device *adapter_adev;
192
193 /* The adapter must be present */
194 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
195 return -ENODEV;
196 if (acpi_bus_get_status(adapter_adev) ||
197 !adapter_adev->status.present)
198 return -ENODEV;
199 }
200
Jarkko Nikula5853b222016-08-12 17:02:53 +0300201 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300202 if (adapter_handle)
203 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200204
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300205 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300206 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300207 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
208 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300209 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300210
211 resource_list_for_each_entry(entry, &resource_list) {
212 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300213 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300214 break;
215 }
216 }
217
218 acpi_dev_free_resource_list(&resource_list);
219
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300220 strlcpy(info->type, dev_name(&adev->dev), sizeof(info->type));
221
222 return 0;
223}
224
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300225static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300226 struct acpi_device *adev,
227 struct i2c_board_info *info)
228{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200229 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300230 acpi_device_set_enumerated(adev);
231
232 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200233 adev->power.flags.ignore_parent = false;
234 dev_err(&adapter->dev,
235 "failed to add I2C device %s from ACPI\n",
236 dev_name(&adev->dev));
237 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300238}
239
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300240static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300241 void *data, void **return_value)
242{
243 struct i2c_adapter *adapter = data;
244 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300245 struct i2c_board_info info;
246
247 if (acpi_bus_get_device(handle, &adev))
248 return AE_OK;
249
Mika Westerberg318ce272016-09-20 16:59:25 +0300250 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300251 return AE_OK;
252
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300253 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200254
255 return AE_OK;
256}
257
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300258#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300259
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200260/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300261 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200262 * @adap: pointer to adapter
263 *
264 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
265 * namespace. When a device is found it will be added to the Linux device
266 * model and bound to the corresponding ACPI handle.
267 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300268static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200269{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200270 acpi_status status;
271
Dustin Byford8eb5c872015-10-23 12:27:07 -0700272 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200273 return;
274
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300275 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300276 I2C_ACPI_MAX_SCAN_DEPTH,
277 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200278 adap, NULL);
279 if (ACPI_FAILURE(status))
280 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
281}
282
Jarkko Nikula5853b222016-08-12 17:02:53 +0300283static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
284 void *data, void **return_value)
285{
286 struct i2c_acpi_lookup *lookup = data;
287 struct acpi_device *adev;
288
289 if (acpi_bus_get_device(handle, &adev))
290 return AE_OK;
291
292 if (i2c_acpi_do_lookup(adev, lookup))
293 return AE_OK;
294
295 if (lookup->search_handle != lookup->adapter_handle)
296 return AE_OK;
297
298 if (lookup->speed <= lookup->min_speed)
299 lookup->min_speed = lookup->speed;
300
301 return AE_OK;
302}
303
304/**
305 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
306 * @dev: The device owning the bus
307 *
308 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
309 * devices connected to this bus and use the speed of slowest device.
310 *
311 * Returns the speed in Hz or zero
312 */
313u32 i2c_acpi_find_bus_speed(struct device *dev)
314{
315 struct i2c_acpi_lookup lookup;
316 struct i2c_board_info dummy;
317 acpi_status status;
318
319 if (!has_acpi_companion(dev))
320 return 0;
321
322 memset(&lookup, 0, sizeof(lookup));
323 lookup.search_handle = ACPI_HANDLE(dev);
324 lookup.min_speed = UINT_MAX;
325 lookup.info = &dummy;
326
327 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
328 I2C_ACPI_MAX_SCAN_DEPTH,
329 i2c_acpi_lookup_speed, NULL,
330 &lookup, NULL);
331
332 if (ACPI_FAILURE(status)) {
333 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
334 return 0;
335 }
336
337 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
338}
339EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
340
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300341static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300342{
343 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
344
345 if (!adapter)
346 return 0;
347
348 return ACPI_HANDLE(dev) == (acpi_handle)data;
349}
350
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300351static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300352{
353 return ACPI_COMPANION(dev) == data;
354}
355
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300356static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300357{
358 struct device *dev;
359
360 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300361 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300362 return dev ? i2c_verify_adapter(dev) : NULL;
363}
364
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300365static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300366{
367 struct device *dev;
368
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300369 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300370 return dev ? i2c_verify_client(dev) : NULL;
371}
372
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300373static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300374 void *arg)
375{
376 struct acpi_device *adev = arg;
377 struct i2c_board_info info;
378 acpi_handle adapter_handle;
379 struct i2c_adapter *adapter;
380 struct i2c_client *client;
381
382 switch (value) {
383 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300384 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300385 break;
386
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300387 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300388 if (!adapter)
389 break;
390
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300391 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300392 break;
393 case ACPI_RECONFIG_DEVICE_REMOVE:
394 if (!acpi_device_enumerated(adev))
395 break;
396
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300397 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300398 if (!client)
399 break;
400
401 i2c_unregister_device(client);
402 put_device(&client->dev);
403 break;
404 }
405
406 return NOTIFY_OK;
407}
408
409static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300410 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300411};
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200412#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300413static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300414extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415#endif /* CONFIG_ACPI */
416
417#ifdef CONFIG_ACPI_I2C_OPREGION
418static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
419 u8 cmd, u8 *data, u8 data_len)
420{
421
422 struct i2c_msg msgs[2];
423 int ret;
424 u8 *buffer;
425
426 buffer = kzalloc(data_len, GFP_KERNEL);
427 if (!buffer)
428 return AE_NO_MEMORY;
429
430 msgs[0].addr = client->addr;
431 msgs[0].flags = client->flags;
432 msgs[0].len = 1;
433 msgs[0].buf = &cmd;
434
435 msgs[1].addr = client->addr;
436 msgs[1].flags = client->flags | I2C_M_RD;
437 msgs[1].len = data_len;
438 msgs[1].buf = buffer;
439
440 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
441 if (ret < 0)
442 dev_err(&client->adapter->dev, "i2c read failed\n");
443 else
444 memcpy(data, buffer, data_len);
445
446 kfree(buffer);
447 return ret;
448}
449
450static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
451 u8 cmd, u8 *data, u8 data_len)
452{
453
454 struct i2c_msg msgs[1];
455 u8 *buffer;
456 int ret = AE_OK;
457
458 buffer = kzalloc(data_len + 1, GFP_KERNEL);
459 if (!buffer)
460 return AE_NO_MEMORY;
461
462 buffer[0] = cmd;
463 memcpy(buffer + 1, data, data_len);
464
465 msgs[0].addr = client->addr;
466 msgs[0].flags = client->flags;
467 msgs[0].len = data_len + 1;
468 msgs[0].buf = buffer;
469
470 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
471 if (ret < 0)
472 dev_err(&client->adapter->dev, "i2c write failed\n");
473
474 kfree(buffer);
475 return ret;
476}
477
478static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300479i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200480 u32 bits, u64 *value64,
481 void *handler_context, void *region_context)
482{
483 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300484 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200485 struct acpi_connection_info *info = &data->info;
486 struct acpi_resource_i2c_serialbus *sb;
487 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300488 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200489 struct acpi_resource *ares;
490 u32 accessor_type = function >> 16;
491 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100492 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200493 int status;
494
495 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
496 if (ACPI_FAILURE(ret))
497 return ret;
498
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300499 client = kzalloc(sizeof(*client), GFP_KERNEL);
500 if (!client) {
501 ret = AE_NO_MEMORY;
502 goto err;
503 }
504
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200505 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
506 ret = AE_BAD_PARAMETER;
507 goto err;
508 }
509
510 sb = &ares->data.i2c_serial_bus;
511 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
512 ret = AE_BAD_PARAMETER;
513 goto err;
514 }
515
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300516 client->adapter = adapter;
517 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200518
519 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300520 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200521
522 switch (accessor_type) {
523 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
524 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300525 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200526 if (status >= 0) {
527 gsb->bdata = status;
528 status = 0;
529 }
530 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300531 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200532 }
533 break;
534
535 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
536 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300537 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200538 if (status >= 0) {
539 gsb->bdata = status;
540 status = 0;
541 }
542 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300543 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200544 gsb->bdata);
545 }
546 break;
547
548 case ACPI_GSB_ACCESS_ATTRIB_WORD:
549 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300550 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200551 if (status >= 0) {
552 gsb->wdata = status;
553 status = 0;
554 }
555 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300556 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200557 gsb->wdata);
558 }
559 break;
560
561 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
562 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300563 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200564 gsb->data);
565 if (status >= 0) {
566 gsb->len = status;
567 status = 0;
568 }
569 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300570 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200571 gsb->len, gsb->data);
572 }
573 break;
574
575 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
576 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300577 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200578 gsb->data, info->access_length);
579 if (status > 0)
580 status = 0;
581 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300582 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200583 gsb->data, info->access_length);
584 }
585 break;
586
587 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900588 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
589 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200590 ret = AE_BAD_PARAMETER;
591 goto err;
592 }
593
594 gsb->status = status;
595
596 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300597 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200598 ACPI_FREE(ares);
599 return ret;
600}
601
602
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300603static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200604{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200605 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300606 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200607 acpi_status status;
608
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200609 if (!adapter->dev.parent)
610 return -ENODEV;
611
612 handle = ACPI_HANDLE(adapter->dev.parent);
613
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200614 if (!handle)
615 return -ENODEV;
616
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300617 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200618 GFP_KERNEL);
619 if (!data)
620 return -ENOMEM;
621
622 data->adapter = adapter;
623 status = acpi_bus_attach_private_data(handle, (void *)data);
624 if (ACPI_FAILURE(status)) {
625 kfree(data);
626 return -ENOMEM;
627 }
628
629 status = acpi_install_address_space_handler(handle,
630 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300631 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200632 NULL,
633 data);
634 if (ACPI_FAILURE(status)) {
635 dev_err(&adapter->dev, "Error installing i2c space handler\n");
636 acpi_bus_detach_private_data(handle);
637 kfree(data);
638 return -ENOMEM;
639 }
640
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800641 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200642 return 0;
643}
644
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300645static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200646{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200647 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300648 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200649 acpi_status status;
650
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200651 if (!adapter->dev.parent)
652 return;
653
654 handle = ACPI_HANDLE(adapter->dev.parent);
655
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200656 if (!handle)
657 return;
658
659 acpi_remove_address_space_handler(handle,
660 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300661 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200662
663 status = acpi_bus_get_private_data(handle, (void **)&data);
664 if (ACPI_SUCCESS(status))
665 kfree(data);
666
667 acpi_bus_detach_private_data(handle);
668}
669#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300670static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200671{ }
672
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300673static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200674{ return 0; }
675#endif /* CONFIG_ACPI_I2C_OPREGION */
676
David Brownellf37dd802007-02-13 22:09:00 +0100677/* ------------------------------------------------------------------------- */
678
Jean Delvared2653e92008-04-29 23:11:39 +0200679static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
680 const struct i2c_client *client)
681{
Lee Jones811073b2016-11-07 12:47:36 +0000682 if (!(id && client))
683 return NULL;
684
Jean Delvared2653e92008-04-29 23:11:39 +0200685 while (id->name[0]) {
686 if (strcmp(client->name, id->name) == 0)
687 return id;
688 id++;
689 }
690 return NULL;
691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static int i2c_device_match(struct device *dev, struct device_driver *drv)
694{
Jean Delvare51298d12009-09-18 22:45:45 +0200695 struct i2c_client *client = i2c_verify_client(dev);
696 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200697
Jean Delvare51298d12009-09-18 22:45:45 +0200698
Grant Likely959e85f2010-06-08 07:48:19 -0600699 /* Attempt an OF style match */
700 if (of_driver_match_device(dev, drv))
701 return 1;
702
Mika Westerberg907ddf82012-11-23 12:23:40 +0100703 /* Then ACPI style match */
704 if (acpi_driver_match_device(dev, drv))
705 return 1;
706
Jean Delvare51298d12009-09-18 22:45:45 +0200707 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000708
709 /* Finally an I2C match */
710 if (i2c_match_id(driver->id_table, client))
711 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200712
Jean Delvareeb8a7902008-05-18 20:49:41 +0200713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Kay Sievers7eff2e72007-08-14 15:15:12 +0200716static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200717{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100718 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800719 int rc;
720
721 rc = acpi_device_uevent_modalias(dev, env);
722 if (rc != -ENODEV)
723 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200724
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100725 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200726}
727
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530728/* i2c bus recovery routines */
729static int get_scl_gpio_value(struct i2c_adapter *adap)
730{
731 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
732}
733
734static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
735{
736 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
737}
738
739static int get_sda_gpio_value(struct i2c_adapter *adap)
740{
741 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
742}
743
744static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
745{
746 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
747 struct device *dev = &adap->dev;
748 int ret = 0;
749
750 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
751 GPIOF_OUT_INIT_HIGH, "i2c-scl");
752 if (ret) {
753 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
754 return ret;
755 }
756
757 if (bri->get_sda) {
758 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
759 /* work without SDA polling */
760 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
761 bri->sda_gpio);
762 bri->get_sda = NULL;
763 }
764 }
765
766 return ret;
767}
768
769static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
770{
771 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
772
773 if (bri->get_sda)
774 gpio_free(bri->sda_gpio);
775
776 gpio_free(bri->scl_gpio);
777}
778
779/*
780 * We are generating clock pulses. ndelay() determines durating of clk pulses.
781 * We will generate clock with rate 100 KHz and so duration of both clock levels
782 * is: delay in ns = (10^6 / 100) / 2
783 */
784#define RECOVERY_NDELAY 5000
785#define RECOVERY_CLK_CNT 9
786
787static int i2c_generic_recovery(struct i2c_adapter *adap)
788{
789 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
790 int i = 0, val = 1, ret = 0;
791
792 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300793 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530794
Jan Luebbe8b062602015-07-08 16:35:06 +0200795 bri->set_scl(adap, val);
796 ndelay(RECOVERY_NDELAY);
797
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530798 /*
799 * By this time SCL is high, as we need to give 9 falling-rising edges
800 */
801 while (i++ < RECOVERY_CLK_CNT * 2) {
802 if (val) {
803 /* Break if SDA is high */
804 if (bri->get_sda && bri->get_sda(adap))
805 break;
806 /* SCL shouldn't be low here */
807 if (!bri->get_scl(adap)) {
808 dev_err(&adap->dev,
809 "SCL is stuck low, exit recovery\n");
810 ret = -EBUSY;
811 break;
812 }
813 }
814
815 val = !val;
816 bri->set_scl(adap, val);
817 ndelay(RECOVERY_NDELAY);
818 }
819
820 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300821 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530822
823 return ret;
824}
825
826int i2c_generic_scl_recovery(struct i2c_adapter *adap)
827{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530828 return i2c_generic_recovery(adap);
829}
Mark Brownc1c21f42015-04-15 19:18:39 +0100830EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530831
832int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
833{
834 int ret;
835
836 ret = i2c_get_gpios_for_recovery(adap);
837 if (ret)
838 return ret;
839
840 ret = i2c_generic_recovery(adap);
841 i2c_put_gpios_for_recovery(adap);
842
843 return ret;
844}
Mark Brownc1c21f42015-04-15 19:18:39 +0100845EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530846
847int i2c_recover_bus(struct i2c_adapter *adap)
848{
849 if (!adap->bus_recovery_info)
850 return -EOPNOTSUPP;
851
852 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
853 return adap->bus_recovery_info->recover_bus(adap);
854}
Mark Brownc1c21f42015-04-15 19:18:39 +0100855EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530856
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900857static void i2c_init_recovery(struct i2c_adapter *adap)
858{
859 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
860 char *err_str;
861
862 if (!bri)
863 return;
864
865 if (!bri->recover_bus) {
866 err_str = "no recover_bus() found";
867 goto err;
868 }
869
870 /* Generic GPIO recovery */
871 if (bri->recover_bus == i2c_generic_gpio_recovery) {
872 if (!gpio_is_valid(bri->scl_gpio)) {
873 err_str = "invalid SCL gpio";
874 goto err;
875 }
876
877 if (gpio_is_valid(bri->sda_gpio))
878 bri->get_sda = get_sda_gpio_value;
879 else
880 bri->get_sda = NULL;
881
882 bri->get_scl = get_scl_gpio_value;
883 bri->set_scl = set_scl_gpio_value;
884 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
885 /* Generic SCL recovery */
886 if (!bri->set_scl || !bri->get_scl) {
887 err_str = "no {get|set}_scl() found";
888 goto err;
889 }
890 }
891
892 return;
893 err:
894 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
895 adap->bus_recovery_info = NULL;
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898static int i2c_device_probe(struct device *dev)
899{
Jean Delvare51298d12009-09-18 22:45:45 +0200900 struct i2c_client *client = i2c_verify_client(dev);
901 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100902 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200903
Jean Delvare51298d12009-09-18 22:45:45 +0200904 if (!client)
905 return 0;
906
Mika Westerberg845c8772015-05-06 13:29:08 +0300907 if (!client->irq) {
908 int irq = -ENOENT;
909
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700910 if (dev->of_node) {
911 irq = of_irq_get_byname(dev->of_node, "irq");
912 if (irq == -EINVAL || irq == -ENODATA)
913 irq = of_irq_get(dev->of_node, 0);
914 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300915 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700916 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100917 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200918 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100919 if (irq < 0)
920 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200921
922 client->irq = irq;
923 }
924
Jean Delvare51298d12009-09-18 22:45:45 +0200925 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200926 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200927 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200928
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700929 if (client->flags & I2C_CLIENT_WAKE) {
930 int wakeirq = -ENOENT;
931
932 if (dev->of_node) {
933 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
934 if (wakeirq == -EPROBE_DEFER)
935 return wakeirq;
936 }
937
938 device_init_wakeup(&client->dev, true);
939
940 if (wakeirq > 0 && wakeirq != client->irq)
941 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
942 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200943 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700944 else
945 status = 0;
946
947 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300948 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700949 }
950
David Brownell7b4fbc52007-05-01 23:26:30 +0200951 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200952
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200953 status = of_clk_set_defaults(dev->of_node, false);
954 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700955 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200956
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200957 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100958 if (status == -EPROBE_DEFER)
959 goto err_clear_wakeup_irq;
960
961 status = driver->probe(client, i2c_match_id(driver->id_table, client));
962 if (status)
963 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100964
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700965 return 0;
966
967err_detach_pm_domain:
968 dev_pm_domain_detach(&client->dev, true);
969err_clear_wakeup_irq:
970 dev_pm_clear_wake_irq(&client->dev);
971 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100972 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static int i2c_device_remove(struct device *dev)
976{
Jean Delvare51298d12009-09-18 22:45:45 +0200977 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200978 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100979 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200980
Jean Delvare51298d12009-09-18 22:45:45 +0200981 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200982 return 0;
983
984 driver = to_i2c_driver(dev->driver);
985 if (driver->remove) {
986 dev_dbg(dev, "remove\n");
987 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200988 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100989
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200990 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700991
992 dev_pm_clear_wake_irq(&client->dev);
993 device_init_wakeup(&client->dev, false);
994
David Brownella1d9e6e2007-05-01 23:26:30 +0200995 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
David Brownellf37dd802007-02-13 22:09:00 +0100998static void i2c_device_shutdown(struct device *dev)
999{
Jean Delvare51298d12009-09-18 22:45:45 +02001000 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001001 struct i2c_driver *driver;
1002
Jean Delvare51298d12009-09-18 22:45:45 +02001003 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001004 return;
1005 driver = to_i2c_driver(dev->driver);
1006 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001007 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001008}
1009
David Brownell9c1600e2007-05-01 23:26:31 +02001010static void i2c_client_dev_release(struct device *dev)
1011{
1012 kfree(to_i2c_client(dev));
1013}
1014
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001015static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001016show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001017{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001018 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1019 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001020}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001021static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001022
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001023static ssize_t
1024show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001025{
1026 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001027 int len;
1028
1029 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1030 if (len != -ENODEV)
1031 return len;
1032
Jean Delvareeb8a7902008-05-18 20:49:41 +02001033 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001034}
Jean Delvare51298d12009-09-18 22:45:45 +02001035static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1036
1037static struct attribute *i2c_dev_attrs[] = {
1038 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001039 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001040 &dev_attr_modalias.attr,
1041 NULL
1042};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001043ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001044
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001045struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001046 .name = "i2c",
1047 .match = i2c_device_match,
1048 .probe = i2c_device_probe,
1049 .remove = i2c_device_remove,
1050 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001051};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001052EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001053
Jean Delvare51298d12009-09-18 22:45:45 +02001054static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001055 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001056 .uevent = i2c_device_uevent,
1057 .release = i2c_client_dev_release,
1058};
1059
David Brownell9b766b82008-01-27 18:14:51 +01001060
1061/**
1062 * i2c_verify_client - return parameter as i2c_client, or NULL
1063 * @dev: device, probably from some driver model iterator
1064 *
1065 * When traversing the driver model tree, perhaps using driver model
1066 * iterators like @device_for_each_child(), you can't assume very much
1067 * about the nodes you find. Use this function to avoid oopses caused
1068 * by wrongly treating some non-I2C device as an i2c_client.
1069 */
1070struct i2c_client *i2c_verify_client(struct device *dev)
1071{
Jean Delvare51298d12009-09-18 22:45:45 +02001072 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001073 ? to_i2c_client(dev)
1074 : NULL;
1075}
1076EXPORT_SYMBOL(i2c_verify_client);
1077
1078
Wolfram Sangda899f52015-05-18 21:09:12 +02001079/* Return a unique address which takes the flags of the client into account */
1080static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1081{
1082 unsigned short addr = client->addr;
1083
1084 /* For some client flags, add an arbitrary offset to avoid collisions */
1085 if (client->flags & I2C_CLIENT_TEN)
1086 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1087
1088 if (client->flags & I2C_CLIENT_SLAVE)
1089 addr |= I2C_ADDR_OFFSET_SLAVE;
1090
1091 return addr;
1092}
1093
Jean Delvare3a89db52010-06-03 11:33:52 +02001094/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001095 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001096static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001097{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001098 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001099 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001100 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001101 return -EINVAL;
1102 } else {
1103 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001104 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001105 return -EINVAL;
1106 }
1107 return 0;
1108}
1109
Jean Delvare656b8762010-06-03 11:33:53 +02001110/* And this is a strict address validity check, used when probing. If a
1111 * device uses a reserved address, then it shouldn't be probed. 7-bit
1112 * addressing is assumed, 10-bit address devices are rare and should be
1113 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001114static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001115{
1116 /*
1117 * Reserved addresses per I2C specification:
1118 * 0x00 General call address / START byte
1119 * 0x01 CBUS address
1120 * 0x02 Reserved for different bus format
1121 * 0x03 Reserved for future purposes
1122 * 0x04-0x07 Hs-mode master code
1123 * 0x78-0x7b 10-bit slave addressing
1124 * 0x7c-0x7f Reserved for future purposes
1125 */
1126 if (addr < 0x08 || addr > 0x77)
1127 return -EINVAL;
1128 return 0;
1129}
1130
Jean Delvare3b5f7942010-06-03 11:33:55 +02001131static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1132{
1133 struct i2c_client *client = i2c_verify_client(dev);
1134 int addr = *(int *)addrp;
1135
Wolfram Sang9bccc702015-07-17 14:48:56 +02001136 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001137 return -EBUSY;
1138 return 0;
1139}
1140
Michael Lawnick08263742010-08-11 18:21:02 +02001141/* walk up mux tree */
1142static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1143{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001144 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001145 int result;
1146
1147 result = device_for_each_child(&adapter->dev, &addr,
1148 __i2c_check_addr_busy);
1149
Jean Delvare97cc4d42010-10-24 18:16:57 +02001150 if (!result && parent)
1151 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001152
1153 return result;
1154}
1155
1156/* recurse down mux tree */
1157static int i2c_check_mux_children(struct device *dev, void *addrp)
1158{
1159 int result;
1160
1161 if (dev->type == &i2c_adapter_type)
1162 result = device_for_each_child(dev, addrp,
1163 i2c_check_mux_children);
1164 else
1165 result = __i2c_check_addr_busy(dev, addrp);
1166
1167 return result;
1168}
1169
Jean Delvare3b5f7942010-06-03 11:33:55 +02001170static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1171{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001172 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001173 int result = 0;
1174
Jean Delvare97cc4d42010-10-24 18:16:57 +02001175 if (parent)
1176 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001177
1178 if (!result)
1179 result = device_for_each_child(&adapter->dev, &addr,
1180 i2c_check_mux_children);
1181
1182 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001183}
1184
David Brownell9c1600e2007-05-01 23:26:31 +02001185/**
Peter Rosin8320f492016-05-04 22:15:27 +02001186 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001187 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001188 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1189 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001190 */
Peter Rosin8320f492016-05-04 22:15:27 +02001191static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1192 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001193{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001194 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001195}
Jean Delvarefe61e072010-08-11 18:20:58 +02001196
1197/**
Peter Rosin8320f492016-05-04 22:15:27 +02001198 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001199 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001200 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1201 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001202 */
Peter Rosin8320f492016-05-04 22:15:27 +02001203static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1204 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001205{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001206 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001207}
1208
1209/**
Peter Rosin8320f492016-05-04 22:15:27 +02001210 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001211 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001212 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1213 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001214 */
Peter Rosin8320f492016-05-04 22:15:27 +02001215static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1216 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001217{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001218 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001219}
Jean Delvarefe61e072010-08-11 18:20:58 +02001220
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001221static void i2c_dev_set_name(struct i2c_adapter *adap,
1222 struct i2c_client *client)
1223{
1224 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1225
1226 if (adev) {
1227 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1228 return;
1229 }
1230
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001231 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001232 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001233}
1234
Jean Delvarefe61e072010-08-11 18:20:58 +02001235/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001236 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001237 * @adap: the adapter managing the device
1238 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001239 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001240 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001241 * Create an i2c device. Binding is handled through driver model
1242 * probe()/remove() methods. A driver may be bound to this device when we
1243 * return from this function, or any later moment (e.g. maybe hotplugging will
1244 * load the driver module). This call is not appropriate for use by mainboard
1245 * initialization logic, which usually runs during an arch_initcall() long
1246 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001247 *
1248 * This returns the new i2c client, which may be saved for later use with
1249 * i2c_unregister_device(); or NULL to indicate an error.
1250 */
1251struct i2c_client *
1252i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1253{
1254 struct i2c_client *client;
1255 int status;
1256
1257 client = kzalloc(sizeof *client, GFP_KERNEL);
1258 if (!client)
1259 return NULL;
1260
1261 client->adapter = adap;
1262
1263 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001264
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001265 if (info->archdata)
1266 client->dev.archdata = *info->archdata;
1267
Marc Pignatee354252008-08-28 08:33:22 +02001268 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001269 client->addr = info->addr;
1270 client->irq = info->irq;
1271
David Brownell9c1600e2007-05-01 23:26:31 +02001272 strlcpy(client->name, info->type, sizeof(client->name));
1273
Wolfram Sangc4019b72015-07-17 12:50:06 +02001274 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001275 if (status) {
1276 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1277 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1278 goto out_err_silent;
1279 }
1280
Jean Delvaref8a227e2009-06-19 16:58:18 +02001281 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001282 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001283 if (status)
1284 goto out_err;
1285
1286 client->dev.parent = &client->adapter->dev;
1287 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001288 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001289 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001290 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001291
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001292 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001293 status = device_register(&client->dev);
1294 if (status)
1295 goto out_err;
1296
Jean Delvaref8a227e2009-06-19 16:58:18 +02001297 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1298 client->name, dev_name(&client->dev));
1299
David Brownell9c1600e2007-05-01 23:26:31 +02001300 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001301
1302out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001303 dev_err(&adap->dev,
1304 "Failed to register i2c client %s at 0x%02x (%d)\n",
1305 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001306out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001307 kfree(client);
1308 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001309}
1310EXPORT_SYMBOL_GPL(i2c_new_device);
1311
1312
1313/**
1314 * i2c_unregister_device - reverse effect of i2c_new_device()
1315 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001316 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001317 */
1318void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001319{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001320 if (client->dev.of_node)
1321 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001322 if (ACPI_COMPANION(&client->dev))
1323 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001324 device_unregister(&client->dev);
1325}
David Brownell9c1600e2007-05-01 23:26:31 +02001326EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001327
1328
Jean Delvare60b129d2008-05-11 20:37:06 +02001329static const struct i2c_device_id dummy_id[] = {
1330 { "dummy", 0 },
1331 { },
1332};
1333
Jean Delvared2653e92008-04-29 23:11:39 +02001334static int dummy_probe(struct i2c_client *client,
1335 const struct i2c_device_id *id)
1336{
1337 return 0;
1338}
1339
1340static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001341{
1342 return 0;
1343}
1344
1345static struct i2c_driver dummy_driver = {
1346 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001347 .probe = dummy_probe,
1348 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001349 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001350};
1351
1352/**
1353 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1354 * @adapter: the adapter managing the device
1355 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001356 * Context: can sleep
1357 *
1358 * This returns an I2C client bound to the "dummy" driver, intended for use
1359 * with devices that consume multiple addresses. Examples of such chips
1360 * include various EEPROMS (like 24c04 and 24c08 models).
1361 *
1362 * These dummy devices have two main uses. First, most I2C and SMBus calls
1363 * except i2c_transfer() need a client handle; the dummy will be that handle.
1364 * And second, this prevents the specified address from being bound to a
1365 * different driver.
1366 *
1367 * This returns the new i2c client, which should be saved for later use with
1368 * i2c_unregister_device(); or NULL to indicate an error.
1369 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001370struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001371{
1372 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001373 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001374 };
1375
David Brownelle9f13732008-01-27 18:14:52 +01001376 return i2c_new_device(adapter, &info);
1377}
1378EXPORT_SYMBOL_GPL(i2c_new_dummy);
1379
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001380/**
1381 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1382 * and create the associated device
1383 * @client: Handle to the primary client
1384 * @name: Handle to specify which secondary address to get
1385 * @default_addr: Used as a fallback if no secondary address was specified
1386 * Context: can sleep
1387 *
1388 * I2C clients can be composed of multiple I2C slaves bound together in a single
1389 * component. The I2C client driver then binds to the master I2C slave and needs
1390 * to create I2C dummy clients to communicate with all the other slaves.
1391 *
1392 * This function creates and returns an I2C dummy client whose I2C address is
1393 * retrieved from the platform firmware based on the given slave name. If no
1394 * address is specified by the firmware default_addr is used.
1395 *
1396 * On DT-based platforms the address is retrieved from the "reg" property entry
1397 * cell whose "reg-names" value matches the slave name.
1398 *
1399 * This returns the new i2c client, which should be saved for later use with
1400 * i2c_unregister_device(); or NULL to indicate an error.
1401 */
1402struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1403 const char *name,
1404 u16 default_addr)
1405{
1406 struct device_node *np = client->dev.of_node;
1407 u32 addr = default_addr;
1408 int i;
1409
1410 if (np) {
1411 i = of_property_match_string(np, "reg-names", name);
1412 if (i >= 0)
1413 of_property_read_u32_index(np, "reg", i, &addr);
1414 }
1415
1416 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1417 return i2c_new_dummy(client->adapter, addr);
1418}
1419EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1420
David Brownellf37dd802007-02-13 22:09:00 +01001421/* ------------------------------------------------------------------------- */
1422
David Brownell16ffadf2007-05-01 23:26:28 +02001423/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1424
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001425static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
David Brownellef2c83212007-05-01 23:26:28 +02001427 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 complete(&adap->dev_released);
1429}
1430
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001431unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001432{
1433 unsigned int depth = 0;
1434
1435 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1436 depth++;
1437
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001438 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1439 "adapter depth exceeds lockdep subclass limit\n");
1440
Jean Delvare390946b2012-09-10 10:14:02 +02001441 return depth;
1442}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001443EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001444
1445/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001446 * Let users instantiate I2C devices through sysfs. This can be used when
1447 * platform initialization code doesn't contain the proper data for
1448 * whatever reason. Also useful for drivers that do device detection and
1449 * detection fails, either because the device uses an unexpected address,
1450 * or this is a compatible device with different ID register values.
1451 *
1452 * Parameter checking may look overzealous, but we really don't want
1453 * the user to provide incorrect parameters.
1454 */
1455static ssize_t
1456i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1457 const char *buf, size_t count)
1458{
1459 struct i2c_adapter *adap = to_i2c_adapter(dev);
1460 struct i2c_board_info info;
1461 struct i2c_client *client;
1462 char *blank, end;
1463 int res;
1464
Jean Delvare99cd8e22009-06-19 16:58:20 +02001465 memset(&info, 0, sizeof(struct i2c_board_info));
1466
1467 blank = strchr(buf, ' ');
1468 if (!blank) {
1469 dev_err(dev, "%s: Missing parameters\n", "new_device");
1470 return -EINVAL;
1471 }
1472 if (blank - buf > I2C_NAME_SIZE - 1) {
1473 dev_err(dev, "%s: Invalid device name\n", "new_device");
1474 return -EINVAL;
1475 }
1476 memcpy(info.type, buf, blank - buf);
1477
1478 /* Parse remaining parameters, reject extra parameters */
1479 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1480 if (res < 1) {
1481 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1482 return -EINVAL;
1483 }
1484 if (res > 1 && end != '\n') {
1485 dev_err(dev, "%s: Extra parameters\n", "new_device");
1486 return -EINVAL;
1487 }
1488
Wolfram Sangcfa03272015-07-27 14:03:38 +02001489 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1490 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1491 info.flags |= I2C_CLIENT_TEN;
1492 }
1493
1494 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1495 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1496 info.flags |= I2C_CLIENT_SLAVE;
1497 }
1498
Jean Delvare99cd8e22009-06-19 16:58:20 +02001499 client = i2c_new_device(adap, &info);
1500 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001501 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001502
1503 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001504 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001505 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001506 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001507 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1508 info.type, info.addr);
1509
1510 return count;
1511}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001512static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001513
1514/*
1515 * And of course let the users delete the devices they instantiated, if
1516 * they got it wrong. This interface can only be used to delete devices
1517 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1518 * don't delete devices to which some kernel code still has references.
1519 *
1520 * Parameter checking may look overzealous, but we really don't want
1521 * the user to delete the wrong device.
1522 */
1523static ssize_t
1524i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1525 const char *buf, size_t count)
1526{
1527 struct i2c_adapter *adap = to_i2c_adapter(dev);
1528 struct i2c_client *client, *next;
1529 unsigned short addr;
1530 char end;
1531 int res;
1532
1533 /* Parse parameters, reject extra parameters */
1534 res = sscanf(buf, "%hi%c", &addr, &end);
1535 if (res < 1) {
1536 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1537 return -EINVAL;
1538 }
1539 if (res > 1 && end != '\n') {
1540 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1541 return -EINVAL;
1542 }
1543
1544 /* Make sure the device was added through sysfs */
1545 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001546 mutex_lock_nested(&adap->userspace_clients_lock,
1547 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001548 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1549 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001550 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001551 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1552 "delete_device", client->name, client->addr);
1553
1554 list_del(&client->detected);
1555 i2c_unregister_device(client);
1556 res = count;
1557 break;
1558 }
1559 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001560 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001561
1562 if (res < 0)
1563 dev_err(dev, "%s: Can't find device in list\n",
1564 "delete_device");
1565 return res;
1566}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001567static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1568 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001569
1570static struct attribute *i2c_adapter_attrs[] = {
1571 &dev_attr_name.attr,
1572 &dev_attr_new_device.attr,
1573 &dev_attr_delete_device.attr,
1574 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001575};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001576ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001577
Michael Lawnick08263742010-08-11 18:21:02 +02001578struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001579 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001580 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001581};
Michael Lawnick08263742010-08-11 18:21:02 +02001582EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Stephen Warren643dd092012-04-17 12:43:33 -06001584/**
1585 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1586 * @dev: device, probably from some driver model iterator
1587 *
1588 * When traversing the driver model tree, perhaps using driver model
1589 * iterators like @device_for_each_child(), you can't assume very much
1590 * about the nodes you find. Use this function to avoid oopses caused
1591 * by wrongly treating some non-I2C device as an i2c_adapter.
1592 */
1593struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1594{
1595 return (dev->type == &i2c_adapter_type)
1596 ? to_i2c_adapter(dev)
1597 : NULL;
1598}
1599EXPORT_SYMBOL(i2c_verify_adapter);
1600
Jean Delvare2bb50952009-09-18 22:45:46 +02001601#ifdef CONFIG_I2C_COMPAT
1602static struct class_compat *i2c_adapter_compat_class;
1603#endif
1604
David Brownell9c1600e2007-05-01 23:26:31 +02001605static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1606{
1607 struct i2c_devinfo *devinfo;
1608
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001609 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001610 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1611 if (devinfo->busnum == adapter->nr
1612 && !i2c_new_device(adapter,
1613 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001614 dev_err(&adapter->dev,
1615 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001616 devinfo->board_info.addr);
1617 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001618 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001619}
1620
Wolfram Sang687b81d2013-07-11 12:56:15 +01001621/* OF support code */
1622
1623#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001624static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1625 struct device_node *node)
1626{
1627 struct i2c_client *result;
1628 struct i2c_board_info info = {};
1629 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001630 const __be32 *addr_be;
1631 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001632 int len;
1633
1634 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1635
1636 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1637 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1638 node->full_name);
1639 return ERR_PTR(-EINVAL);
1640 }
1641
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001642 addr_be = of_get_property(node, "reg", &len);
1643 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001644 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1645 node->full_name);
1646 return ERR_PTR(-EINVAL);
1647 }
1648
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001649 addr = be32_to_cpup(addr_be);
1650 if (addr & I2C_TEN_BIT_ADDRESS) {
1651 addr &= ~I2C_TEN_BIT_ADDRESS;
1652 info.flags |= I2C_CLIENT_TEN;
1653 }
1654
1655 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1656 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1657 info.flags |= I2C_CLIENT_SLAVE;
1658 }
1659
1660 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001661 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1662 info.addr, node->full_name);
1663 return ERR_PTR(-EINVAL);
1664 }
1665
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001666 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001667 info.of_node = of_node_get(node);
1668 info.archdata = &dev_ad;
1669
1670 if (of_get_property(node, "wakeup-source", NULL))
1671 info.flags |= I2C_CLIENT_WAKE;
1672
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001673 result = i2c_new_device(adap, &info);
1674 if (result == NULL) {
1675 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1676 node->full_name);
1677 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001678 return ERR_PTR(-EINVAL);
1679 }
1680 return result;
1681}
1682
Wolfram Sang687b81d2013-07-11 12:56:15 +01001683static void of_i2c_register_devices(struct i2c_adapter *adap)
1684{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001685 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001686 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001687
1688 /* Only register child devices if the adapter has a node pointer set */
1689 if (!adap->dev.of_node)
1690 return;
1691
1692 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1693
Jon Hunter7e4c2242016-06-29 10:17:53 +01001694 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1695 if (!bus)
1696 bus = of_node_get(adap->dev.of_node);
1697
1698 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001699 if (of_node_test_and_set_flag(node, OF_POPULATED))
1700 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001701
1702 client = of_i2c_register_device(adap, node);
1703 if (IS_ERR(client)) {
1704 dev_warn(&adap->dev,
1705 "Failed to create I2C device for %s\n",
1706 node->full_name);
1707 of_node_clear_flag(node, OF_POPULATED);
1708 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001709 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001710
1711 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001712}
1713
1714static int of_dev_node_match(struct device *dev, void *data)
1715{
1716 return dev->of_node == data;
1717}
1718
1719/* must call put_device() when done with returned i2c_client device */
1720struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1721{
1722 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001723 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001724
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001725 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001726 if (!dev)
1727 return NULL;
1728
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001729 client = i2c_verify_client(dev);
1730 if (!client)
1731 put_device(dev);
1732
1733 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001734}
1735EXPORT_SYMBOL(of_find_i2c_device_by_node);
1736
1737/* must call put_device() when done with returned i2c_adapter device */
1738struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1739{
1740 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001741 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001742
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001743 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001744 if (!dev)
1745 return NULL;
1746
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001747 adapter = i2c_verify_adapter(dev);
1748 if (!adapter)
1749 put_device(dev);
1750
1751 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001752}
1753EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001754
1755/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1756struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1757{
1758 struct i2c_adapter *adapter;
1759
1760 adapter = of_find_i2c_adapter_by_node(node);
1761 if (!adapter)
1762 return NULL;
1763
1764 if (!try_module_get(adapter->owner)) {
1765 put_device(&adapter->dev);
1766 adapter = NULL;
1767 }
1768
1769 return adapter;
1770}
1771EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Lee Jonescabcf4f2016-11-07 12:47:37 +00001772
1773static const struct of_device_id*
1774i2c_of_match_device_sysfs(const struct of_device_id *matches,
1775 struct i2c_client *client)
1776{
1777 const char *name;
1778
1779 for (; matches->compatible[0]; matches++) {
1780 /*
1781 * Adding devices through the i2c sysfs interface provides us
1782 * a string to match which may be compatible with the device
1783 * tree compatible strings, however with no actual of_node the
1784 * of_match_device() will not match
1785 */
1786 if (sysfs_streq(client->name, matches->compatible))
1787 return matches;
1788
1789 name = strchr(matches->compatible, ',');
1790 if (!name)
1791 name = matches->compatible;
1792 else
1793 name++;
1794
1795 if (sysfs_streq(client->name, name))
1796 return matches;
1797 }
1798
1799 return NULL;
1800}
1801
Wolfram Sang687b81d2013-07-11 12:56:15 +01001802#else
1803static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1804#endif /* CONFIG_OF */
1805
Jean Delvare69b00892009-12-06 17:06:27 +01001806static int i2c_do_add_adapter(struct i2c_driver *driver,
1807 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001808{
Jean Delvare4735c982008-07-14 22:38:36 +02001809 /* Detect supported devices on that bus, and instantiate them */
1810 i2c_detect(adap, driver);
1811
1812 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001813 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001814 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1815 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001816 dev_warn(&adap->dev,
1817 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001818 /* We ignore the return code; if it fails, too bad */
1819 driver->attach_adapter(adap);
1820 }
1821 return 0;
1822}
1823
Jean Delvare69b00892009-12-06 17:06:27 +01001824static int __process_new_adapter(struct device_driver *d, void *data)
1825{
1826 return i2c_do_add_adapter(to_i2c_driver(d), data);
1827}
1828
Peter Rosind1ed7982016-08-25 23:07:01 +02001829static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1830 .lock_bus = i2c_adapter_lock_bus,
1831 .trylock_bus = i2c_adapter_trylock_bus,
1832 .unlock_bus = i2c_adapter_unlock_bus,
1833};
1834
David Brownell6e13e642007-05-01 23:26:31 +02001835static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001837 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David Brownell1d0b19c2008-10-14 17:30:05 +02001839 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301840 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001841 res = -EAGAIN;
1842 goto out_list;
1843 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001844
Jean Delvare2236baa2010-11-15 22:40:38 +01001845 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001846 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001847 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001848
1849 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001850 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001851 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001852 }
1853
Peter Rosind1ed7982016-08-25 23:07:01 +02001854 if (!adap->lock_ops)
1855 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001856
Mika Kuoppala194684e2009-12-06 17:06:22 +01001857 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001858 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001859 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001860 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Jean Delvare8fcfef62009-03-28 21:34:43 +01001862 /* Set default timeout to 1 second if not already set */
1863 if (adap->timeout == 0)
1864 adap->timeout = HZ;
1865
Kay Sievers27d9c182009-01-07 14:29:16 +01001866 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001867 adap->dev.bus = &i2c_bus_type;
1868 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001869 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001870 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001871 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001872 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001875 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1876
Charles Keepax6ada5c12015-04-16 13:05:19 +01001877 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001878 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001879 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001880
Jean Delvare2bb50952009-09-18 22:45:46 +02001881#ifdef CONFIG_I2C_COMPAT
1882 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1883 adap->dev.parent);
1884 if (res)
1885 dev_warn(&adap->dev,
1886 "Failed to create compatibility class link\n");
1887#endif
1888
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001889 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301890
Jean Delvare729d6dd2009-06-19 16:58:18 +02001891 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001892 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001893 i2c_acpi_register_devices(adap);
1894 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001895
David Brownell6e13e642007-05-01 23:26:31 +02001896 if (adap->nr < __i2c_first_dynamic_bus_num)
1897 i2c_scan_static_board_info(adap);
1898
Jean Delvare4735c982008-07-14 22:38:36 +02001899 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001900 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001901 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001902 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001903
1904 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001905
Jean Delvareb119c6c2006-08-15 18:26:30 +02001906out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001907 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001908 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001909 mutex_unlock(&core_lock);
1910 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
David Brownell6e13e642007-05-01 23:26:31 +02001913/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001914 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1915 * @adap: the adapter to register (with adap->nr initialized)
1916 * Context: can sleep
1917 *
1918 * See i2c_add_numbered_adapter() for details.
1919 */
1920static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1921{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001922 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001923
1924 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001925 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001926 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001927 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001928 return id == -ENOSPC ? -EBUSY : id;
1929
1930 return i2c_register_adapter(adap);
1931}
1932
1933/**
David Brownell6e13e642007-05-01 23:26:31 +02001934 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1935 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001936 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001937 *
1938 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001939 * doesn't matter or when its bus number is specified by an dt alias.
1940 * Examples of bases when the bus number doesn't matter: I2C adapters
1941 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001942 *
1943 * When this returns zero, a new bus number was allocated and stored
1944 * in adap->nr, and the specified adapter became available for clients.
1945 * Otherwise, a negative errno value is returned.
1946 */
1947int i2c_add_adapter(struct i2c_adapter *adapter)
1948{
Doug Andersonee5c2742013-03-01 08:57:31 -08001949 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001950 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001951
Doug Andersonee5c2742013-03-01 08:57:31 -08001952 if (dev->of_node) {
1953 id = of_alias_get_id(dev->of_node, "i2c");
1954 if (id >= 0) {
1955 adapter->nr = id;
1956 return __i2c_add_numbered_adapter(adapter);
1957 }
1958 }
1959
Jean Delvarecaada322008-01-27 18:14:49 +01001960 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001961 id = idr_alloc(&i2c_adapter_idr, adapter,
1962 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001963 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001964 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001965 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001966
1967 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001968
David Brownell6e13e642007-05-01 23:26:31 +02001969 return i2c_register_adapter(adapter);
1970}
1971EXPORT_SYMBOL(i2c_add_adapter);
1972
1973/**
1974 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1975 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001976 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001977 *
1978 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001979 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1980 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001981 * is used to properly configure I2C devices.
1982 *
Grant Likely488bf312011-07-25 17:49:43 +02001983 * If the requested bus number is set to -1, then this function will behave
1984 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1985 *
David Brownell6e13e642007-05-01 23:26:31 +02001986 * If no devices have pre-been declared for this bus, then be sure to
1987 * register the adapter before any dynamically allocated ones. Otherwise
1988 * the required bus ID may not be available.
1989 *
1990 * When this returns zero, the specified adapter became available for
1991 * clients using the bus number provided in adap->nr. Also, the table
1992 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1993 * and the appropriate driver model device nodes are created. Otherwise, a
1994 * negative errno value is returned.
1995 */
1996int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1997{
Grant Likely488bf312011-07-25 17:49:43 +02001998 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1999 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002000
Doug Andersonee5c2742013-03-01 08:57:31 -08002001 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002002}
2003EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2004
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002005static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002006 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002007{
Jean Delvare4735c982008-07-14 22:38:36 +02002008 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002009
Jean Delvareacec2112009-03-28 21:34:40 +01002010 /* Remove the devices we created ourselves as the result of hardware
2011 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002012 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2013 if (client->adapter == adapter) {
2014 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2015 client->name, client->addr);
2016 list_del(&client->detected);
2017 i2c_unregister_device(client);
2018 }
2019 }
Jean Delvare026526f2008-01-27 18:14:49 +01002020}
2021
Jean Delvaree549c2b2009-06-19 16:58:19 +02002022static int __unregister_client(struct device *dev, void *dummy)
2023{
2024 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002025 if (client && strcmp(client->name, "dummy"))
2026 i2c_unregister_device(client);
2027 return 0;
2028}
2029
2030static int __unregister_dummy(struct device *dev, void *dummy)
2031{
2032 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002033 if (client)
2034 i2c_unregister_device(client);
2035 return 0;
2036}
2037
Jean Delvare69b00892009-12-06 17:06:27 +01002038static int __process_removed_adapter(struct device_driver *d, void *data)
2039{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002040 i2c_do_del_adapter(to_i2c_driver(d), data);
2041 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002042}
2043
David Brownelld64f73b2007-07-12 14:12:28 +02002044/**
2045 * i2c_del_adapter - unregister I2C adapter
2046 * @adap: the adapter being unregistered
2047 * Context: can sleep
2048 *
2049 * This unregisters an I2C adapter which was previously registered
2050 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2051 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002052void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002054 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002055 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002058 mutex_lock(&core_lock);
2059 found = idr_find(&i2c_adapter_idr, adap->nr);
2060 mutex_unlock(&core_lock);
2061 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002062 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002063 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002066 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002067 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002068 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002069 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002070 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002071 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002073 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002074 mutex_lock_nested(&adap->userspace_clients_lock,
2075 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002076 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2077 detected) {
2078 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2079 client->addr);
2080 list_del(&client->detected);
2081 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002082 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002083 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002084
Jean Delvaree549c2b2009-06-19 16:58:19 +02002085 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002086 * check the returned value. This is a two-pass process, because
2087 * we can't remove the dummy devices during the first pass: they
2088 * could have been instantiated by real devices wishing to clean
2089 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002090 device_for_each_child(&adap->dev, NULL, __unregister_client);
2091 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Jean Delvare2bb50952009-09-18 22:45:46 +02002093#ifdef CONFIG_I2C_COMPAT
2094 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2095 adap->dev.parent);
2096#endif
2097
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002098 /* device name is gone after device_unregister */
2099 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2100
Wolfram Sang9f924162015-12-23 18:19:28 +01002101 pm_runtime_disable(&adap->dev);
2102
Wolfram Sang26680ee2015-01-29 22:45:09 +01002103 /* wait until all references to the device are gone
2104 *
2105 * FIXME: This is old code and should ideally be replaced by an
2106 * alternative which results in decoupling the lifetime of the struct
2107 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302108 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
David Brownell6e13e642007-05-01 23:26:31 +02002114 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002115 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002117 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002119 /* Clear the device structure in case this adapter is ever going to be
2120 added again */
2121 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
David Brownellc0564602007-05-01 23:26:31 +02002123EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Wolfram Sang54177cc2015-12-17 13:32:36 +01002125/**
2126 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2127 * @dev: The device to scan for I2C timing properties
2128 * @t: the i2c_timings struct to be filled with values
2129 * @use_defaults: bool to use sane defaults derived from the I2C specification
2130 * when properties are not found, otherwise use 0
2131 *
2132 * Scan the device for the generic I2C properties describing timing parameters
2133 * for the signal and fill the given struct with the results. If a property was
2134 * not found and use_defaults was true, then maximum timings are assumed which
2135 * are derived from the I2C specification. If use_defaults is not used, the
2136 * results will be 0, so drivers can apply their own defaults later. The latter
2137 * is mainly intended for avoiding regressions of existing drivers which want
2138 * to switch to this function. New drivers almost always should use the defaults.
2139 */
2140
2141void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2142{
2143 int ret;
2144
2145 memset(t, 0, sizeof(*t));
2146
2147 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2148 if (ret && use_defaults)
2149 t->bus_freq_hz = 100000;
2150
2151 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2152 if (ret && use_defaults) {
2153 if (t->bus_freq_hz <= 100000)
2154 t->scl_rise_ns = 1000;
2155 else if (t->bus_freq_hz <= 400000)
2156 t->scl_rise_ns = 300;
2157 else
2158 t->scl_rise_ns = 120;
2159 }
2160
2161 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2162 if (ret && use_defaults) {
2163 if (t->bus_freq_hz <= 400000)
2164 t->scl_fall_ns = 300;
2165 else
2166 t->scl_fall_ns = 120;
2167 }
2168
2169 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2170
2171 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2172 if (ret && use_defaults)
2173 t->sda_fall_ns = t->scl_fall_ns;
2174}
2175EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2176
David Brownell7b4fbc52007-05-01 23:26:30 +02002177/* ------------------------------------------------------------------------- */
2178
Jean Delvare7ae31482011-03-20 14:50:52 +01002179int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2180{
2181 int res;
2182
2183 mutex_lock(&core_lock);
2184 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2185 mutex_unlock(&core_lock);
2186
2187 return res;
2188}
2189EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2190
Jean Delvare69b00892009-12-06 17:06:27 +01002191static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002192{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002193 if (dev->type != &i2c_adapter_type)
2194 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002195 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002196}
2197
David Brownell7b4fbc52007-05-01 23:26:30 +02002198/*
2199 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002200 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 */
2202
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002203int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002205 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
David Brownell1d0b19c2008-10-14 17:30:05 +02002207 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302208 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002209 return -EAGAIN;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002212 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002214 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Jean Delvare729d6dd2009-06-19 16:58:18 +02002216 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002217 * will have called probe() for all matching-but-unbound devices.
2218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 res = driver_register(&driver->driver);
2220 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002221 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002222
Wolfram Sang44239a52016-07-09 13:35:04 +09002223 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Jean Delvare4735c982008-07-14 22:38:36 +02002225 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002226 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002227
Jean Delvare7eebcb72006-02-05 23:28:21 +01002228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002230EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Jean Delvare69b00892009-12-06 17:06:27 +01002232static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002233{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002234 if (dev->type == &i2c_adapter_type)
2235 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2236 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002237}
2238
David Brownella1d9e6e2007-05-01 23:26:30 +02002239/**
2240 * i2c_del_driver - unregister I2C driver
2241 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002242 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002243 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002244void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Jean Delvare7ae31482011-03-20 14:50:52 +01002246 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002249 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
David Brownellc0564602007-05-01 23:26:31 +02002251EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
David Brownell7b4fbc52007-05-01 23:26:30 +02002253/* ------------------------------------------------------------------------- */
2254
Jean Delvaree48d3312008-01-27 18:14:48 +01002255/**
2256 * i2c_use_client - increments the reference count of the i2c client structure
2257 * @client: the client being referenced
2258 *
2259 * Each live reference to a client should be refcounted. The driver model does
2260 * that automatically as part of driver binding, so that most drivers don't
2261 * need to do this explicitly: they hold a reference until they're unbound
2262 * from the device.
2263 *
2264 * A pointer to the client with the incremented reference counter is returned.
2265 */
2266struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
David Brownell6ea438e2008-07-14 22:38:24 +02002268 if (client && get_device(&client->dev))
2269 return client;
2270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
David Brownellc0564602007-05-01 23:26:31 +02002272EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Jean Delvaree48d3312008-01-27 18:14:48 +01002274/**
2275 * i2c_release_client - release a use of the i2c client structure
2276 * @client: the client being no longer referenced
2277 *
2278 * Must be called when a user of a client is finished with it.
2279 */
2280void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
David Brownell6ea438e2008-07-14 22:38:24 +02002282 if (client)
2283 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
David Brownellc0564602007-05-01 23:26:31 +02002285EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
David Brownell9b766b82008-01-27 18:14:51 +01002287struct i2c_cmd_arg {
2288 unsigned cmd;
2289 void *arg;
2290};
2291
2292static int i2c_cmd(struct device *dev, void *_arg)
2293{
2294 struct i2c_client *client = i2c_verify_client(dev);
2295 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002296 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002297
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002298 if (!client || !client->dev.driver)
2299 return 0;
2300
2301 driver = to_i2c_driver(client->dev.driver);
2302 if (driver->command)
2303 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002304 return 0;
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2308{
David Brownell9b766b82008-01-27 18:14:51 +01002309 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
David Brownell9b766b82008-01-27 18:14:51 +01002311 cmd_arg.cmd = cmd;
2312 cmd_arg.arg = arg;
2313 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
David Brownellc0564602007-05-01 23:26:31 +02002315EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002317#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2318static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2319 void *arg)
2320{
2321 struct of_reconfig_data *rd = arg;
2322 struct i2c_adapter *adap;
2323 struct i2c_client *client;
2324
2325 switch (of_reconfig_get_state_change(action, rd)) {
2326 case OF_RECONFIG_CHANGE_ADD:
2327 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2328 if (adap == NULL)
2329 return NOTIFY_OK; /* not for us */
2330
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002331 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2332 put_device(&adap->dev);
2333 return NOTIFY_OK;
2334 }
2335
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002336 client = of_i2c_register_device(adap, rd->dn);
2337 put_device(&adap->dev);
2338
2339 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002340 dev_err(&adap->dev, "failed to create client for '%s'\n",
2341 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002342 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002343 return notifier_from_errno(PTR_ERR(client));
2344 }
2345 break;
2346 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002347 /* already depopulated? */
2348 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2349 return NOTIFY_OK;
2350
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002351 /* find our device by node */
2352 client = of_find_i2c_device_by_node(rd->dn);
2353 if (client == NULL)
2354 return NOTIFY_OK; /* no? not meant for us */
2355
2356 /* unregister takes one ref away */
2357 i2c_unregister_device(client);
2358
2359 /* and put the reference of the find */
2360 put_device(&client->dev);
2361 break;
2362 }
2363
2364 return NOTIFY_OK;
2365}
2366static struct notifier_block i2c_of_notifier = {
2367 .notifier_call = of_i2c_notify,
2368};
2369#else
2370extern struct notifier_block i2c_of_notifier;
2371#endif /* CONFIG_OF_DYNAMIC */
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373static int __init i2c_init(void)
2374{
2375 int retval;
2376
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002377 retval = of_alias_get_highest_id("i2c");
2378
2379 down_write(&__i2c_board_lock);
2380 if (retval >= __i2c_first_dynamic_bus_num)
2381 __i2c_first_dynamic_bus_num = retval + 1;
2382 up_write(&__i2c_board_lock);
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 retval = bus_register(&i2c_bus_type);
2385 if (retval)
2386 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002387
2388 is_registered = true;
2389
Jean Delvare2bb50952009-09-18 22:45:46 +02002390#ifdef CONFIG_I2C_COMPAT
2391 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2392 if (!i2c_adapter_compat_class) {
2393 retval = -ENOMEM;
2394 goto bus_err;
2395 }
2396#endif
David Brownelle9f13732008-01-27 18:14:52 +01002397 retval = i2c_add_driver(&dummy_driver);
2398 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002399 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002400
2401 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2402 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002403 if (IS_ENABLED(CONFIG_ACPI))
2404 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002405
David Brownelle9f13732008-01-27 18:14:52 +01002406 return 0;
2407
Jean Delvare2bb50952009-09-18 22:45:46 +02002408class_err:
2409#ifdef CONFIG_I2C_COMPAT
2410 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002411bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002412#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002413 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002414 bus_unregister(&i2c_bus_type);
2415 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418static void __exit i2c_exit(void)
2419{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002420 if (IS_ENABLED(CONFIG_ACPI))
2421 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002422 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2423 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002424 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002425#ifdef CONFIG_I2C_COMPAT
2426 class_compat_unregister(i2c_adapter_compat_class);
2427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002429 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
David Brownella10f9e72008-10-14 17:30:06 +02002432/* We must initialize early, because some subsystems register i2c drivers
2433 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2434 */
2435postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436module_exit(i2c_exit);
2437
2438/* ----------------------------------------------------
2439 * the functional interface to the i2c busses.
2440 * ----------------------------------------------------
2441 */
2442
Wolfram Sangb7f62582015-01-05 23:45:59 +01002443/* Check if val is exceeding the quirk IFF quirk is non 0 */
2444#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2445
2446static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2447{
2448 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2449 err_msg, msg->addr, msg->len,
2450 msg->flags & I2C_M_RD ? "read" : "write");
2451 return -EOPNOTSUPP;
2452}
2453
2454static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2455{
2456 const struct i2c_adapter_quirks *q = adap->quirks;
2457 int max_num = q->max_num_msgs, i;
2458 bool do_len_check = true;
2459
2460 if (q->flags & I2C_AQ_COMB) {
2461 max_num = 2;
2462
2463 /* special checks for combined messages */
2464 if (num == 2) {
2465 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2466 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2467
2468 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2469 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2470
2471 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2472 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2473
2474 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2475 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2476
2477 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2478 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2479
2480 do_len_check = false;
2481 }
2482 }
2483
2484 if (i2c_quirk_exceeded(num, max_num))
2485 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2486
2487 for (i = 0; i < num; i++) {
2488 u16 len = msgs[i].len;
2489
2490 if (msgs[i].flags & I2C_M_RD) {
2491 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2492 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2493 } else {
2494 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2495 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2496 }
2497 }
2498
2499 return 0;
2500}
2501
David Brownella1cdeda2008-07-14 22:38:24 +02002502/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002503 * __i2c_transfer - unlocked flavor of i2c_transfer
2504 * @adap: Handle to I2C bus
2505 * @msgs: One or more messages to execute before STOP is issued to
2506 * terminate the operation; each message begins with a START.
2507 * @num: Number of messages to be executed.
2508 *
2509 * Returns negative errno, else the number of messages executed.
2510 *
2511 * Adapter lock must be held when calling this function. No debug logging
2512 * takes place. adap->algo->master_xfer existence isn't checked.
2513 */
2514int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2515{
2516 unsigned long orig_jiffies;
2517 int ret, try;
2518
Wolfram Sangb7f62582015-01-05 23:45:59 +01002519 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2520 return -EOPNOTSUPP;
2521
David Howellsd9a83d62014-03-06 13:35:59 +00002522 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2523 * enabled. This is an efficient way of keeping the for-loop from
2524 * being executed when not needed.
2525 */
2526 if (static_key_false(&i2c_trace_msg)) {
2527 int i;
2528 for (i = 0; i < num; i++)
2529 if (msgs[i].flags & I2C_M_RD)
2530 trace_i2c_read(adap, &msgs[i], i);
2531 else
2532 trace_i2c_write(adap, &msgs[i], i);
2533 }
2534
Jean Delvareb37d2a32012-06-29 07:47:19 -03002535 /* Retry automatically on arbitration loss */
2536 orig_jiffies = jiffies;
2537 for (ret = 0, try = 0; try <= adap->retries; try++) {
2538 ret = adap->algo->master_xfer(adap, msgs, num);
2539 if (ret != -EAGAIN)
2540 break;
2541 if (time_after(jiffies, orig_jiffies + adap->timeout))
2542 break;
2543 }
2544
David Howellsd9a83d62014-03-06 13:35:59 +00002545 if (static_key_false(&i2c_trace_msg)) {
2546 int i;
2547 for (i = 0; i < ret; i++)
2548 if (msgs[i].flags & I2C_M_RD)
2549 trace_i2c_reply(adap, &msgs[i], i);
2550 trace_i2c_result(adap, i, ret);
2551 }
2552
Jean Delvareb37d2a32012-06-29 07:47:19 -03002553 return ret;
2554}
2555EXPORT_SYMBOL(__i2c_transfer);
2556
2557/**
David Brownella1cdeda2008-07-14 22:38:24 +02002558 * i2c_transfer - execute a single or combined I2C message
2559 * @adap: Handle to I2C bus
2560 * @msgs: One or more messages to execute before STOP is issued to
2561 * terminate the operation; each message begins with a START.
2562 * @num: Number of messages to be executed.
2563 *
2564 * Returns negative errno, else the number of messages executed.
2565 *
2566 * Note that there is no requirement that each message be sent to
2567 * the same slave address, although that is the most common model.
2568 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002569int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002571 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
David Brownella1cdeda2008-07-14 22:38:24 +02002573 /* REVISIT the fault reporting model here is weak:
2574 *
2575 * - When we get an error after receiving N bytes from a slave,
2576 * there is no way to report "N".
2577 *
2578 * - When we get a NAK after transmitting N bytes to a slave,
2579 * there is no way to report "N" ... or to let the master
2580 * continue executing the rest of this combined message, if
2581 * that's the appropriate response.
2582 *
2583 * - When for example "num" is two and we successfully complete
2584 * the first message but get an error part way through the
2585 * second, it's unclear whether that should be reported as
2586 * one (discarding status on the second message) or errno
2587 * (discarding status on the first one).
2588 */
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (adap->algo->master_xfer) {
2591#ifdef DEBUG
2592 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002593 dev_dbg(&adap->dev,
2594 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2595 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2596 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002597 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599#endif
2600
Mike Rapoportcea443a82008-01-27 18:14:50 +01002601 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002602 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002603 if (!ret)
2604 /* I2C activity is ongoing. */
2605 return -EAGAIN;
2606 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002607 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002608 }
2609
Jean Delvareb37d2a32012-06-29 07:47:19 -03002610 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002611 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 return ret;
2614 } else {
2615 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002616 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618}
David Brownellc0564602007-05-01 23:26:31 +02002619EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
David Brownella1cdeda2008-07-14 22:38:24 +02002621/**
2622 * i2c_master_send - issue a single I2C message in master transmit mode
2623 * @client: Handle to slave device
2624 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002625 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002626 *
2627 * Returns negative errno, or else the number of bytes written.
2628 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002629int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002632 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct i2c_msg msg;
2634
Jean Delvare815f55f2005-05-07 22:58:46 +02002635 msg.addr = client->addr;
2636 msg.flags = client->flags & I2C_M_TEN;
2637 msg.len = count;
2638 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002639
Jean Delvare815f55f2005-05-07 22:58:46 +02002640 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002642 /*
2643 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2644 * transmitted, else error code.
2645 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002646 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
David Brownellc0564602007-05-01 23:26:31 +02002648EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
David Brownella1cdeda2008-07-14 22:38:24 +02002650/**
2651 * i2c_master_recv - issue a single I2C message in master receive mode
2652 * @client: Handle to slave device
2653 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002654 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002655 *
2656 * Returns negative errno, or else the number of bytes read.
2657 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002658int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Farid Hammane7225acf2010-05-21 18:40:58 +02002660 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct i2c_msg msg;
2662 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Jean Delvare815f55f2005-05-07 22:58:46 +02002664 msg.addr = client->addr;
2665 msg.flags = client->flags & I2C_M_TEN;
2666 msg.flags |= I2C_M_RD;
2667 msg.len = count;
2668 msg.buf = buf;
2669
2670 ret = i2c_transfer(adap, &msg, 1);
2671
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002672 /*
2673 * If everything went ok (i.e. 1 msg received), return #bytes received,
2674 * else error code.
2675 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002676 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
David Brownellc0564602007-05-01 23:26:31 +02002678EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/* ----------------------------------------------------
2681 * the i2c address scanning function
2682 * Will not work for 10-bit addresses!
2683 * ----------------------------------------------------
2684 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002685
Jean Delvare63e4e802010-06-03 11:33:51 +02002686/*
2687 * Legacy default probe function, mostly relevant for SMBus. The default
2688 * probe method is a quick write, but it is known to corrupt the 24RF08
2689 * EEPROMs due to a state machine bug, and could also irreversibly
2690 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2691 * we use a short byte read instead. Also, some bus drivers don't implement
2692 * quick write, so we fallback to a byte read in that case too.
2693 * On x86, there is another special case for FSC hardware monitoring chips,
2694 * which want regular byte reads (address 0x73.) Fortunately, these are the
2695 * only known chips using this I2C address on PC hardware.
2696 * Returns 1 if probe succeeded, 0 if not.
2697 */
2698static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2699{
2700 int err;
2701 union i2c_smbus_data dummy;
2702
2703#ifdef CONFIG_X86
2704 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2705 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2706 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2707 I2C_SMBUS_BYTE_DATA, &dummy);
2708 else
2709#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002710 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2711 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002712 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2713 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002714 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2715 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2716 I2C_SMBUS_BYTE, &dummy);
2717 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002718 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2719 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002720 err = -EOPNOTSUPP;
2721 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002722
2723 return err >= 0;
2724}
2725
Jean Delvareccfbbd02009-12-06 17:06:25 +01002726static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002727 struct i2c_driver *driver)
2728{
2729 struct i2c_board_info info;
2730 struct i2c_adapter *adapter = temp_client->adapter;
2731 int addr = temp_client->addr;
2732 int err;
2733
2734 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002735 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002736 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002737 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2738 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002739 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002740 }
2741
Wolfram Sang9bccc702015-07-17 14:48:56 +02002742 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002743 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002744 return 0;
2745
Jean Delvareccfbbd02009-12-06 17:06:25 +01002746 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002747 if (!i2c_default_probe(adapter, addr))
2748 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002749
2750 /* Finally call the custom detection function */
2751 memset(&info, 0, sizeof(struct i2c_board_info));
2752 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002753 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002754 if (err) {
2755 /* -ENODEV is returned if the detection fails. We catch it
2756 here as this isn't an error. */
2757 return err == -ENODEV ? 0 : err;
2758 }
2759
2760 /* Consistency check */
2761 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002762 dev_err(&adapter->dev,
2763 "%s detection function provided no name for 0x%x\n",
2764 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002765 } else {
2766 struct i2c_client *client;
2767
2768 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002769 if (adapter->class & I2C_CLASS_DEPRECATED)
2770 dev_warn(&adapter->dev,
2771 "This adapter will soon drop class based instantiation of devices. "
2772 "Please make sure client 0x%02x gets instantiated by other means. "
2773 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2774 info.addr);
2775
Jean Delvare4735c982008-07-14 22:38:36 +02002776 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2777 info.type, info.addr);
2778 client = i2c_new_device(adapter, &info);
2779 if (client)
2780 list_add_tail(&client->detected, &driver->clients);
2781 else
2782 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2783 info.type, info.addr);
2784 }
2785 return 0;
2786}
2787
2788static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2789{
Jean Delvarec3813d62009-12-14 21:17:25 +01002790 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002791 struct i2c_client *temp_client;
2792 int i, err = 0;
2793 int adap_id = i2c_adapter_id(adapter);
2794
Jean Delvarec3813d62009-12-14 21:17:25 +01002795 address_list = driver->address_list;
2796 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002797 return 0;
2798
Wolfram Sang45552272014-07-10 13:46:21 +02002799 /* Warn that the adapter lost class based instantiation */
2800 if (adapter->class == I2C_CLASS_DEPRECATED) {
2801 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002802 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2803 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002804 driver->driver.name);
2805 return 0;
2806 }
2807
Jean Delvare51b54ba2010-10-24 18:16:58 +02002808 /* Stop here if the classes do not match */
2809 if (!(adapter->class & driver->class))
2810 return 0;
2811
Jean Delvare4735c982008-07-14 22:38:36 +02002812 /* Set up a temporary client to help detect callback */
2813 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2814 if (!temp_client)
2815 return -ENOMEM;
2816 temp_client->adapter = adapter;
2817
Jean Delvarec3813d62009-12-14 21:17:25 +01002818 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002819 dev_dbg(&adapter->dev,
2820 "found normal entry for adapter %d, addr 0x%02x\n",
2821 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002822 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002823 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002824 if (unlikely(err))
2825 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002826 }
2827
Jean Delvare4735c982008-07-14 22:38:36 +02002828 kfree(temp_client);
2829 return err;
2830}
2831
Jean Delvared44f19d2010-08-11 18:20:57 +02002832int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2833{
2834 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2835 I2C_SMBUS_QUICK, NULL) >= 0;
2836}
2837EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2838
Jean Delvare12b5053a2007-05-01 23:26:31 +02002839struct i2c_client *
2840i2c_new_probed_device(struct i2c_adapter *adap,
2841 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002842 unsigned short const *addr_list,
2843 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002844{
2845 int i;
2846
Jean Delvare8031d792010-08-11 18:21:00 +02002847 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002848 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002849
Jean Delvare12b5053a2007-05-01 23:26:31 +02002850 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2851 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002852 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002853 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2854 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002855 continue;
2856 }
2857
Wolfram Sang9bccc702015-07-17 14:48:56 +02002858 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002859 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002860 dev_dbg(&adap->dev,
2861 "Address 0x%02x already in use, not probing\n",
2862 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002863 continue;
2864 }
2865
Jean Delvare63e4e802010-06-03 11:33:51 +02002866 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002867 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002868 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002869 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002870
2871 if (addr_list[i] == I2C_CLIENT_END) {
2872 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2873 return NULL;
2874 }
2875
2876 info->addr = addr_list[i];
2877 return i2c_new_device(adap, info);
2878}
2879EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2880
Jean Delvared735b342011-03-20 14:50:52 +01002881struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002884
Jean Delvarecaada322008-01-27 18:14:49 +01002885 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002886 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002887 if (!adapter)
2888 goto exit;
2889
2890 if (try_module_get(adapter->owner))
2891 get_device(&adapter->dev);
2892 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002893 adapter = NULL;
2894
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002895 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002896 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002897 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
David Brownellc0564602007-05-01 23:26:31 +02002899EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901void i2c_put_adapter(struct i2c_adapter *adap)
2902{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002903 if (!adap)
2904 return;
2905
2906 put_device(&adap->dev);
2907 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908}
David Brownellc0564602007-05-01 23:26:31 +02002909EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911/* The SMBus parts */
2912
David Brownell438d6c22006-12-10 21:21:31 +01002913#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002914static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002917
Farid Hammane7225acf2010-05-21 18:40:58 +02002918 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002919 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 data = data ^ POLY;
2921 data = data << 1;
2922 }
2923 return (u8)(data >> 8);
2924}
2925
Jean Delvare421ef472005-10-26 21:28:55 +02002926/* Incremental CRC8 over count bytes in the array pointed to by p */
2927static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 int i;
2930
Farid Hammane7225acf2010-05-21 18:40:58 +02002931 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002932 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return crc;
2934}
2935
Jean Delvare421ef472005-10-26 21:28:55 +02002936/* Assume a 7-bit address, which is reasonable for SMBus */
2937static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Jean Delvare421ef472005-10-26 21:28:55 +02002939 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002940 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002941 pec = i2c_smbus_pec(pec, &addr, 1);
2942
2943 /* The data buffer follows */
2944 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Jean Delvare421ef472005-10-26 21:28:55 +02002947/* Used for write only transactions */
2948static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Jean Delvare421ef472005-10-26 21:28:55 +02002950 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2951 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
Jean Delvare421ef472005-10-26 21:28:55 +02002954/* Return <0 on CRC error
2955 If there was a write before this read (most cases) we need to take the
2956 partial CRC from the write part into account.
2957 Note that this function does modify the message (we need to decrease the
2958 message length to hide the CRC byte from the caller). */
2959static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
Jean Delvare421ef472005-10-26 21:28:55 +02002961 u8 rpec = msg->buf[--msg->len];
2962 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002965 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002967 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
David Brownell438d6c22006-12-10 21:21:31 +01002969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
David Brownella1cdeda2008-07-14 22:38:24 +02002972/**
2973 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2974 * @client: Handle to slave device
2975 *
2976 * This executes the SMBus "receive byte" protocol, returning negative errno
2977 * else the byte received from the device.
2978 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002979s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002982 int status;
2983
2984 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2985 I2C_SMBUS_READ, 0,
2986 I2C_SMBUS_BYTE, &data);
2987 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
David Brownellc0564602007-05-01 23:26:31 +02002989EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
David Brownella1cdeda2008-07-14 22:38:24 +02002991/**
2992 * i2c_smbus_write_byte - SMBus "send byte" protocol
2993 * @client: Handle to slave device
2994 * @value: Byte to be sent
2995 *
2996 * This executes the SMBus "send byte" protocol, returning negative errno
2997 * else zero on success.
2998 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002999s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Farid Hammane7225acf2010-05-21 18:40:58 +02003001 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02003002 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
David Brownellc0564602007-05-01 23:26:31 +02003004EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
David Brownella1cdeda2008-07-14 22:38:24 +02003006/**
3007 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
3008 * @client: Handle to slave device
3009 * @command: Byte interpreted by slave
3010 *
3011 * This executes the SMBus "read byte" protocol, returning negative errno
3012 * else a data byte received from the device.
3013 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003014s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003017 int status;
3018
3019 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3020 I2C_SMBUS_READ, command,
3021 I2C_SMBUS_BYTE_DATA, &data);
3022 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
David Brownellc0564602007-05-01 23:26:31 +02003024EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
David Brownella1cdeda2008-07-14 22:38:24 +02003026/**
3027 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
3028 * @client: Handle to slave device
3029 * @command: Byte interpreted by slave
3030 * @value: Byte being written
3031 *
3032 * This executes the SMBus "write byte" protocol, returning negative errno
3033 * else zero on success.
3034 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003035s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3036 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 union i2c_smbus_data data;
3039 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003040 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3041 I2C_SMBUS_WRITE, command,
3042 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
David Brownellc0564602007-05-01 23:26:31 +02003044EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
David Brownella1cdeda2008-07-14 22:38:24 +02003046/**
3047 * i2c_smbus_read_word_data - SMBus "read word" protocol
3048 * @client: Handle to slave device
3049 * @command: Byte interpreted by slave
3050 *
3051 * This executes the SMBus "read word" protocol, returning negative errno
3052 * else a 16-bit unsigned "word" received from the device.
3053 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003054s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
3056 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003057 int status;
3058
3059 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3060 I2C_SMBUS_READ, command,
3061 I2C_SMBUS_WORD_DATA, &data);
3062 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
David Brownellc0564602007-05-01 23:26:31 +02003064EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
David Brownella1cdeda2008-07-14 22:38:24 +02003066/**
3067 * i2c_smbus_write_word_data - SMBus "write word" protocol
3068 * @client: Handle to slave device
3069 * @command: Byte interpreted by slave
3070 * @value: 16-bit "word" being written
3071 *
3072 * This executes the SMBus "write word" protocol, returning negative errno
3073 * else zero on success.
3074 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003075s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3076 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
3078 union i2c_smbus_data data;
3079 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003080 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3081 I2C_SMBUS_WRITE, command,
3082 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
David Brownellc0564602007-05-01 23:26:31 +02003084EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
David Brownella64ec072007-10-13 23:56:31 +02003086/**
David Brownella1cdeda2008-07-14 22:38:24 +02003087 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003088 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003089 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003090 * @values: Byte array into which data will be read; big enough to hold
3091 * the data returned by the slave. SMBus allows at most 32 bytes.
3092 *
David Brownella1cdeda2008-07-14 22:38:24 +02003093 * This executes the SMBus "block read" protocol, returning negative errno
3094 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003095 *
3096 * Note that using this function requires that the client's adapter support
3097 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3098 * support this; its emulation through I2C messaging relies on a specific
3099 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3100 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003101s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003102 u8 *values)
3103{
3104 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003105 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003106
David Brownell24a5bb72008-07-14 22:38:23 +02003107 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3108 I2C_SMBUS_READ, command,
3109 I2C_SMBUS_BLOCK_DATA, &data);
3110 if (status)
3111 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003112
3113 memcpy(values, &data.block[1], data.block[0]);
3114 return data.block[0];
3115}
3116EXPORT_SYMBOL(i2c_smbus_read_block_data);
3117
David Brownella1cdeda2008-07-14 22:38:24 +02003118/**
3119 * i2c_smbus_write_block_data - SMBus "block write" protocol
3120 * @client: Handle to slave device
3121 * @command: Byte interpreted by slave
3122 * @length: Size of data block; SMBus allows at most 32 bytes
3123 * @values: Byte array which will be written.
3124 *
3125 * This executes the SMBus "block write" protocol, returning negative errno
3126 * else zero on success.
3127 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003128s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003129 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
3131 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (length > I2C_SMBUS_BLOCK_MAX)
3134 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003136 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003137 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3138 I2C_SMBUS_WRITE, command,
3139 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
David Brownellc0564602007-05-01 23:26:31 +02003141EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003144s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003145 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003148 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003149
Jean Delvare4b2643d2007-07-12 14:12:29 +02003150 if (length > I2C_SMBUS_BLOCK_MAX)
3151 length = I2C_SMBUS_BLOCK_MAX;
3152 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003153 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3154 I2C_SMBUS_READ, command,
3155 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3156 if (status < 0)
3157 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003158
3159 memcpy(values, &data.block[1], data.block[0]);
3160 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
David Brownellc0564602007-05-01 23:26:31 +02003162EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Jean Delvare0cc43a12011-01-10 22:11:23 +01003164s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003165 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003166{
3167 union i2c_smbus_data data;
3168
3169 if (length > I2C_SMBUS_BLOCK_MAX)
3170 length = I2C_SMBUS_BLOCK_MAX;
3171 data.block[0] = length;
3172 memcpy(data.block + 1, values, length);
3173 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3174 I2C_SMBUS_WRITE, command,
3175 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3176}
David Brownellc0564602007-05-01 23:26:31 +02003177EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003178
David Brownell438d6c22006-12-10 21:21:31 +01003179/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003181static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3182 unsigned short flags,
3183 char read_write, u8 command, int size,
3184 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 /* So we need to generate a series of msgs. In the case of writing, we
3187 need to use only one message; when reading, we need two. We initialize
3188 most things with sane defaults, to keep the code below somewhat
3189 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003190 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3191 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003192 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003194 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003195 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003196 struct i2c_msg msg[2] = {
3197 {
3198 .addr = addr,
3199 .flags = flags,
3200 .len = 1,
3201 .buf = msgbuf0,
3202 }, {
3203 .addr = addr,
3204 .flags = flags | I2C_M_RD,
3205 .len = 0,
3206 .buf = msgbuf1,
3207 },
3208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003211 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 case I2C_SMBUS_QUICK:
3213 msg[0].len = 0;
3214 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003215 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3216 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 num = 1;
3218 break;
3219 case I2C_SMBUS_BYTE:
3220 if (read_write == I2C_SMBUS_READ) {
3221 /* Special case: only a read! */
3222 msg[0].flags = I2C_M_RD | flags;
3223 num = 1;
3224 }
3225 break;
3226 case I2C_SMBUS_BYTE_DATA:
3227 if (read_write == I2C_SMBUS_READ)
3228 msg[1].len = 1;
3229 else {
3230 msg[0].len = 2;
3231 msgbuf0[1] = data->byte;
3232 }
3233 break;
3234 case I2C_SMBUS_WORD_DATA:
3235 if (read_write == I2C_SMBUS_READ)
3236 msg[1].len = 2;
3237 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003238 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003240 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242 break;
3243 case I2C_SMBUS_PROC_CALL:
3244 num = 2; /* Special case */
3245 read_write = I2C_SMBUS_READ;
3246 msg[0].len = 3;
3247 msg[1].len = 2;
3248 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003249 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 break;
3251 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003253 msg[1].flags |= I2C_M_RECV_LEN;
3254 msg[1].len = 1; /* block length will be added by
3255 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 } else {
3257 msg[0].len = data->block[0] + 2;
3258 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003259 dev_err(&adapter->dev,
3260 "Invalid block write size %d\n",
3261 data->block[0]);
3262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003264 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 msgbuf0[i] = data->block[i-1];
3266 }
3267 break;
3268 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003269 num = 2; /* Another special case */
3270 read_write = I2C_SMBUS_READ;
3271 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003272 dev_err(&adapter->dev,
3273 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003274 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003275 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003276 }
3277 msg[0].len = data->block[0] + 2;
3278 for (i = 1; i < msg[0].len; i++)
3279 msgbuf0[i] = data->block[i-1];
3280 msg[1].flags |= I2C_M_RECV_LEN;
3281 msg[1].len = 1; /* block length will be added by
3282 the underlying bus driver */
3283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 case I2C_SMBUS_I2C_BLOCK_DATA:
3285 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003286 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 } else {
3288 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003289 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003290 dev_err(&adapter->dev,
3291 "Invalid block write size %d\n",
3292 data->block[0]);
3293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295 for (i = 1; i <= data->block[0]; i++)
3296 msgbuf0[i] = data->block[i];
3297 }
3298 break;
3299 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003300 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3301 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303
Jean Delvare421ef472005-10-26 21:28:55 +02003304 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3305 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3306 if (i) {
3307 /* Compute PEC if first message is a write */
3308 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003309 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003310 i2c_smbus_add_pec(&msg[0]);
3311 else /* Write followed by read */
3312 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3313 }
3314 /* Ask for PEC if last message is a read */
3315 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003316 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003317 }
3318
David Brownell24a5bb72008-07-14 22:38:23 +02003319 status = i2c_transfer(adapter, msg, num);
3320 if (status < 0)
3321 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Jean Delvare421ef472005-10-26 21:28:55 +02003323 /* Check PEC if last message is a read */
3324 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003325 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3326 if (status < 0)
3327 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003328 }
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003331 switch (size) {
3332 case I2C_SMBUS_BYTE:
3333 data->byte = msgbuf0[0];
3334 break;
3335 case I2C_SMBUS_BYTE_DATA:
3336 data->byte = msgbuf1[0];
3337 break;
3338 case I2C_SMBUS_WORD_DATA:
3339 case I2C_SMBUS_PROC_CALL:
3340 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3341 break;
3342 case I2C_SMBUS_I2C_BLOCK_DATA:
3343 for (i = 0; i < data->block[0]; i++)
3344 data->block[i+1] = msgbuf1[i];
3345 break;
3346 case I2C_SMBUS_BLOCK_DATA:
3347 case I2C_SMBUS_BLOCK_PROC_CALL:
3348 for (i = 0; i < msgbuf1[0] + 1; i++)
3349 data->block[i] = msgbuf1[i];
3350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352 return 0;
3353}
3354
David Brownella1cdeda2008-07-14 22:38:24 +02003355/**
3356 * i2c_smbus_xfer - execute SMBus protocol operations
3357 * @adapter: Handle to I2C bus
3358 * @addr: Address of SMBus slave on that bus
3359 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3360 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3361 * @command: Byte interpreted by slave, for protocols which use such bytes
3362 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3363 * @data: Data to be read or written
3364 *
3365 * This executes an SMBus protocol operation, and returns a negative
3366 * errno code else zero on success.
3367 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003368s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003369 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003370 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003372 unsigned long orig_jiffies;
3373 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
David Howells8a3259972014-03-06 13:36:06 +00003376 /* If enabled, the following two tracepoints are conditional on
3377 * read_write and protocol.
3378 */
3379 trace_smbus_write(adapter, addr, flags, read_write,
3380 command, protocol, data);
3381 trace_smbus_read(adapter, addr, flags, read_write,
3382 command, protocol);
3383
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003384 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
3386 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003387 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003388
3389 /* Retry automatically on arbitration loss */
3390 orig_jiffies = jiffies;
3391 for (res = 0, try = 0; try <= adapter->retries; try++) {
3392 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3393 read_write, command,
3394 protocol, data);
3395 if (res != -EAGAIN)
3396 break;
3397 if (time_after(jiffies,
3398 orig_jiffies + adapter->timeout))
3399 break;
3400 }
Peter Rosin8320f492016-05-04 22:15:27 +02003401 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003403 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a3259972014-03-06 13:36:06 +00003404 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003405 /*
3406 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3407 * implement native support for the SMBus operation.
3408 */
3409 }
3410
David Howells8a3259972014-03-06 13:36:06 +00003411 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3412 command, protocol, data);
3413
3414trace:
3415 /* If enabled, the reply tracepoint is conditional on read_write. */
3416 trace_smbus_reply(adapter, addr, flags, read_write,
3417 command, protocol, data);
3418 trace_smbus_result(adapter, addr, flags, read_write,
3419 command, protocol, res);
3420
3421 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Irina Tirdea01eef962015-08-12 17:31:33 +03003425/**
3426 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3427 * @client: Handle to slave device
3428 * @command: Byte interpreted by slave
3429 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3430 * @values: Byte array into which data will be read; big enough to hold
3431 * the data returned by the slave. SMBus allows at most
3432 * I2C_SMBUS_BLOCK_MAX bytes.
3433 *
3434 * This executes the SMBus "block read" protocol if supported by the adapter.
3435 * If block read is not supported, it emulates it using either word or byte
3436 * read protocols depending on availability.
3437 *
3438 * The addresses of the I2C slave device that are accessed with this function
3439 * must be mapped to a linear region, so that a block read will have the same
3440 * effect as a byte read. Before using this function you must double-check
3441 * if the I2C slave does support exchanging a block transfer with a byte
3442 * transfer.
3443 */
3444s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3445 u8 command, u8 length, u8 *values)
3446{
3447 u8 i = 0;
3448 int status;
3449
3450 if (length > I2C_SMBUS_BLOCK_MAX)
3451 length = I2C_SMBUS_BLOCK_MAX;
3452
3453 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3454 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3455
3456 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3457 return -EOPNOTSUPP;
3458
3459 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3460 while ((i + 2) <= length) {
3461 status = i2c_smbus_read_word_data(client, command + i);
3462 if (status < 0)
3463 return status;
3464 values[i] = status & 0xff;
3465 values[i + 1] = status >> 8;
3466 i += 2;
3467 }
3468 }
3469
3470 while (i < length) {
3471 status = i2c_smbus_read_byte_data(client, command + i);
3472 if (status < 0)
3473 return status;
3474 values[i] = status;
3475 i++;
3476 }
3477
3478 return i;
3479}
3480EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3481
Jean Delvared5fd1202015-01-26 20:59:31 +01003482#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003483int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3484{
3485 int ret;
3486
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003487 if (!client || !slave_cb) {
3488 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003489 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003490 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003491
Wolfram Sangc6909d62015-08-05 21:12:54 +02003492 if (!(client->flags & I2C_CLIENT_SLAVE))
3493 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3494 __func__);
3495
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003496 if (!(client->flags & I2C_CLIENT_TEN)) {
3497 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003498 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003499 if (ret) {
3500 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003501 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003502 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003503 }
3504
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003505 if (!client->adapter->algo->reg_slave) {
3506 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003507 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003508 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003509
3510 client->slave_cb = slave_cb;
3511
3512 i2c_lock_adapter(client->adapter);
3513 ret = client->adapter->algo->reg_slave(client);
3514 i2c_unlock_adapter(client->adapter);
3515
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003516 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003517 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003518 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3519 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003520
3521 return ret;
3522}
3523EXPORT_SYMBOL_GPL(i2c_slave_register);
3524
3525int i2c_slave_unregister(struct i2c_client *client)
3526{
3527 int ret;
3528
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003529 if (!client->adapter->algo->unreg_slave) {
3530 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003531 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003532 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003533
3534 i2c_lock_adapter(client->adapter);
3535 ret = client->adapter->algo->unreg_slave(client);
3536 i2c_unlock_adapter(client->adapter);
3537
3538 if (ret == 0)
3539 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003540 else
3541 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003542
3543 return ret;
3544}
3545EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003546#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3549MODULE_DESCRIPTION("I2C-Bus main module");
3550MODULE_LICENSE("GPL");