blob: 80d82c6792d8d2bf9658ead8b53dbffd4df075bb [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{
682 while (id->name[0]) {
683 if (strcmp(client->name, id->name) == 0)
684 return id;
685 id++;
686 }
687 return NULL;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int i2c_device_match(struct device *dev, struct device_driver *drv)
691{
Jean Delvare51298d12009-09-18 22:45:45 +0200692 struct i2c_client *client = i2c_verify_client(dev);
693 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200694
Jean Delvare51298d12009-09-18 22:45:45 +0200695 if (!client)
696 return 0;
697
Grant Likely959e85f2010-06-08 07:48:19 -0600698 /* Attempt an OF style match */
699 if (of_driver_match_device(dev, drv))
700 return 1;
701
Mika Westerberg907ddf82012-11-23 12:23:40 +0100702 /* Then ACPI style match */
703 if (acpi_driver_match_device(dev, drv))
704 return 1;
705
Jean Delvare51298d12009-09-18 22:45:45 +0200706 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200707 /* match on an id table if there is one */
708 if (driver->id_table)
709 return i2c_match_id(driver->id_table, client) != NULL;
710
Jean Delvareeb8a7902008-05-18 20:49:41 +0200711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Kay Sievers7eff2e72007-08-14 15:15:12 +0200714static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200715{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100716 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800717 int rc;
718
719 rc = acpi_device_uevent_modalias(dev, env);
720 if (rc != -ENODEV)
721 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200722
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100723 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200724}
725
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530726/* i2c bus recovery routines */
727static int get_scl_gpio_value(struct i2c_adapter *adap)
728{
729 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
730}
731
732static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
733{
734 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
735}
736
737static int get_sda_gpio_value(struct i2c_adapter *adap)
738{
739 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
740}
741
742static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
743{
744 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
745 struct device *dev = &adap->dev;
746 int ret = 0;
747
748 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
749 GPIOF_OUT_INIT_HIGH, "i2c-scl");
750 if (ret) {
751 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
752 return ret;
753 }
754
755 if (bri->get_sda) {
756 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
757 /* work without SDA polling */
758 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
759 bri->sda_gpio);
760 bri->get_sda = NULL;
761 }
762 }
763
764 return ret;
765}
766
767static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
768{
769 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
770
771 if (bri->get_sda)
772 gpio_free(bri->sda_gpio);
773
774 gpio_free(bri->scl_gpio);
775}
776
777/*
778 * We are generating clock pulses. ndelay() determines durating of clk pulses.
779 * We will generate clock with rate 100 KHz and so duration of both clock levels
780 * is: delay in ns = (10^6 / 100) / 2
781 */
782#define RECOVERY_NDELAY 5000
783#define RECOVERY_CLK_CNT 9
784
785static int i2c_generic_recovery(struct i2c_adapter *adap)
786{
787 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
788 int i = 0, val = 1, ret = 0;
789
790 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300791 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530792
Jan Luebbe8b062602015-07-08 16:35:06 +0200793 bri->set_scl(adap, val);
794 ndelay(RECOVERY_NDELAY);
795
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530796 /*
797 * By this time SCL is high, as we need to give 9 falling-rising edges
798 */
799 while (i++ < RECOVERY_CLK_CNT * 2) {
800 if (val) {
801 /* Break if SDA is high */
802 if (bri->get_sda && bri->get_sda(adap))
803 break;
804 /* SCL shouldn't be low here */
805 if (!bri->get_scl(adap)) {
806 dev_err(&adap->dev,
807 "SCL is stuck low, exit recovery\n");
808 ret = -EBUSY;
809 break;
810 }
811 }
812
813 val = !val;
814 bri->set_scl(adap, val);
815 ndelay(RECOVERY_NDELAY);
816 }
817
818 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300819 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530820
821 return ret;
822}
823
824int i2c_generic_scl_recovery(struct i2c_adapter *adap)
825{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530826 return i2c_generic_recovery(adap);
827}
Mark Brownc1c21f42015-04-15 19:18:39 +0100828EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530829
830int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
831{
832 int ret;
833
834 ret = i2c_get_gpios_for_recovery(adap);
835 if (ret)
836 return ret;
837
838 ret = i2c_generic_recovery(adap);
839 i2c_put_gpios_for_recovery(adap);
840
841 return ret;
842}
Mark Brownc1c21f42015-04-15 19:18:39 +0100843EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530844
845int i2c_recover_bus(struct i2c_adapter *adap)
846{
847 if (!adap->bus_recovery_info)
848 return -EOPNOTSUPP;
849
850 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
851 return adap->bus_recovery_info->recover_bus(adap);
852}
Mark Brownc1c21f42015-04-15 19:18:39 +0100853EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530854
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900855static void i2c_init_recovery(struct i2c_adapter *adap)
856{
857 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
858 char *err_str;
859
860 if (!bri)
861 return;
862
863 if (!bri->recover_bus) {
864 err_str = "no recover_bus() found";
865 goto err;
866 }
867
868 /* Generic GPIO recovery */
869 if (bri->recover_bus == i2c_generic_gpio_recovery) {
870 if (!gpio_is_valid(bri->scl_gpio)) {
871 err_str = "invalid SCL gpio";
872 goto err;
873 }
874
875 if (gpio_is_valid(bri->sda_gpio))
876 bri->get_sda = get_sda_gpio_value;
877 else
878 bri->get_sda = NULL;
879
880 bri->get_scl = get_scl_gpio_value;
881 bri->set_scl = set_scl_gpio_value;
882 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
883 /* Generic SCL recovery */
884 if (!bri->set_scl || !bri->get_scl) {
885 err_str = "no {get|set}_scl() found";
886 goto err;
887 }
888 }
889
890 return;
891 err:
892 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
893 adap->bus_recovery_info = NULL;
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static int i2c_device_probe(struct device *dev)
897{
Jean Delvare51298d12009-09-18 22:45:45 +0200898 struct i2c_client *client = i2c_verify_client(dev);
899 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100900 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200901
Jean Delvare51298d12009-09-18 22:45:45 +0200902 if (!client)
903 return 0;
904
Mika Westerberg845c8772015-05-06 13:29:08 +0300905 if (!client->irq) {
906 int irq = -ENOENT;
907
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700908 if (dev->of_node) {
909 irq = of_irq_get_byname(dev->of_node, "irq");
910 if (irq == -EINVAL || irq == -ENODATA)
911 irq = of_irq_get(dev->of_node, 0);
912 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300913 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700914 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100915 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200916 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100917 if (irq < 0)
918 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200919
920 client->irq = irq;
921 }
922
Jean Delvare51298d12009-09-18 22:45:45 +0200923 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200924 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200925 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200926
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700927 if (client->flags & I2C_CLIENT_WAKE) {
928 int wakeirq = -ENOENT;
929
930 if (dev->of_node) {
931 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
932 if (wakeirq == -EPROBE_DEFER)
933 return wakeirq;
934 }
935
936 device_init_wakeup(&client->dev, true);
937
938 if (wakeirq > 0 && wakeirq != client->irq)
939 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
940 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200941 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700942 else
943 status = 0;
944
945 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300946 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700947 }
948
David Brownell7b4fbc52007-05-01 23:26:30 +0200949 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200950
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200951 status = of_clk_set_defaults(dev->of_node, false);
952 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700953 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200954
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200955 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100956 if (status == -EPROBE_DEFER)
957 goto err_clear_wakeup_irq;
958
959 status = driver->probe(client, i2c_match_id(driver->id_table, client));
960 if (status)
961 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100962
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700963 return 0;
964
965err_detach_pm_domain:
966 dev_pm_domain_detach(&client->dev, true);
967err_clear_wakeup_irq:
968 dev_pm_clear_wake_irq(&client->dev);
969 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100970 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static int i2c_device_remove(struct device *dev)
974{
Jean Delvare51298d12009-09-18 22:45:45 +0200975 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200976 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100977 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200978
Jean Delvare51298d12009-09-18 22:45:45 +0200979 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200980 return 0;
981
982 driver = to_i2c_driver(dev->driver);
983 if (driver->remove) {
984 dev_dbg(dev, "remove\n");
985 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200986 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100987
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200988 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700989
990 dev_pm_clear_wake_irq(&client->dev);
991 device_init_wakeup(&client->dev, false);
992
David Brownella1d9e6e2007-05-01 23:26:30 +0200993 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
David Brownellf37dd802007-02-13 22:09:00 +0100996static void i2c_device_shutdown(struct device *dev)
997{
Jean Delvare51298d12009-09-18 22:45:45 +0200998 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100999 struct i2c_driver *driver;
1000
Jean Delvare51298d12009-09-18 22:45:45 +02001001 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001002 return;
1003 driver = to_i2c_driver(dev->driver);
1004 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001005 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001006}
1007
David Brownell9c1600e2007-05-01 23:26:31 +02001008static void i2c_client_dev_release(struct device *dev)
1009{
1010 kfree(to_i2c_client(dev));
1011}
1012
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001013static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001014show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001015{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001016 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1017 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001018}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001019static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001020
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001021static ssize_t
1022show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001023{
1024 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001025 int len;
1026
1027 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1028 if (len != -ENODEV)
1029 return len;
1030
Jean Delvareeb8a7902008-05-18 20:49:41 +02001031 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001032}
Jean Delvare51298d12009-09-18 22:45:45 +02001033static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1034
1035static struct attribute *i2c_dev_attrs[] = {
1036 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001037 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001038 &dev_attr_modalias.attr,
1039 NULL
1040};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001041ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001042
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001043struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001044 .name = "i2c",
1045 .match = i2c_device_match,
1046 .probe = i2c_device_probe,
1047 .remove = i2c_device_remove,
1048 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001049};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001050EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001051
Jean Delvare51298d12009-09-18 22:45:45 +02001052static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001053 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001054 .uevent = i2c_device_uevent,
1055 .release = i2c_client_dev_release,
1056};
1057
David Brownell9b766b82008-01-27 18:14:51 +01001058
1059/**
1060 * i2c_verify_client - return parameter as i2c_client, or NULL
1061 * @dev: device, probably from some driver model iterator
1062 *
1063 * When traversing the driver model tree, perhaps using driver model
1064 * iterators like @device_for_each_child(), you can't assume very much
1065 * about the nodes you find. Use this function to avoid oopses caused
1066 * by wrongly treating some non-I2C device as an i2c_client.
1067 */
1068struct i2c_client *i2c_verify_client(struct device *dev)
1069{
Jean Delvare51298d12009-09-18 22:45:45 +02001070 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001071 ? to_i2c_client(dev)
1072 : NULL;
1073}
1074EXPORT_SYMBOL(i2c_verify_client);
1075
1076
Wolfram Sangda899f52015-05-18 21:09:12 +02001077/* Return a unique address which takes the flags of the client into account */
1078static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1079{
1080 unsigned short addr = client->addr;
1081
1082 /* For some client flags, add an arbitrary offset to avoid collisions */
1083 if (client->flags & I2C_CLIENT_TEN)
1084 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1085
1086 if (client->flags & I2C_CLIENT_SLAVE)
1087 addr |= I2C_ADDR_OFFSET_SLAVE;
1088
1089 return addr;
1090}
1091
Jean Delvare3a89db52010-06-03 11:33:52 +02001092/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001093 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001094static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001095{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001096 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001097 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001098 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001099 return -EINVAL;
1100 } else {
1101 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001102 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001103 return -EINVAL;
1104 }
1105 return 0;
1106}
1107
Jean Delvare656b8762010-06-03 11:33:53 +02001108/* And this is a strict address validity check, used when probing. If a
1109 * device uses a reserved address, then it shouldn't be probed. 7-bit
1110 * addressing is assumed, 10-bit address devices are rare and should be
1111 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +02001112static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001113{
1114 /*
1115 * Reserved addresses per I2C specification:
1116 * 0x00 General call address / START byte
1117 * 0x01 CBUS address
1118 * 0x02 Reserved for different bus format
1119 * 0x03 Reserved for future purposes
1120 * 0x04-0x07 Hs-mode master code
1121 * 0x78-0x7b 10-bit slave addressing
1122 * 0x7c-0x7f Reserved for future purposes
1123 */
1124 if (addr < 0x08 || addr > 0x77)
1125 return -EINVAL;
1126 return 0;
1127}
1128
Jean Delvare3b5f7942010-06-03 11:33:55 +02001129static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1130{
1131 struct i2c_client *client = i2c_verify_client(dev);
1132 int addr = *(int *)addrp;
1133
Wolfram Sang9bccc702015-07-17 14:48:56 +02001134 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001135 return -EBUSY;
1136 return 0;
1137}
1138
Michael Lawnick08263742010-08-11 18:21:02 +02001139/* walk up mux tree */
1140static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1141{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001142 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001143 int result;
1144
1145 result = device_for_each_child(&adapter->dev, &addr,
1146 __i2c_check_addr_busy);
1147
Jean Delvare97cc4d42010-10-24 18:16:57 +02001148 if (!result && parent)
1149 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001150
1151 return result;
1152}
1153
1154/* recurse down mux tree */
1155static int i2c_check_mux_children(struct device *dev, void *addrp)
1156{
1157 int result;
1158
1159 if (dev->type == &i2c_adapter_type)
1160 result = device_for_each_child(dev, addrp,
1161 i2c_check_mux_children);
1162 else
1163 result = __i2c_check_addr_busy(dev, addrp);
1164
1165 return result;
1166}
1167
Jean Delvare3b5f7942010-06-03 11:33:55 +02001168static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1169{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001170 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001171 int result = 0;
1172
Jean Delvare97cc4d42010-10-24 18:16:57 +02001173 if (parent)
1174 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001175
1176 if (!result)
1177 result = device_for_each_child(&adapter->dev, &addr,
1178 i2c_check_mux_children);
1179
1180 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001181}
1182
David Brownell9c1600e2007-05-01 23:26:31 +02001183/**
Peter Rosin8320f492016-05-04 22:15:27 +02001184 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001185 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001186 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1187 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001188 */
Peter Rosin8320f492016-05-04 22:15:27 +02001189static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1190 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001191{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001192 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001193}
Jean Delvarefe61e072010-08-11 18:20:58 +02001194
1195/**
Peter Rosin8320f492016-05-04 22:15:27 +02001196 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001197 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001198 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1199 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001200 */
Peter Rosin8320f492016-05-04 22:15:27 +02001201static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1202 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001203{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001204 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001205}
1206
1207/**
Peter Rosin8320f492016-05-04 22:15:27 +02001208 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001209 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001210 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1211 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001212 */
Peter Rosin8320f492016-05-04 22:15:27 +02001213static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1214 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001215{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001216 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001217}
Jean Delvarefe61e072010-08-11 18:20:58 +02001218
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001219static void i2c_dev_set_name(struct i2c_adapter *adap,
1220 struct i2c_client *client)
1221{
1222 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1223
1224 if (adev) {
1225 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1226 return;
1227 }
1228
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001229 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001230 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001231}
1232
Jean Delvarefe61e072010-08-11 18:20:58 +02001233/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001234 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001235 * @adap: the adapter managing the device
1236 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001237 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001238 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001239 * Create an i2c device. Binding is handled through driver model
1240 * probe()/remove() methods. A driver may be bound to this device when we
1241 * return from this function, or any later moment (e.g. maybe hotplugging will
1242 * load the driver module). This call is not appropriate for use by mainboard
1243 * initialization logic, which usually runs during an arch_initcall() long
1244 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001245 *
1246 * This returns the new i2c client, which may be saved for later use with
1247 * i2c_unregister_device(); or NULL to indicate an error.
1248 */
1249struct i2c_client *
1250i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1251{
1252 struct i2c_client *client;
1253 int status;
1254
1255 client = kzalloc(sizeof *client, GFP_KERNEL);
1256 if (!client)
1257 return NULL;
1258
1259 client->adapter = adap;
1260
1261 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001262
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001263 if (info->archdata)
1264 client->dev.archdata = *info->archdata;
1265
Marc Pignatee354252008-08-28 08:33:22 +02001266 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001267 client->addr = info->addr;
1268 client->irq = info->irq;
1269
David Brownell9c1600e2007-05-01 23:26:31 +02001270 strlcpy(client->name, info->type, sizeof(client->name));
1271
Wolfram Sangc4019b72015-07-17 12:50:06 +02001272 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001273 if (status) {
1274 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1275 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1276 goto out_err_silent;
1277 }
1278
Jean Delvaref8a227e2009-06-19 16:58:18 +02001279 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001280 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001281 if (status)
1282 goto out_err;
1283
1284 client->dev.parent = &client->adapter->dev;
1285 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001286 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001287 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001288 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001289
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001290 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001291 status = device_register(&client->dev);
1292 if (status)
1293 goto out_err;
1294
Jean Delvaref8a227e2009-06-19 16:58:18 +02001295 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1296 client->name, dev_name(&client->dev));
1297
David Brownell9c1600e2007-05-01 23:26:31 +02001298 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001299
1300out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001301 dev_err(&adap->dev,
1302 "Failed to register i2c client %s at 0x%02x (%d)\n",
1303 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001304out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001305 kfree(client);
1306 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001307}
1308EXPORT_SYMBOL_GPL(i2c_new_device);
1309
1310
1311/**
1312 * i2c_unregister_device - reverse effect of i2c_new_device()
1313 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001314 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001315 */
1316void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001317{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001318 if (client->dev.of_node)
1319 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001320 if (ACPI_COMPANION(&client->dev))
1321 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001322 device_unregister(&client->dev);
1323}
David Brownell9c1600e2007-05-01 23:26:31 +02001324EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001325
1326
Jean Delvare60b129d2008-05-11 20:37:06 +02001327static const struct i2c_device_id dummy_id[] = {
1328 { "dummy", 0 },
1329 { },
1330};
1331
Jean Delvared2653e92008-04-29 23:11:39 +02001332static int dummy_probe(struct i2c_client *client,
1333 const struct i2c_device_id *id)
1334{
1335 return 0;
1336}
1337
1338static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001339{
1340 return 0;
1341}
1342
1343static struct i2c_driver dummy_driver = {
1344 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001345 .probe = dummy_probe,
1346 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001347 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001348};
1349
1350/**
1351 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1352 * @adapter: the adapter managing the device
1353 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001354 * Context: can sleep
1355 *
1356 * This returns an I2C client bound to the "dummy" driver, intended for use
1357 * with devices that consume multiple addresses. Examples of such chips
1358 * include various EEPROMS (like 24c04 and 24c08 models).
1359 *
1360 * These dummy devices have two main uses. First, most I2C and SMBus calls
1361 * except i2c_transfer() need a client handle; the dummy will be that handle.
1362 * And second, this prevents the specified address from being bound to a
1363 * different driver.
1364 *
1365 * This returns the new i2c client, which should be saved for later use with
1366 * i2c_unregister_device(); or NULL to indicate an error.
1367 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001368struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001369{
1370 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001371 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001372 };
1373
David Brownelle9f13732008-01-27 18:14:52 +01001374 return i2c_new_device(adapter, &info);
1375}
1376EXPORT_SYMBOL_GPL(i2c_new_dummy);
1377
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001378/**
1379 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1380 * and create the associated device
1381 * @client: Handle to the primary client
1382 * @name: Handle to specify which secondary address to get
1383 * @default_addr: Used as a fallback if no secondary address was specified
1384 * Context: can sleep
1385 *
1386 * I2C clients can be composed of multiple I2C slaves bound together in a single
1387 * component. The I2C client driver then binds to the master I2C slave and needs
1388 * to create I2C dummy clients to communicate with all the other slaves.
1389 *
1390 * This function creates and returns an I2C dummy client whose I2C address is
1391 * retrieved from the platform firmware based on the given slave name. If no
1392 * address is specified by the firmware default_addr is used.
1393 *
1394 * On DT-based platforms the address is retrieved from the "reg" property entry
1395 * cell whose "reg-names" value matches the slave name.
1396 *
1397 * This returns the new i2c client, which should be saved for later use with
1398 * i2c_unregister_device(); or NULL to indicate an error.
1399 */
1400struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1401 const char *name,
1402 u16 default_addr)
1403{
1404 struct device_node *np = client->dev.of_node;
1405 u32 addr = default_addr;
1406 int i;
1407
1408 if (np) {
1409 i = of_property_match_string(np, "reg-names", name);
1410 if (i >= 0)
1411 of_property_read_u32_index(np, "reg", i, &addr);
1412 }
1413
1414 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1415 return i2c_new_dummy(client->adapter, addr);
1416}
1417EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1418
David Brownellf37dd802007-02-13 22:09:00 +01001419/* ------------------------------------------------------------------------- */
1420
David Brownell16ffadf2007-05-01 23:26:28 +02001421/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1422
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001423static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
David Brownellef2c83212007-05-01 23:26:28 +02001425 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 complete(&adap->dev_released);
1427}
1428
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001429unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001430{
1431 unsigned int depth = 0;
1432
1433 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1434 depth++;
1435
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001436 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1437 "adapter depth exceeds lockdep subclass limit\n");
1438
Jean Delvare390946b2012-09-10 10:14:02 +02001439 return depth;
1440}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001441EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001442
1443/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001444 * Let users instantiate I2C devices through sysfs. This can be used when
1445 * platform initialization code doesn't contain the proper data for
1446 * whatever reason. Also useful for drivers that do device detection and
1447 * detection fails, either because the device uses an unexpected address,
1448 * or this is a compatible device with different ID register values.
1449 *
1450 * Parameter checking may look overzealous, but we really don't want
1451 * the user to provide incorrect parameters.
1452 */
1453static ssize_t
1454i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1455 const char *buf, size_t count)
1456{
1457 struct i2c_adapter *adap = to_i2c_adapter(dev);
1458 struct i2c_board_info info;
1459 struct i2c_client *client;
1460 char *blank, end;
1461 int res;
1462
Jean Delvare99cd8e22009-06-19 16:58:20 +02001463 memset(&info, 0, sizeof(struct i2c_board_info));
1464
1465 blank = strchr(buf, ' ');
1466 if (!blank) {
1467 dev_err(dev, "%s: Missing parameters\n", "new_device");
1468 return -EINVAL;
1469 }
1470 if (blank - buf > I2C_NAME_SIZE - 1) {
1471 dev_err(dev, "%s: Invalid device name\n", "new_device");
1472 return -EINVAL;
1473 }
1474 memcpy(info.type, buf, blank - buf);
1475
1476 /* Parse remaining parameters, reject extra parameters */
1477 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1478 if (res < 1) {
1479 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1480 return -EINVAL;
1481 }
1482 if (res > 1 && end != '\n') {
1483 dev_err(dev, "%s: Extra parameters\n", "new_device");
1484 return -EINVAL;
1485 }
1486
Wolfram Sangcfa03272015-07-27 14:03:38 +02001487 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1488 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1489 info.flags |= I2C_CLIENT_TEN;
1490 }
1491
1492 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1493 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1494 info.flags |= I2C_CLIENT_SLAVE;
1495 }
1496
Jean Delvare99cd8e22009-06-19 16:58:20 +02001497 client = i2c_new_device(adap, &info);
1498 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001499 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001500
1501 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001502 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001503 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001504 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001505 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1506 info.type, info.addr);
1507
1508 return count;
1509}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001510static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001511
1512/*
1513 * And of course let the users delete the devices they instantiated, if
1514 * they got it wrong. This interface can only be used to delete devices
1515 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1516 * don't delete devices to which some kernel code still has references.
1517 *
1518 * Parameter checking may look overzealous, but we really don't want
1519 * the user to delete the wrong device.
1520 */
1521static ssize_t
1522i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1523 const char *buf, size_t count)
1524{
1525 struct i2c_adapter *adap = to_i2c_adapter(dev);
1526 struct i2c_client *client, *next;
1527 unsigned short addr;
1528 char end;
1529 int res;
1530
1531 /* Parse parameters, reject extra parameters */
1532 res = sscanf(buf, "%hi%c", &addr, &end);
1533 if (res < 1) {
1534 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1535 return -EINVAL;
1536 }
1537 if (res > 1 && end != '\n') {
1538 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1539 return -EINVAL;
1540 }
1541
1542 /* Make sure the device was added through sysfs */
1543 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001544 mutex_lock_nested(&adap->userspace_clients_lock,
1545 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001546 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1547 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001548 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001549 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1550 "delete_device", client->name, client->addr);
1551
1552 list_del(&client->detected);
1553 i2c_unregister_device(client);
1554 res = count;
1555 break;
1556 }
1557 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001558 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001559
1560 if (res < 0)
1561 dev_err(dev, "%s: Can't find device in list\n",
1562 "delete_device");
1563 return res;
1564}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001565static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1566 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001567
1568static struct attribute *i2c_adapter_attrs[] = {
1569 &dev_attr_name.attr,
1570 &dev_attr_new_device.attr,
1571 &dev_attr_delete_device.attr,
1572 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001573};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001574ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001575
Michael Lawnick08263742010-08-11 18:21:02 +02001576struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001577 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001578 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001579};
Michael Lawnick08263742010-08-11 18:21:02 +02001580EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Stephen Warren643dd092012-04-17 12:43:33 -06001582/**
1583 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1584 * @dev: device, probably from some driver model iterator
1585 *
1586 * When traversing the driver model tree, perhaps using driver model
1587 * iterators like @device_for_each_child(), you can't assume very much
1588 * about the nodes you find. Use this function to avoid oopses caused
1589 * by wrongly treating some non-I2C device as an i2c_adapter.
1590 */
1591struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1592{
1593 return (dev->type == &i2c_adapter_type)
1594 ? to_i2c_adapter(dev)
1595 : NULL;
1596}
1597EXPORT_SYMBOL(i2c_verify_adapter);
1598
Jean Delvare2bb50952009-09-18 22:45:46 +02001599#ifdef CONFIG_I2C_COMPAT
1600static struct class_compat *i2c_adapter_compat_class;
1601#endif
1602
David Brownell9c1600e2007-05-01 23:26:31 +02001603static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1604{
1605 struct i2c_devinfo *devinfo;
1606
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001607 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001608 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1609 if (devinfo->busnum == adapter->nr
1610 && !i2c_new_device(adapter,
1611 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001612 dev_err(&adapter->dev,
1613 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001614 devinfo->board_info.addr);
1615 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001616 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001617}
1618
Wolfram Sang687b81d2013-07-11 12:56:15 +01001619/* OF support code */
1620
1621#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001622static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1623 struct device_node *node)
1624{
1625 struct i2c_client *result;
1626 struct i2c_board_info info = {};
1627 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001628 const __be32 *addr_be;
1629 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001630 int len;
1631
1632 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1633
1634 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1635 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1636 node->full_name);
1637 return ERR_PTR(-EINVAL);
1638 }
1639
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001640 addr_be = of_get_property(node, "reg", &len);
1641 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001642 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1643 node->full_name);
1644 return ERR_PTR(-EINVAL);
1645 }
1646
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001647 addr = be32_to_cpup(addr_be);
1648 if (addr & I2C_TEN_BIT_ADDRESS) {
1649 addr &= ~I2C_TEN_BIT_ADDRESS;
1650 info.flags |= I2C_CLIENT_TEN;
1651 }
1652
1653 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1654 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1655 info.flags |= I2C_CLIENT_SLAVE;
1656 }
1657
1658 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001659 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
John Garry93c94ec2017-01-06 19:02:57 +08001660 addr, node->full_name);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001661 return ERR_PTR(-EINVAL);
1662 }
1663
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001664 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001665 info.of_node = of_node_get(node);
1666 info.archdata = &dev_ad;
1667
1668 if (of_get_property(node, "wakeup-source", NULL))
1669 info.flags |= I2C_CLIENT_WAKE;
1670
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001671 result = i2c_new_device(adap, &info);
1672 if (result == NULL) {
1673 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1674 node->full_name);
1675 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001676 return ERR_PTR(-EINVAL);
1677 }
1678 return result;
1679}
1680
Wolfram Sang687b81d2013-07-11 12:56:15 +01001681static void of_i2c_register_devices(struct i2c_adapter *adap)
1682{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001683 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001684 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001685
1686 /* Only register child devices if the adapter has a node pointer set */
1687 if (!adap->dev.of_node)
1688 return;
1689
1690 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1691
Jon Hunter7e4c2242016-06-29 10:17:53 +01001692 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1693 if (!bus)
1694 bus = of_node_get(adap->dev.of_node);
1695
1696 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001697 if (of_node_test_and_set_flag(node, OF_POPULATED))
1698 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001699
1700 client = of_i2c_register_device(adap, node);
1701 if (IS_ERR(client)) {
1702 dev_warn(&adap->dev,
1703 "Failed to create I2C device for %s\n",
1704 node->full_name);
1705 of_node_clear_flag(node, OF_POPULATED);
1706 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001707 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001708
1709 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001710}
1711
1712static int of_dev_node_match(struct device *dev, void *data)
1713{
1714 return dev->of_node == data;
1715}
1716
1717/* must call put_device() when done with returned i2c_client device */
1718struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1719{
1720 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001721 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001722
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001723 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001724 if (!dev)
1725 return NULL;
1726
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001727 client = i2c_verify_client(dev);
1728 if (!client)
1729 put_device(dev);
1730
1731 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001732}
1733EXPORT_SYMBOL(of_find_i2c_device_by_node);
1734
1735/* must call put_device() when done with returned i2c_adapter device */
1736struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1737{
1738 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001739 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001740
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001741 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001742 if (!dev)
1743 return NULL;
1744
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001745 adapter = i2c_verify_adapter(dev);
1746 if (!adapter)
1747 put_device(dev);
1748
1749 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001750}
1751EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001752
1753/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1754struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1755{
1756 struct i2c_adapter *adapter;
1757
1758 adapter = of_find_i2c_adapter_by_node(node);
1759 if (!adapter)
1760 return NULL;
1761
1762 if (!try_module_get(adapter->owner)) {
1763 put_device(&adapter->dev);
1764 adapter = NULL;
1765 }
1766
1767 return adapter;
1768}
1769EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001770#else
1771static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1772#endif /* CONFIG_OF */
1773
Jean Delvare69b00892009-12-06 17:06:27 +01001774static int i2c_do_add_adapter(struct i2c_driver *driver,
1775 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001776{
Jean Delvare4735c982008-07-14 22:38:36 +02001777 /* Detect supported devices on that bus, and instantiate them */
1778 i2c_detect(adap, driver);
1779
1780 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001781 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001782 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1783 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001784 dev_warn(&adap->dev,
1785 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001786 /* We ignore the return code; if it fails, too bad */
1787 driver->attach_adapter(adap);
1788 }
1789 return 0;
1790}
1791
Jean Delvare69b00892009-12-06 17:06:27 +01001792static int __process_new_adapter(struct device_driver *d, void *data)
1793{
1794 return i2c_do_add_adapter(to_i2c_driver(d), data);
1795}
1796
Peter Rosind1ed7982016-08-25 23:07:01 +02001797static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1798 .lock_bus = i2c_adapter_lock_bus,
1799 .trylock_bus = i2c_adapter_trylock_bus,
1800 .unlock_bus = i2c_adapter_unlock_bus,
1801};
1802
David Brownell6e13e642007-05-01 23:26:31 +02001803static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001805 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
David Brownell1d0b19c2008-10-14 17:30:05 +02001807 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301808 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001809 res = -EAGAIN;
1810 goto out_list;
1811 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001812
Jean Delvare2236baa2010-11-15 22:40:38 +01001813 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001814 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001815 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001816
1817 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001818 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001819 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001820 }
1821
Peter Rosind1ed7982016-08-25 23:07:01 +02001822 if (!adap->lock_ops)
1823 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001824
Mika Kuoppala194684e2009-12-06 17:06:22 +01001825 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001826 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001827 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001828 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Jean Delvare8fcfef62009-03-28 21:34:43 +01001830 /* Set default timeout to 1 second if not already set */
1831 if (adap->timeout == 0)
1832 adap->timeout = HZ;
1833
Kay Sievers27d9c182009-01-07 14:29:16 +01001834 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001835 adap->dev.bus = &i2c_bus_type;
1836 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001837 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001838 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001839 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001840 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001843 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1844
Charles Keepax6ada5c12015-04-16 13:05:19 +01001845 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001846 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001847 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001848
Jean Delvare2bb50952009-09-18 22:45:46 +02001849#ifdef CONFIG_I2C_COMPAT
1850 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1851 adap->dev.parent);
1852 if (res)
1853 dev_warn(&adap->dev,
1854 "Failed to create compatibility class link\n");
1855#endif
1856
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001857 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301858
Jean Delvare729d6dd2009-06-19 16:58:18 +02001859 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001860 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001861 i2c_acpi_register_devices(adap);
1862 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001863
David Brownell6e13e642007-05-01 23:26:31 +02001864 if (adap->nr < __i2c_first_dynamic_bus_num)
1865 i2c_scan_static_board_info(adap);
1866
Jean Delvare4735c982008-07-14 22:38:36 +02001867 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001868 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001869 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001870 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001871
1872 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001873
Jean Delvareb119c6c2006-08-15 18:26:30 +02001874out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001875 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001876 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001877 mutex_unlock(&core_lock);
1878 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
David Brownell6e13e642007-05-01 23:26:31 +02001881/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001882 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1883 * @adap: the adapter to register (with adap->nr initialized)
1884 * Context: can sleep
1885 *
1886 * See i2c_add_numbered_adapter() for details.
1887 */
1888static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1889{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001890 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001891
1892 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001893 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001894 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001895 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001896 return id == -ENOSPC ? -EBUSY : id;
1897
1898 return i2c_register_adapter(adap);
1899}
1900
1901/**
David Brownell6e13e642007-05-01 23:26:31 +02001902 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1903 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001904 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001905 *
1906 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001907 * doesn't matter or when its bus number is specified by an dt alias.
1908 * Examples of bases when the bus number doesn't matter: I2C adapters
1909 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001910 *
1911 * When this returns zero, a new bus number was allocated and stored
1912 * in adap->nr, and the specified adapter became available for clients.
1913 * Otherwise, a negative errno value is returned.
1914 */
1915int i2c_add_adapter(struct i2c_adapter *adapter)
1916{
Doug Andersonee5c2742013-03-01 08:57:31 -08001917 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001918 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001919
Doug Andersonee5c2742013-03-01 08:57:31 -08001920 if (dev->of_node) {
1921 id = of_alias_get_id(dev->of_node, "i2c");
1922 if (id >= 0) {
1923 adapter->nr = id;
1924 return __i2c_add_numbered_adapter(adapter);
1925 }
1926 }
1927
Jean Delvarecaada322008-01-27 18:14:49 +01001928 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001929 id = idr_alloc(&i2c_adapter_idr, adapter,
1930 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001931 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001932 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001933 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001934
1935 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001936
David Brownell6e13e642007-05-01 23:26:31 +02001937 return i2c_register_adapter(adapter);
1938}
1939EXPORT_SYMBOL(i2c_add_adapter);
1940
1941/**
1942 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1943 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001944 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001945 *
1946 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001947 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1948 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001949 * is used to properly configure I2C devices.
1950 *
Grant Likely488bf312011-07-25 17:49:43 +02001951 * If the requested bus number is set to -1, then this function will behave
1952 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1953 *
David Brownell6e13e642007-05-01 23:26:31 +02001954 * If no devices have pre-been declared for this bus, then be sure to
1955 * register the adapter before any dynamically allocated ones. Otherwise
1956 * the required bus ID may not be available.
1957 *
1958 * When this returns zero, the specified adapter became available for
1959 * clients using the bus number provided in adap->nr. Also, the table
1960 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1961 * and the appropriate driver model device nodes are created. Otherwise, a
1962 * negative errno value is returned.
1963 */
1964int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1965{
Grant Likely488bf312011-07-25 17:49:43 +02001966 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1967 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001968
Doug Andersonee5c2742013-03-01 08:57:31 -08001969 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001970}
1971EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1972
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001973static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001974 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001975{
Jean Delvare4735c982008-07-14 22:38:36 +02001976 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001977
Jean Delvareacec2112009-03-28 21:34:40 +01001978 /* Remove the devices we created ourselves as the result of hardware
1979 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001980 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1981 if (client->adapter == adapter) {
1982 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1983 client->name, client->addr);
1984 list_del(&client->detected);
1985 i2c_unregister_device(client);
1986 }
1987 }
Jean Delvare026526f2008-01-27 18:14:49 +01001988}
1989
Jean Delvaree549c2b2009-06-19 16:58:19 +02001990static int __unregister_client(struct device *dev, void *dummy)
1991{
1992 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001993 if (client && strcmp(client->name, "dummy"))
1994 i2c_unregister_device(client);
1995 return 0;
1996}
1997
1998static int __unregister_dummy(struct device *dev, void *dummy)
1999{
2000 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002001 if (client)
2002 i2c_unregister_device(client);
2003 return 0;
2004}
2005
Jean Delvare69b00892009-12-06 17:06:27 +01002006static int __process_removed_adapter(struct device_driver *d, void *data)
2007{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002008 i2c_do_del_adapter(to_i2c_driver(d), data);
2009 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002010}
2011
David Brownelld64f73b2007-07-12 14:12:28 +02002012/**
2013 * i2c_del_adapter - unregister I2C adapter
2014 * @adap: the adapter being unregistered
2015 * Context: can sleep
2016 *
2017 * This unregisters an I2C adapter which was previously registered
2018 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2019 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002020void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002022 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002023 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002026 mutex_lock(&core_lock);
2027 found = idr_find(&i2c_adapter_idr, adap->nr);
2028 mutex_unlock(&core_lock);
2029 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002030 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002031 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002034 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002035 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002036 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002037 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002038 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002039 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002041 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002042 mutex_lock_nested(&adap->userspace_clients_lock,
2043 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002044 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2045 detected) {
2046 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2047 client->addr);
2048 list_del(&client->detected);
2049 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002050 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002051 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002052
Jean Delvaree549c2b2009-06-19 16:58:19 +02002053 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002054 * check the returned value. This is a two-pass process, because
2055 * we can't remove the dummy devices during the first pass: they
2056 * could have been instantiated by real devices wishing to clean
2057 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002058 device_for_each_child(&adap->dev, NULL, __unregister_client);
2059 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Jean Delvare2bb50952009-09-18 22:45:46 +02002061#ifdef CONFIG_I2C_COMPAT
2062 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2063 adap->dev.parent);
2064#endif
2065
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002066 /* device name is gone after device_unregister */
2067 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2068
Wolfram Sang9f924162015-12-23 18:19:28 +01002069 pm_runtime_disable(&adap->dev);
2070
Wolfram Sang26680ee2015-01-29 22:45:09 +01002071 /* wait until all references to the device are gone
2072 *
2073 * FIXME: This is old code and should ideally be replaced by an
2074 * alternative which results in decoupling the lifetime of the struct
2075 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302076 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
David Brownell6e13e642007-05-01 23:26:31 +02002082 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002083 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002085 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002087 /* Clear the device structure in case this adapter is ever going to be
2088 added again */
2089 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
David Brownellc0564602007-05-01 23:26:31 +02002091EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Wolfram Sang54177cc2015-12-17 13:32:36 +01002093/**
2094 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2095 * @dev: The device to scan for I2C timing properties
2096 * @t: the i2c_timings struct to be filled with values
2097 * @use_defaults: bool to use sane defaults derived from the I2C specification
2098 * when properties are not found, otherwise use 0
2099 *
2100 * Scan the device for the generic I2C properties describing timing parameters
2101 * for the signal and fill the given struct with the results. If a property was
2102 * not found and use_defaults was true, then maximum timings are assumed which
2103 * are derived from the I2C specification. If use_defaults is not used, the
2104 * results will be 0, so drivers can apply their own defaults later. The latter
2105 * is mainly intended for avoiding regressions of existing drivers which want
2106 * to switch to this function. New drivers almost always should use the defaults.
2107 */
2108
2109void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2110{
2111 int ret;
2112
2113 memset(t, 0, sizeof(*t));
2114
2115 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2116 if (ret && use_defaults)
2117 t->bus_freq_hz = 100000;
2118
2119 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2120 if (ret && use_defaults) {
2121 if (t->bus_freq_hz <= 100000)
2122 t->scl_rise_ns = 1000;
2123 else if (t->bus_freq_hz <= 400000)
2124 t->scl_rise_ns = 300;
2125 else
2126 t->scl_rise_ns = 120;
2127 }
2128
2129 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2130 if (ret && use_defaults) {
2131 if (t->bus_freq_hz <= 400000)
2132 t->scl_fall_ns = 300;
2133 else
2134 t->scl_fall_ns = 120;
2135 }
2136
2137 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2138
2139 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2140 if (ret && use_defaults)
2141 t->sda_fall_ns = t->scl_fall_ns;
2142}
2143EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2144
David Brownell7b4fbc52007-05-01 23:26:30 +02002145/* ------------------------------------------------------------------------- */
2146
Jean Delvare7ae31482011-03-20 14:50:52 +01002147int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2148{
2149 int res;
2150
2151 mutex_lock(&core_lock);
2152 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2153 mutex_unlock(&core_lock);
2154
2155 return res;
2156}
2157EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2158
Jean Delvare69b00892009-12-06 17:06:27 +01002159static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002160{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002161 if (dev->type != &i2c_adapter_type)
2162 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002163 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002164}
2165
David Brownell7b4fbc52007-05-01 23:26:30 +02002166/*
2167 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002168 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
2170
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002171int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002173 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
David Brownell1d0b19c2008-10-14 17:30:05 +02002175 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302176 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002177 return -EAGAIN;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002180 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002182 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Jean Delvare729d6dd2009-06-19 16:58:18 +02002184 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002185 * will have called probe() for all matching-but-unbound devices.
2186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 res = driver_register(&driver->driver);
2188 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002189 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002190
Wolfram Sang44239a52016-07-09 13:35:04 +09002191 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jean Delvare4735c982008-07-14 22:38:36 +02002193 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002194 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002195
Jean Delvare7eebcb72006-02-05 23:28:21 +01002196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002198EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Jean Delvare69b00892009-12-06 17:06:27 +01002200static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002201{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002202 if (dev->type == &i2c_adapter_type)
2203 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2204 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002205}
2206
David Brownella1d9e6e2007-05-01 23:26:30 +02002207/**
2208 * i2c_del_driver - unregister I2C driver
2209 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002210 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002211 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002212void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Jean Delvare7ae31482011-03-20 14:50:52 +01002214 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002217 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
David Brownellc0564602007-05-01 23:26:31 +02002219EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
David Brownell7b4fbc52007-05-01 23:26:30 +02002221/* ------------------------------------------------------------------------- */
2222
Jean Delvaree48d3312008-01-27 18:14:48 +01002223/**
2224 * i2c_use_client - increments the reference count of the i2c client structure
2225 * @client: the client being referenced
2226 *
2227 * Each live reference to a client should be refcounted. The driver model does
2228 * that automatically as part of driver binding, so that most drivers don't
2229 * need to do this explicitly: they hold a reference until they're unbound
2230 * from the device.
2231 *
2232 * A pointer to the client with the incremented reference counter is returned.
2233 */
2234struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
David Brownell6ea438e2008-07-14 22:38:24 +02002236 if (client && get_device(&client->dev))
2237 return client;
2238 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
David Brownellc0564602007-05-01 23:26:31 +02002240EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Jean Delvaree48d3312008-01-27 18:14:48 +01002242/**
2243 * i2c_release_client - release a use of the i2c client structure
2244 * @client: the client being no longer referenced
2245 *
2246 * Must be called when a user of a client is finished with it.
2247 */
2248void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
David Brownell6ea438e2008-07-14 22:38:24 +02002250 if (client)
2251 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
David Brownellc0564602007-05-01 23:26:31 +02002253EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
David Brownell9b766b82008-01-27 18:14:51 +01002255struct i2c_cmd_arg {
2256 unsigned cmd;
2257 void *arg;
2258};
2259
2260static int i2c_cmd(struct device *dev, void *_arg)
2261{
2262 struct i2c_client *client = i2c_verify_client(dev);
2263 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002264 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002265
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002266 if (!client || !client->dev.driver)
2267 return 0;
2268
2269 driver = to_i2c_driver(client->dev.driver);
2270 if (driver->command)
2271 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002272 return 0;
2273}
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2276{
David Brownell9b766b82008-01-27 18:14:51 +01002277 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
David Brownell9b766b82008-01-27 18:14:51 +01002279 cmd_arg.cmd = cmd;
2280 cmd_arg.arg = arg;
2281 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
David Brownellc0564602007-05-01 23:26:31 +02002283EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002285#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2286static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2287 void *arg)
2288{
2289 struct of_reconfig_data *rd = arg;
2290 struct i2c_adapter *adap;
2291 struct i2c_client *client;
2292
2293 switch (of_reconfig_get_state_change(action, rd)) {
2294 case OF_RECONFIG_CHANGE_ADD:
2295 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2296 if (adap == NULL)
2297 return NOTIFY_OK; /* not for us */
2298
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002299 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2300 put_device(&adap->dev);
2301 return NOTIFY_OK;
2302 }
2303
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002304 client = of_i2c_register_device(adap, rd->dn);
2305 put_device(&adap->dev);
2306
2307 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002308 dev_err(&adap->dev, "failed to create client for '%s'\n",
2309 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002310 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002311 return notifier_from_errno(PTR_ERR(client));
2312 }
2313 break;
2314 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002315 /* already depopulated? */
2316 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2317 return NOTIFY_OK;
2318
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002319 /* find our device by node */
2320 client = of_find_i2c_device_by_node(rd->dn);
2321 if (client == NULL)
2322 return NOTIFY_OK; /* no? not meant for us */
2323
2324 /* unregister takes one ref away */
2325 i2c_unregister_device(client);
2326
2327 /* and put the reference of the find */
2328 put_device(&client->dev);
2329 break;
2330 }
2331
2332 return NOTIFY_OK;
2333}
2334static struct notifier_block i2c_of_notifier = {
2335 .notifier_call = of_i2c_notify,
2336};
2337#else
2338extern struct notifier_block i2c_of_notifier;
2339#endif /* CONFIG_OF_DYNAMIC */
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static int __init i2c_init(void)
2342{
2343 int retval;
2344
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002345 retval = of_alias_get_highest_id("i2c");
2346
2347 down_write(&__i2c_board_lock);
2348 if (retval >= __i2c_first_dynamic_bus_num)
2349 __i2c_first_dynamic_bus_num = retval + 1;
2350 up_write(&__i2c_board_lock);
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 retval = bus_register(&i2c_bus_type);
2353 if (retval)
2354 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002355
2356 is_registered = true;
2357
Jean Delvare2bb50952009-09-18 22:45:46 +02002358#ifdef CONFIG_I2C_COMPAT
2359 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2360 if (!i2c_adapter_compat_class) {
2361 retval = -ENOMEM;
2362 goto bus_err;
2363 }
2364#endif
David Brownelle9f13732008-01-27 18:14:52 +01002365 retval = i2c_add_driver(&dummy_driver);
2366 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002367 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002368
2369 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2370 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002371 if (IS_ENABLED(CONFIG_ACPI))
2372 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002373
David Brownelle9f13732008-01-27 18:14:52 +01002374 return 0;
2375
Jean Delvare2bb50952009-09-18 22:45:46 +02002376class_err:
2377#ifdef CONFIG_I2C_COMPAT
2378 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002379bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002380#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002381 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002382 bus_unregister(&i2c_bus_type);
2383 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
2386static void __exit i2c_exit(void)
2387{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002388 if (IS_ENABLED(CONFIG_ACPI))
2389 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002390 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2391 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002392 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002393#ifdef CONFIG_I2C_COMPAT
2394 class_compat_unregister(i2c_adapter_compat_class);
2395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002397 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
David Brownella10f9e72008-10-14 17:30:06 +02002400/* We must initialize early, because some subsystems register i2c drivers
2401 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2402 */
2403postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404module_exit(i2c_exit);
2405
2406/* ----------------------------------------------------
2407 * the functional interface to the i2c busses.
2408 * ----------------------------------------------------
2409 */
2410
Wolfram Sangb7f62582015-01-05 23:45:59 +01002411/* Check if val is exceeding the quirk IFF quirk is non 0 */
2412#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2413
2414static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2415{
2416 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2417 err_msg, msg->addr, msg->len,
2418 msg->flags & I2C_M_RD ? "read" : "write");
2419 return -EOPNOTSUPP;
2420}
2421
2422static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2423{
2424 const struct i2c_adapter_quirks *q = adap->quirks;
2425 int max_num = q->max_num_msgs, i;
2426 bool do_len_check = true;
2427
2428 if (q->flags & I2C_AQ_COMB) {
2429 max_num = 2;
2430
2431 /* special checks for combined messages */
2432 if (num == 2) {
2433 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2434 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2435
2436 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2437 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2438
2439 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2440 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2441
2442 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2443 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2444
2445 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2446 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2447
2448 do_len_check = false;
2449 }
2450 }
2451
2452 if (i2c_quirk_exceeded(num, max_num))
2453 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2454
2455 for (i = 0; i < num; i++) {
2456 u16 len = msgs[i].len;
2457
2458 if (msgs[i].flags & I2C_M_RD) {
2459 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2460 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2461 } else {
2462 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2463 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2464 }
2465 }
2466
2467 return 0;
2468}
2469
David Brownella1cdeda2008-07-14 22:38:24 +02002470/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002471 * __i2c_transfer - unlocked flavor of i2c_transfer
2472 * @adap: Handle to I2C bus
2473 * @msgs: One or more messages to execute before STOP is issued to
2474 * terminate the operation; each message begins with a START.
2475 * @num: Number of messages to be executed.
2476 *
2477 * Returns negative errno, else the number of messages executed.
2478 *
2479 * Adapter lock must be held when calling this function. No debug logging
2480 * takes place. adap->algo->master_xfer existence isn't checked.
2481 */
2482int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2483{
2484 unsigned long orig_jiffies;
2485 int ret, try;
2486
Wolfram Sangb7f62582015-01-05 23:45:59 +01002487 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2488 return -EOPNOTSUPP;
2489
David Howellsd9a83d62014-03-06 13:35:59 +00002490 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2491 * enabled. This is an efficient way of keeping the for-loop from
2492 * being executed when not needed.
2493 */
2494 if (static_key_false(&i2c_trace_msg)) {
2495 int i;
2496 for (i = 0; i < num; i++)
2497 if (msgs[i].flags & I2C_M_RD)
2498 trace_i2c_read(adap, &msgs[i], i);
2499 else
2500 trace_i2c_write(adap, &msgs[i], i);
2501 }
2502
Jean Delvareb37d2a32012-06-29 07:47:19 -03002503 /* Retry automatically on arbitration loss */
2504 orig_jiffies = jiffies;
2505 for (ret = 0, try = 0; try <= adap->retries; try++) {
2506 ret = adap->algo->master_xfer(adap, msgs, num);
2507 if (ret != -EAGAIN)
2508 break;
2509 if (time_after(jiffies, orig_jiffies + adap->timeout))
2510 break;
2511 }
2512
David Howellsd9a83d62014-03-06 13:35:59 +00002513 if (static_key_false(&i2c_trace_msg)) {
2514 int i;
2515 for (i = 0; i < ret; i++)
2516 if (msgs[i].flags & I2C_M_RD)
2517 trace_i2c_reply(adap, &msgs[i], i);
2518 trace_i2c_result(adap, i, ret);
2519 }
2520
Jean Delvareb37d2a32012-06-29 07:47:19 -03002521 return ret;
2522}
2523EXPORT_SYMBOL(__i2c_transfer);
2524
2525/**
David Brownella1cdeda2008-07-14 22:38:24 +02002526 * i2c_transfer - execute a single or combined I2C message
2527 * @adap: Handle to I2C bus
2528 * @msgs: One or more messages to execute before STOP is issued to
2529 * terminate the operation; each message begins with a START.
2530 * @num: Number of messages to be executed.
2531 *
2532 * Returns negative errno, else the number of messages executed.
2533 *
2534 * Note that there is no requirement that each message be sent to
2535 * the same slave address, although that is the most common model.
2536 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002537int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
David Brownella1cdeda2008-07-14 22:38:24 +02002541 /* REVISIT the fault reporting model here is weak:
2542 *
2543 * - When we get an error after receiving N bytes from a slave,
2544 * there is no way to report "N".
2545 *
2546 * - When we get a NAK after transmitting N bytes to a slave,
2547 * there is no way to report "N" ... or to let the master
2548 * continue executing the rest of this combined message, if
2549 * that's the appropriate response.
2550 *
2551 * - When for example "num" is two and we successfully complete
2552 * the first message but get an error part way through the
2553 * second, it's unclear whether that should be reported as
2554 * one (discarding status on the second message) or errno
2555 * (discarding status on the first one).
2556 */
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (adap->algo->master_xfer) {
2559#ifdef DEBUG
2560 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002561 dev_dbg(&adap->dev,
2562 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2563 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2564 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002565 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567#endif
2568
Mike Rapoportcea443a2008-01-27 18:14:50 +01002569 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002570 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002571 if (!ret)
2572 /* I2C activity is ongoing. */
2573 return -EAGAIN;
2574 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002575 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002576 }
2577
Jean Delvareb37d2a32012-06-29 07:47:19 -03002578 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002579 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 return ret;
2582 } else {
2583 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002584 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586}
David Brownellc0564602007-05-01 23:26:31 +02002587EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
David Brownella1cdeda2008-07-14 22:38:24 +02002589/**
2590 * i2c_master_send - issue a single I2C message in master transmit mode
2591 * @client: Handle to slave device
2592 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002593 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002594 *
2595 * Returns negative errno, or else the number of bytes written.
2596 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002597int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002600 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 struct i2c_msg msg;
2602
Jean Delvare815f55f2005-05-07 22:58:46 +02002603 msg.addr = client->addr;
2604 msg.flags = client->flags & I2C_M_TEN;
2605 msg.len = count;
2606 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002607
Jean Delvare815f55f2005-05-07 22:58:46 +02002608 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002610 /*
2611 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2612 * transmitted, else error code.
2613 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002614 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
David Brownellc0564602007-05-01 23:26:31 +02002616EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
David Brownella1cdeda2008-07-14 22:38:24 +02002618/**
2619 * i2c_master_recv - issue a single I2C message in master receive mode
2620 * @client: Handle to slave device
2621 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002622 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002623 *
2624 * Returns negative errno, or else the number of bytes read.
2625 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002626int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Farid Hammane7225acf2010-05-21 18:40:58 +02002628 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 struct i2c_msg msg;
2630 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Jean Delvare815f55f2005-05-07 22:58:46 +02002632 msg.addr = client->addr;
2633 msg.flags = client->flags & I2C_M_TEN;
2634 msg.flags |= I2C_M_RD;
2635 msg.len = count;
2636 msg.buf = buf;
2637
2638 ret = i2c_transfer(adap, &msg, 1);
2639
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002640 /*
2641 * If everything went ok (i.e. 1 msg received), return #bytes received,
2642 * else error code.
2643 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002644 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
David Brownellc0564602007-05-01 23:26:31 +02002646EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648/* ----------------------------------------------------
2649 * the i2c address scanning function
2650 * Will not work for 10-bit addresses!
2651 * ----------------------------------------------------
2652 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002653
Jean Delvare63e4e802010-06-03 11:33:51 +02002654/*
2655 * Legacy default probe function, mostly relevant for SMBus. The default
2656 * probe method is a quick write, but it is known to corrupt the 24RF08
2657 * EEPROMs due to a state machine bug, and could also irreversibly
2658 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2659 * we use a short byte read instead. Also, some bus drivers don't implement
2660 * quick write, so we fallback to a byte read in that case too.
2661 * On x86, there is another special case for FSC hardware monitoring chips,
2662 * which want regular byte reads (address 0x73.) Fortunately, these are the
2663 * only known chips using this I2C address on PC hardware.
2664 * Returns 1 if probe succeeded, 0 if not.
2665 */
2666static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2667{
2668 int err;
2669 union i2c_smbus_data dummy;
2670
2671#ifdef CONFIG_X86
2672 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2673 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2674 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2675 I2C_SMBUS_BYTE_DATA, &dummy);
2676 else
2677#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002678 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2679 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002680 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2681 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002682 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2683 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2684 I2C_SMBUS_BYTE, &dummy);
2685 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002686 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2687 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002688 err = -EOPNOTSUPP;
2689 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002690
2691 return err >= 0;
2692}
2693
Jean Delvareccfbbd02009-12-06 17:06:25 +01002694static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002695 struct i2c_driver *driver)
2696{
2697 struct i2c_board_info info;
2698 struct i2c_adapter *adapter = temp_client->adapter;
2699 int addr = temp_client->addr;
2700 int err;
2701
2702 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002703 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002704 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002705 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2706 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002707 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002708 }
2709
Wolfram Sang9bccc702015-07-17 14:48:56 +02002710 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002711 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002712 return 0;
2713
Jean Delvareccfbbd02009-12-06 17:06:25 +01002714 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002715 if (!i2c_default_probe(adapter, addr))
2716 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002717
2718 /* Finally call the custom detection function */
2719 memset(&info, 0, sizeof(struct i2c_board_info));
2720 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002721 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002722 if (err) {
2723 /* -ENODEV is returned if the detection fails. We catch it
2724 here as this isn't an error. */
2725 return err == -ENODEV ? 0 : err;
2726 }
2727
2728 /* Consistency check */
2729 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002730 dev_err(&adapter->dev,
2731 "%s detection function provided no name for 0x%x\n",
2732 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002733 } else {
2734 struct i2c_client *client;
2735
2736 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002737 if (adapter->class & I2C_CLASS_DEPRECATED)
2738 dev_warn(&adapter->dev,
2739 "This adapter will soon drop class based instantiation of devices. "
2740 "Please make sure client 0x%02x gets instantiated by other means. "
2741 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2742 info.addr);
2743
Jean Delvare4735c982008-07-14 22:38:36 +02002744 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2745 info.type, info.addr);
2746 client = i2c_new_device(adapter, &info);
2747 if (client)
2748 list_add_tail(&client->detected, &driver->clients);
2749 else
2750 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2751 info.type, info.addr);
2752 }
2753 return 0;
2754}
2755
2756static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2757{
Jean Delvarec3813d62009-12-14 21:17:25 +01002758 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002759 struct i2c_client *temp_client;
2760 int i, err = 0;
2761 int adap_id = i2c_adapter_id(adapter);
2762
Jean Delvarec3813d62009-12-14 21:17:25 +01002763 address_list = driver->address_list;
2764 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002765 return 0;
2766
Wolfram Sang45552272014-07-10 13:46:21 +02002767 /* Warn that the adapter lost class based instantiation */
2768 if (adapter->class == I2C_CLASS_DEPRECATED) {
2769 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002770 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2771 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002772 driver->driver.name);
2773 return 0;
2774 }
2775
Jean Delvare51b54ba2010-10-24 18:16:58 +02002776 /* Stop here if the classes do not match */
2777 if (!(adapter->class & driver->class))
2778 return 0;
2779
Jean Delvare4735c982008-07-14 22:38:36 +02002780 /* Set up a temporary client to help detect callback */
2781 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2782 if (!temp_client)
2783 return -ENOMEM;
2784 temp_client->adapter = adapter;
2785
Jean Delvarec3813d62009-12-14 21:17:25 +01002786 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002787 dev_dbg(&adapter->dev,
2788 "found normal entry for adapter %d, addr 0x%02x\n",
2789 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002790 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002791 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002792 if (unlikely(err))
2793 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002794 }
2795
Jean Delvare4735c982008-07-14 22:38:36 +02002796 kfree(temp_client);
2797 return err;
2798}
2799
Jean Delvared44f19d2010-08-11 18:20:57 +02002800int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2801{
2802 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2803 I2C_SMBUS_QUICK, NULL) >= 0;
2804}
2805EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2806
Jean Delvare12b5053a2007-05-01 23:26:31 +02002807struct i2c_client *
2808i2c_new_probed_device(struct i2c_adapter *adap,
2809 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002810 unsigned short const *addr_list,
2811 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002812{
2813 int i;
2814
Jean Delvare8031d792010-08-11 18:21:00 +02002815 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002816 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002817
Jean Delvare12b5053a2007-05-01 23:26:31 +02002818 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2819 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002820 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002821 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2822 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002823 continue;
2824 }
2825
Wolfram Sang9bccc702015-07-17 14:48:56 +02002826 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002827 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002828 dev_dbg(&adap->dev,
2829 "Address 0x%02x already in use, not probing\n",
2830 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002831 continue;
2832 }
2833
Jean Delvare63e4e802010-06-03 11:33:51 +02002834 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002835 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002836 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002837 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002838
2839 if (addr_list[i] == I2C_CLIENT_END) {
2840 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2841 return NULL;
2842 }
2843
2844 info->addr = addr_list[i];
2845 return i2c_new_device(adap, info);
2846}
2847EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2848
Jean Delvared735b342011-03-20 14:50:52 +01002849struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002852
Jean Delvarecaada322008-01-27 18:14:49 +01002853 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002854 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002855 if (!adapter)
2856 goto exit;
2857
2858 if (try_module_get(adapter->owner))
2859 get_device(&adapter->dev);
2860 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002861 adapter = NULL;
2862
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002863 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002864 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002865 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
David Brownellc0564602007-05-01 23:26:31 +02002867EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869void i2c_put_adapter(struct i2c_adapter *adap)
2870{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002871 if (!adap)
2872 return;
2873
2874 put_device(&adap->dev);
2875 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
David Brownellc0564602007-05-01 23:26:31 +02002877EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879/* The SMBus parts */
2880
David Brownell438d6c22006-12-10 21:21:31 +01002881#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002882static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002885
Farid Hammane7225acf2010-05-21 18:40:58 +02002886 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002887 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 data = data ^ POLY;
2889 data = data << 1;
2890 }
2891 return (u8)(data >> 8);
2892}
2893
Jean Delvare421ef472005-10-26 21:28:55 +02002894/* Incremental CRC8 over count bytes in the array pointed to by p */
2895static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 int i;
2898
Farid Hammane7225acf2010-05-21 18:40:58 +02002899 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002900 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return crc;
2902}
2903
Jean Delvare421ef472005-10-26 21:28:55 +02002904/* Assume a 7-bit address, which is reasonable for SMBus */
2905static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Jean Delvare421ef472005-10-26 21:28:55 +02002907 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02002908 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02002909 pec = i2c_smbus_pec(pec, &addr, 1);
2910
2911 /* The data buffer follows */
2912 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Jean Delvare421ef472005-10-26 21:28:55 +02002915/* Used for write only transactions */
2916static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Jean Delvare421ef472005-10-26 21:28:55 +02002918 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2919 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
Jean Delvare421ef472005-10-26 21:28:55 +02002922/* Return <0 on CRC error
2923 If there was a write before this read (most cases) we need to take the
2924 partial CRC from the write part into account.
2925 Note that this function does modify the message (we need to decrease the
2926 message length to hide the CRC byte from the caller). */
2927static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Jean Delvare421ef472005-10-26 21:28:55 +02002929 u8 rpec = msg->buf[--msg->len];
2930 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002933 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002935 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
David Brownell438d6c22006-12-10 21:21:31 +01002937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
David Brownella1cdeda2008-07-14 22:38:24 +02002940/**
2941 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2942 * @client: Handle to slave device
2943 *
2944 * This executes the SMBus "receive byte" protocol, returning negative errno
2945 * else the byte received from the device.
2946 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002947s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002950 int status;
2951
2952 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2953 I2C_SMBUS_READ, 0,
2954 I2C_SMBUS_BYTE, &data);
2955 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
David Brownellc0564602007-05-01 23:26:31 +02002957EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
David Brownella1cdeda2008-07-14 22:38:24 +02002959/**
2960 * i2c_smbus_write_byte - SMBus "send byte" protocol
2961 * @client: Handle to slave device
2962 * @value: Byte to be sent
2963 *
2964 * This executes the SMBus "send byte" protocol, returning negative errno
2965 * else zero on success.
2966 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002967s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Farid Hammane7225acf2010-05-21 18:40:58 +02002969 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002970 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
David Brownellc0564602007-05-01 23:26:31 +02002972EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
David Brownella1cdeda2008-07-14 22:38:24 +02002974/**
2975 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2976 * @client: Handle to slave device
2977 * @command: Byte interpreted by slave
2978 *
2979 * This executes the SMBus "read byte" protocol, returning negative errno
2980 * else a data byte received from the device.
2981 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002982s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002985 int status;
2986
2987 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2988 I2C_SMBUS_READ, command,
2989 I2C_SMBUS_BYTE_DATA, &data);
2990 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
David Brownellc0564602007-05-01 23:26:31 +02002992EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
David Brownella1cdeda2008-07-14 22:38:24 +02002994/**
2995 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2996 * @client: Handle to slave device
2997 * @command: Byte interpreted by slave
2998 * @value: Byte being written
2999 *
3000 * This executes the SMBus "write byte" protocol, returning negative errno
3001 * else zero on success.
3002 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003003s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3004 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 union i2c_smbus_data data;
3007 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003008 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3009 I2C_SMBUS_WRITE, command,
3010 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
David Brownellc0564602007-05-01 23:26:31 +02003012EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
David Brownella1cdeda2008-07-14 22:38:24 +02003014/**
3015 * i2c_smbus_read_word_data - SMBus "read word" protocol
3016 * @client: Handle to slave device
3017 * @command: Byte interpreted by slave
3018 *
3019 * This executes the SMBus "read word" protocol, returning negative errno
3020 * else a 16-bit unsigned "word" received from the device.
3021 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003022s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
3024 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003025 int status;
3026
3027 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3028 I2C_SMBUS_READ, command,
3029 I2C_SMBUS_WORD_DATA, &data);
3030 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
David Brownellc0564602007-05-01 23:26:31 +02003032EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
David Brownella1cdeda2008-07-14 22:38:24 +02003034/**
3035 * i2c_smbus_write_word_data - SMBus "write word" protocol
3036 * @client: Handle to slave device
3037 * @command: Byte interpreted by slave
3038 * @value: 16-bit "word" being written
3039 *
3040 * This executes the SMBus "write word" protocol, returning negative errno
3041 * else zero on success.
3042 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003043s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3044 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 union i2c_smbus_data data;
3047 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003048 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3049 I2C_SMBUS_WRITE, command,
3050 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
David Brownellc0564602007-05-01 23:26:31 +02003052EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
David Brownella64ec072007-10-13 23:56:31 +02003054/**
David Brownella1cdeda2008-07-14 22:38:24 +02003055 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003056 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003057 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003058 * @values: Byte array into which data will be read; big enough to hold
3059 * the data returned by the slave. SMBus allows at most 32 bytes.
3060 *
David Brownella1cdeda2008-07-14 22:38:24 +02003061 * This executes the SMBus "block read" protocol, returning negative errno
3062 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003063 *
3064 * Note that using this function requires that the client's adapter support
3065 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3066 * support this; its emulation through I2C messaging relies on a specific
3067 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3068 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003069s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003070 u8 *values)
3071{
3072 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003073 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003074
David Brownell24a5bb72008-07-14 22:38:23 +02003075 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3076 I2C_SMBUS_READ, command,
3077 I2C_SMBUS_BLOCK_DATA, &data);
3078 if (status)
3079 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003080
3081 memcpy(values, &data.block[1], data.block[0]);
3082 return data.block[0];
3083}
3084EXPORT_SYMBOL(i2c_smbus_read_block_data);
3085
David Brownella1cdeda2008-07-14 22:38:24 +02003086/**
3087 * i2c_smbus_write_block_data - SMBus "block write" protocol
3088 * @client: Handle to slave device
3089 * @command: Byte interpreted by slave
3090 * @length: Size of data block; SMBus allows at most 32 bytes
3091 * @values: Byte array which will be written.
3092 *
3093 * This executes the SMBus "block write" protocol, returning negative errno
3094 * else zero on success.
3095 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003096s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003097 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (length > I2C_SMBUS_BLOCK_MAX)
3102 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003104 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003105 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3106 I2C_SMBUS_WRITE, command,
3107 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
David Brownellc0564602007-05-01 23:26:31 +02003109EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003112s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003113 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
3115 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003116 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003117
Jean Delvare4b2643d2007-07-12 14:12:29 +02003118 if (length > I2C_SMBUS_BLOCK_MAX)
3119 length = I2C_SMBUS_BLOCK_MAX;
3120 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003121 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3122 I2C_SMBUS_READ, command,
3123 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3124 if (status < 0)
3125 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003126
3127 memcpy(values, &data.block[1], data.block[0]);
3128 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
David Brownellc0564602007-05-01 23:26:31 +02003130EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Jean Delvare0cc43a12011-01-10 22:11:23 +01003132s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003133 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003134{
3135 union i2c_smbus_data data;
3136
3137 if (length > I2C_SMBUS_BLOCK_MAX)
3138 length = I2C_SMBUS_BLOCK_MAX;
3139 data.block[0] = length;
3140 memcpy(data.block + 1, values, length);
3141 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3142 I2C_SMBUS_WRITE, command,
3143 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3144}
David Brownellc0564602007-05-01 23:26:31 +02003145EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003146
David Brownell438d6c22006-12-10 21:21:31 +01003147/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003149static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3150 unsigned short flags,
3151 char read_write, u8 command, int size,
3152 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
3154 /* So we need to generate a series of msgs. In the case of writing, we
3155 need to use only one message; when reading, we need two. We initialize
3156 most things with sane defaults, to keep the code below somewhat
3157 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003158 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3159 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003160 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003162 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003163 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003164 struct i2c_msg msg[2] = {
3165 {
3166 .addr = addr,
3167 .flags = flags,
3168 .len = 1,
3169 .buf = msgbuf0,
3170 }, {
3171 .addr = addr,
3172 .flags = flags | I2C_M_RD,
3173 .len = 0,
3174 .buf = msgbuf1,
3175 },
3176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003179 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 case I2C_SMBUS_QUICK:
3181 msg[0].len = 0;
3182 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003183 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3184 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 num = 1;
3186 break;
3187 case I2C_SMBUS_BYTE:
3188 if (read_write == I2C_SMBUS_READ) {
3189 /* Special case: only a read! */
3190 msg[0].flags = I2C_M_RD | flags;
3191 num = 1;
3192 }
3193 break;
3194 case I2C_SMBUS_BYTE_DATA:
3195 if (read_write == I2C_SMBUS_READ)
3196 msg[1].len = 1;
3197 else {
3198 msg[0].len = 2;
3199 msgbuf0[1] = data->byte;
3200 }
3201 break;
3202 case I2C_SMBUS_WORD_DATA:
3203 if (read_write == I2C_SMBUS_READ)
3204 msg[1].len = 2;
3205 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003206 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003208 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 break;
3211 case I2C_SMBUS_PROC_CALL:
3212 num = 2; /* Special case */
3213 read_write = I2C_SMBUS_READ;
3214 msg[0].len = 3;
3215 msg[1].len = 2;
3216 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003217 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 break;
3219 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003221 msg[1].flags |= I2C_M_RECV_LEN;
3222 msg[1].len = 1; /* block length will be added by
3223 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 } else {
3225 msg[0].len = data->block[0] + 2;
3226 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003227 dev_err(&adapter->dev,
3228 "Invalid block write size %d\n",
3229 data->block[0]);
3230 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003232 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 msgbuf0[i] = data->block[i-1];
3234 }
3235 break;
3236 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003237 num = 2; /* Another special case */
3238 read_write = I2C_SMBUS_READ;
3239 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003240 dev_err(&adapter->dev,
3241 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003242 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003243 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003244 }
3245 msg[0].len = data->block[0] + 2;
3246 for (i = 1; i < msg[0].len; i++)
3247 msgbuf0[i] = data->block[i-1];
3248 msg[1].flags |= I2C_M_RECV_LEN;
3249 msg[1].len = 1; /* block length will be added by
3250 the underlying bus driver */
3251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 case I2C_SMBUS_I2C_BLOCK_DATA:
Jeremy Compostella25550292017-11-15 12:31:44 -07003253 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
3254 dev_err(&adapter->dev, "Invalid block %s size %d\n",
3255 read_write == I2C_SMBUS_READ ? "read" : "write",
3256 data->block[0]);
3257 return -EINVAL;
3258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003260 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 } else {
3262 msg[0].len = data->block[0] + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 for (i = 1; i <= data->block[0]; i++)
3264 msgbuf0[i] = data->block[i];
3265 }
3266 break;
3267 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003268 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3269 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
3271
Jean Delvare421ef472005-10-26 21:28:55 +02003272 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3273 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3274 if (i) {
3275 /* Compute PEC if first message is a write */
3276 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003277 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003278 i2c_smbus_add_pec(&msg[0]);
3279 else /* Write followed by read */
3280 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3281 }
3282 /* Ask for PEC if last message is a read */
3283 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003284 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003285 }
3286
David Brownell24a5bb72008-07-14 22:38:23 +02003287 status = i2c_transfer(adapter, msg, num);
3288 if (status < 0)
3289 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Jean Delvare421ef472005-10-26 21:28:55 +02003291 /* Check PEC if last message is a read */
3292 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003293 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3294 if (status < 0)
3295 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003296 }
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003299 switch (size) {
3300 case I2C_SMBUS_BYTE:
3301 data->byte = msgbuf0[0];
3302 break;
3303 case I2C_SMBUS_BYTE_DATA:
3304 data->byte = msgbuf1[0];
3305 break;
3306 case I2C_SMBUS_WORD_DATA:
3307 case I2C_SMBUS_PROC_CALL:
3308 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3309 break;
3310 case I2C_SMBUS_I2C_BLOCK_DATA:
3311 for (i = 0; i < data->block[0]; i++)
3312 data->block[i+1] = msgbuf1[i];
3313 break;
3314 case I2C_SMBUS_BLOCK_DATA:
3315 case I2C_SMBUS_BLOCK_PROC_CALL:
3316 for (i = 0; i < msgbuf1[0] + 1; i++)
3317 data->block[i] = msgbuf1[i];
3318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 return 0;
3321}
3322
David Brownella1cdeda2008-07-14 22:38:24 +02003323/**
3324 * i2c_smbus_xfer - execute SMBus protocol operations
3325 * @adapter: Handle to I2C bus
3326 * @addr: Address of SMBus slave on that bus
3327 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3328 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3329 * @command: Byte interpreted by slave, for protocols which use such bytes
3330 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3331 * @data: Data to be read or written
3332 *
3333 * This executes an SMBus protocol operation, and returns a negative
3334 * errno code else zero on success.
3335 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003336s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003337 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003338 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003340 unsigned long orig_jiffies;
3341 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
David Howells8a325992014-03-06 13:36:06 +00003344 /* If enabled, the following two tracepoints are conditional on
3345 * read_write and protocol.
3346 */
3347 trace_smbus_write(adapter, addr, flags, read_write,
3348 command, protocol, data);
3349 trace_smbus_read(adapter, addr, flags, read_write,
3350 command, protocol);
3351
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003352 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003355 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003356
3357 /* Retry automatically on arbitration loss */
3358 orig_jiffies = jiffies;
3359 for (res = 0, try = 0; try <= adapter->retries; try++) {
3360 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3361 read_write, command,
3362 protocol, data);
3363 if (res != -EAGAIN)
3364 break;
3365 if (time_after(jiffies,
3366 orig_jiffies + adapter->timeout))
3367 break;
3368 }
Peter Rosin8320f492016-05-04 22:15:27 +02003369 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003371 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003372 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003373 /*
3374 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3375 * implement native support for the SMBus operation.
3376 */
3377 }
3378
David Howells8a325992014-03-06 13:36:06 +00003379 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3380 command, protocol, data);
3381
3382trace:
3383 /* If enabled, the reply tracepoint is conditional on read_write. */
3384 trace_smbus_reply(adapter, addr, flags, read_write,
3385 command, protocol, data);
3386 trace_smbus_result(adapter, addr, flags, read_write,
3387 command, protocol, res);
3388
3389 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Irina Tirdea01eef962015-08-12 17:31:33 +03003393/**
3394 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3395 * @client: Handle to slave device
3396 * @command: Byte interpreted by slave
3397 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3398 * @values: Byte array into which data will be read; big enough to hold
3399 * the data returned by the slave. SMBus allows at most
3400 * I2C_SMBUS_BLOCK_MAX bytes.
3401 *
3402 * This executes the SMBus "block read" protocol if supported by the adapter.
3403 * If block read is not supported, it emulates it using either word or byte
3404 * read protocols depending on availability.
3405 *
3406 * The addresses of the I2C slave device that are accessed with this function
3407 * must be mapped to a linear region, so that a block read will have the same
3408 * effect as a byte read. Before using this function you must double-check
3409 * if the I2C slave does support exchanging a block transfer with a byte
3410 * transfer.
3411 */
3412s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3413 u8 command, u8 length, u8 *values)
3414{
3415 u8 i = 0;
3416 int status;
3417
3418 if (length > I2C_SMBUS_BLOCK_MAX)
3419 length = I2C_SMBUS_BLOCK_MAX;
3420
3421 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3422 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3423
3424 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3425 return -EOPNOTSUPP;
3426
3427 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3428 while ((i + 2) <= length) {
3429 status = i2c_smbus_read_word_data(client, command + i);
3430 if (status < 0)
3431 return status;
3432 values[i] = status & 0xff;
3433 values[i + 1] = status >> 8;
3434 i += 2;
3435 }
3436 }
3437
3438 while (i < length) {
3439 status = i2c_smbus_read_byte_data(client, command + i);
3440 if (status < 0)
3441 return status;
3442 values[i] = status;
3443 i++;
3444 }
3445
3446 return i;
3447}
3448EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3449
Jean Delvared5fd1202015-01-26 20:59:31 +01003450#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003451int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3452{
3453 int ret;
3454
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003455 if (!client || !slave_cb) {
3456 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003457 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003458 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003459
Wolfram Sangc6909d62015-08-05 21:12:54 +02003460 if (!(client->flags & I2C_CLIENT_SLAVE))
3461 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3462 __func__);
3463
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003464 if (!(client->flags & I2C_CLIENT_TEN)) {
3465 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003466 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003467 if (ret) {
3468 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003469 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003470 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003471 }
3472
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003473 if (!client->adapter->algo->reg_slave) {
3474 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003475 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003476 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003477
3478 client->slave_cb = slave_cb;
3479
3480 i2c_lock_adapter(client->adapter);
3481 ret = client->adapter->algo->reg_slave(client);
3482 i2c_unlock_adapter(client->adapter);
3483
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003484 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003485 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003486 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3487 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003488
3489 return ret;
3490}
3491EXPORT_SYMBOL_GPL(i2c_slave_register);
3492
3493int i2c_slave_unregister(struct i2c_client *client)
3494{
3495 int ret;
3496
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003497 if (!client->adapter->algo->unreg_slave) {
3498 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003499 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003500 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003501
3502 i2c_lock_adapter(client->adapter);
3503 ret = client->adapter->algo->unreg_slave(client);
3504 i2c_unlock_adapter(client->adapter);
3505
3506 if (ret == 0)
3507 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003508 else
3509 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003510
3511 return ret;
3512}
3513EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003514#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3517MODULE_DESCRIPTION("I2C-Bus main module");
3518MODULE_LICENSE("GPL");