blob: 461451da106504f46f90d6d2ccdeed4a84ded1e3 [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>.
Michael Lawnick08263742010-08-11 18:21:02 +020017 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010018 Michael Lawnick <michael.lawnick.ext@nsn.com>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020019 I2C ACPI code Copyright (C) 2014 Intel Corp
20 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Wolfram Sang44239a52016-07-09 13:35:04 +090023#define pr_fmt(fmt) "i2c-core: " fmt
24
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020025#include <dt-bindings/i2c/i2c.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010027#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020028#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010029#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010030#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020031#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010032#include <linux/errno.h>
33#include <linux/gpio.h>
34#include <linux/hardirq.h>
35#include <linux/i2c.h>
36#include <linux/idr.h>
37#include <linux/init.h>
38#include <linux/irqflags.h>
39#include <linux/jump_label.h>
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/mutex.h>
43#include <linux/of_device.h>
44#include <linux/of.h>
45#include <linux/of_irq.h>
46#include <linux/pm_domain.h>
47#include <linux/pm_runtime.h>
48#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010049#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010050#include <linux/rwsem.h>
51#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
David Howellsd9a83d62014-03-06 13:35:59 +000055#define CREATE_TRACE_POINTS
56#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Wolfram Sangda899f52015-05-18 21:09:12 +020058#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
59#define I2C_ADDR_OFFSET_SLAVE 0x1000
60
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020061#define I2C_ADDR_7BITS_MAX 0x77
62#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
63
Jean Delvare6629dcf2010-05-04 11:09:28 +020064/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020065 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000066 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010067static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_IDR(i2c_adapter_idr);
69
Jean Delvare4735c982008-07-14 22:38:36 +020070static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010071
David Howellsd9a83d62014-03-06 13:35:59 +000072static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053073static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000074
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050075int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000076{
77 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050078 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000079}
80
81void i2c_transfer_trace_unreg(void)
82{
83 static_key_slow_dec(&i2c_trace_msg);
84}
85
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020086#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030087struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020088 struct acpi_connection_info info;
89 struct i2c_adapter *adapter;
90};
91
92struct gsb_buffer {
93 u8 status;
94 u8 len;
95 union {
96 u16 wdata;
97 u8 bdata;
98 u8 data[0];
99 };
100} __packed;
101
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300102struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300103 struct i2c_board_info *info;
104 acpi_handle adapter_handle;
105 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300106 acpi_handle search_handle;
Hans de Goede417f7842017-04-05 00:03:32 +0200107 int n;
108 int index;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300109 u32 speed;
110 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300111};
112
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300113static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200114{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300115 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300116 struct i2c_board_info *info = lookup->info;
117 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300118 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200119
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300120 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
121 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200122
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300123 sb = &ares->data.i2c_serial_bus;
124 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
125 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200126
Hans de Goede417f7842017-04-05 00:03:32 +0200127 if (lookup->index != -1 && lookup->n++ != lookup->index)
128 return 1;
129
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300130 status = acpi_get_handle(lookup->device_handle,
131 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300132 &lookup->adapter_handle);
133 if (!ACPI_SUCCESS(status))
134 return 1;
135
136 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300137 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300138 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
139 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200140
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200141 return 1;
142}
143
Jarkko Nikula5853b222016-08-12 17:02:53 +0300144static int i2c_acpi_do_lookup(struct acpi_device *adev,
145 struct i2c_acpi_lookup *lookup)
146{
147 struct i2c_board_info *info = lookup->info;
148 struct list_head resource_list;
149 int ret;
150
151 if (acpi_bus_get_status(adev) || !adev->status.present ||
152 acpi_device_enumerated(adev))
153 return -EINVAL;
154
155 memset(info, 0, sizeof(*info));
156 lookup->device_handle = acpi_device_handle(adev);
157
158 /* Look up for I2cSerialBus resource */
159 INIT_LIST_HEAD(&resource_list);
160 ret = acpi_dev_get_resources(adev, &resource_list,
161 i2c_acpi_fill_info, lookup);
162 acpi_dev_free_resource_list(&resource_list);
163
164 if (ret < 0 || !info->addr)
165 return -EINVAL;
166
167 return 0;
168}
169
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300170static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300171 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300172 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300173 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200174{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200175 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300176 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300177 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200178 int ret;
179
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300180 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300181 lookup.info = info;
Hans de Goede417f7842017-04-05 00:03:32 +0200182 lookup.index = -1;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300183
Jarkko Nikula5853b222016-08-12 17:02:53 +0300184 ret = i2c_acpi_do_lookup(adev, &lookup);
185 if (ret)
186 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200187
Mika Westerberg318ce272016-09-20 16:59:25 +0300188 if (adapter) {
189 /* The adapter must match the one in I2cSerialBus() connector */
190 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
191 return -ENODEV;
192 } else {
193 struct acpi_device *adapter_adev;
194
195 /* The adapter must be present */
196 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
197 return -ENODEV;
198 if (acpi_bus_get_status(adapter_adev) ||
199 !adapter_adev->status.present)
200 return -ENODEV;
201 }
202
Jarkko Nikula5853b222016-08-12 17:02:53 +0300203 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300204 if (adapter_handle)
205 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200206
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300207 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300208 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300209 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
210 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300211 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300212
213 resource_list_for_each_entry(entry, &resource_list) {
214 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300215 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300216 break;
217 }
218 }
219
220 acpi_dev_free_resource_list(&resource_list);
221
Dan O'Donovan622b3072017-02-05 16:30:13 +0000222 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
223 sizeof(info->type));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300224
225 return 0;
226}
227
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300228static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300229 struct acpi_device *adev,
230 struct i2c_board_info *info)
231{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200232 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300233 acpi_device_set_enumerated(adev);
234
235 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200236 adev->power.flags.ignore_parent = false;
237 dev_err(&adapter->dev,
238 "failed to add I2C device %s from ACPI\n",
239 dev_name(&adev->dev));
240 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300241}
242
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300243static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300244 void *data, void **return_value)
245{
246 struct i2c_adapter *adapter = data;
247 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300248 struct i2c_board_info info;
249
250 if (acpi_bus_get_device(handle, &adev))
251 return AE_OK;
252
Mika Westerberg318ce272016-09-20 16:59:25 +0300253 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300254 return AE_OK;
255
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300256 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200257
258 return AE_OK;
259}
260
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300261#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300262
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200263/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300264 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 * @adap: pointer to adapter
266 *
267 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
268 * namespace. When a device is found it will be added to the Linux device
269 * model and bound to the corresponding ACPI handle.
270 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300271static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200272{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200273 acpi_status status;
274
Dustin Byford8eb5c872015-10-23 12:27:07 -0700275 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200276 return;
277
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300278 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300279 I2C_ACPI_MAX_SCAN_DEPTH,
280 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200281 adap, NULL);
282 if (ACPI_FAILURE(status))
283 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
284}
285
Jarkko Nikula5853b222016-08-12 17:02:53 +0300286static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
287 void *data, void **return_value)
288{
289 struct i2c_acpi_lookup *lookup = data;
290 struct acpi_device *adev;
291
292 if (acpi_bus_get_device(handle, &adev))
293 return AE_OK;
294
295 if (i2c_acpi_do_lookup(adev, lookup))
296 return AE_OK;
297
298 if (lookup->search_handle != lookup->adapter_handle)
299 return AE_OK;
300
301 if (lookup->speed <= lookup->min_speed)
302 lookup->min_speed = lookup->speed;
303
304 return AE_OK;
305}
306
307/**
308 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
309 * @dev: The device owning the bus
310 *
311 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
312 * devices connected to this bus and use the speed of slowest device.
313 *
314 * Returns the speed in Hz or zero
315 */
316u32 i2c_acpi_find_bus_speed(struct device *dev)
317{
318 struct i2c_acpi_lookup lookup;
319 struct i2c_board_info dummy;
320 acpi_status status;
321
322 if (!has_acpi_companion(dev))
323 return 0;
324
325 memset(&lookup, 0, sizeof(lookup));
326 lookup.search_handle = ACPI_HANDLE(dev);
327 lookup.min_speed = UINT_MAX;
328 lookup.info = &dummy;
Hans de Goede417f7842017-04-05 00:03:32 +0200329 lookup.index = -1;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300330
331 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
332 I2C_ACPI_MAX_SCAN_DEPTH,
333 i2c_acpi_lookup_speed, NULL,
334 &lookup, NULL);
335
336 if (ACPI_FAILURE(status)) {
337 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
338 return 0;
339 }
340
341 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
342}
343EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
344
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300345static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300346{
347 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
348
349 if (!adapter)
350 return 0;
351
352 return ACPI_HANDLE(dev) == (acpi_handle)data;
353}
354
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300355static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300356{
357 return ACPI_COMPANION(dev) == data;
358}
359
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300360static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300361{
362 struct device *dev;
363
364 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300365 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300366 return dev ? i2c_verify_adapter(dev) : NULL;
367}
368
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300369static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300370{
371 struct device *dev;
372
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300373 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300374 return dev ? i2c_verify_client(dev) : NULL;
375}
376
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300377static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300378 void *arg)
379{
380 struct acpi_device *adev = arg;
381 struct i2c_board_info info;
382 acpi_handle adapter_handle;
383 struct i2c_adapter *adapter;
384 struct i2c_client *client;
385
386 switch (value) {
387 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300388 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300389 break;
390
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300391 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300392 if (!adapter)
393 break;
394
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300395 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300396 break;
397 case ACPI_RECONFIG_DEVICE_REMOVE:
398 if (!acpi_device_enumerated(adev))
399 break;
400
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300401 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300402 if (!client)
403 break;
404
405 i2c_unregister_device(client);
406 put_device(&client->dev);
407 break;
408 }
409
410 return NOTIFY_OK;
411}
412
413static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300414 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300415};
Hans de Goede605f8fc2017-04-05 00:03:33 +0200416
417/**
418 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
419 * @dev: Device owning the ACPI resources to get the client from
420 * @index: Index of ACPI resource to get
421 * @info: describes the I2C device; note this is modified (addr gets set)
422 * Context: can sleep
423 *
424 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
425 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
426 * resources, in that case this function can be used to create an i2c-client
427 * for other I2cSerialBus resources in the Current Resource Settings table.
428 *
429 * Also see i2c_new_device, which this function calls to create the i2c-client.
430 *
431 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
432 */
433struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
434 struct i2c_board_info *info)
435{
436 struct i2c_acpi_lookup lookup;
437 struct i2c_adapter *adapter;
438 struct acpi_device *adev;
439 LIST_HEAD(resource_list);
440 int ret;
441
442 adev = ACPI_COMPANION(dev);
443 if (!adev)
444 return NULL;
445
446 memset(&lookup, 0, sizeof(lookup));
447 lookup.info = info;
448 lookup.device_handle = acpi_device_handle(adev);
449 lookup.index = index;
450
451 ret = acpi_dev_get_resources(adev, &resource_list,
452 i2c_acpi_fill_info, &lookup);
453 acpi_dev_free_resource_list(&resource_list);
454
455 if (ret < 0 || !info->addr)
456 return NULL;
457
458 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
459 if (!adapter)
460 return NULL;
461
462 return i2c_new_device(adapter, info);
463}
464EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200465#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300466static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300467extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200468#endif /* CONFIG_ACPI */
469
470#ifdef CONFIG_ACPI_I2C_OPREGION
471static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
472 u8 cmd, u8 *data, u8 data_len)
473{
474
475 struct i2c_msg msgs[2];
476 int ret;
477 u8 *buffer;
478
479 buffer = kzalloc(data_len, GFP_KERNEL);
480 if (!buffer)
481 return AE_NO_MEMORY;
482
483 msgs[0].addr = client->addr;
484 msgs[0].flags = client->flags;
485 msgs[0].len = 1;
486 msgs[0].buf = &cmd;
487
488 msgs[1].addr = client->addr;
489 msgs[1].flags = client->flags | I2C_M_RD;
490 msgs[1].len = data_len;
491 msgs[1].buf = buffer;
492
493 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
494 if (ret < 0)
495 dev_err(&client->adapter->dev, "i2c read failed\n");
496 else
497 memcpy(data, buffer, data_len);
498
499 kfree(buffer);
500 return ret;
501}
502
503static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
504 u8 cmd, u8 *data, u8 data_len)
505{
506
507 struct i2c_msg msgs[1];
508 u8 *buffer;
509 int ret = AE_OK;
510
511 buffer = kzalloc(data_len + 1, GFP_KERNEL);
512 if (!buffer)
513 return AE_NO_MEMORY;
514
515 buffer[0] = cmd;
516 memcpy(buffer + 1, data, data_len);
517
518 msgs[0].addr = client->addr;
519 msgs[0].flags = client->flags;
520 msgs[0].len = data_len + 1;
521 msgs[0].buf = buffer;
522
523 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
524 if (ret < 0)
525 dev_err(&client->adapter->dev, "i2c write failed\n");
526
527 kfree(buffer);
528 return ret;
529}
530
531static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300532i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200533 u32 bits, u64 *value64,
534 void *handler_context, void *region_context)
535{
536 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300537 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200538 struct acpi_connection_info *info = &data->info;
539 struct acpi_resource_i2c_serialbus *sb;
540 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300541 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200542 struct acpi_resource *ares;
543 u32 accessor_type = function >> 16;
544 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100545 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200546 int status;
547
548 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
549 if (ACPI_FAILURE(ret))
550 return ret;
551
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300552 client = kzalloc(sizeof(*client), GFP_KERNEL);
553 if (!client) {
554 ret = AE_NO_MEMORY;
555 goto err;
556 }
557
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200558 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
559 ret = AE_BAD_PARAMETER;
560 goto err;
561 }
562
563 sb = &ares->data.i2c_serial_bus;
564 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
565 ret = AE_BAD_PARAMETER;
566 goto err;
567 }
568
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300569 client->adapter = adapter;
570 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200571
572 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300573 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200574
575 switch (accessor_type) {
576 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
577 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300578 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200579 if (status >= 0) {
580 gsb->bdata = status;
581 status = 0;
582 }
583 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300584 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200585 }
586 break;
587
588 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
589 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300590 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200591 if (status >= 0) {
592 gsb->bdata = status;
593 status = 0;
594 }
595 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300596 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200597 gsb->bdata);
598 }
599 break;
600
601 case ACPI_GSB_ACCESS_ATTRIB_WORD:
602 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300603 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200604 if (status >= 0) {
605 gsb->wdata = status;
606 status = 0;
607 }
608 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300609 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200610 gsb->wdata);
611 }
612 break;
613
614 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
615 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300616 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200617 gsb->data);
618 if (status >= 0) {
619 gsb->len = status;
620 status = 0;
621 }
622 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300623 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200624 gsb->len, gsb->data);
625 }
626 break;
627
628 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
629 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300630 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200631 gsb->data, info->access_length);
632 if (status > 0)
633 status = 0;
634 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300635 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200636 gsb->data, info->access_length);
637 }
638 break;
639
640 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900641 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
642 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200643 ret = AE_BAD_PARAMETER;
644 goto err;
645 }
646
647 gsb->status = status;
648
649 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300650 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200651 ACPI_FREE(ares);
652 return ret;
653}
654
655
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300656static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200657{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200658 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300659 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200660 acpi_status status;
661
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200662 if (!adapter->dev.parent)
663 return -ENODEV;
664
665 handle = ACPI_HANDLE(adapter->dev.parent);
666
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200667 if (!handle)
668 return -ENODEV;
669
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300670 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200671 GFP_KERNEL);
672 if (!data)
673 return -ENOMEM;
674
675 data->adapter = adapter;
676 status = acpi_bus_attach_private_data(handle, (void *)data);
677 if (ACPI_FAILURE(status)) {
678 kfree(data);
679 return -ENOMEM;
680 }
681
682 status = acpi_install_address_space_handler(handle,
683 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300684 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200685 NULL,
686 data);
687 if (ACPI_FAILURE(status)) {
688 dev_err(&adapter->dev, "Error installing i2c space handler\n");
689 acpi_bus_detach_private_data(handle);
690 kfree(data);
691 return -ENOMEM;
692 }
693
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800694 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200695 return 0;
696}
697
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300698static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200699{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200700 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300701 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200702 acpi_status status;
703
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200704 if (!adapter->dev.parent)
705 return;
706
707 handle = ACPI_HANDLE(adapter->dev.parent);
708
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200709 if (!handle)
710 return;
711
712 acpi_remove_address_space_handler(handle,
713 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300714 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200715
716 status = acpi_bus_get_private_data(handle, (void **)&data);
717 if (ACPI_SUCCESS(status))
718 kfree(data);
719
720 acpi_bus_detach_private_data(handle);
721}
722#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300723static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200724{ }
725
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300726static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200727{ return 0; }
728#endif /* CONFIG_ACPI_I2C_OPREGION */
729
David Brownellf37dd802007-02-13 22:09:00 +0100730/* ------------------------------------------------------------------------- */
731
Lee Jones5f441fc2016-11-07 12:47:40 +0000732const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +0200733 const struct i2c_client *client)
734{
Lee Jones811073b2016-11-07 12:47:36 +0000735 if (!(id && client))
736 return NULL;
737
Jean Delvared2653e92008-04-29 23:11:39 +0200738 while (id->name[0]) {
739 if (strcmp(client->name, id->name) == 0)
740 return id;
741 id++;
742 }
743 return NULL;
744}
Lee Jones5f441fc2016-11-07 12:47:40 +0000745EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int i2c_device_match(struct device *dev, struct device_driver *drv)
748{
Jean Delvare51298d12009-09-18 22:45:45 +0200749 struct i2c_client *client = i2c_verify_client(dev);
750 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200751
Jean Delvare51298d12009-09-18 22:45:45 +0200752
Grant Likely959e85f2010-06-08 07:48:19 -0600753 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000754 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600755 return 1;
756
Mika Westerberg907ddf82012-11-23 12:23:40 +0100757 /* Then ACPI style match */
758 if (acpi_driver_match_device(dev, drv))
759 return 1;
760
Jean Delvare51298d12009-09-18 22:45:45 +0200761 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000762
763 /* Finally an I2C match */
764 if (i2c_match_id(driver->id_table, client))
765 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200766
Jean Delvareeb8a7902008-05-18 20:49:41 +0200767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Kay Sievers7eff2e72007-08-14 15:15:12 +0200770static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200771{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100772 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800773 int rc;
774
775 rc = acpi_device_uevent_modalias(dev, env);
776 if (rc != -ENODEV)
777 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200778
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100779 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200780}
781
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530782/* i2c bus recovery routines */
783static int get_scl_gpio_value(struct i2c_adapter *adap)
784{
785 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
786}
787
788static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
789{
790 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
791}
792
793static int get_sda_gpio_value(struct i2c_adapter *adap)
794{
795 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
796}
797
798static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
799{
800 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
801 struct device *dev = &adap->dev;
802 int ret = 0;
803
804 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
805 GPIOF_OUT_INIT_HIGH, "i2c-scl");
806 if (ret) {
807 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
808 return ret;
809 }
810
811 if (bri->get_sda) {
812 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
813 /* work without SDA polling */
814 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
815 bri->sda_gpio);
816 bri->get_sda = NULL;
817 }
818 }
819
820 return ret;
821}
822
823static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
824{
825 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
826
827 if (bri->get_sda)
828 gpio_free(bri->sda_gpio);
829
830 gpio_free(bri->scl_gpio);
831}
832
833/*
834 * We are generating clock pulses. ndelay() determines durating of clk pulses.
835 * We will generate clock with rate 100 KHz and so duration of both clock levels
836 * is: delay in ns = (10^6 / 100) / 2
837 */
838#define RECOVERY_NDELAY 5000
839#define RECOVERY_CLK_CNT 9
840
841static int i2c_generic_recovery(struct i2c_adapter *adap)
842{
843 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
844 int i = 0, val = 1, ret = 0;
845
846 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300847 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530848
Jan Luebbe8b062602015-07-08 16:35:06 +0200849 bri->set_scl(adap, val);
850 ndelay(RECOVERY_NDELAY);
851
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530852 /*
853 * By this time SCL is high, as we need to give 9 falling-rising edges
854 */
855 while (i++ < RECOVERY_CLK_CNT * 2) {
856 if (val) {
857 /* Break if SDA is high */
858 if (bri->get_sda && bri->get_sda(adap))
859 break;
860 /* SCL shouldn't be low here */
861 if (!bri->get_scl(adap)) {
862 dev_err(&adap->dev,
863 "SCL is stuck low, exit recovery\n");
864 ret = -EBUSY;
865 break;
866 }
867 }
868
869 val = !val;
870 bri->set_scl(adap, val);
871 ndelay(RECOVERY_NDELAY);
872 }
873
874 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300875 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530876
877 return ret;
878}
879
880int i2c_generic_scl_recovery(struct i2c_adapter *adap)
881{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530882 return i2c_generic_recovery(adap);
883}
Mark Brownc1c21f42015-04-15 19:18:39 +0100884EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530885
886int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
887{
888 int ret;
889
890 ret = i2c_get_gpios_for_recovery(adap);
891 if (ret)
892 return ret;
893
894 ret = i2c_generic_recovery(adap);
895 i2c_put_gpios_for_recovery(adap);
896
897 return ret;
898}
Mark Brownc1c21f42015-04-15 19:18:39 +0100899EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530900
901int i2c_recover_bus(struct i2c_adapter *adap)
902{
903 if (!adap->bus_recovery_info)
904 return -EOPNOTSUPP;
905
906 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
907 return adap->bus_recovery_info->recover_bus(adap);
908}
Mark Brownc1c21f42015-04-15 19:18:39 +0100909EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530910
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900911static void i2c_init_recovery(struct i2c_adapter *adap)
912{
913 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
914 char *err_str;
915
916 if (!bri)
917 return;
918
919 if (!bri->recover_bus) {
920 err_str = "no recover_bus() found";
921 goto err;
922 }
923
924 /* Generic GPIO recovery */
925 if (bri->recover_bus == i2c_generic_gpio_recovery) {
926 if (!gpio_is_valid(bri->scl_gpio)) {
927 err_str = "invalid SCL gpio";
928 goto err;
929 }
930
931 if (gpio_is_valid(bri->sda_gpio))
932 bri->get_sda = get_sda_gpio_value;
933 else
934 bri->get_sda = NULL;
935
936 bri->get_scl = get_scl_gpio_value;
937 bri->set_scl = set_scl_gpio_value;
938 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
939 /* Generic SCL recovery */
940 if (!bri->set_scl || !bri->get_scl) {
941 err_str = "no {get|set}_scl() found";
942 goto err;
943 }
944 }
945
946 return;
947 err:
948 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
949 adap->bus_recovery_info = NULL;
950}
951
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200952static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
953{
954 struct i2c_adapter *adap = client->adapter;
955 unsigned int irq;
956
957 if (!adap->host_notify_domain)
958 return -ENXIO;
959
960 if (client->flags & I2C_CLIENT_TEN)
961 return -EINVAL;
962
963 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
964 if (!irq)
965 irq = irq_create_mapping(adap->host_notify_domain,
966 client->addr);
967
968 return irq > 0 ? irq : -ENXIO;
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static int i2c_device_probe(struct device *dev)
972{
Jean Delvare51298d12009-09-18 22:45:45 +0200973 struct i2c_client *client = i2c_verify_client(dev);
974 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100975 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200976
Jean Delvare51298d12009-09-18 22:45:45 +0200977 if (!client)
978 return 0;
979
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200980 driver = to_i2c_driver(dev->driver);
981
982 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300983 int irq = -ENOENT;
984
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800985 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
986 dev_dbg(dev, "Using Host Notify IRQ\n");
987 irq = i2c_smbus_host_notify_to_irq(client);
988 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700989 irq = of_irq_get_byname(dev->of_node, "irq");
990 if (irq == -EINVAL || irq == -ENODATA)
991 irq = of_irq_get(dev->of_node, 0);
992 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300993 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700994 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100995 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200996 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800997
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100998 if (irq < 0)
999 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +02001000
1001 client->irq = irq;
1002 }
1003
Lee Jonesda10c062016-11-07 12:47:39 +00001004 /*
1005 * An I2C ID table is not mandatory, if and only if, a suitable Device
1006 * Tree match table entry is supplied for the probing device.
1007 */
1008 if (!driver->id_table &&
1009 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +02001010 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001011
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001012 if (client->flags & I2C_CLIENT_WAKE) {
1013 int wakeirq = -ENOENT;
1014
1015 if (dev->of_node) {
1016 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
1017 if (wakeirq == -EPROBE_DEFER)
1018 return wakeirq;
1019 }
1020
1021 device_init_wakeup(&client->dev, true);
1022
1023 if (wakeirq > 0 && wakeirq != client->irq)
1024 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
1025 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +02001026 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001027 else
1028 status = 0;
1029
1030 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001031 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001032 }
1033
David Brownell7b4fbc52007-05-01 23:26:30 +02001034 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +02001035
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02001036 status = of_clk_set_defaults(dev->of_node, false);
1037 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001038 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02001039
Ulf Hanssone09b0d42014-09-19 20:27:39 +02001040 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +01001041 if (status == -EPROBE_DEFER)
1042 goto err_clear_wakeup_irq;
1043
Lee Jonesb8a1a4c2016-11-07 12:47:41 +00001044 /*
1045 * When there are no more users of probe(),
1046 * rename probe_new to probe.
1047 */
1048 if (driver->probe_new)
1049 status = driver->probe_new(client);
1050 else if (driver->probe)
1051 status = driver->probe(client,
1052 i2c_match_id(driver->id_table, client));
1053 else
1054 status = -EINVAL;
1055
Kieran Bingham74cedd32015-10-12 21:54:43 +01001056 if (status)
1057 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001058
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001059 return 0;
1060
1061err_detach_pm_domain:
1062 dev_pm_domain_detach(&client->dev, true);
1063err_clear_wakeup_irq:
1064 dev_pm_clear_wake_irq(&client->dev);
1065 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +01001066 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static int i2c_device_remove(struct device *dev)
1070{
Jean Delvare51298d12009-09-18 22:45:45 +02001071 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +02001072 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001073 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +02001074
Jean Delvare51298d12009-09-18 22:45:45 +02001075 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +02001076 return 0;
1077
1078 driver = to_i2c_driver(dev->driver);
1079 if (driver->remove) {
1080 dev_dbg(dev, "remove\n");
1081 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +02001082 }
Wolfram Sang72fa8182014-01-21 17:48:34 +01001083
Ulf Hanssone09b0d42014-09-19 20:27:39 +02001084 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001085
1086 dev_pm_clear_wake_irq(&client->dev);
1087 device_init_wakeup(&client->dev, false);
1088
David Brownella1d9e6e2007-05-01 23:26:30 +02001089 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
David Brownellf37dd802007-02-13 22:09:00 +01001092static void i2c_device_shutdown(struct device *dev)
1093{
Jean Delvare51298d12009-09-18 22:45:45 +02001094 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001095 struct i2c_driver *driver;
1096
Jean Delvare51298d12009-09-18 22:45:45 +02001097 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001098 return;
1099 driver = to_i2c_driver(dev->driver);
1100 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001101 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001102}
1103
David Brownell9c1600e2007-05-01 23:26:31 +02001104static void i2c_client_dev_release(struct device *dev)
1105{
1106 kfree(to_i2c_client(dev));
1107}
1108
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001109static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001110show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001111{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001112 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1113 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001114}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001115static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001116
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001117static ssize_t
1118show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001119{
1120 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001121 int len;
1122
1123 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1124 if (len != -ENODEV)
1125 return len;
1126
Jean Delvareeb8a7902008-05-18 20:49:41 +02001127 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001128}
Jean Delvare51298d12009-09-18 22:45:45 +02001129static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1130
1131static struct attribute *i2c_dev_attrs[] = {
1132 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001133 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001134 &dev_attr_modalias.attr,
1135 NULL
1136};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001137ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001138
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001139struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001140 .name = "i2c",
1141 .match = i2c_device_match,
1142 .probe = i2c_device_probe,
1143 .remove = i2c_device_remove,
1144 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001145};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001146EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001147
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001148struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001149 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001150 .uevent = i2c_device_uevent,
1151 .release = i2c_client_dev_release,
1152};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001153EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +02001154
David Brownell9b766b82008-01-27 18:14:51 +01001155
1156/**
1157 * i2c_verify_client - return parameter as i2c_client, or NULL
1158 * @dev: device, probably from some driver model iterator
1159 *
1160 * When traversing the driver model tree, perhaps using driver model
1161 * iterators like @device_for_each_child(), you can't assume very much
1162 * about the nodes you find. Use this function to avoid oopses caused
1163 * by wrongly treating some non-I2C device as an i2c_client.
1164 */
1165struct i2c_client *i2c_verify_client(struct device *dev)
1166{
Jean Delvare51298d12009-09-18 22:45:45 +02001167 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001168 ? to_i2c_client(dev)
1169 : NULL;
1170}
1171EXPORT_SYMBOL(i2c_verify_client);
1172
1173
Wolfram Sangda899f52015-05-18 21:09:12 +02001174/* Return a unique address which takes the flags of the client into account */
1175static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1176{
1177 unsigned short addr = client->addr;
1178
1179 /* For some client flags, add an arbitrary offset to avoid collisions */
1180 if (client->flags & I2C_CLIENT_TEN)
1181 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1182
1183 if (client->flags & I2C_CLIENT_SLAVE)
1184 addr |= I2C_ADDR_OFFSET_SLAVE;
1185
1186 return addr;
1187}
1188
Jean Delvare3a89db52010-06-03 11:33:52 +02001189/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001190 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +02001191int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001192{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001193 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001194 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001195 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001196 return -EINVAL;
1197 } else {
1198 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001199 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001200 return -EINVAL;
1201 }
1202 return 0;
1203}
1204
Jean Delvare656b8762010-06-03 11:33:53 +02001205/* And this is a strict address validity check, used when probing. If a
1206 * device uses a reserved address, then it shouldn't be probed. 7-bit
1207 * addressing is assumed, 10-bit address devices are rare and should be
1208 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +02001209int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001210{
1211 /*
1212 * Reserved addresses per I2C specification:
1213 * 0x00 General call address / START byte
1214 * 0x01 CBUS address
1215 * 0x02 Reserved for different bus format
1216 * 0x03 Reserved for future purposes
1217 * 0x04-0x07 Hs-mode master code
1218 * 0x78-0x7b 10-bit slave addressing
1219 * 0x7c-0x7f Reserved for future purposes
1220 */
1221 if (addr < 0x08 || addr > 0x77)
1222 return -EINVAL;
1223 return 0;
1224}
1225
Jean Delvare3b5f7942010-06-03 11:33:55 +02001226static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1227{
1228 struct i2c_client *client = i2c_verify_client(dev);
1229 int addr = *(int *)addrp;
1230
Wolfram Sang9bccc702015-07-17 14:48:56 +02001231 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001232 return -EBUSY;
1233 return 0;
1234}
1235
Michael Lawnick08263742010-08-11 18:21:02 +02001236/* walk up mux tree */
1237static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1238{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001239 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001240 int result;
1241
1242 result = device_for_each_child(&adapter->dev, &addr,
1243 __i2c_check_addr_busy);
1244
Jean Delvare97cc4d42010-10-24 18:16:57 +02001245 if (!result && parent)
1246 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001247
1248 return result;
1249}
1250
1251/* recurse down mux tree */
1252static int i2c_check_mux_children(struct device *dev, void *addrp)
1253{
1254 int result;
1255
1256 if (dev->type == &i2c_adapter_type)
1257 result = device_for_each_child(dev, addrp,
1258 i2c_check_mux_children);
1259 else
1260 result = __i2c_check_addr_busy(dev, addrp);
1261
1262 return result;
1263}
1264
Jean Delvare3b5f7942010-06-03 11:33:55 +02001265static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1266{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001267 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001268 int result = 0;
1269
Jean Delvare97cc4d42010-10-24 18:16:57 +02001270 if (parent)
1271 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001272
1273 if (!result)
1274 result = device_for_each_child(&adapter->dev, &addr,
1275 i2c_check_mux_children);
1276
1277 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001278}
1279
David Brownell9c1600e2007-05-01 23:26:31 +02001280/**
Peter Rosin8320f492016-05-04 22:15:27 +02001281 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001282 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001283 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1284 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001285 */
Peter Rosin8320f492016-05-04 22:15:27 +02001286static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1287 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001288{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001289 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001290}
Jean Delvarefe61e072010-08-11 18:20:58 +02001291
1292/**
Peter Rosin8320f492016-05-04 22:15:27 +02001293 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001294 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001295 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1296 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001297 */
Peter Rosin8320f492016-05-04 22:15:27 +02001298static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1299 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001300{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001301 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001302}
1303
1304/**
Peter Rosin8320f492016-05-04 22:15:27 +02001305 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001306 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001307 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1308 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001309 */
Peter Rosin8320f492016-05-04 22:15:27 +02001310static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1311 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001312{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001313 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001314}
Jean Delvarefe61e072010-08-11 18:20:58 +02001315
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001316static void i2c_dev_set_name(struct i2c_adapter *adap,
1317 struct i2c_client *client)
1318{
1319 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1320
1321 if (adev) {
1322 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1323 return;
1324 }
1325
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001326 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001327 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001328}
1329
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001330static int i2c_dev_irq_from_resources(const struct resource *resources,
1331 unsigned int num_resources)
1332{
1333 struct irq_data *irqd;
1334 int i;
1335
1336 for (i = 0; i < num_resources; i++) {
1337 const struct resource *r = &resources[i];
1338
1339 if (resource_type(r) != IORESOURCE_IRQ)
1340 continue;
1341
1342 if (r->flags & IORESOURCE_BITS) {
1343 irqd = irq_get_irq_data(r->start);
1344 if (!irqd)
1345 break;
1346
1347 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
1348 }
1349
1350 return r->start;
1351 }
1352
1353 return 0;
1354}
1355
Jean Delvarefe61e072010-08-11 18:20:58 +02001356/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001357 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001358 * @adap: the adapter managing the device
1359 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001360 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001361 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001362 * Create an i2c device. Binding is handled through driver model
1363 * probe()/remove() methods. A driver may be bound to this device when we
1364 * return from this function, or any later moment (e.g. maybe hotplugging will
1365 * load the driver module). This call is not appropriate for use by mainboard
1366 * initialization logic, which usually runs during an arch_initcall() long
1367 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001368 *
1369 * This returns the new i2c client, which may be saved for later use with
1370 * i2c_unregister_device(); or NULL to indicate an error.
1371 */
1372struct i2c_client *
1373i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1374{
1375 struct i2c_client *client;
1376 int status;
1377
1378 client = kzalloc(sizeof *client, GFP_KERNEL);
1379 if (!client)
1380 return NULL;
1381
1382 client->adapter = adap;
1383
1384 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001385
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001386 if (info->archdata)
1387 client->dev.archdata = *info->archdata;
1388
Marc Pignatee354252008-08-28 08:33:22 +02001389 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001390 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001391
David Brownell9c1600e2007-05-01 23:26:31 +02001392 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001393 if (!client->irq)
1394 client->irq = i2c_dev_irq_from_resources(info->resources,
1395 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +02001396
David Brownell9c1600e2007-05-01 23:26:31 +02001397 strlcpy(client->name, info->type, sizeof(client->name));
1398
Wolfram Sangc4019b72015-07-17 12:50:06 +02001399 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001400 if (status) {
1401 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1402 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1403 goto out_err_silent;
1404 }
1405
Jean Delvaref8a227e2009-06-19 16:58:18 +02001406 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001407 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001408 if (status)
1409 goto out_err;
1410
1411 client->dev.parent = &client->adapter->dev;
1412 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001413 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001414 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001415 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001416
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001417 i2c_dev_set_name(adap, client);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001418
1419 if (info->properties) {
1420 status = device_add_properties(&client->dev, info->properties);
1421 if (status) {
1422 dev_err(&adap->dev,
1423 "Failed to add properties to client %s: %d\n",
1424 client->name, status);
1425 goto out_err;
1426 }
1427 }
1428
Jean Delvaref8a227e2009-06-19 16:58:18 +02001429 status = device_register(&client->dev);
1430 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001431 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001432
Jean Delvaref8a227e2009-06-19 16:58:18 +02001433 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1434 client->name, dev_name(&client->dev));
1435
David Brownell9c1600e2007-05-01 23:26:31 +02001436 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001437
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001438out_free_props:
1439 if (info->properties)
1440 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001441out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001442 dev_err(&adap->dev,
1443 "Failed to register i2c client %s at 0x%02x (%d)\n",
1444 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001445out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001446 kfree(client);
1447 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001448}
1449EXPORT_SYMBOL_GPL(i2c_new_device);
1450
1451
1452/**
1453 * i2c_unregister_device - reverse effect of i2c_new_device()
1454 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001455 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001456 */
1457void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001458{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001459 if (client->dev.of_node)
1460 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001461 if (ACPI_COMPANION(&client->dev))
1462 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001463 device_unregister(&client->dev);
1464}
David Brownell9c1600e2007-05-01 23:26:31 +02001465EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001466
1467
Jean Delvare60b129d2008-05-11 20:37:06 +02001468static const struct i2c_device_id dummy_id[] = {
1469 { "dummy", 0 },
1470 { },
1471};
1472
Jean Delvared2653e92008-04-29 23:11:39 +02001473static int dummy_probe(struct i2c_client *client,
1474 const struct i2c_device_id *id)
1475{
1476 return 0;
1477}
1478
1479static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001480{
1481 return 0;
1482}
1483
1484static struct i2c_driver dummy_driver = {
1485 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001486 .probe = dummy_probe,
1487 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001488 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001489};
1490
1491/**
1492 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1493 * @adapter: the adapter managing the device
1494 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001495 * Context: can sleep
1496 *
1497 * This returns an I2C client bound to the "dummy" driver, intended for use
1498 * with devices that consume multiple addresses. Examples of such chips
1499 * include various EEPROMS (like 24c04 and 24c08 models).
1500 *
1501 * These dummy devices have two main uses. First, most I2C and SMBus calls
1502 * except i2c_transfer() need a client handle; the dummy will be that handle.
1503 * And second, this prevents the specified address from being bound to a
1504 * different driver.
1505 *
1506 * This returns the new i2c client, which should be saved for later use with
1507 * i2c_unregister_device(); or NULL to indicate an error.
1508 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001509struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001510{
1511 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001512 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001513 };
1514
David Brownelle9f13732008-01-27 18:14:52 +01001515 return i2c_new_device(adapter, &info);
1516}
1517EXPORT_SYMBOL_GPL(i2c_new_dummy);
1518
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001519/**
1520 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1521 * and create the associated device
1522 * @client: Handle to the primary client
1523 * @name: Handle to specify which secondary address to get
1524 * @default_addr: Used as a fallback if no secondary address was specified
1525 * Context: can sleep
1526 *
1527 * I2C clients can be composed of multiple I2C slaves bound together in a single
1528 * component. The I2C client driver then binds to the master I2C slave and needs
1529 * to create I2C dummy clients to communicate with all the other slaves.
1530 *
1531 * This function creates and returns an I2C dummy client whose I2C address is
1532 * retrieved from the platform firmware based on the given slave name. If no
1533 * address is specified by the firmware default_addr is used.
1534 *
1535 * On DT-based platforms the address is retrieved from the "reg" property entry
1536 * cell whose "reg-names" value matches the slave name.
1537 *
1538 * This returns the new i2c client, which should be saved for later use with
1539 * i2c_unregister_device(); or NULL to indicate an error.
1540 */
1541struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1542 const char *name,
1543 u16 default_addr)
1544{
1545 struct device_node *np = client->dev.of_node;
1546 u32 addr = default_addr;
1547 int i;
1548
1549 if (np) {
1550 i = of_property_match_string(np, "reg-names", name);
1551 if (i >= 0)
1552 of_property_read_u32_index(np, "reg", i, &addr);
1553 }
1554
1555 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1556 return i2c_new_dummy(client->adapter, addr);
1557}
1558EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1559
David Brownellf37dd802007-02-13 22:09:00 +01001560/* ------------------------------------------------------------------------- */
1561
David Brownell16ffadf2007-05-01 23:26:28 +02001562/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1563
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001564static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
David Brownellef2c83212007-05-01 23:26:28 +02001566 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 complete(&adap->dev_released);
1568}
1569
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001570unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001571{
1572 unsigned int depth = 0;
1573
1574 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1575 depth++;
1576
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001577 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1578 "adapter depth exceeds lockdep subclass limit\n");
1579
Jean Delvare390946b2012-09-10 10:14:02 +02001580 return depth;
1581}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001582EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001583
1584/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001585 * Let users instantiate I2C devices through sysfs. This can be used when
1586 * platform initialization code doesn't contain the proper data for
1587 * whatever reason. Also useful for drivers that do device detection and
1588 * detection fails, either because the device uses an unexpected address,
1589 * or this is a compatible device with different ID register values.
1590 *
1591 * Parameter checking may look overzealous, but we really don't want
1592 * the user to provide incorrect parameters.
1593 */
1594static ssize_t
1595i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1596 const char *buf, size_t count)
1597{
1598 struct i2c_adapter *adap = to_i2c_adapter(dev);
1599 struct i2c_board_info info;
1600 struct i2c_client *client;
1601 char *blank, end;
1602 int res;
1603
Jean Delvare99cd8e22009-06-19 16:58:20 +02001604 memset(&info, 0, sizeof(struct i2c_board_info));
1605
1606 blank = strchr(buf, ' ');
1607 if (!blank) {
1608 dev_err(dev, "%s: Missing parameters\n", "new_device");
1609 return -EINVAL;
1610 }
1611 if (blank - buf > I2C_NAME_SIZE - 1) {
1612 dev_err(dev, "%s: Invalid device name\n", "new_device");
1613 return -EINVAL;
1614 }
1615 memcpy(info.type, buf, blank - buf);
1616
1617 /* Parse remaining parameters, reject extra parameters */
1618 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1619 if (res < 1) {
1620 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1621 return -EINVAL;
1622 }
1623 if (res > 1 && end != '\n') {
1624 dev_err(dev, "%s: Extra parameters\n", "new_device");
1625 return -EINVAL;
1626 }
1627
Wolfram Sangcfa03272015-07-27 14:03:38 +02001628 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1629 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1630 info.flags |= I2C_CLIENT_TEN;
1631 }
1632
1633 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1634 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1635 info.flags |= I2C_CLIENT_SLAVE;
1636 }
1637
Jean Delvare99cd8e22009-06-19 16:58:20 +02001638 client = i2c_new_device(adap, &info);
1639 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001640 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001641
1642 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001643 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001644 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001645 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001646 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1647 info.type, info.addr);
1648
1649 return count;
1650}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001651static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001652
1653/*
1654 * And of course let the users delete the devices they instantiated, if
1655 * they got it wrong. This interface can only be used to delete devices
1656 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1657 * don't delete devices to which some kernel code still has references.
1658 *
1659 * Parameter checking may look overzealous, but we really don't want
1660 * the user to delete the wrong device.
1661 */
1662static ssize_t
1663i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1664 const char *buf, size_t count)
1665{
1666 struct i2c_adapter *adap = to_i2c_adapter(dev);
1667 struct i2c_client *client, *next;
1668 unsigned short addr;
1669 char end;
1670 int res;
1671
1672 /* Parse parameters, reject extra parameters */
1673 res = sscanf(buf, "%hi%c", &addr, &end);
1674 if (res < 1) {
1675 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1676 return -EINVAL;
1677 }
1678 if (res > 1 && end != '\n') {
1679 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1680 return -EINVAL;
1681 }
1682
1683 /* Make sure the device was added through sysfs */
1684 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001685 mutex_lock_nested(&adap->userspace_clients_lock,
1686 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001687 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1688 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001689 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001690 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1691 "delete_device", client->name, client->addr);
1692
1693 list_del(&client->detected);
1694 i2c_unregister_device(client);
1695 res = count;
1696 break;
1697 }
1698 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001699 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001700
1701 if (res < 0)
1702 dev_err(dev, "%s: Can't find device in list\n",
1703 "delete_device");
1704 return res;
1705}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001706static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1707 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001708
1709static struct attribute *i2c_adapter_attrs[] = {
1710 &dev_attr_name.attr,
1711 &dev_attr_new_device.attr,
1712 &dev_attr_delete_device.attr,
1713 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001714};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001715ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001716
Michael Lawnick08263742010-08-11 18:21:02 +02001717struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001718 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001719 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001720};
Michael Lawnick08263742010-08-11 18:21:02 +02001721EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Stephen Warren643dd092012-04-17 12:43:33 -06001723/**
1724 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1725 * @dev: device, probably from some driver model iterator
1726 *
1727 * When traversing the driver model tree, perhaps using driver model
1728 * iterators like @device_for_each_child(), you can't assume very much
1729 * about the nodes you find. Use this function to avoid oopses caused
1730 * by wrongly treating some non-I2C device as an i2c_adapter.
1731 */
1732struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1733{
1734 return (dev->type == &i2c_adapter_type)
1735 ? to_i2c_adapter(dev)
1736 : NULL;
1737}
1738EXPORT_SYMBOL(i2c_verify_adapter);
1739
Jean Delvare2bb50952009-09-18 22:45:46 +02001740#ifdef CONFIG_I2C_COMPAT
1741static struct class_compat *i2c_adapter_compat_class;
1742#endif
1743
David Brownell9c1600e2007-05-01 23:26:31 +02001744static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1745{
1746 struct i2c_devinfo *devinfo;
1747
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001748 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001749 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1750 if (devinfo->busnum == adapter->nr
1751 && !i2c_new_device(adapter,
1752 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001753 dev_err(&adapter->dev,
1754 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001755 devinfo->board_info.addr);
1756 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001757 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001758}
1759
Jean Delvare69b00892009-12-06 17:06:27 +01001760static int i2c_do_add_adapter(struct i2c_driver *driver,
1761 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001762{
Jean Delvare4735c982008-07-14 22:38:36 +02001763 /* Detect supported devices on that bus, and instantiate them */
1764 i2c_detect(adap, driver);
1765
1766 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001767 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001768 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1769 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001770 dev_warn(&adap->dev,
1771 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001772 /* We ignore the return code; if it fails, too bad */
1773 driver->attach_adapter(adap);
1774 }
1775 return 0;
1776}
1777
Jean Delvare69b00892009-12-06 17:06:27 +01001778static int __process_new_adapter(struct device_driver *d, void *data)
1779{
1780 return i2c_do_add_adapter(to_i2c_driver(d), data);
1781}
1782
Peter Rosind1ed7982016-08-25 23:07:01 +02001783static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1784 .lock_bus = i2c_adapter_lock_bus,
1785 .trylock_bus = i2c_adapter_trylock_bus,
1786 .unlock_bus = i2c_adapter_unlock_bus,
1787};
1788
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001789static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1790{
1791 struct irq_domain *domain = adap->host_notify_domain;
1792 irq_hw_number_t hwirq;
1793
1794 if (!domain)
1795 return;
1796
1797 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1798 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1799
1800 irq_domain_remove(domain);
1801 adap->host_notify_domain = NULL;
1802}
1803
1804static int i2c_host_notify_irq_map(struct irq_domain *h,
1805 unsigned int virq,
1806 irq_hw_number_t hw_irq_num)
1807{
1808 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1809
1810 return 0;
1811}
1812
1813static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1814 .map = i2c_host_notify_irq_map,
1815};
1816
1817static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1818{
1819 struct irq_domain *domain;
1820
1821 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1822 return 0;
1823
1824 domain = irq_domain_create_linear(adap->dev.fwnode,
1825 I2C_ADDR_7BITS_COUNT,
1826 &i2c_host_notify_irq_ops, adap);
1827 if (!domain)
1828 return -ENOMEM;
1829
1830 adap->host_notify_domain = domain;
1831
1832 return 0;
1833}
1834
1835/**
1836 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1837 * I2C client.
1838 * @adap: the adapter
1839 * @addr: the I2C address of the notifying device
1840 * Context: can't sleep
1841 *
1842 * Helper function to be called from an I2C bus driver's interrupt
1843 * handler. It will schedule the Host Notify IRQ.
1844 */
1845int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1846{
1847 int irq;
1848
1849 if (!adap)
1850 return -EINVAL;
1851
1852 irq = irq_find_mapping(adap->host_notify_domain, addr);
1853 if (irq <= 0)
1854 return -ENXIO;
1855
1856 generic_handle_irq(irq);
1857
1858 return 0;
1859}
1860EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1861
David Brownell6e13e642007-05-01 23:26:31 +02001862static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001864 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
David Brownell1d0b19c2008-10-14 17:30:05 +02001866 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301867 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001868 res = -EAGAIN;
1869 goto out_list;
1870 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001871
Jean Delvare2236baa72010-11-15 22:40:38 +01001872 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001873 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001874 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001875
1876 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001877 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001878 goto out_list;
Jean Delvare2236baa72010-11-15 22:40:38 +01001879 }
1880
Peter Rosind1ed7982016-08-25 23:07:01 +02001881 if (!adap->lock_ops)
1882 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001883
Mika Kuoppala194684e2009-12-06 17:06:22 +01001884 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001885 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001886 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001887 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Jean Delvare8fcfef62009-03-28 21:34:43 +01001889 /* Set default timeout to 1 second if not already set */
1890 if (adap->timeout == 0)
1891 adap->timeout = HZ;
1892
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001893 /* register soft irqs for Host Notify */
1894 res = i2c_setup_host_notify_irq_domain(adap);
1895 if (res) {
1896 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1897 adap->name, res);
1898 goto out_list;
1899 }
1900
Kay Sievers27d9c182009-01-07 14:29:16 +01001901 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001902 adap->dev.bus = &i2c_bus_type;
1903 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001904 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001905 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001906 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001907 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001910 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1911
Charles Keepax6ada5c12015-04-16 13:05:19 +01001912 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001913 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001914 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001915
Jean Delvare2bb50952009-09-18 22:45:46 +02001916#ifdef CONFIG_I2C_COMPAT
1917 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1918 adap->dev.parent);
1919 if (res)
1920 dev_warn(&adap->dev,
1921 "Failed to create compatibility class link\n");
1922#endif
1923
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001924 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301925
Jean Delvare729d6dd2009-06-19 16:58:18 +02001926 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001927 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001928 i2c_acpi_register_devices(adap);
1929 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001930
David Brownell6e13e642007-05-01 23:26:31 +02001931 if (adap->nr < __i2c_first_dynamic_bus_num)
1932 i2c_scan_static_board_info(adap);
1933
Jean Delvare4735c982008-07-14 22:38:36 +02001934 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001935 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001936 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001937 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001938
1939 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001940
Jean Delvareb119c6c2006-08-15 18:26:30 +02001941out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001942 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001943 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001944 mutex_unlock(&core_lock);
1945 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
David Brownell6e13e642007-05-01 23:26:31 +02001948/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001949 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1950 * @adap: the adapter to register (with adap->nr initialized)
1951 * Context: can sleep
1952 *
1953 * See i2c_add_numbered_adapter() for details.
1954 */
1955static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1956{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001957 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001958
1959 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001960 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001961 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001962 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001963 return id == -ENOSPC ? -EBUSY : id;
1964
1965 return i2c_register_adapter(adap);
1966}
1967
1968/**
David Brownell6e13e642007-05-01 23:26:31 +02001969 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1970 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001971 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001972 *
1973 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001974 * doesn't matter or when its bus number is specified by an dt alias.
1975 * Examples of bases when the bus number doesn't matter: I2C adapters
1976 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001977 *
1978 * When this returns zero, a new bus number was allocated and stored
1979 * in adap->nr, and the specified adapter became available for clients.
1980 * Otherwise, a negative errno value is returned.
1981 */
1982int i2c_add_adapter(struct i2c_adapter *adapter)
1983{
Doug Andersonee5c2742013-03-01 08:57:31 -08001984 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001985 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001986
Doug Andersonee5c2742013-03-01 08:57:31 -08001987 if (dev->of_node) {
1988 id = of_alias_get_id(dev->of_node, "i2c");
1989 if (id >= 0) {
1990 adapter->nr = id;
1991 return __i2c_add_numbered_adapter(adapter);
1992 }
1993 }
1994
Jean Delvarecaada322008-01-27 18:14:49 +01001995 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001996 id = idr_alloc(&i2c_adapter_idr, adapter,
1997 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001998 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001999 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08002000 return id;
David Brownell6e13e642007-05-01 23:26:31 +02002001
2002 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08002003
David Brownell6e13e642007-05-01 23:26:31 +02002004 return i2c_register_adapter(adapter);
2005}
2006EXPORT_SYMBOL(i2c_add_adapter);
2007
2008/**
2009 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
2010 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02002011 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002012 *
2013 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01002014 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
2015 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02002016 * is used to properly configure I2C devices.
2017 *
Grant Likely488bf312011-07-25 17:49:43 +02002018 * If the requested bus number is set to -1, then this function will behave
2019 * identically to i2c_add_adapter, and will dynamically assign a bus number.
2020 *
David Brownell6e13e642007-05-01 23:26:31 +02002021 * If no devices have pre-been declared for this bus, then be sure to
2022 * register the adapter before any dynamically allocated ones. Otherwise
2023 * the required bus ID may not be available.
2024 *
2025 * When this returns zero, the specified adapter became available for
2026 * clients using the bus number provided in adap->nr. Also, the table
2027 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
2028 * and the appropriate driver model device nodes are created. Otherwise, a
2029 * negative errno value is returned.
2030 */
2031int i2c_add_numbered_adapter(struct i2c_adapter *adap)
2032{
Grant Likely488bf312011-07-25 17:49:43 +02002033 if (adap->nr == -1) /* -1 means dynamically assign bus id */
2034 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002035
Doug Andersonee5c2742013-03-01 08:57:31 -08002036 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002037}
2038EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2039
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002040static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002041 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002042{
Jean Delvare4735c982008-07-14 22:38:36 +02002043 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002044
Jean Delvareacec2112009-03-28 21:34:40 +01002045 /* Remove the devices we created ourselves as the result of hardware
2046 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002047 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2048 if (client->adapter == adapter) {
2049 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2050 client->name, client->addr);
2051 list_del(&client->detected);
2052 i2c_unregister_device(client);
2053 }
2054 }
Jean Delvare026526f2008-01-27 18:14:49 +01002055}
2056
Jean Delvaree549c2b2009-06-19 16:58:19 +02002057static int __unregister_client(struct device *dev, void *dummy)
2058{
2059 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002060 if (client && strcmp(client->name, "dummy"))
2061 i2c_unregister_device(client);
2062 return 0;
2063}
2064
2065static int __unregister_dummy(struct device *dev, void *dummy)
2066{
2067 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002068 if (client)
2069 i2c_unregister_device(client);
2070 return 0;
2071}
2072
Jean Delvare69b00892009-12-06 17:06:27 +01002073static int __process_removed_adapter(struct device_driver *d, void *data)
2074{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002075 i2c_do_del_adapter(to_i2c_driver(d), data);
2076 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002077}
2078
David Brownelld64f73b2007-07-12 14:12:28 +02002079/**
2080 * i2c_del_adapter - unregister I2C adapter
2081 * @adap: the adapter being unregistered
2082 * Context: can sleep
2083 *
2084 * This unregisters an I2C adapter which was previously registered
2085 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2086 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002087void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002089 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002090 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002093 mutex_lock(&core_lock);
2094 found = idr_find(&i2c_adapter_idr, adap->nr);
2095 mutex_unlock(&core_lock);
2096 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002097 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002101 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002102 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002103 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002104 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002105 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002106 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002108 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002109 mutex_lock_nested(&adap->userspace_clients_lock,
2110 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002111 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2112 detected) {
2113 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2114 client->addr);
2115 list_del(&client->detected);
2116 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002117 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002118 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002119
Jean Delvaree549c2b2009-06-19 16:58:19 +02002120 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002121 * check the returned value. This is a two-pass process, because
2122 * we can't remove the dummy devices during the first pass: they
2123 * could have been instantiated by real devices wishing to clean
2124 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002125 device_for_each_child(&adap->dev, NULL, __unregister_client);
2126 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Jean Delvare2bb50952009-09-18 22:45:46 +02002128#ifdef CONFIG_I2C_COMPAT
2129 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2130 adap->dev.parent);
2131#endif
2132
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002133 /* device name is gone after device_unregister */
2134 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2135
Wolfram Sang9f924162015-12-23 18:19:28 +01002136 pm_runtime_disable(&adap->dev);
2137
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02002138 i2c_host_notify_irq_teardown(adap);
2139
Wolfram Sang26680ee2015-01-29 22:45:09 +01002140 /* wait until all references to the device are gone
2141 *
2142 * FIXME: This is old code and should ideally be replaced by an
2143 * alternative which results in decoupling the lifetime of the struct
2144 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302145 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
David Brownell6e13e642007-05-01 23:26:31 +02002151 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002152 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002154 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002156 /* Clear the device structure in case this adapter is ever going to be
2157 added again */
2158 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
David Brownellc0564602007-05-01 23:26:31 +02002160EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Wolfram Sang54177cc2015-12-17 13:32:36 +01002162/**
2163 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2164 * @dev: The device to scan for I2C timing properties
2165 * @t: the i2c_timings struct to be filled with values
2166 * @use_defaults: bool to use sane defaults derived from the I2C specification
2167 * when properties are not found, otherwise use 0
2168 *
2169 * Scan the device for the generic I2C properties describing timing parameters
2170 * for the signal and fill the given struct with the results. If a property was
2171 * not found and use_defaults was true, then maximum timings are assumed which
2172 * are derived from the I2C specification. If use_defaults is not used, the
2173 * results will be 0, so drivers can apply their own defaults later. The latter
2174 * is mainly intended for avoiding regressions of existing drivers which want
2175 * to switch to this function. New drivers almost always should use the defaults.
2176 */
2177
2178void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2179{
2180 int ret;
2181
2182 memset(t, 0, sizeof(*t));
2183
2184 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2185 if (ret && use_defaults)
2186 t->bus_freq_hz = 100000;
2187
2188 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2189 if (ret && use_defaults) {
2190 if (t->bus_freq_hz <= 100000)
2191 t->scl_rise_ns = 1000;
2192 else if (t->bus_freq_hz <= 400000)
2193 t->scl_rise_ns = 300;
2194 else
2195 t->scl_rise_ns = 120;
2196 }
2197
2198 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2199 if (ret && use_defaults) {
2200 if (t->bus_freq_hz <= 400000)
2201 t->scl_fall_ns = 300;
2202 else
2203 t->scl_fall_ns = 120;
2204 }
2205
2206 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2207
2208 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2209 if (ret && use_defaults)
2210 t->sda_fall_ns = t->scl_fall_ns;
2211}
2212EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2213
David Brownell7b4fbc52007-05-01 23:26:30 +02002214/* ------------------------------------------------------------------------- */
2215
Jean Delvare7ae31482011-03-20 14:50:52 +01002216int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2217{
2218 int res;
2219
2220 mutex_lock(&core_lock);
2221 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2222 mutex_unlock(&core_lock);
2223
2224 return res;
2225}
2226EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2227
Jean Delvare69b00892009-12-06 17:06:27 +01002228static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002229{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002230 if (dev->type != &i2c_adapter_type)
2231 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002232 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002233}
2234
David Brownell7b4fbc52007-05-01 23:26:30 +02002235/*
2236 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002237 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 */
2239
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002240int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002242 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
David Brownell1d0b19c2008-10-14 17:30:05 +02002244 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302245 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002246 return -EAGAIN;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002249 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002251 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Jean Delvare729d6dd2009-06-19 16:58:18 +02002253 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002254 * will have called probe() for all matching-but-unbound devices.
2255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 res = driver_register(&driver->driver);
2257 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002258 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002259
Wolfram Sang44239a52016-07-09 13:35:04 +09002260 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Jean Delvare4735c982008-07-14 22:38:36 +02002262 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002263 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002264
Jean Delvare7eebcb72006-02-05 23:28:21 +01002265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002267EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Jean Delvare69b00892009-12-06 17:06:27 +01002269static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002270{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002271 if (dev->type == &i2c_adapter_type)
2272 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2273 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002274}
2275
David Brownella1d9e6e2007-05-01 23:26:30 +02002276/**
2277 * i2c_del_driver - unregister I2C driver
2278 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002279 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002280 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002281void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Jean Delvare7ae31482011-03-20 14:50:52 +01002283 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002286 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
David Brownellc0564602007-05-01 23:26:31 +02002288EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
David Brownell7b4fbc52007-05-01 23:26:30 +02002290/* ------------------------------------------------------------------------- */
2291
Jean Delvaree48d3312008-01-27 18:14:48 +01002292/**
2293 * i2c_use_client - increments the reference count of the i2c client structure
2294 * @client: the client being referenced
2295 *
2296 * Each live reference to a client should be refcounted. The driver model does
2297 * that automatically as part of driver binding, so that most drivers don't
2298 * need to do this explicitly: they hold a reference until they're unbound
2299 * from the device.
2300 *
2301 * A pointer to the client with the incremented reference counter is returned.
2302 */
2303struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
David Brownell6ea438e2008-07-14 22:38:24 +02002305 if (client && get_device(&client->dev))
2306 return client;
2307 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
David Brownellc0564602007-05-01 23:26:31 +02002309EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jean Delvaree48d3312008-01-27 18:14:48 +01002311/**
2312 * i2c_release_client - release a use of the i2c client structure
2313 * @client: the client being no longer referenced
2314 *
2315 * Must be called when a user of a client is finished with it.
2316 */
2317void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
David Brownell6ea438e2008-07-14 22:38:24 +02002319 if (client)
2320 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
David Brownellc0564602007-05-01 23:26:31 +02002322EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
David Brownell9b766b82008-01-27 18:14:51 +01002324struct i2c_cmd_arg {
2325 unsigned cmd;
2326 void *arg;
2327};
2328
2329static int i2c_cmd(struct device *dev, void *_arg)
2330{
2331 struct i2c_client *client = i2c_verify_client(dev);
2332 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002333 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002334
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002335 if (!client || !client->dev.driver)
2336 return 0;
2337
2338 driver = to_i2c_driver(client->dev.driver);
2339 if (driver->command)
2340 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002341 return 0;
2342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2345{
David Brownell9b766b82008-01-27 18:14:51 +01002346 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
David Brownell9b766b82008-01-27 18:14:51 +01002348 cmd_arg.cmd = cmd;
2349 cmd_arg.arg = arg;
2350 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
David Brownellc0564602007-05-01 23:26:31 +02002352EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354static int __init i2c_init(void)
2355{
2356 int retval;
2357
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002358 retval = of_alias_get_highest_id("i2c");
2359
2360 down_write(&__i2c_board_lock);
2361 if (retval >= __i2c_first_dynamic_bus_num)
2362 __i2c_first_dynamic_bus_num = retval + 1;
2363 up_write(&__i2c_board_lock);
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 retval = bus_register(&i2c_bus_type);
2366 if (retval)
2367 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002368
2369 is_registered = true;
2370
Jean Delvare2bb50952009-09-18 22:45:46 +02002371#ifdef CONFIG_I2C_COMPAT
2372 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2373 if (!i2c_adapter_compat_class) {
2374 retval = -ENOMEM;
2375 goto bus_err;
2376 }
2377#endif
David Brownelle9f13732008-01-27 18:14:52 +01002378 retval = i2c_add_driver(&dummy_driver);
2379 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002380 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002381
2382 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2383 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002384 if (IS_ENABLED(CONFIG_ACPI))
2385 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002386
David Brownelle9f13732008-01-27 18:14:52 +01002387 return 0;
2388
Jean Delvare2bb50952009-09-18 22:45:46 +02002389class_err:
2390#ifdef CONFIG_I2C_COMPAT
2391 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002392bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002393#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002394 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002395 bus_unregister(&i2c_bus_type);
2396 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static void __exit i2c_exit(void)
2400{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002401 if (IS_ENABLED(CONFIG_ACPI))
2402 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002403 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2404 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002405 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002406#ifdef CONFIG_I2C_COMPAT
2407 class_compat_unregister(i2c_adapter_compat_class);
2408#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002410 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
David Brownella10f9e72008-10-14 17:30:06 +02002413/* We must initialize early, because some subsystems register i2c drivers
2414 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2415 */
2416postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417module_exit(i2c_exit);
2418
2419/* ----------------------------------------------------
2420 * the functional interface to the i2c busses.
2421 * ----------------------------------------------------
2422 */
2423
Wolfram Sangb7f62582015-01-05 23:45:59 +01002424/* Check if val is exceeding the quirk IFF quirk is non 0 */
2425#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2426
2427static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2428{
2429 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2430 err_msg, msg->addr, msg->len,
2431 msg->flags & I2C_M_RD ? "read" : "write");
2432 return -EOPNOTSUPP;
2433}
2434
2435static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2436{
2437 const struct i2c_adapter_quirks *q = adap->quirks;
2438 int max_num = q->max_num_msgs, i;
2439 bool do_len_check = true;
2440
2441 if (q->flags & I2C_AQ_COMB) {
2442 max_num = 2;
2443
2444 /* special checks for combined messages */
2445 if (num == 2) {
2446 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2447 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2448
2449 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2450 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2451
2452 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2453 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2454
2455 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2456 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2457
2458 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2459 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2460
2461 do_len_check = false;
2462 }
2463 }
2464
2465 if (i2c_quirk_exceeded(num, max_num))
2466 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2467
2468 for (i = 0; i < num; i++) {
2469 u16 len = msgs[i].len;
2470
2471 if (msgs[i].flags & I2C_M_RD) {
2472 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2473 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2474 } else {
2475 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2476 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2477 }
2478 }
2479
2480 return 0;
2481}
2482
David Brownella1cdeda2008-07-14 22:38:24 +02002483/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002484 * __i2c_transfer - unlocked flavor of i2c_transfer
2485 * @adap: Handle to I2C bus
2486 * @msgs: One or more messages to execute before STOP is issued to
2487 * terminate the operation; each message begins with a START.
2488 * @num: Number of messages to be executed.
2489 *
2490 * Returns negative errno, else the number of messages executed.
2491 *
2492 * Adapter lock must be held when calling this function. No debug logging
2493 * takes place. adap->algo->master_xfer existence isn't checked.
2494 */
2495int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2496{
2497 unsigned long orig_jiffies;
2498 int ret, try;
2499
Wolfram Sangb7f62582015-01-05 23:45:59 +01002500 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2501 return -EOPNOTSUPP;
2502
David Howellsd9a83d62014-03-06 13:35:59 +00002503 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2504 * enabled. This is an efficient way of keeping the for-loop from
2505 * being executed when not needed.
2506 */
2507 if (static_key_false(&i2c_trace_msg)) {
2508 int i;
2509 for (i = 0; i < num; i++)
2510 if (msgs[i].flags & I2C_M_RD)
2511 trace_i2c_read(adap, &msgs[i], i);
2512 else
2513 trace_i2c_write(adap, &msgs[i], i);
2514 }
2515
Jean Delvareb37d2a32012-06-29 07:47:19 -03002516 /* Retry automatically on arbitration loss */
2517 orig_jiffies = jiffies;
2518 for (ret = 0, try = 0; try <= adap->retries; try++) {
2519 ret = adap->algo->master_xfer(adap, msgs, num);
2520 if (ret != -EAGAIN)
2521 break;
2522 if (time_after(jiffies, orig_jiffies + adap->timeout))
2523 break;
2524 }
2525
David Howellsd9a83d62014-03-06 13:35:59 +00002526 if (static_key_false(&i2c_trace_msg)) {
2527 int i;
2528 for (i = 0; i < ret; i++)
2529 if (msgs[i].flags & I2C_M_RD)
2530 trace_i2c_reply(adap, &msgs[i], i);
2531 trace_i2c_result(adap, i, ret);
2532 }
2533
Jean Delvareb37d2a32012-06-29 07:47:19 -03002534 return ret;
2535}
2536EXPORT_SYMBOL(__i2c_transfer);
2537
2538/**
David Brownella1cdeda2008-07-14 22:38:24 +02002539 * i2c_transfer - execute a single or combined I2C message
2540 * @adap: Handle to I2C bus
2541 * @msgs: One or more messages to execute before STOP is issued to
2542 * terminate the operation; each message begins with a START.
2543 * @num: Number of messages to be executed.
2544 *
2545 * Returns negative errno, else the number of messages executed.
2546 *
2547 * Note that there is no requirement that each message be sent to
2548 * the same slave address, although that is the most common model.
2549 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002550int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
David Brownella1cdeda2008-07-14 22:38:24 +02002554 /* REVISIT the fault reporting model here is weak:
2555 *
2556 * - When we get an error after receiving N bytes from a slave,
2557 * there is no way to report "N".
2558 *
2559 * - When we get a NAK after transmitting N bytes to a slave,
2560 * there is no way to report "N" ... or to let the master
2561 * continue executing the rest of this combined message, if
2562 * that's the appropriate response.
2563 *
2564 * - When for example "num" is two and we successfully complete
2565 * the first message but get an error part way through the
2566 * second, it's unclear whether that should be reported as
2567 * one (discarding status on the second message) or errno
2568 * (discarding status on the first one).
2569 */
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (adap->algo->master_xfer) {
2572#ifdef DEBUG
2573 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002574 dev_dbg(&adap->dev,
2575 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2576 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2577 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002578 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580#endif
2581
Mike Rapoportcea443a82008-01-27 18:14:50 +01002582 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002583 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002584 if (!ret)
2585 /* I2C activity is ongoing. */
2586 return -EAGAIN;
2587 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002588 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002589 }
2590
Jean Delvareb37d2a32012-06-29 07:47:19 -03002591 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002592 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 return ret;
2595 } else {
2596 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002597 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599}
David Brownellc0564602007-05-01 23:26:31 +02002600EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
David Brownella1cdeda2008-07-14 22:38:24 +02002602/**
2603 * i2c_master_send - issue a single I2C message in master transmit mode
2604 * @client: Handle to slave device
2605 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002606 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002607 *
2608 * Returns negative errno, or else the number of bytes written.
2609 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002610int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002613 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct i2c_msg msg;
2615
Jean Delvare815f55f2005-05-07 22:58:46 +02002616 msg.addr = client->addr;
2617 msg.flags = client->flags & I2C_M_TEN;
2618 msg.len = count;
2619 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002620
Jean Delvare815f55f2005-05-07 22:58:46 +02002621 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002623 /*
2624 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2625 * transmitted, else error code.
2626 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002627 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
David Brownellc0564602007-05-01 23:26:31 +02002629EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
David Brownella1cdeda2008-07-14 22:38:24 +02002631/**
2632 * i2c_master_recv - issue a single I2C message in master receive mode
2633 * @client: Handle to slave device
2634 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002635 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002636 *
2637 * Returns negative errno, or else the number of bytes read.
2638 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002639int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Farid Hammane7225acf2010-05-21 18:40:58 +02002641 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct i2c_msg msg;
2643 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Jean Delvare815f55f2005-05-07 22:58:46 +02002645 msg.addr = client->addr;
2646 msg.flags = client->flags & I2C_M_TEN;
2647 msg.flags |= I2C_M_RD;
2648 msg.len = count;
2649 msg.buf = buf;
2650
2651 ret = i2c_transfer(adap, &msg, 1);
2652
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002653 /*
2654 * If everything went ok (i.e. 1 msg received), return #bytes received,
2655 * else error code.
2656 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002657 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
David Brownellc0564602007-05-01 23:26:31 +02002659EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/* ----------------------------------------------------
2662 * the i2c address scanning function
2663 * Will not work for 10-bit addresses!
2664 * ----------------------------------------------------
2665 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002666
Jean Delvare63e4e802010-06-03 11:33:51 +02002667/*
2668 * Legacy default probe function, mostly relevant for SMBus. The default
2669 * probe method is a quick write, but it is known to corrupt the 24RF08
2670 * EEPROMs due to a state machine bug, and could also irreversibly
2671 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2672 * we use a short byte read instead. Also, some bus drivers don't implement
2673 * quick write, so we fallback to a byte read in that case too.
2674 * On x86, there is another special case for FSC hardware monitoring chips,
2675 * which want regular byte reads (address 0x73.) Fortunately, these are the
2676 * only known chips using this I2C address on PC hardware.
2677 * Returns 1 if probe succeeded, 0 if not.
2678 */
2679static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2680{
2681 int err;
2682 union i2c_smbus_data dummy;
2683
2684#ifdef CONFIG_X86
2685 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2686 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2687 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2688 I2C_SMBUS_BYTE_DATA, &dummy);
2689 else
2690#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002691 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2692 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002693 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2694 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002695 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2696 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2697 I2C_SMBUS_BYTE, &dummy);
2698 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002699 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2700 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002701 err = -EOPNOTSUPP;
2702 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002703
2704 return err >= 0;
2705}
2706
Jean Delvareccfbbd02009-12-06 17:06:25 +01002707static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002708 struct i2c_driver *driver)
2709{
2710 struct i2c_board_info info;
2711 struct i2c_adapter *adapter = temp_client->adapter;
2712 int addr = temp_client->addr;
2713 int err;
2714
2715 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002716 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002717 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002718 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2719 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002720 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002721 }
2722
Wolfram Sang9bccc702015-07-17 14:48:56 +02002723 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002724 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002725 return 0;
2726
Jean Delvareccfbbd02009-12-06 17:06:25 +01002727 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002728 if (!i2c_default_probe(adapter, addr))
2729 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002730
2731 /* Finally call the custom detection function */
2732 memset(&info, 0, sizeof(struct i2c_board_info));
2733 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002734 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002735 if (err) {
2736 /* -ENODEV is returned if the detection fails. We catch it
2737 here as this isn't an error. */
2738 return err == -ENODEV ? 0 : err;
2739 }
2740
2741 /* Consistency check */
2742 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002743 dev_err(&adapter->dev,
2744 "%s detection function provided no name for 0x%x\n",
2745 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002746 } else {
2747 struct i2c_client *client;
2748
2749 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002750 if (adapter->class & I2C_CLASS_DEPRECATED)
2751 dev_warn(&adapter->dev,
2752 "This adapter will soon drop class based instantiation of devices. "
2753 "Please make sure client 0x%02x gets instantiated by other means. "
2754 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2755 info.addr);
2756
Jean Delvare4735c982008-07-14 22:38:36 +02002757 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2758 info.type, info.addr);
2759 client = i2c_new_device(adapter, &info);
2760 if (client)
2761 list_add_tail(&client->detected, &driver->clients);
2762 else
2763 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2764 info.type, info.addr);
2765 }
2766 return 0;
2767}
2768
2769static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2770{
Jean Delvarec3813d62009-12-14 21:17:25 +01002771 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002772 struct i2c_client *temp_client;
2773 int i, err = 0;
2774 int adap_id = i2c_adapter_id(adapter);
2775
Jean Delvarec3813d62009-12-14 21:17:25 +01002776 address_list = driver->address_list;
2777 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002778 return 0;
2779
Wolfram Sang45552272014-07-10 13:46:21 +02002780 /* Warn that the adapter lost class based instantiation */
2781 if (adapter->class == I2C_CLASS_DEPRECATED) {
2782 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002783 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2784 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002785 driver->driver.name);
2786 return 0;
2787 }
2788
Jean Delvare51b54ba2010-10-24 18:16:58 +02002789 /* Stop here if the classes do not match */
2790 if (!(adapter->class & driver->class))
2791 return 0;
2792
Jean Delvare4735c982008-07-14 22:38:36 +02002793 /* Set up a temporary client to help detect callback */
2794 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2795 if (!temp_client)
2796 return -ENOMEM;
2797 temp_client->adapter = adapter;
2798
Jean Delvarec3813d62009-12-14 21:17:25 +01002799 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002800 dev_dbg(&adapter->dev,
2801 "found normal entry for adapter %d, addr 0x%02x\n",
2802 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002803 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002804 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002805 if (unlikely(err))
2806 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002807 }
2808
Jean Delvare4735c982008-07-14 22:38:36 +02002809 kfree(temp_client);
2810 return err;
2811}
2812
Jean Delvared44f19d2010-08-11 18:20:57 +02002813int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2814{
2815 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2816 I2C_SMBUS_QUICK, NULL) >= 0;
2817}
2818EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2819
Jean Delvare12b5053a2007-05-01 23:26:31 +02002820struct i2c_client *
2821i2c_new_probed_device(struct i2c_adapter *adap,
2822 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002823 unsigned short const *addr_list,
2824 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002825{
2826 int i;
2827
Jean Delvare8031d792010-08-11 18:21:00 +02002828 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002829 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002830
Jean Delvare12b5053a2007-05-01 23:26:31 +02002831 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2832 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002833 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002834 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2835 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002836 continue;
2837 }
2838
Wolfram Sang9bccc702015-07-17 14:48:56 +02002839 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002840 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002841 dev_dbg(&adap->dev,
2842 "Address 0x%02x already in use, not probing\n",
2843 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002844 continue;
2845 }
2846
Jean Delvare63e4e802010-06-03 11:33:51 +02002847 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002848 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002849 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002850 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002851
2852 if (addr_list[i] == I2C_CLIENT_END) {
2853 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2854 return NULL;
2855 }
2856
2857 info->addr = addr_list[i];
2858 return i2c_new_device(adap, info);
2859}
2860EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2861
Jean Delvared735b342011-03-20 14:50:52 +01002862struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002865
Jean Delvarecaada322008-01-27 18:14:49 +01002866 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002867 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002868 if (!adapter)
2869 goto exit;
2870
2871 if (try_module_get(adapter->owner))
2872 get_device(&adapter->dev);
2873 else
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002874 adapter = NULL;
2875
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002876 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002877 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002878 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
David Brownellc0564602007-05-01 23:26:31 +02002880EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882void i2c_put_adapter(struct i2c_adapter *adap)
2883{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002884 if (!adap)
2885 return;
2886
2887 put_device(&adap->dev);
2888 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
David Brownellc0564602007-05-01 23:26:31 +02002890EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2893MODULE_DESCRIPTION("I2C-Bus main module");
2894MODULE_LICENSE("GPL");