blob: 88c0ca664a7b8f6eef0e87afe2cca12490d7d9b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Wolfram Sang44239a52016-07-09 13:35:04 +090029#define pr_fmt(fmt) "i2c-core: " fmt
30
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020031#include <dt-bindings/i2c/i2c.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020034#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010036#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020037#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010038#include <linux/errno.h>
39#include <linux/gpio.h>
40#include <linux/hardirq.h>
41#include <linux/i2c.h>
42#include <linux/idr.h>
43#include <linux/init.h>
44#include <linux/irqflags.h>
45#include <linux/jump_label.h>
46#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/mutex.h>
49#include <linux/of_device.h>
50#include <linux/of.h>
51#include <linux/of_irq.h>
52#include <linux/pm_domain.h>
53#include <linux/pm_runtime.h>
54#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010055#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010056#include <linux/rwsem.h>
57#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Brownell9c1600e2007-05-01 23:26:31 +020059#include "i2c-core.h"
60
David Howellsd9a83d62014-03-06 13:35:59 +000061#define CREATE_TRACE_POINTS
62#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Wolfram Sangda899f52015-05-18 21:09:12 +020064#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
65#define I2C_ADDR_OFFSET_SLAVE 0x1000
66
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020067#define I2C_ADDR_7BITS_MAX 0x77
68#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
69
Jean Delvare6629dcf2010-05-04 11:09:28 +020070/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020071 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000072 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010073static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static DEFINE_IDR(i2c_adapter_idr);
75
Jean Delvare4735c982008-07-14 22:38:36 +020076static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010077
David Howellsd9a83d62014-03-06 13:35:59 +000078static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053079static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000080
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050081int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000082{
83 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050084 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000085}
86
87void i2c_transfer_trace_unreg(void)
88{
89 static_key_slow_dec(&i2c_trace_msg);
90}
91
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020092#if defined(CONFIG_ACPI)
Jarkko Nikulaaec809f2016-08-12 17:02:52 +030093struct i2c_acpi_handler_data {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020094 struct acpi_connection_info info;
95 struct i2c_adapter *adapter;
96};
97
98struct gsb_buffer {
99 u8 status;
100 u8 len;
101 union {
102 u16 wdata;
103 u8 bdata;
104 u8 data[0];
105 };
106} __packed;
107
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300108struct i2c_acpi_lookup {
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300109 struct i2c_board_info *info;
110 acpi_handle adapter_handle;
111 acpi_handle device_handle;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300112 acpi_handle search_handle;
Hans de Goede417f7842017-04-05 00:03:32 +0200113 int n;
114 int index;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300115 u32 speed;
116 u32 min_speed;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300117};
118
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300119static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200120{
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300121 struct i2c_acpi_lookup *lookup = data;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300122 struct i2c_board_info *info = lookup->info;
123 struct acpi_resource_i2c_serialbus *sb;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300124 acpi_status status;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200125
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300126 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
127 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200128
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300129 sb = &ares->data.i2c_serial_bus;
130 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
131 return 1;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200132
Hans de Goede417f7842017-04-05 00:03:32 +0200133 if (lookup->index != -1 && lookup->n++ != lookup->index)
134 return 1;
135
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300136 status = acpi_get_handle(lookup->device_handle,
137 sb->resource_source.string_ptr,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300138 &lookup->adapter_handle);
139 if (!ACPI_SUCCESS(status))
140 return 1;
141
142 info->addr = sb->slave_address;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300143 lookup->speed = sb->connection_speed;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300144 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
145 info->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200146
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200147 return 1;
148}
149
Jarkko Nikula5853b222016-08-12 17:02:53 +0300150static int i2c_acpi_do_lookup(struct acpi_device *adev,
151 struct i2c_acpi_lookup *lookup)
152{
153 struct i2c_board_info *info = lookup->info;
154 struct list_head resource_list;
155 int ret;
156
157 if (acpi_bus_get_status(adev) || !adev->status.present ||
158 acpi_device_enumerated(adev))
159 return -EINVAL;
160
161 memset(info, 0, sizeof(*info));
162 lookup->device_handle = acpi_device_handle(adev);
163
164 /* Look up for I2cSerialBus resource */
165 INIT_LIST_HEAD(&resource_list);
166 ret = acpi_dev_get_resources(adev, &resource_list,
167 i2c_acpi_fill_info, lookup);
168 acpi_dev_free_resource_list(&resource_list);
169
170 if (ret < 0 || !info->addr)
171 return -EINVAL;
172
173 return 0;
174}
175
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300176static int i2c_acpi_get_info(struct acpi_device *adev,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300177 struct i2c_board_info *info,
Mika Westerberg318ce272016-09-20 16:59:25 +0300178 struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300179 acpi_handle *adapter_handle)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200180{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200181 struct list_head resource_list;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300182 struct resource_entry *entry;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300183 struct i2c_acpi_lookup lookup;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200184 int ret;
185
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300186 memset(&lookup, 0, sizeof(lookup));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300187 lookup.info = info;
Hans de Goede417f7842017-04-05 00:03:32 +0200188 lookup.index = -1;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300189
Jarkko Nikula5853b222016-08-12 17:02:53 +0300190 ret = i2c_acpi_do_lookup(adev, &lookup);
191 if (ret)
192 return ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200193
Mika Westerberg318ce272016-09-20 16:59:25 +0300194 if (adapter) {
195 /* The adapter must match the one in I2cSerialBus() connector */
196 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
197 return -ENODEV;
198 } else {
199 struct acpi_device *adapter_adev;
200
201 /* The adapter must be present */
202 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
203 return -ENODEV;
204 if (acpi_bus_get_status(adapter_adev) ||
205 !adapter_adev->status.present)
206 return -ENODEV;
207 }
208
Jarkko Nikula5853b222016-08-12 17:02:53 +0300209 info->fwnode = acpi_fwnode_handle(adev);
Mika Westerberg318ce272016-09-20 16:59:25 +0300210 if (adapter_handle)
211 *adapter_handle = lookup.adapter_handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200212
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300213 /* Then fill IRQ number if any */
Jarkko Nikula5853b222016-08-12 17:02:53 +0300214 INIT_LIST_HEAD(&resource_list);
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300215 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
216 if (ret < 0)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300217 return -EINVAL;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300218
219 resource_list_for_each_entry(entry, &resource_list) {
220 if (resource_type(entry->res) == IORESOURCE_IRQ) {
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300221 info->irq = entry->res->start;
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300222 break;
223 }
224 }
225
226 acpi_dev_free_resource_list(&resource_list);
227
Dan O'Donovan622b3072017-02-05 16:30:13 +0000228 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
229 sizeof(info->type));
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300230
231 return 0;
232}
233
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300234static void i2c_acpi_register_device(struct i2c_adapter *adapter,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300235 struct acpi_device *adev,
236 struct i2c_board_info *info)
237{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200238 adev->power.flags.ignore_parent = true;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300239 acpi_device_set_enumerated(adev);
240
241 if (!i2c_new_device(adapter, info)) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200242 adev->power.flags.ignore_parent = false;
243 dev_err(&adapter->dev,
244 "failed to add I2C device %s from ACPI\n",
245 dev_name(&adev->dev));
246 }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300247}
248
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300249static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300250 void *data, void **return_value)
251{
252 struct i2c_adapter *adapter = data;
253 struct acpi_device *adev;
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300254 struct i2c_board_info info;
255
256 if (acpi_bus_get_device(handle, &adev))
257 return AE_OK;
258
Mika Westerberg318ce272016-09-20 16:59:25 +0300259 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300260 return AE_OK;
261
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300262 i2c_acpi_register_device(adapter, adev, &info);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200263
264 return AE_OK;
265}
266
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300267#define I2C_ACPI_MAX_SCAN_DEPTH 32
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300268
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200269/**
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300270 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200271 * @adap: pointer to adapter
272 *
273 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
274 * namespace. When a device is found it will be added to the Linux device
275 * model and bound to the corresponding ACPI handle.
276 */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300277static void i2c_acpi_register_devices(struct i2c_adapter *adap)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200278{
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200279 acpi_status status;
280
Dustin Byford8eb5c872015-10-23 12:27:07 -0700281 if (!has_acpi_companion(&adap->dev))
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200282 return;
283
Mika Westerberg166c2ba2015-10-07 13:18:44 +0300284 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300285 I2C_ACPI_MAX_SCAN_DEPTH,
286 i2c_acpi_add_device, NULL,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200287 adap, NULL);
288 if (ACPI_FAILURE(status))
289 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
290}
291
Jarkko Nikula5853b222016-08-12 17:02:53 +0300292static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
293 void *data, void **return_value)
294{
295 struct i2c_acpi_lookup *lookup = data;
296 struct acpi_device *adev;
297
298 if (acpi_bus_get_device(handle, &adev))
299 return AE_OK;
300
301 if (i2c_acpi_do_lookup(adev, lookup))
302 return AE_OK;
303
304 if (lookup->search_handle != lookup->adapter_handle)
305 return AE_OK;
306
307 if (lookup->speed <= lookup->min_speed)
308 lookup->min_speed = lookup->speed;
309
310 return AE_OK;
311}
312
313/**
314 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
315 * @dev: The device owning the bus
316 *
317 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
318 * devices connected to this bus and use the speed of slowest device.
319 *
320 * Returns the speed in Hz or zero
321 */
322u32 i2c_acpi_find_bus_speed(struct device *dev)
323{
324 struct i2c_acpi_lookup lookup;
325 struct i2c_board_info dummy;
326 acpi_status status;
327
328 if (!has_acpi_companion(dev))
329 return 0;
330
331 memset(&lookup, 0, sizeof(lookup));
332 lookup.search_handle = ACPI_HANDLE(dev);
333 lookup.min_speed = UINT_MAX;
334 lookup.info = &dummy;
Hans de Goede417f7842017-04-05 00:03:32 +0200335 lookup.index = -1;
Jarkko Nikula5853b222016-08-12 17:02:53 +0300336
337 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
338 I2C_ACPI_MAX_SCAN_DEPTH,
339 i2c_acpi_lookup_speed, NULL,
340 &lookup, NULL);
341
342 if (ACPI_FAILURE(status)) {
343 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
344 return 0;
345 }
346
347 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
348}
349EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
350
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300351static int i2c_acpi_match_adapter(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300352{
353 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
354
355 if (!adapter)
356 return 0;
357
358 return ACPI_HANDLE(dev) == (acpi_handle)data;
359}
360
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300361static int i2c_acpi_match_device(struct device *dev, void *data)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300362{
363 return ACPI_COMPANION(dev) == data;
364}
365
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300366static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300367{
368 struct device *dev;
369
370 dev = bus_find_device(&i2c_bus_type, NULL, handle,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300371 i2c_acpi_match_adapter);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300372 return dev ? i2c_verify_adapter(dev) : NULL;
373}
374
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300375static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300376{
377 struct device *dev;
378
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300379 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300380 return dev ? i2c_verify_client(dev) : NULL;
381}
382
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300383static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300384 void *arg)
385{
386 struct acpi_device *adev = arg;
387 struct i2c_board_info info;
388 acpi_handle adapter_handle;
389 struct i2c_adapter *adapter;
390 struct i2c_client *client;
391
392 switch (value) {
393 case ACPI_RECONFIG_DEVICE_ADD:
Mika Westerberg318ce272016-09-20 16:59:25 +0300394 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300395 break;
396
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300397 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300398 if (!adapter)
399 break;
400
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300401 i2c_acpi_register_device(adapter, adev, &info);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300402 break;
403 case ACPI_RECONFIG_DEVICE_REMOVE:
404 if (!acpi_device_enumerated(adev))
405 break;
406
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300407 client = i2c_acpi_find_client_by_adev(adev);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300408 if (!client)
409 break;
410
411 i2c_unregister_device(client);
412 put_device(&client->dev);
413 break;
414 }
415
416 return NOTIFY_OK;
417}
418
419static struct notifier_block i2c_acpi_notifier = {
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300420 .notifier_call = i2c_acpi_notify,
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300421};
Hans de Goede605f8fc2017-04-05 00:03:33 +0200422
423/**
424 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
425 * @dev: Device owning the ACPI resources to get the client from
426 * @index: Index of ACPI resource to get
427 * @info: describes the I2C device; note this is modified (addr gets set)
428 * Context: can sleep
429 *
430 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
431 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
432 * resources, in that case this function can be used to create an i2c-client
433 * for other I2cSerialBus resources in the Current Resource Settings table.
434 *
435 * Also see i2c_new_device, which this function calls to create the i2c-client.
436 *
437 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
438 */
439struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
440 struct i2c_board_info *info)
441{
442 struct i2c_acpi_lookup lookup;
443 struct i2c_adapter *adapter;
444 struct acpi_device *adev;
445 LIST_HEAD(resource_list);
446 int ret;
447
448 adev = ACPI_COMPANION(dev);
449 if (!adev)
450 return NULL;
451
452 memset(&lookup, 0, sizeof(lookup));
453 lookup.info = info;
454 lookup.device_handle = acpi_device_handle(adev);
455 lookup.index = index;
456
457 ret = acpi_dev_get_resources(adev, &resource_list,
458 i2c_acpi_fill_info, &lookup);
459 acpi_dev_free_resource_list(&resource_list);
460
461 if (ret < 0 || !info->addr)
462 return NULL;
463
464 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
465 if (!adapter)
466 return NULL;
467
468 return i2c_new_device(adapter, info);
469}
470EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200471#else /* CONFIG_ACPI */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300472static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300473extern struct notifier_block i2c_acpi_notifier;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200474#endif /* CONFIG_ACPI */
475
476#ifdef CONFIG_ACPI_I2C_OPREGION
477static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
478 u8 cmd, u8 *data, u8 data_len)
479{
480
481 struct i2c_msg msgs[2];
482 int ret;
483 u8 *buffer;
484
485 buffer = kzalloc(data_len, GFP_KERNEL);
486 if (!buffer)
487 return AE_NO_MEMORY;
488
489 msgs[0].addr = client->addr;
490 msgs[0].flags = client->flags;
491 msgs[0].len = 1;
492 msgs[0].buf = &cmd;
493
494 msgs[1].addr = client->addr;
495 msgs[1].flags = client->flags | I2C_M_RD;
496 msgs[1].len = data_len;
497 msgs[1].buf = buffer;
498
499 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
500 if (ret < 0)
501 dev_err(&client->adapter->dev, "i2c read failed\n");
502 else
503 memcpy(data, buffer, data_len);
504
505 kfree(buffer);
506 return ret;
507}
508
509static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
510 u8 cmd, u8 *data, u8 data_len)
511{
512
513 struct i2c_msg msgs[1];
514 u8 *buffer;
515 int ret = AE_OK;
516
517 buffer = kzalloc(data_len + 1, GFP_KERNEL);
518 if (!buffer)
519 return AE_NO_MEMORY;
520
521 buffer[0] = cmd;
522 memcpy(buffer + 1, data, data_len);
523
524 msgs[0].addr = client->addr;
525 msgs[0].flags = client->flags;
526 msgs[0].len = data_len + 1;
527 msgs[0].buf = buffer;
528
529 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
530 if (ret < 0)
531 dev_err(&client->adapter->dev, "i2c write failed\n");
532
533 kfree(buffer);
534 return ret;
535}
536
537static acpi_status
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300538i2c_acpi_space_handler(u32 function, acpi_physical_address command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200539 u32 bits, u64 *value64,
540 void *handler_context, void *region_context)
541{
542 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300543 struct i2c_acpi_handler_data *data = handler_context;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200544 struct acpi_connection_info *info = &data->info;
545 struct acpi_resource_i2c_serialbus *sb;
546 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300547 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200548 struct acpi_resource *ares;
549 u32 accessor_type = function >> 16;
550 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100551 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200552 int status;
553
554 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
555 if (ACPI_FAILURE(ret))
556 return ret;
557
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300558 client = kzalloc(sizeof(*client), GFP_KERNEL);
559 if (!client) {
560 ret = AE_NO_MEMORY;
561 goto err;
562 }
563
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200564 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
565 ret = AE_BAD_PARAMETER;
566 goto err;
567 }
568
569 sb = &ares->data.i2c_serial_bus;
570 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
571 ret = AE_BAD_PARAMETER;
572 goto err;
573 }
574
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300575 client->adapter = adapter;
576 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200577
578 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300579 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200580
581 switch (accessor_type) {
582 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
583 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300584 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200585 if (status >= 0) {
586 gsb->bdata = status;
587 status = 0;
588 }
589 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300590 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200591 }
592 break;
593
594 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
595 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300596 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200597 if (status >= 0) {
598 gsb->bdata = status;
599 status = 0;
600 }
601 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300602 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200603 gsb->bdata);
604 }
605 break;
606
607 case ACPI_GSB_ACCESS_ATTRIB_WORD:
608 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300609 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200610 if (status >= 0) {
611 gsb->wdata = status;
612 status = 0;
613 }
614 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300615 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200616 gsb->wdata);
617 }
618 break;
619
620 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
621 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300622 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200623 gsb->data);
624 if (status >= 0) {
625 gsb->len = status;
626 status = 0;
627 }
628 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300629 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200630 gsb->len, gsb->data);
631 }
632 break;
633
634 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
635 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300636 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200637 gsb->data, info->access_length);
638 if (status > 0)
639 status = 0;
640 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300641 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200642 gsb->data, info->access_length);
643 }
644 break;
645
646 default:
Wolfram Sangbe309c32016-07-09 13:35:03 +0900647 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
648 accessor_type, client->addr);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200649 ret = AE_BAD_PARAMETER;
650 goto err;
651 }
652
653 gsb->status = status;
654
655 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300656 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200657 ACPI_FREE(ares);
658 return ret;
659}
660
661
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300662static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200663{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200664 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300665 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200666 acpi_status status;
667
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200668 if (!adapter->dev.parent)
669 return -ENODEV;
670
671 handle = ACPI_HANDLE(adapter->dev.parent);
672
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200673 if (!handle)
674 return -ENODEV;
675
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300676 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200677 GFP_KERNEL);
678 if (!data)
679 return -ENOMEM;
680
681 data->adapter = adapter;
682 status = acpi_bus_attach_private_data(handle, (void *)data);
683 if (ACPI_FAILURE(status)) {
684 kfree(data);
685 return -ENOMEM;
686 }
687
688 status = acpi_install_address_space_handler(handle,
689 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300690 &i2c_acpi_space_handler,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200691 NULL,
692 data);
693 if (ACPI_FAILURE(status)) {
694 dev_err(&adapter->dev, "Error installing i2c space handler\n");
695 acpi_bus_detach_private_data(handle);
696 kfree(data);
697 return -ENOMEM;
698 }
699
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800700 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200701 return 0;
702}
703
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300704static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200705{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200706 acpi_handle handle;
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300707 struct i2c_acpi_handler_data *data;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200708 acpi_status status;
709
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200710 if (!adapter->dev.parent)
711 return;
712
713 handle = ACPI_HANDLE(adapter->dev.parent);
714
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200715 if (!handle)
716 return;
717
718 acpi_remove_address_space_handler(handle,
719 ACPI_ADR_SPACE_GSBUS,
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300720 &i2c_acpi_space_handler);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200721
722 status = acpi_bus_get_private_data(handle, (void **)&data);
723 if (ACPI_SUCCESS(status))
724 kfree(data);
725
726 acpi_bus_detach_private_data(handle);
727}
728#else /* CONFIG_ACPI_I2C_OPREGION */
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300729static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200730{ }
731
Jarkko Nikulaaec809f2016-08-12 17:02:52 +0300732static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200733{ return 0; }
734#endif /* CONFIG_ACPI_I2C_OPREGION */
735
David Brownellf37dd802007-02-13 22:09:00 +0100736/* ------------------------------------------------------------------------- */
737
Lee Jones5f441fc2016-11-07 12:47:40 +0000738const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +0200739 const struct i2c_client *client)
740{
Lee Jones811073b2016-11-07 12:47:36 +0000741 if (!(id && client))
742 return NULL;
743
Jean Delvared2653e92008-04-29 23:11:39 +0200744 while (id->name[0]) {
745 if (strcmp(client->name, id->name) == 0)
746 return id;
747 id++;
748 }
749 return NULL;
750}
Lee Jones5f441fc2016-11-07 12:47:40 +0000751EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static int i2c_device_match(struct device *dev, struct device_driver *drv)
754{
Jean Delvare51298d12009-09-18 22:45:45 +0200755 struct i2c_client *client = i2c_verify_client(dev);
756 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200757
Jean Delvare51298d12009-09-18 22:45:45 +0200758
Grant Likely959e85f2010-06-08 07:48:19 -0600759 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000760 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600761 return 1;
762
Mika Westerberg907ddf82012-11-23 12:23:40 +0100763 /* Then ACPI style match */
764 if (acpi_driver_match_device(dev, drv))
765 return 1;
766
Jean Delvare51298d12009-09-18 22:45:45 +0200767 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000768
769 /* Finally an I2C match */
770 if (i2c_match_id(driver->id_table, client))
771 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200772
Jean Delvareeb8a7902008-05-18 20:49:41 +0200773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Kay Sievers7eff2e72007-08-14 15:15:12 +0200776static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200777{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100778 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800779 int rc;
780
781 rc = acpi_device_uevent_modalias(dev, env);
782 if (rc != -ENODEV)
783 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200784
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100785 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200786}
787
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530788/* i2c bus recovery routines */
789static int get_scl_gpio_value(struct i2c_adapter *adap)
790{
791 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
792}
793
794static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
795{
796 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
797}
798
799static int get_sda_gpio_value(struct i2c_adapter *adap)
800{
801 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
802}
803
804static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
805{
806 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
807 struct device *dev = &adap->dev;
808 int ret = 0;
809
810 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
811 GPIOF_OUT_INIT_HIGH, "i2c-scl");
812 if (ret) {
813 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
814 return ret;
815 }
816
817 if (bri->get_sda) {
818 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
819 /* work without SDA polling */
820 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
821 bri->sda_gpio);
822 bri->get_sda = NULL;
823 }
824 }
825
826 return ret;
827}
828
829static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
830{
831 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
832
833 if (bri->get_sda)
834 gpio_free(bri->sda_gpio);
835
836 gpio_free(bri->scl_gpio);
837}
838
839/*
840 * We are generating clock pulses. ndelay() determines durating of clk pulses.
841 * We will generate clock with rate 100 KHz and so duration of both clock levels
842 * is: delay in ns = (10^6 / 100) / 2
843 */
844#define RECOVERY_NDELAY 5000
845#define RECOVERY_CLK_CNT 9
846
847static int i2c_generic_recovery(struct i2c_adapter *adap)
848{
849 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
850 int i = 0, val = 1, ret = 0;
851
852 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300853 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530854
Jan Luebbe8b062602015-07-08 16:35:06 +0200855 bri->set_scl(adap, val);
856 ndelay(RECOVERY_NDELAY);
857
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530858 /*
859 * By this time SCL is high, as we need to give 9 falling-rising edges
860 */
861 while (i++ < RECOVERY_CLK_CNT * 2) {
862 if (val) {
863 /* Break if SDA is high */
864 if (bri->get_sda && bri->get_sda(adap))
865 break;
866 /* SCL shouldn't be low here */
867 if (!bri->get_scl(adap)) {
868 dev_err(&adap->dev,
869 "SCL is stuck low, exit recovery\n");
870 ret = -EBUSY;
871 break;
872 }
873 }
874
875 val = !val;
876 bri->set_scl(adap, val);
877 ndelay(RECOVERY_NDELAY);
878 }
879
880 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300881 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530882
883 return ret;
884}
885
886int i2c_generic_scl_recovery(struct i2c_adapter *adap)
887{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530888 return i2c_generic_recovery(adap);
889}
Mark Brownc1c21f42015-04-15 19:18:39 +0100890EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530891
892int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
893{
894 int ret;
895
896 ret = i2c_get_gpios_for_recovery(adap);
897 if (ret)
898 return ret;
899
900 ret = i2c_generic_recovery(adap);
901 i2c_put_gpios_for_recovery(adap);
902
903 return ret;
904}
Mark Brownc1c21f42015-04-15 19:18:39 +0100905EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530906
907int i2c_recover_bus(struct i2c_adapter *adap)
908{
909 if (!adap->bus_recovery_info)
910 return -EOPNOTSUPP;
911
912 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
913 return adap->bus_recovery_info->recover_bus(adap);
914}
Mark Brownc1c21f42015-04-15 19:18:39 +0100915EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530916
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900917static void i2c_init_recovery(struct i2c_adapter *adap)
918{
919 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
920 char *err_str;
921
922 if (!bri)
923 return;
924
925 if (!bri->recover_bus) {
926 err_str = "no recover_bus() found";
927 goto err;
928 }
929
930 /* Generic GPIO recovery */
931 if (bri->recover_bus == i2c_generic_gpio_recovery) {
932 if (!gpio_is_valid(bri->scl_gpio)) {
933 err_str = "invalid SCL gpio";
934 goto err;
935 }
936
937 if (gpio_is_valid(bri->sda_gpio))
938 bri->get_sda = get_sda_gpio_value;
939 else
940 bri->get_sda = NULL;
941
942 bri->get_scl = get_scl_gpio_value;
943 bri->set_scl = set_scl_gpio_value;
944 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
945 /* Generic SCL recovery */
946 if (!bri->set_scl || !bri->get_scl) {
947 err_str = "no {get|set}_scl() found";
948 goto err;
949 }
950 }
951
952 return;
953 err:
954 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
955 adap->bus_recovery_info = NULL;
956}
957
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200958static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
959{
960 struct i2c_adapter *adap = client->adapter;
961 unsigned int irq;
962
963 if (!adap->host_notify_domain)
964 return -ENXIO;
965
966 if (client->flags & I2C_CLIENT_TEN)
967 return -EINVAL;
968
969 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
970 if (!irq)
971 irq = irq_create_mapping(adap->host_notify_domain,
972 client->addr);
973
974 return irq > 0 ? irq : -ENXIO;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int i2c_device_probe(struct device *dev)
978{
Jean Delvare51298d12009-09-18 22:45:45 +0200979 struct i2c_client *client = i2c_verify_client(dev);
980 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100981 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200982
Jean Delvare51298d12009-09-18 22:45:45 +0200983 if (!client)
984 return 0;
985
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200986 driver = to_i2c_driver(dev->driver);
987
988 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300989 int irq = -ENOENT;
990
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800991 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
992 dev_dbg(dev, "Using Host Notify IRQ\n");
993 irq = i2c_smbus_host_notify_to_irq(client);
994 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700995 irq = of_irq_get_byname(dev->of_node, "irq");
996 if (irq == -EINVAL || irq == -ENODATA)
997 irq = of_irq_get(dev->of_node, 0);
998 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300999 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001000 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +01001001 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +02001002 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -08001003
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +01001004 if (irq < 0)
1005 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +02001006
1007 client->irq = irq;
1008 }
1009
Lee Jonesda10c062016-11-07 12:47:39 +00001010 /*
1011 * An I2C ID table is not mandatory, if and only if, a suitable Device
1012 * Tree match table entry is supplied for the probing device.
1013 */
1014 if (!driver->id_table &&
1015 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +02001016 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001017
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001018 if (client->flags & I2C_CLIENT_WAKE) {
1019 int wakeirq = -ENOENT;
1020
1021 if (dev->of_node) {
1022 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
1023 if (wakeirq == -EPROBE_DEFER)
1024 return wakeirq;
1025 }
1026
1027 device_init_wakeup(&client->dev, true);
1028
1029 if (wakeirq > 0 && wakeirq != client->irq)
1030 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
1031 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +02001032 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001033 else
1034 status = 0;
1035
1036 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001037 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001038 }
1039
David Brownell7b4fbc52007-05-01 23:26:30 +02001040 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +02001041
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02001042 status = of_clk_set_defaults(dev->of_node, false);
1043 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001044 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +02001045
Ulf Hanssone09b0d42014-09-19 20:27:39 +02001046 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +01001047 if (status == -EPROBE_DEFER)
1048 goto err_clear_wakeup_irq;
1049
Lee Jonesb8a1a4c2016-11-07 12:47:41 +00001050 /*
1051 * When there are no more users of probe(),
1052 * rename probe_new to probe.
1053 */
1054 if (driver->probe_new)
1055 status = driver->probe_new(client);
1056 else if (driver->probe)
1057 status = driver->probe(client,
1058 i2c_match_id(driver->id_table, client));
1059 else
1060 status = -EINVAL;
1061
Kieran Bingham74cedd32015-10-12 21:54:43 +01001062 if (status)
1063 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001064
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001065 return 0;
1066
1067err_detach_pm_domain:
1068 dev_pm_domain_detach(&client->dev, true);
1069err_clear_wakeup_irq:
1070 dev_pm_clear_wake_irq(&client->dev);
1071 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +01001072 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static int i2c_device_remove(struct device *dev)
1076{
Jean Delvare51298d12009-09-18 22:45:45 +02001077 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +02001078 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +01001079 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +02001080
Jean Delvare51298d12009-09-18 22:45:45 +02001081 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +02001082 return 0;
1083
1084 driver = to_i2c_driver(dev->driver);
1085 if (driver->remove) {
1086 dev_dbg(dev, "remove\n");
1087 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +02001088 }
Wolfram Sang72fa8182014-01-21 17:48:34 +01001089
Ulf Hanssone09b0d42014-09-19 20:27:39 +02001090 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -07001091
1092 dev_pm_clear_wake_irq(&client->dev);
1093 device_init_wakeup(&client->dev, false);
1094
David Brownella1d9e6e2007-05-01 23:26:30 +02001095 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
David Brownellf37dd802007-02-13 22:09:00 +01001098static void i2c_device_shutdown(struct device *dev)
1099{
Jean Delvare51298d12009-09-18 22:45:45 +02001100 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +01001101 struct i2c_driver *driver;
1102
Jean Delvare51298d12009-09-18 22:45:45 +02001103 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +01001104 return;
1105 driver = to_i2c_driver(dev->driver);
1106 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +02001107 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +01001108}
1109
David Brownell9c1600e2007-05-01 23:26:31 +02001110static void i2c_client_dev_release(struct device *dev)
1111{
1112 kfree(to_i2c_client(dev));
1113}
1114
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001115static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +02001116show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001117{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001118 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
1119 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001120}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001121static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +02001122
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001123static ssize_t
1124show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +02001125{
1126 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +08001127 int len;
1128
1129 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
1130 if (len != -ENODEV)
1131 return len;
1132
Jean Delvareeb8a7902008-05-18 20:49:41 +02001133 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +02001134}
Jean Delvare51298d12009-09-18 22:45:45 +02001135static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
1136
1137static struct attribute *i2c_dev_attrs[] = {
1138 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +02001139 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +02001140 &dev_attr_modalias.attr,
1141 NULL
1142};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001143ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +01001144
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001145struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +01001146 .name = "i2c",
1147 .match = i2c_device_match,
1148 .probe = i2c_device_probe,
1149 .remove = i2c_device_remove,
1150 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +00001151};
Jon Smirle9ca9eb2008-07-14 22:38:35 +02001152EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +00001153
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001154struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001155 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +02001156 .uevent = i2c_device_uevent,
1157 .release = i2c_client_dev_release,
1158};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -08001159EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +02001160
David Brownell9b766b82008-01-27 18:14:51 +01001161
1162/**
1163 * i2c_verify_client - return parameter as i2c_client, or NULL
1164 * @dev: device, probably from some driver model iterator
1165 *
1166 * When traversing the driver model tree, perhaps using driver model
1167 * iterators like @device_for_each_child(), you can't assume very much
1168 * about the nodes you find. Use this function to avoid oopses caused
1169 * by wrongly treating some non-I2C device as an i2c_client.
1170 */
1171struct i2c_client *i2c_verify_client(struct device *dev)
1172{
Jean Delvare51298d12009-09-18 22:45:45 +02001173 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +01001174 ? to_i2c_client(dev)
1175 : NULL;
1176}
1177EXPORT_SYMBOL(i2c_verify_client);
1178
1179
Wolfram Sangda899f52015-05-18 21:09:12 +02001180/* Return a unique address which takes the flags of the client into account */
1181static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
1182{
1183 unsigned short addr = client->addr;
1184
1185 /* For some client flags, add an arbitrary offset to avoid collisions */
1186 if (client->flags & I2C_CLIENT_TEN)
1187 addr |= I2C_ADDR_OFFSET_TEN_BIT;
1188
1189 if (client->flags & I2C_CLIENT_SLAVE)
1190 addr |= I2C_ADDR_OFFSET_SLAVE;
1191
1192 return addr;
1193}
1194
Jean Delvare3a89db52010-06-03 11:33:52 +02001195/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001196 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001197static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +02001198{
Wolfram Sangc4019b72015-07-17 12:50:06 +02001199 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +02001200 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001201 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +02001202 return -EINVAL;
1203 } else {
1204 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +02001205 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +02001206 return -EINVAL;
1207 }
1208 return 0;
1209}
1210
Jean Delvare656b8762010-06-03 11:33:53 +02001211/* And this is a strict address validity check, used when probing. If a
1212 * device uses a reserved address, then it shouldn't be probed. 7-bit
1213 * addressing is assumed, 10-bit address devices are rare and should be
1214 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +02001215int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +02001216{
1217 /*
1218 * Reserved addresses per I2C specification:
1219 * 0x00 General call address / START byte
1220 * 0x01 CBUS address
1221 * 0x02 Reserved for different bus format
1222 * 0x03 Reserved for future purposes
1223 * 0x04-0x07 Hs-mode master code
1224 * 0x78-0x7b 10-bit slave addressing
1225 * 0x7c-0x7f Reserved for future purposes
1226 */
1227 if (addr < 0x08 || addr > 0x77)
1228 return -EINVAL;
1229 return 0;
1230}
1231
Jean Delvare3b5f7942010-06-03 11:33:55 +02001232static int __i2c_check_addr_busy(struct device *dev, void *addrp)
1233{
1234 struct i2c_client *client = i2c_verify_client(dev);
1235 int addr = *(int *)addrp;
1236
Wolfram Sang9bccc702015-07-17 14:48:56 +02001237 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +02001238 return -EBUSY;
1239 return 0;
1240}
1241
Michael Lawnick08263742010-08-11 18:21:02 +02001242/* walk up mux tree */
1243static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
1244{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001245 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001246 int result;
1247
1248 result = device_for_each_child(&adapter->dev, &addr,
1249 __i2c_check_addr_busy);
1250
Jean Delvare97cc4d42010-10-24 18:16:57 +02001251 if (!result && parent)
1252 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001253
1254 return result;
1255}
1256
1257/* recurse down mux tree */
1258static int i2c_check_mux_children(struct device *dev, void *addrp)
1259{
1260 int result;
1261
1262 if (dev->type == &i2c_adapter_type)
1263 result = device_for_each_child(dev, addrp,
1264 i2c_check_mux_children);
1265 else
1266 result = __i2c_check_addr_busy(dev, addrp);
1267
1268 return result;
1269}
1270
Jean Delvare3b5f7942010-06-03 11:33:55 +02001271static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
1272{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001273 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +02001274 int result = 0;
1275
Jean Delvare97cc4d42010-10-24 18:16:57 +02001276 if (parent)
1277 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +02001278
1279 if (!result)
1280 result = device_for_each_child(&adapter->dev, &addr,
1281 i2c_check_mux_children);
1282
1283 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +02001284}
1285
David Brownell9c1600e2007-05-01 23:26:31 +02001286/**
Peter Rosin8320f492016-05-04 22:15:27 +02001287 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001288 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001289 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1290 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001291 */
Peter Rosin8320f492016-05-04 22:15:27 +02001292static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
1293 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001294{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001295 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001296}
Jean Delvarefe61e072010-08-11 18:20:58 +02001297
1298/**
Peter Rosin8320f492016-05-04 22:15:27 +02001299 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001300 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001301 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1302 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001303 */
Peter Rosin8320f492016-05-04 22:15:27 +02001304static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
1305 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001306{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001307 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001308}
1309
1310/**
Peter Rosin8320f492016-05-04 22:15:27 +02001311 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +02001312 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +02001313 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1314 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +02001315 */
Peter Rosin8320f492016-05-04 22:15:27 +02001316static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
1317 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +02001318{
Peter Rosinfa96f0c2016-05-04 22:15:28 +02001319 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001320}
Jean Delvarefe61e072010-08-11 18:20:58 +02001321
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001322static void i2c_dev_set_name(struct i2c_adapter *adap,
1323 struct i2c_client *client)
1324{
1325 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1326
1327 if (adev) {
1328 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1329 return;
1330 }
1331
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001332 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +02001333 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001334}
1335
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001336static int i2c_dev_irq_from_resources(const struct resource *resources,
1337 unsigned int num_resources)
1338{
1339 struct irq_data *irqd;
1340 int i;
1341
1342 for (i = 0; i < num_resources; i++) {
1343 const struct resource *r = &resources[i];
1344
1345 if (resource_type(r) != IORESOURCE_IRQ)
1346 continue;
1347
1348 if (r->flags & IORESOURCE_BITS) {
1349 irqd = irq_get_irq_data(r->start);
1350 if (!irqd)
1351 break;
1352
1353 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
1354 }
1355
1356 return r->start;
1357 }
1358
1359 return 0;
1360}
1361
Jean Delvarefe61e072010-08-11 18:20:58 +02001362/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001363 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001364 * @adap: the adapter managing the device
1365 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001366 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001367 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001368 * Create an i2c device. Binding is handled through driver model
1369 * probe()/remove() methods. A driver may be bound to this device when we
1370 * return from this function, or any later moment (e.g. maybe hotplugging will
1371 * load the driver module). This call is not appropriate for use by mainboard
1372 * initialization logic, which usually runs during an arch_initcall() long
1373 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001374 *
1375 * This returns the new i2c client, which may be saved for later use with
1376 * i2c_unregister_device(); or NULL to indicate an error.
1377 */
1378struct i2c_client *
1379i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1380{
1381 struct i2c_client *client;
1382 int status;
1383
1384 client = kzalloc(sizeof *client, GFP_KERNEL);
1385 if (!client)
1386 return NULL;
1387
1388 client->adapter = adap;
1389
1390 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001391
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001392 if (info->archdata)
1393 client->dev.archdata = *info->archdata;
1394
Marc Pignatee354252008-08-28 08:33:22 +02001395 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001396 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001397
David Brownell9c1600e2007-05-01 23:26:31 +02001398 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -08001399 if (!client->irq)
1400 client->irq = i2c_dev_irq_from_resources(info->resources,
1401 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +02001402
David Brownell9c1600e2007-05-01 23:26:31 +02001403 strlcpy(client->name, info->type, sizeof(client->name));
1404
Wolfram Sangc4019b72015-07-17 12:50:06 +02001405 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001406 if (status) {
1407 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1408 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1409 goto out_err_silent;
1410 }
1411
Jean Delvaref8a227e2009-06-19 16:58:18 +02001412 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001413 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001414 if (status)
1415 goto out_err;
1416
1417 client->dev.parent = &client->adapter->dev;
1418 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001419 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001420 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001421 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001422
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001423 i2c_dev_set_name(adap, client);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001424
1425 if (info->properties) {
1426 status = device_add_properties(&client->dev, info->properties);
1427 if (status) {
1428 dev_err(&adap->dev,
1429 "Failed to add properties to client %s: %d\n",
1430 client->name, status);
1431 goto out_err;
1432 }
1433 }
1434
Jean Delvaref8a227e2009-06-19 16:58:18 +02001435 status = device_register(&client->dev);
1436 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001437 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001438
Jean Delvaref8a227e2009-06-19 16:58:18 +02001439 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1440 client->name, dev_name(&client->dev));
1441
David Brownell9c1600e2007-05-01 23:26:31 +02001442 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001443
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -08001444out_free_props:
1445 if (info->properties)
1446 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001447out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001448 dev_err(&adap->dev,
1449 "Failed to register i2c client %s at 0x%02x (%d)\n",
1450 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001451out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001452 kfree(client);
1453 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001454}
1455EXPORT_SYMBOL_GPL(i2c_new_device);
1456
1457
1458/**
1459 * i2c_unregister_device - reverse effect of i2c_new_device()
1460 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001461 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001462 */
1463void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001464{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001465 if (client->dev.of_node)
1466 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001467 if (ACPI_COMPANION(&client->dev))
1468 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +02001469 device_unregister(&client->dev);
1470}
David Brownell9c1600e2007-05-01 23:26:31 +02001471EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001472
1473
Jean Delvare60b129d2008-05-11 20:37:06 +02001474static const struct i2c_device_id dummy_id[] = {
1475 { "dummy", 0 },
1476 { },
1477};
1478
Jean Delvared2653e92008-04-29 23:11:39 +02001479static int dummy_probe(struct i2c_client *client,
1480 const struct i2c_device_id *id)
1481{
1482 return 0;
1483}
1484
1485static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001486{
1487 return 0;
1488}
1489
1490static struct i2c_driver dummy_driver = {
1491 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001492 .probe = dummy_probe,
1493 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001494 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001495};
1496
1497/**
1498 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1499 * @adapter: the adapter managing the device
1500 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001501 * Context: can sleep
1502 *
1503 * This returns an I2C client bound to the "dummy" driver, intended for use
1504 * with devices that consume multiple addresses. Examples of such chips
1505 * include various EEPROMS (like 24c04 and 24c08 models).
1506 *
1507 * These dummy devices have two main uses. First, most I2C and SMBus calls
1508 * except i2c_transfer() need a client handle; the dummy will be that handle.
1509 * And second, this prevents the specified address from being bound to a
1510 * different driver.
1511 *
1512 * This returns the new i2c client, which should be saved for later use with
1513 * i2c_unregister_device(); or NULL to indicate an error.
1514 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001515struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001516{
1517 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001518 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001519 };
1520
David Brownelle9f13732008-01-27 18:14:52 +01001521 return i2c_new_device(adapter, &info);
1522}
1523EXPORT_SYMBOL_GPL(i2c_new_dummy);
1524
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001525/**
1526 * i2c_new_secondary_device - Helper to get the instantiated secondary address
1527 * and create the associated device
1528 * @client: Handle to the primary client
1529 * @name: Handle to specify which secondary address to get
1530 * @default_addr: Used as a fallback if no secondary address was specified
1531 * Context: can sleep
1532 *
1533 * I2C clients can be composed of multiple I2C slaves bound together in a single
1534 * component. The I2C client driver then binds to the master I2C slave and needs
1535 * to create I2C dummy clients to communicate with all the other slaves.
1536 *
1537 * This function creates and returns an I2C dummy client whose I2C address is
1538 * retrieved from the platform firmware based on the given slave name. If no
1539 * address is specified by the firmware default_addr is used.
1540 *
1541 * On DT-based platforms the address is retrieved from the "reg" property entry
1542 * cell whose "reg-names" value matches the slave name.
1543 *
1544 * This returns the new i2c client, which should be saved for later use with
1545 * i2c_unregister_device(); or NULL to indicate an error.
1546 */
1547struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1548 const char *name,
1549 u16 default_addr)
1550{
1551 struct device_node *np = client->dev.of_node;
1552 u32 addr = default_addr;
1553 int i;
1554
1555 if (np) {
1556 i = of_property_match_string(np, "reg-names", name);
1557 if (i >= 0)
1558 of_property_read_u32_index(np, "reg", i, &addr);
1559 }
1560
1561 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
1562 return i2c_new_dummy(client->adapter, addr);
1563}
1564EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1565
David Brownellf37dd802007-02-13 22:09:00 +01001566/* ------------------------------------------------------------------------- */
1567
David Brownell16ffadf2007-05-01 23:26:28 +02001568/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1569
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001570static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
David Brownellef2c83212007-05-01 23:26:28 +02001572 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 complete(&adap->dev_released);
1574}
1575
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001576unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001577{
1578 unsigned int depth = 0;
1579
1580 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1581 depth++;
1582
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001583 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1584 "adapter depth exceeds lockdep subclass limit\n");
1585
Jean Delvare390946b2012-09-10 10:14:02 +02001586 return depth;
1587}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001588EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001589
1590/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001591 * Let users instantiate I2C devices through sysfs. This can be used when
1592 * platform initialization code doesn't contain the proper data for
1593 * whatever reason. Also useful for drivers that do device detection and
1594 * detection fails, either because the device uses an unexpected address,
1595 * or this is a compatible device with different ID register values.
1596 *
1597 * Parameter checking may look overzealous, but we really don't want
1598 * the user to provide incorrect parameters.
1599 */
1600static ssize_t
1601i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1602 const char *buf, size_t count)
1603{
1604 struct i2c_adapter *adap = to_i2c_adapter(dev);
1605 struct i2c_board_info info;
1606 struct i2c_client *client;
1607 char *blank, end;
1608 int res;
1609
Jean Delvare99cd8e22009-06-19 16:58:20 +02001610 memset(&info, 0, sizeof(struct i2c_board_info));
1611
1612 blank = strchr(buf, ' ');
1613 if (!blank) {
1614 dev_err(dev, "%s: Missing parameters\n", "new_device");
1615 return -EINVAL;
1616 }
1617 if (blank - buf > I2C_NAME_SIZE - 1) {
1618 dev_err(dev, "%s: Invalid device name\n", "new_device");
1619 return -EINVAL;
1620 }
1621 memcpy(info.type, buf, blank - buf);
1622
1623 /* Parse remaining parameters, reject extra parameters */
1624 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1625 if (res < 1) {
1626 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1627 return -EINVAL;
1628 }
1629 if (res > 1 && end != '\n') {
1630 dev_err(dev, "%s: Extra parameters\n", "new_device");
1631 return -EINVAL;
1632 }
1633
Wolfram Sangcfa03272015-07-27 14:03:38 +02001634 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1635 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1636 info.flags |= I2C_CLIENT_TEN;
1637 }
1638
1639 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1640 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1641 info.flags |= I2C_CLIENT_SLAVE;
1642 }
1643
Jean Delvare99cd8e22009-06-19 16:58:20 +02001644 client = i2c_new_device(adap, &info);
1645 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001646 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001647
1648 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001649 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001650 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001651 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001652 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1653 info.type, info.addr);
1654
1655 return count;
1656}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001657static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001658
1659/*
1660 * And of course let the users delete the devices they instantiated, if
1661 * they got it wrong. This interface can only be used to delete devices
1662 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1663 * don't delete devices to which some kernel code still has references.
1664 *
1665 * Parameter checking may look overzealous, but we really don't want
1666 * the user to delete the wrong device.
1667 */
1668static ssize_t
1669i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1670 const char *buf, size_t count)
1671{
1672 struct i2c_adapter *adap = to_i2c_adapter(dev);
1673 struct i2c_client *client, *next;
1674 unsigned short addr;
1675 char end;
1676 int res;
1677
1678 /* Parse parameters, reject extra parameters */
1679 res = sscanf(buf, "%hi%c", &addr, &end);
1680 if (res < 1) {
1681 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1682 return -EINVAL;
1683 }
1684 if (res > 1 && end != '\n') {
1685 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1686 return -EINVAL;
1687 }
1688
1689 /* Make sure the device was added through sysfs */
1690 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001691 mutex_lock_nested(&adap->userspace_clients_lock,
1692 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001693 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1694 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001695 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001696 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1697 "delete_device", client->name, client->addr);
1698
1699 list_del(&client->detected);
1700 i2c_unregister_device(client);
1701 res = count;
1702 break;
1703 }
1704 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001705 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001706
1707 if (res < 0)
1708 dev_err(dev, "%s: Can't find device in list\n",
1709 "delete_device");
1710 return res;
1711}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001712static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1713 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001714
1715static struct attribute *i2c_adapter_attrs[] = {
1716 &dev_attr_name.attr,
1717 &dev_attr_new_device.attr,
1718 &dev_attr_delete_device.attr,
1719 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001720};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001721ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001722
Michael Lawnick08263742010-08-11 18:21:02 +02001723struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001724 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001725 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001726};
Michael Lawnick08263742010-08-11 18:21:02 +02001727EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Stephen Warren643dd092012-04-17 12:43:33 -06001729/**
1730 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1731 * @dev: device, probably from some driver model iterator
1732 *
1733 * When traversing the driver model tree, perhaps using driver model
1734 * iterators like @device_for_each_child(), you can't assume very much
1735 * about the nodes you find. Use this function to avoid oopses caused
1736 * by wrongly treating some non-I2C device as an i2c_adapter.
1737 */
1738struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1739{
1740 return (dev->type == &i2c_adapter_type)
1741 ? to_i2c_adapter(dev)
1742 : NULL;
1743}
1744EXPORT_SYMBOL(i2c_verify_adapter);
1745
Jean Delvare2bb50952009-09-18 22:45:46 +02001746#ifdef CONFIG_I2C_COMPAT
1747static struct class_compat *i2c_adapter_compat_class;
1748#endif
1749
David Brownell9c1600e2007-05-01 23:26:31 +02001750static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1751{
1752 struct i2c_devinfo *devinfo;
1753
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001754 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001755 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1756 if (devinfo->busnum == adapter->nr
1757 && !i2c_new_device(adapter,
1758 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001759 dev_err(&adapter->dev,
1760 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001761 devinfo->board_info.addr);
1762 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001763 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001764}
1765
Wolfram Sang687b81d2013-07-11 12:56:15 +01001766/* OF support code */
1767
1768#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001769static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1770 struct device_node *node)
1771{
1772 struct i2c_client *result;
1773 struct i2c_board_info info = {};
1774 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001775 const __be32 *addr_be;
1776 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001777 int len;
1778
1779 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1780
1781 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1782 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1783 node->full_name);
1784 return ERR_PTR(-EINVAL);
1785 }
1786
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001787 addr_be = of_get_property(node, "reg", &len);
1788 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001789 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1790 node->full_name);
1791 return ERR_PTR(-EINVAL);
1792 }
1793
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001794 addr = be32_to_cpup(addr_be);
1795 if (addr & I2C_TEN_BIT_ADDRESS) {
1796 addr &= ~I2C_TEN_BIT_ADDRESS;
1797 info.flags |= I2C_CLIENT_TEN;
1798 }
1799
1800 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1801 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1802 info.flags |= I2C_CLIENT_SLAVE;
1803 }
1804
1805 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001806 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
John Garry6f724fb2017-01-06 19:02:57 +08001807 addr, node->full_name);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001808 return ERR_PTR(-EINVAL);
1809 }
1810
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001811 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001812 info.of_node = of_node_get(node);
1813 info.archdata = &dev_ad;
1814
Dmitry Torokhov331c3422017-01-04 20:57:22 -08001815 if (of_property_read_bool(node, "host-notify"))
1816 info.flags |= I2C_CLIENT_HOST_NOTIFY;
1817
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001818 if (of_get_property(node, "wakeup-source", NULL))
1819 info.flags |= I2C_CLIENT_WAKE;
1820
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001821 result = i2c_new_device(adap, &info);
1822 if (result == NULL) {
1823 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1824 node->full_name);
1825 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001826 return ERR_PTR(-EINVAL);
1827 }
1828 return result;
1829}
1830
Wolfram Sang687b81d2013-07-11 12:56:15 +01001831static void of_i2c_register_devices(struct i2c_adapter *adap)
1832{
Jon Hunter7e4c2242016-06-29 10:17:53 +01001833 struct device_node *bus, *node;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001834 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001835
1836 /* Only register child devices if the adapter has a node pointer set */
1837 if (!adap->dev.of_node)
1838 return;
1839
1840 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1841
Jon Hunter7e4c2242016-06-29 10:17:53 +01001842 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
1843 if (!bus)
1844 bus = of_node_get(adap->dev.of_node);
1845
1846 for_each_available_child_of_node(bus, node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001847 if (of_node_test_and_set_flag(node, OF_POPULATED))
1848 continue;
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02001849
1850 client = of_i2c_register_device(adap, node);
1851 if (IS_ERR(client)) {
1852 dev_warn(&adap->dev,
1853 "Failed to create I2C device for %s\n",
1854 node->full_name);
1855 of_node_clear_flag(node, OF_POPULATED);
1856 }
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001857 }
Jon Hunter7e4c2242016-06-29 10:17:53 +01001858
1859 of_node_put(bus);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001860}
1861
1862static int of_dev_node_match(struct device *dev, void *data)
1863{
1864 return dev->of_node == data;
1865}
1866
1867/* must call put_device() when done with returned i2c_client device */
1868struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1869{
1870 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001871 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001872
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001873 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001874 if (!dev)
1875 return NULL;
1876
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001877 client = i2c_verify_client(dev);
1878 if (!client)
1879 put_device(dev);
1880
1881 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001882}
1883EXPORT_SYMBOL(of_find_i2c_device_by_node);
1884
1885/* must call put_device() when done with returned i2c_adapter device */
1886struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1887{
1888 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001889 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001890
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001891 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001892 if (!dev)
1893 return NULL;
1894
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001895 adapter = i2c_verify_adapter(dev);
1896 if (!adapter)
1897 put_device(dev);
1898
1899 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001900}
1901EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001902
1903/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1904struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1905{
1906 struct i2c_adapter *adapter;
1907
1908 adapter = of_find_i2c_adapter_by_node(node);
1909 if (!adapter)
1910 return NULL;
1911
1912 if (!try_module_get(adapter->owner)) {
1913 put_device(&adapter->dev);
1914 adapter = NULL;
1915 }
1916
1917 return adapter;
1918}
1919EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Lee Jonescabcf4f2016-11-07 12:47:37 +00001920
1921static const struct of_device_id*
1922i2c_of_match_device_sysfs(const struct of_device_id *matches,
1923 struct i2c_client *client)
1924{
1925 const char *name;
1926
1927 for (; matches->compatible[0]; matches++) {
1928 /*
1929 * Adding devices through the i2c sysfs interface provides us
1930 * a string to match which may be compatible with the device
1931 * tree compatible strings, however with no actual of_node the
1932 * of_match_device() will not match
1933 */
1934 if (sysfs_streq(client->name, matches->compatible))
1935 return matches;
1936
1937 name = strchr(matches->compatible, ',');
1938 if (!name)
1939 name = matches->compatible;
1940 else
1941 name++;
1942
1943 if (sysfs_streq(client->name, name))
1944 return matches;
1945 }
1946
1947 return NULL;
1948}
1949
Lee Jones298d4de2016-11-07 12:47:38 +00001950const struct of_device_id
1951*i2c_of_match_device(const struct of_device_id *matches,
1952 struct i2c_client *client)
1953{
1954 const struct of_device_id *match;
1955
1956 if (!(client && matches))
1957 return NULL;
1958
1959 match = of_match_device(matches, &client->dev);
1960 if (match)
1961 return match;
1962
1963 return i2c_of_match_device_sysfs(matches, client);
1964}
1965EXPORT_SYMBOL_GPL(i2c_of_match_device);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001966#else
1967static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1968#endif /* CONFIG_OF */
1969
Jean Delvare69b00892009-12-06 17:06:27 +01001970static int i2c_do_add_adapter(struct i2c_driver *driver,
1971 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001972{
Jean Delvare4735c982008-07-14 22:38:36 +02001973 /* Detect supported devices on that bus, and instantiate them */
1974 i2c_detect(adap, driver);
1975
1976 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001977 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001978 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1979 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001980 dev_warn(&adap->dev,
1981 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001982 /* We ignore the return code; if it fails, too bad */
1983 driver->attach_adapter(adap);
1984 }
1985 return 0;
1986}
1987
Jean Delvare69b00892009-12-06 17:06:27 +01001988static int __process_new_adapter(struct device_driver *d, void *data)
1989{
1990 return i2c_do_add_adapter(to_i2c_driver(d), data);
1991}
1992
Peter Rosind1ed7982016-08-25 23:07:01 +02001993static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1994 .lock_bus = i2c_adapter_lock_bus,
1995 .trylock_bus = i2c_adapter_trylock_bus,
1996 .unlock_bus = i2c_adapter_unlock_bus,
1997};
1998
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001999static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
2000{
2001 struct irq_domain *domain = adap->host_notify_domain;
2002 irq_hw_number_t hwirq;
2003
2004 if (!domain)
2005 return;
2006
2007 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
2008 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
2009
2010 irq_domain_remove(domain);
2011 adap->host_notify_domain = NULL;
2012}
2013
2014static int i2c_host_notify_irq_map(struct irq_domain *h,
2015 unsigned int virq,
2016 irq_hw_number_t hw_irq_num)
2017{
2018 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
2019
2020 return 0;
2021}
2022
2023static const struct irq_domain_ops i2c_host_notify_irq_ops = {
2024 .map = i2c_host_notify_irq_map,
2025};
2026
2027static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
2028{
2029 struct irq_domain *domain;
2030
2031 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
2032 return 0;
2033
2034 domain = irq_domain_create_linear(adap->dev.fwnode,
2035 I2C_ADDR_7BITS_COUNT,
2036 &i2c_host_notify_irq_ops, adap);
2037 if (!domain)
2038 return -ENOMEM;
2039
2040 adap->host_notify_domain = domain;
2041
2042 return 0;
2043}
2044
2045/**
2046 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
2047 * I2C client.
2048 * @adap: the adapter
2049 * @addr: the I2C address of the notifying device
2050 * Context: can't sleep
2051 *
2052 * Helper function to be called from an I2C bus driver's interrupt
2053 * handler. It will schedule the Host Notify IRQ.
2054 */
2055int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
2056{
2057 int irq;
2058
2059 if (!adap)
2060 return -EINVAL;
2061
2062 irq = irq_find_mapping(adap->host_notify_domain, addr);
2063 if (irq <= 0)
2064 return -ENXIO;
2065
2066 generic_handle_irq(irq);
2067
2068 return 0;
2069}
2070EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
2071
David Brownell6e13e642007-05-01 23:26:31 +02002072static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002074 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
David Brownell1d0b19c2008-10-14 17:30:05 +02002076 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302077 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02002078 res = -EAGAIN;
2079 goto out_list;
2080 }
David Brownell1d0b19c2008-10-14 17:30:05 +02002081
Jean Delvare2236baa2010-11-15 22:40:38 +01002082 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002083 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002084 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002085
2086 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002087 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09002088 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01002089 }
2090
Peter Rosind1ed7982016-08-25 23:07:01 +02002091 if (!adap->lock_ops)
2092 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02002093
Mika Kuoppala194684e2009-12-06 17:06:22 +01002094 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02002095 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02002096 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02002097 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Jean Delvare8fcfef62009-03-28 21:34:43 +01002099 /* Set default timeout to 1 second if not already set */
2100 if (adap->timeout == 0)
2101 adap->timeout = HZ;
2102
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02002103 /* register soft irqs for Host Notify */
2104 res = i2c_setup_host_notify_irq_domain(adap);
2105 if (res) {
2106 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
2107 adap->name, res);
2108 goto out_list;
2109 }
2110
Kay Sievers27d9c182009-01-07 14:29:16 +01002111 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02002112 adap->dev.bus = &i2c_bus_type;
2113 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02002114 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002115 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002116 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02002117 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02002120 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
2121
Charles Keepax6ada5c12015-04-16 13:05:19 +01002122 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02002123 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01002124 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01002125
Jean Delvare2bb50952009-09-18 22:45:46 +02002126#ifdef CONFIG_I2C_COMPAT
2127 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
2128 adap->dev.parent);
2129 if (res)
2130 dev_warn(&adap->dev,
2131 "Failed to create compatibility class link\n");
2132#endif
2133
Wolfram Sangd3b11d82016-07-09 13:34:59 +09002134 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05302135
Jean Delvare729d6dd2009-06-19 16:58:18 +02002136 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01002137 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002138 i2c_acpi_register_devices(adap);
2139 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01002140
David Brownell6e13e642007-05-01 23:26:31 +02002141 if (adap->nr < __i2c_first_dynamic_bus_num)
2142 i2c_scan_static_board_info(adap);
2143
Jean Delvare4735c982008-07-14 22:38:36 +02002144 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002145 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02002146 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01002147 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002148
2149 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02002150
Jean Delvareb119c6c2006-08-15 18:26:30 +02002151out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02002152 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02002153 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002154 mutex_unlock(&core_lock);
2155 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
David Brownell6e13e642007-05-01 23:26:31 +02002158/**
Doug Andersonee5c2742013-03-01 08:57:31 -08002159 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
2160 * @adap: the adapter to register (with adap->nr initialized)
2161 * Context: can sleep
2162 *
2163 * See i2c_add_numbered_adapter() for details.
2164 */
2165static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
2166{
Wolfram Sang84d0b612016-07-09 13:35:01 +09002167 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08002168
2169 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002170 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08002171 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002172 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08002173 return id == -ENOSPC ? -EBUSY : id;
2174
2175 return i2c_register_adapter(adap);
2176}
2177
2178/**
David Brownell6e13e642007-05-01 23:26:31 +02002179 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
2180 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02002181 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002182 *
2183 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08002184 * doesn't matter or when its bus number is specified by an dt alias.
2185 * Examples of bases when the bus number doesn't matter: I2C adapters
2186 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02002187 *
2188 * When this returns zero, a new bus number was allocated and stored
2189 * in adap->nr, and the specified adapter became available for clients.
2190 * Otherwise, a negative errno value is returned.
2191 */
2192int i2c_add_adapter(struct i2c_adapter *adapter)
2193{
Doug Andersonee5c2742013-03-01 08:57:31 -08002194 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08002195 int id;
David Brownell6e13e642007-05-01 23:26:31 +02002196
Doug Andersonee5c2742013-03-01 08:57:31 -08002197 if (dev->of_node) {
2198 id = of_alias_get_id(dev->of_node, "i2c");
2199 if (id >= 0) {
2200 adapter->nr = id;
2201 return __i2c_add_numbered_adapter(adapter);
2202 }
2203 }
2204
Jean Delvarecaada322008-01-27 18:14:49 +01002205 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08002206 id = idr_alloc(&i2c_adapter_idr, adapter,
2207 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01002208 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09002209 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08002210 return id;
David Brownell6e13e642007-05-01 23:26:31 +02002211
2212 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08002213
David Brownell6e13e642007-05-01 23:26:31 +02002214 return i2c_register_adapter(adapter);
2215}
2216EXPORT_SYMBOL(i2c_add_adapter);
2217
2218/**
2219 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
2220 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02002221 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02002222 *
2223 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01002224 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
2225 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02002226 * is used to properly configure I2C devices.
2227 *
Grant Likely488bf312011-07-25 17:49:43 +02002228 * If the requested bus number is set to -1, then this function will behave
2229 * identically to i2c_add_adapter, and will dynamically assign a bus number.
2230 *
David Brownell6e13e642007-05-01 23:26:31 +02002231 * If no devices have pre-been declared for this bus, then be sure to
2232 * register the adapter before any dynamically allocated ones. Otherwise
2233 * the required bus ID may not be available.
2234 *
2235 * When this returns zero, the specified adapter became available for
2236 * clients using the bus number provided in adap->nr. Also, the table
2237 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
2238 * and the appropriate driver model device nodes are created. Otherwise, a
2239 * negative errno value is returned.
2240 */
2241int i2c_add_numbered_adapter(struct i2c_adapter *adap)
2242{
Grant Likely488bf312011-07-25 17:49:43 +02002243 if (adap->nr == -1) /* -1 means dynamically assign bus id */
2244 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002245
Doug Andersonee5c2742013-03-01 08:57:31 -08002246 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02002247}
2248EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
2249
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002250static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01002251 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01002252{
Jean Delvare4735c982008-07-14 22:38:36 +02002253 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01002254
Jean Delvareacec2112009-03-28 21:34:40 +01002255 /* Remove the devices we created ourselves as the result of hardware
2256 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02002257 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
2258 if (client->adapter == adapter) {
2259 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
2260 client->name, client->addr);
2261 list_del(&client->detected);
2262 i2c_unregister_device(client);
2263 }
2264 }
Jean Delvare026526f2008-01-27 18:14:49 +01002265}
2266
Jean Delvaree549c2b2009-06-19 16:58:19 +02002267static int __unregister_client(struct device *dev, void *dummy)
2268{
2269 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01002270 if (client && strcmp(client->name, "dummy"))
2271 i2c_unregister_device(client);
2272 return 0;
2273}
2274
2275static int __unregister_dummy(struct device *dev, void *dummy)
2276{
2277 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02002278 if (client)
2279 i2c_unregister_device(client);
2280 return 0;
2281}
2282
Jean Delvare69b00892009-12-06 17:06:27 +01002283static int __process_removed_adapter(struct device_driver *d, void *data)
2284{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002285 i2c_do_del_adapter(to_i2c_driver(d), data);
2286 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002287}
2288
David Brownelld64f73b2007-07-12 14:12:28 +02002289/**
2290 * i2c_del_adapter - unregister I2C adapter
2291 * @adap: the adapter being unregistered
2292 * Context: can sleep
2293 *
2294 * This unregisters an I2C adapter which was previously registered
2295 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
2296 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002297void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Jean Delvare35fc37f2009-06-19 16:58:19 +02002299 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002300 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002303 mutex_lock(&core_lock);
2304 found = idr_find(&i2c_adapter_idr, adap->nr);
2305 mutex_unlock(&core_lock);
2306 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09002307 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00002308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03002311 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01002312 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002313 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002314 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01002315 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002316 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002318 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02002319 mutex_lock_nested(&adap->userspace_clients_lock,
2320 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02002321 list_for_each_entry_safe(client, next, &adap->userspace_clients,
2322 detected) {
2323 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
2324 client->addr);
2325 list_del(&client->detected);
2326 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002327 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02002328 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01002329
Jean Delvaree549c2b2009-06-19 16:58:19 +02002330 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01002331 * check the returned value. This is a two-pass process, because
2332 * we can't remove the dummy devices during the first pass: they
2333 * could have been instantiated by real devices wishing to clean
2334 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002335 device_for_each_child(&adap->dev, NULL, __unregister_client);
2336 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Jean Delvare2bb50952009-09-18 22:45:46 +02002338#ifdef CONFIG_I2C_COMPAT
2339 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
2340 adap->dev.parent);
2341#endif
2342
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01002343 /* device name is gone after device_unregister */
2344 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
2345
Wolfram Sang9f924162015-12-23 18:19:28 +01002346 pm_runtime_disable(&adap->dev);
2347
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02002348 i2c_host_notify_irq_teardown(adap);
2349
Wolfram Sang26680ee2015-01-29 22:45:09 +01002350 /* wait until all references to the device are gone
2351 *
2352 * FIXME: This is old code and should ideally be replaced by an
2353 * alternative which results in decoupling the lifetime of the struct
2354 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05302355 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01002356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
David Brownell6e13e642007-05-01 23:26:31 +02002361 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02002362 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002364 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02002366 /* Clear the device structure in case this adapter is ever going to be
2367 added again */
2368 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
David Brownellc0564602007-05-01 23:26:31 +02002370EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Wolfram Sang54177cc2015-12-17 13:32:36 +01002372/**
2373 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
2374 * @dev: The device to scan for I2C timing properties
2375 * @t: the i2c_timings struct to be filled with values
2376 * @use_defaults: bool to use sane defaults derived from the I2C specification
2377 * when properties are not found, otherwise use 0
2378 *
2379 * Scan the device for the generic I2C properties describing timing parameters
2380 * for the signal and fill the given struct with the results. If a property was
2381 * not found and use_defaults was true, then maximum timings are assumed which
2382 * are derived from the I2C specification. If use_defaults is not used, the
2383 * results will be 0, so drivers can apply their own defaults later. The latter
2384 * is mainly intended for avoiding regressions of existing drivers which want
2385 * to switch to this function. New drivers almost always should use the defaults.
2386 */
2387
2388void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
2389{
2390 int ret;
2391
2392 memset(t, 0, sizeof(*t));
2393
2394 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
2395 if (ret && use_defaults)
2396 t->bus_freq_hz = 100000;
2397
2398 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
2399 if (ret && use_defaults) {
2400 if (t->bus_freq_hz <= 100000)
2401 t->scl_rise_ns = 1000;
2402 else if (t->bus_freq_hz <= 400000)
2403 t->scl_rise_ns = 300;
2404 else
2405 t->scl_rise_ns = 120;
2406 }
2407
2408 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
2409 if (ret && use_defaults) {
2410 if (t->bus_freq_hz <= 400000)
2411 t->scl_fall_ns = 300;
2412 else
2413 t->scl_fall_ns = 120;
2414 }
2415
2416 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
2417
2418 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
2419 if (ret && use_defaults)
2420 t->sda_fall_ns = t->scl_fall_ns;
2421}
2422EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
2423
David Brownell7b4fbc52007-05-01 23:26:30 +02002424/* ------------------------------------------------------------------------- */
2425
Jean Delvare7ae31482011-03-20 14:50:52 +01002426int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
2427{
2428 int res;
2429
2430 mutex_lock(&core_lock);
2431 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
2432 mutex_unlock(&core_lock);
2433
2434 return res;
2435}
2436EXPORT_SYMBOL_GPL(i2c_for_each_dev);
2437
Jean Delvare69b00892009-12-06 17:06:27 +01002438static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002439{
Jean Delvare4f8cf822009-09-18 22:45:46 +02002440 if (dev->type != &i2c_adapter_type)
2441 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01002442 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02002443}
2444
David Brownell7b4fbc52007-05-01 23:26:30 +02002445/*
2446 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02002447 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 */
2449
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002450int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Jean Delvare7eebcb72006-02-05 23:28:21 +01002452 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
David Brownell1d0b19c2008-10-14 17:30:05 +02002454 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05302455 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02002456 return -EAGAIN;
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002459 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02002461 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Jean Delvare729d6dd2009-06-19 16:58:18 +02002463 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02002464 * will have called probe() for all matching-but-unbound devices.
2465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 res = driver_register(&driver->driver);
2467 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01002468 return res;
David Brownell438d6c22006-12-10 21:21:31 +01002469
Wolfram Sang44239a52016-07-09 13:35:04 +09002470 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Jean Delvare4735c982008-07-14 22:38:36 +02002472 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01002473 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02002474
Jean Delvare7eebcb72006-02-05 23:28:21 +01002475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08002477EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Jean Delvare69b00892009-12-06 17:06:27 +01002479static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02002480{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00002481 if (dev->type == &i2c_adapter_type)
2482 i2c_do_del_adapter(data, to_i2c_adapter(dev));
2483 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02002484}
2485
David Brownella1d9e6e2007-05-01 23:26:30 +02002486/**
2487 * i2c_del_driver - unregister I2C driver
2488 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02002489 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02002490 */
Jean Delvareb3e82092007-05-01 23:26:32 +02002491void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Jean Delvare7ae31482011-03-20 14:50:52 +01002493 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09002496 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
David Brownellc0564602007-05-01 23:26:31 +02002498EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
David Brownell7b4fbc52007-05-01 23:26:30 +02002500/* ------------------------------------------------------------------------- */
2501
Jean Delvaree48d3312008-01-27 18:14:48 +01002502/**
2503 * i2c_use_client - increments the reference count of the i2c client structure
2504 * @client: the client being referenced
2505 *
2506 * Each live reference to a client should be refcounted. The driver model does
2507 * that automatically as part of driver binding, so that most drivers don't
2508 * need to do this explicitly: they hold a reference until they're unbound
2509 * from the device.
2510 *
2511 * A pointer to the client with the incremented reference counter is returned.
2512 */
2513struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
David Brownell6ea438e2008-07-14 22:38:24 +02002515 if (client && get_device(&client->dev))
2516 return client;
2517 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
David Brownellc0564602007-05-01 23:26:31 +02002519EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Jean Delvaree48d3312008-01-27 18:14:48 +01002521/**
2522 * i2c_release_client - release a use of the i2c client structure
2523 * @client: the client being no longer referenced
2524 *
2525 * Must be called when a user of a client is finished with it.
2526 */
2527void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
David Brownell6ea438e2008-07-14 22:38:24 +02002529 if (client)
2530 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
David Brownellc0564602007-05-01 23:26:31 +02002532EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
David Brownell9b766b82008-01-27 18:14:51 +01002534struct i2c_cmd_arg {
2535 unsigned cmd;
2536 void *arg;
2537};
2538
2539static int i2c_cmd(struct device *dev, void *_arg)
2540{
2541 struct i2c_client *client = i2c_verify_client(dev);
2542 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002543 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01002544
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02002545 if (!client || !client->dev.driver)
2546 return 0;
2547
2548 driver = to_i2c_driver(client->dev.driver);
2549 if (driver->command)
2550 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01002551 return 0;
2552}
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
2555{
David Brownell9b766b82008-01-27 18:14:51 +01002556 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
David Brownell9b766b82008-01-27 18:14:51 +01002558 cmd_arg.cmd = cmd;
2559 cmd_arg.arg = arg;
2560 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
David Brownellc0564602007-05-01 23:26:31 +02002562EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002564#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2565static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
2566 void *arg)
2567{
2568 struct of_reconfig_data *rd = arg;
2569 struct i2c_adapter *adap;
2570 struct i2c_client *client;
2571
2572 switch (of_reconfig_get_state_change(action, rd)) {
2573 case OF_RECONFIG_CHANGE_ADD:
2574 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
2575 if (adap == NULL)
2576 return NOTIFY_OK; /* not for us */
2577
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002578 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
2579 put_device(&adap->dev);
2580 return NOTIFY_OK;
2581 }
2582
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002583 client = of_i2c_register_device(adap, rd->dn);
2584 put_device(&adap->dev);
2585
2586 if (IS_ERR(client)) {
Wolfram Sang399d62a2016-07-09 13:35:02 +09002587 dev_err(&adap->dev, "failed to create client for '%s'\n",
2588 rd->dn->full_name);
Ralf Ramsauer6a676fb2016-10-17 15:59:57 +02002589 of_node_clear_flag(rd->dn, OF_POPULATED);
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002590 return notifier_from_errno(PTR_ERR(client));
2591 }
2592 break;
2593 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02002594 /* already depopulated? */
2595 if (!of_node_check_flag(rd->dn, OF_POPULATED))
2596 return NOTIFY_OK;
2597
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002598 /* find our device by node */
2599 client = of_find_i2c_device_by_node(rd->dn);
2600 if (client == NULL)
2601 return NOTIFY_OK; /* no? not meant for us */
2602
2603 /* unregister takes one ref away */
2604 i2c_unregister_device(client);
2605
2606 /* and put the reference of the find */
2607 put_device(&client->dev);
2608 break;
2609 }
2610
2611 return NOTIFY_OK;
2612}
2613static struct notifier_block i2c_of_notifier = {
2614 .notifier_call = of_i2c_notify,
2615};
2616#else
2617extern struct notifier_block i2c_of_notifier;
2618#endif /* CONFIG_OF_DYNAMIC */
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620static int __init i2c_init(void)
2621{
2622 int retval;
2623
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002624 retval = of_alias_get_highest_id("i2c");
2625
2626 down_write(&__i2c_board_lock);
2627 if (retval >= __i2c_first_dynamic_bus_num)
2628 __i2c_first_dynamic_bus_num = retval + 1;
2629 up_write(&__i2c_board_lock);
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 retval = bus_register(&i2c_bus_type);
2632 if (retval)
2633 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01002634
2635 is_registered = true;
2636
Jean Delvare2bb50952009-09-18 22:45:46 +02002637#ifdef CONFIG_I2C_COMPAT
2638 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2639 if (!i2c_adapter_compat_class) {
2640 retval = -ENOMEM;
2641 goto bus_err;
2642 }
2643#endif
David Brownelle9f13732008-01-27 18:14:52 +01002644 retval = i2c_add_driver(&dummy_driver);
2645 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002646 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002647
2648 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2649 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002650 if (IS_ENABLED(CONFIG_ACPI))
2651 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002652
David Brownelle9f13732008-01-27 18:14:52 +01002653 return 0;
2654
Jean Delvare2bb50952009-09-18 22:45:46 +02002655class_err:
2656#ifdef CONFIG_I2C_COMPAT
2657 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002658bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002659#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01002660 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01002661 bus_unregister(&i2c_bus_type);
2662 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665static void __exit i2c_exit(void)
2666{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03002667 if (IS_ENABLED(CONFIG_ACPI))
2668 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002669 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2670 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002671 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002672#ifdef CONFIG_I2C_COMPAT
2673 class_compat_unregister(i2c_adapter_compat_class);
2674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002676 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
David Brownella10f9e72008-10-14 17:30:06 +02002679/* We must initialize early, because some subsystems register i2c drivers
2680 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2681 */
2682postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683module_exit(i2c_exit);
2684
2685/* ----------------------------------------------------
2686 * the functional interface to the i2c busses.
2687 * ----------------------------------------------------
2688 */
2689
Wolfram Sangb7f62582015-01-05 23:45:59 +01002690/* Check if val is exceeding the quirk IFF quirk is non 0 */
2691#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2692
2693static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2694{
2695 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2696 err_msg, msg->addr, msg->len,
2697 msg->flags & I2C_M_RD ? "read" : "write");
2698 return -EOPNOTSUPP;
2699}
2700
2701static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2702{
2703 const struct i2c_adapter_quirks *q = adap->quirks;
2704 int max_num = q->max_num_msgs, i;
2705 bool do_len_check = true;
2706
2707 if (q->flags & I2C_AQ_COMB) {
2708 max_num = 2;
2709
2710 /* special checks for combined messages */
2711 if (num == 2) {
2712 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2713 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2714
2715 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2716 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2717
2718 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2719 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2720
2721 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2722 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2723
2724 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2725 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2726
2727 do_len_check = false;
2728 }
2729 }
2730
2731 if (i2c_quirk_exceeded(num, max_num))
2732 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2733
2734 for (i = 0; i < num; i++) {
2735 u16 len = msgs[i].len;
2736
2737 if (msgs[i].flags & I2C_M_RD) {
2738 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2739 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2740 } else {
2741 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2742 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2743 }
2744 }
2745
2746 return 0;
2747}
2748
David Brownella1cdeda2008-07-14 22:38:24 +02002749/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002750 * __i2c_transfer - unlocked flavor of i2c_transfer
2751 * @adap: Handle to I2C bus
2752 * @msgs: One or more messages to execute before STOP is issued to
2753 * terminate the operation; each message begins with a START.
2754 * @num: Number of messages to be executed.
2755 *
2756 * Returns negative errno, else the number of messages executed.
2757 *
2758 * Adapter lock must be held when calling this function. No debug logging
2759 * takes place. adap->algo->master_xfer existence isn't checked.
2760 */
2761int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2762{
2763 unsigned long orig_jiffies;
2764 int ret, try;
2765
Wolfram Sangb7f62582015-01-05 23:45:59 +01002766 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2767 return -EOPNOTSUPP;
2768
David Howellsd9a83d62014-03-06 13:35:59 +00002769 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2770 * enabled. This is an efficient way of keeping the for-loop from
2771 * being executed when not needed.
2772 */
2773 if (static_key_false(&i2c_trace_msg)) {
2774 int i;
2775 for (i = 0; i < num; i++)
2776 if (msgs[i].flags & I2C_M_RD)
2777 trace_i2c_read(adap, &msgs[i], i);
2778 else
2779 trace_i2c_write(adap, &msgs[i], i);
2780 }
2781
Jean Delvareb37d2a32012-06-29 07:47:19 -03002782 /* Retry automatically on arbitration loss */
2783 orig_jiffies = jiffies;
2784 for (ret = 0, try = 0; try <= adap->retries; try++) {
2785 ret = adap->algo->master_xfer(adap, msgs, num);
2786 if (ret != -EAGAIN)
2787 break;
2788 if (time_after(jiffies, orig_jiffies + adap->timeout))
2789 break;
2790 }
2791
David Howellsd9a83d62014-03-06 13:35:59 +00002792 if (static_key_false(&i2c_trace_msg)) {
2793 int i;
2794 for (i = 0; i < ret; i++)
2795 if (msgs[i].flags & I2C_M_RD)
2796 trace_i2c_reply(adap, &msgs[i], i);
2797 trace_i2c_result(adap, i, ret);
2798 }
2799
Jean Delvareb37d2a32012-06-29 07:47:19 -03002800 return ret;
2801}
2802EXPORT_SYMBOL(__i2c_transfer);
2803
2804/**
David Brownella1cdeda2008-07-14 22:38:24 +02002805 * i2c_transfer - execute a single or combined I2C message
2806 * @adap: Handle to I2C bus
2807 * @msgs: One or more messages to execute before STOP is issued to
2808 * terminate the operation; each message begins with a START.
2809 * @num: Number of messages to be executed.
2810 *
2811 * Returns negative errno, else the number of messages executed.
2812 *
2813 * Note that there is no requirement that each message be sent to
2814 * the same slave address, although that is the most common model.
2815 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002816int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002818 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
David Brownella1cdeda2008-07-14 22:38:24 +02002820 /* REVISIT the fault reporting model here is weak:
2821 *
2822 * - When we get an error after receiving N bytes from a slave,
2823 * there is no way to report "N".
2824 *
2825 * - When we get a NAK after transmitting N bytes to a slave,
2826 * there is no way to report "N" ... or to let the master
2827 * continue executing the rest of this combined message, if
2828 * that's the appropriate response.
2829 *
2830 * - When for example "num" is two and we successfully complete
2831 * the first message but get an error part way through the
2832 * second, it's unclear whether that should be reported as
2833 * one (discarding status on the second message) or errno
2834 * (discarding status on the first one).
2835 */
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (adap->algo->master_xfer) {
2838#ifdef DEBUG
2839 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002840 dev_dbg(&adap->dev,
2841 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
2842 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
2843 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02002844 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846#endif
2847
Mike Rapoportcea443a82008-01-27 18:14:50 +01002848 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02002849 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002850 if (!ret)
2851 /* I2C activity is ongoing. */
2852 return -EAGAIN;
2853 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02002854 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002855 }
2856
Jean Delvareb37d2a32012-06-29 07:47:19 -03002857 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02002858 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 return ret;
2861 } else {
2862 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002863 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865}
David Brownellc0564602007-05-01 23:26:31 +02002866EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
David Brownella1cdeda2008-07-14 22:38:24 +02002868/**
2869 * i2c_master_send - issue a single I2C message in master transmit mode
2870 * @client: Handle to slave device
2871 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002872 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002873 *
2874 * Returns negative errno, or else the number of bytes written.
2875 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002876int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002879 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 struct i2c_msg msg;
2881
Jean Delvare815f55f2005-05-07 22:58:46 +02002882 msg.addr = client->addr;
2883 msg.flags = client->flags & I2C_M_TEN;
2884 msg.len = count;
2885 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002886
Jean Delvare815f55f2005-05-07 22:58:46 +02002887 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002889 /*
2890 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2891 * transmitted, else error code.
2892 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002893 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
David Brownellc0564602007-05-01 23:26:31 +02002895EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
David Brownella1cdeda2008-07-14 22:38:24 +02002897/**
2898 * i2c_master_recv - issue a single I2C message in master receive mode
2899 * @client: Handle to slave device
2900 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002901 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002902 *
2903 * Returns negative errno, or else the number of bytes read.
2904 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002905int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Farid Hammane7225acf2010-05-21 18:40:58 +02002907 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct i2c_msg msg;
2909 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Jean Delvare815f55f2005-05-07 22:58:46 +02002911 msg.addr = client->addr;
2912 msg.flags = client->flags & I2C_M_TEN;
2913 msg.flags |= I2C_M_RD;
2914 msg.len = count;
2915 msg.buf = buf;
2916
2917 ret = i2c_transfer(adap, &msg, 1);
2918
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002919 /*
2920 * If everything went ok (i.e. 1 msg received), return #bytes received,
2921 * else error code.
2922 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002923 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
David Brownellc0564602007-05-01 23:26:31 +02002925EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927/* ----------------------------------------------------
2928 * the i2c address scanning function
2929 * Will not work for 10-bit addresses!
2930 * ----------------------------------------------------
2931 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002932
Jean Delvare63e4e802010-06-03 11:33:51 +02002933/*
2934 * Legacy default probe function, mostly relevant for SMBus. The default
2935 * probe method is a quick write, but it is known to corrupt the 24RF08
2936 * EEPROMs due to a state machine bug, and could also irreversibly
2937 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2938 * we use a short byte read instead. Also, some bus drivers don't implement
2939 * quick write, so we fallback to a byte read in that case too.
2940 * On x86, there is another special case for FSC hardware monitoring chips,
2941 * which want regular byte reads (address 0x73.) Fortunately, these are the
2942 * only known chips using this I2C address on PC hardware.
2943 * Returns 1 if probe succeeded, 0 if not.
2944 */
2945static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2946{
2947 int err;
2948 union i2c_smbus_data dummy;
2949
2950#ifdef CONFIG_X86
2951 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2952 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2953 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2954 I2C_SMBUS_BYTE_DATA, &dummy);
2955 else
2956#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002957 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2958 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002959 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2960 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002961 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2962 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2963 I2C_SMBUS_BYTE, &dummy);
2964 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002965 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2966 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002967 err = -EOPNOTSUPP;
2968 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002969
2970 return err >= 0;
2971}
2972
Jean Delvareccfbbd02009-12-06 17:06:25 +01002973static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002974 struct i2c_driver *driver)
2975{
2976 struct i2c_board_info info;
2977 struct i2c_adapter *adapter = temp_client->adapter;
2978 int addr = temp_client->addr;
2979 int err;
2980
2981 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002982 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002983 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002984 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2985 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002986 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002987 }
2988
Wolfram Sang9bccc702015-07-17 14:48:56 +02002989 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002990 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002991 return 0;
2992
Jean Delvareccfbbd02009-12-06 17:06:25 +01002993 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002994 if (!i2c_default_probe(adapter, addr))
2995 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002996
2997 /* Finally call the custom detection function */
2998 memset(&info, 0, sizeof(struct i2c_board_info));
2999 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01003000 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02003001 if (err) {
3002 /* -ENODEV is returned if the detection fails. We catch it
3003 here as this isn't an error. */
3004 return err == -ENODEV ? 0 : err;
3005 }
3006
3007 /* Consistency check */
3008 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003009 dev_err(&adapter->dev,
3010 "%s detection function provided no name for 0x%x\n",
3011 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02003012 } else {
3013 struct i2c_client *client;
3014
3015 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01003016 if (adapter->class & I2C_CLASS_DEPRECATED)
3017 dev_warn(&adapter->dev,
3018 "This adapter will soon drop class based instantiation of devices. "
3019 "Please make sure client 0x%02x gets instantiated by other means. "
3020 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
3021 info.addr);
3022
Jean Delvare4735c982008-07-14 22:38:36 +02003023 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
3024 info.type, info.addr);
3025 client = i2c_new_device(adapter, &info);
3026 if (client)
3027 list_add_tail(&client->detected, &driver->clients);
3028 else
3029 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
3030 info.type, info.addr);
3031 }
3032 return 0;
3033}
3034
3035static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
3036{
Jean Delvarec3813d62009-12-14 21:17:25 +01003037 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02003038 struct i2c_client *temp_client;
3039 int i, err = 0;
3040 int adap_id = i2c_adapter_id(adapter);
3041
Jean Delvarec3813d62009-12-14 21:17:25 +01003042 address_list = driver->address_list;
3043 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02003044 return 0;
3045
Wolfram Sang45552272014-07-10 13:46:21 +02003046 /* Warn that the adapter lost class based instantiation */
3047 if (adapter->class == I2C_CLASS_DEPRECATED) {
3048 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003049 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
3050 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02003051 driver->driver.name);
3052 return 0;
3053 }
3054
Jean Delvare51b54ba2010-10-24 18:16:58 +02003055 /* Stop here if the classes do not match */
3056 if (!(adapter->class & driver->class))
3057 return 0;
3058
Jean Delvare4735c982008-07-14 22:38:36 +02003059 /* Set up a temporary client to help detect callback */
3060 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
3061 if (!temp_client)
3062 return -ENOMEM;
3063 temp_client->adapter = adapter;
3064
Jean Delvarec3813d62009-12-14 21:17:25 +01003065 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003066 dev_dbg(&adapter->dev,
3067 "found normal entry for adapter %d, addr 0x%02x\n",
3068 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01003069 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01003070 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02003071 if (unlikely(err))
3072 break;
Jean Delvare4735c982008-07-14 22:38:36 +02003073 }
3074
Jean Delvare4735c982008-07-14 22:38:36 +02003075 kfree(temp_client);
3076 return err;
3077}
3078
Jean Delvared44f19d2010-08-11 18:20:57 +02003079int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
3080{
3081 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
3082 I2C_SMBUS_QUICK, NULL) >= 0;
3083}
3084EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
3085
Jean Delvare12b5053a2007-05-01 23:26:31 +02003086struct i2c_client *
3087i2c_new_probed_device(struct i2c_adapter *adap,
3088 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02003089 unsigned short const *addr_list,
3090 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02003091{
3092 int i;
3093
Jean Delvare8031d792010-08-11 18:21:00 +02003094 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02003095 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02003096
Jean Delvare12b5053a2007-05-01 23:26:31 +02003097 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
3098 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02003099 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003100 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
3101 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02003102 continue;
3103 }
3104
Wolfram Sang9bccc702015-07-17 14:48:56 +02003105 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02003106 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03003107 dev_dbg(&adap->dev,
3108 "Address 0x%02x already in use, not probing\n",
3109 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02003110 continue;
3111 }
3112
Jean Delvare63e4e802010-06-03 11:33:51 +02003113 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02003114 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02003115 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02003116 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02003117
3118 if (addr_list[i] == I2C_CLIENT_END) {
3119 dev_dbg(&adap->dev, "Probing failed, no device found\n");
3120 return NULL;
3121 }
3122
3123 info->addr = addr_list[i];
3124 return i2c_new_device(adap, info);
3125}
3126EXPORT_SYMBOL_GPL(i2c_new_probed_device);
3127
Jean Delvared735b342011-03-20 14:50:52 +01003128struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01003131
Jean Delvarecaada322008-01-27 18:14:49 +01003132 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01003133 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003134 if (!adapter)
3135 goto exit;
3136
3137 if (try_module_get(adapter->owner))
3138 get_device(&adapter->dev);
3139 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04003140 adapter = NULL;
3141
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003142 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01003143 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04003144 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
David Brownellc0564602007-05-01 23:26:31 +02003146EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148void i2c_put_adapter(struct i2c_adapter *adap)
3149{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03003150 if (!adap)
3151 return;
3152
3153 put_device(&adap->dev);
3154 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
David Brownellc0564602007-05-01 23:26:31 +02003156EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158/* The SMBus parts */
3159
David Brownell438d6c22006-12-10 21:21:31 +01003160#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003161static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 int i;
David Brownell438d6c22006-12-10 21:21:31 +01003164
Farid Hammane7225acf2010-05-21 18:40:58 +02003165 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01003166 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 data = data ^ POLY;
3168 data = data << 1;
3169 }
3170 return (u8)(data >> 8);
3171}
3172
Jean Delvare421ef472005-10-26 21:28:55 +02003173/* Incremental CRC8 over count bytes in the array pointed to by p */
3174static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
3176 int i;
3177
Farid Hammane7225acf2010-05-21 18:40:58 +02003178 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02003179 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 return crc;
3181}
3182
Jean Delvare421ef472005-10-26 21:28:55 +02003183/* Assume a 7-bit address, which is reasonable for SMBus */
3184static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Jean Delvare421ef472005-10-26 21:28:55 +02003186 /* The address will be sent first */
Wolfram Sang041edda2016-04-03 20:44:46 +02003187 u8 addr = i2c_8bit_addr_from_msg(msg);
Jean Delvare421ef472005-10-26 21:28:55 +02003188 pec = i2c_smbus_pec(pec, &addr, 1);
3189
3190 /* The data buffer follows */
3191 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Jean Delvare421ef472005-10-26 21:28:55 +02003194/* Used for write only transactions */
3195static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Jean Delvare421ef472005-10-26 21:28:55 +02003197 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
3198 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
Jean Delvare421ef472005-10-26 21:28:55 +02003201/* Return <0 on CRC error
3202 If there was a write before this read (most cases) we need to take the
3203 partial CRC from the write part into account.
3204 Note that this function does modify the message (we need to decrease the
3205 message length to hide the CRC byte from the caller). */
3206static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Jean Delvare421ef472005-10-26 21:28:55 +02003208 u8 rpec = msg->buf[--msg->len];
3209 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (rpec != cpec) {
Wolfram Sang44239a52016-07-09 13:35:04 +09003212 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02003214 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
David Brownell438d6c22006-12-10 21:21:31 +01003216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
David Brownella1cdeda2008-07-14 22:38:24 +02003219/**
3220 * i2c_smbus_read_byte - SMBus "receive byte" protocol
3221 * @client: Handle to slave device
3222 *
3223 * This executes the SMBus "receive byte" protocol, returning negative errno
3224 * else the byte received from the device.
3225 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003226s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
3228 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003229 int status;
3230
3231 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3232 I2C_SMBUS_READ, 0,
3233 I2C_SMBUS_BYTE, &data);
3234 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
David Brownellc0564602007-05-01 23:26:31 +02003236EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
David Brownella1cdeda2008-07-14 22:38:24 +02003238/**
3239 * i2c_smbus_write_byte - SMBus "send byte" protocol
3240 * @client: Handle to slave device
3241 * @value: Byte to be sent
3242 *
3243 * This executes the SMBus "send byte" protocol, returning negative errno
3244 * else zero on success.
3245 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003246s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Farid Hammane7225acf2010-05-21 18:40:58 +02003248 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02003249 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
David Brownellc0564602007-05-01 23:26:31 +02003251EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
David Brownella1cdeda2008-07-14 22:38:24 +02003253/**
3254 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
3255 * @client: Handle to slave device
3256 * @command: Byte interpreted by slave
3257 *
3258 * This executes the SMBus "read byte" protocol, returning negative errno
3259 * else a data byte received from the device.
3260 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003261s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003264 int status;
3265
3266 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3267 I2C_SMBUS_READ, command,
3268 I2C_SMBUS_BYTE_DATA, &data);
3269 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
David Brownellc0564602007-05-01 23:26:31 +02003271EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
David Brownella1cdeda2008-07-14 22:38:24 +02003273/**
3274 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
3275 * @client: Handle to slave device
3276 * @command: Byte interpreted by slave
3277 * @value: Byte being written
3278 *
3279 * This executes the SMBus "write byte" protocol, returning negative errno
3280 * else zero on success.
3281 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003282s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
3283 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
3285 union i2c_smbus_data data;
3286 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003287 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3288 I2C_SMBUS_WRITE, command,
3289 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
David Brownellc0564602007-05-01 23:26:31 +02003291EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
David Brownella1cdeda2008-07-14 22:38:24 +02003293/**
3294 * i2c_smbus_read_word_data - SMBus "read word" protocol
3295 * @client: Handle to slave device
3296 * @command: Byte interpreted by slave
3297 *
3298 * This executes the SMBus "read word" protocol, returning negative errno
3299 * else a 16-bit unsigned "word" received from the device.
3300 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003301s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003304 int status;
3305
3306 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3307 I2C_SMBUS_READ, command,
3308 I2C_SMBUS_WORD_DATA, &data);
3309 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
David Brownellc0564602007-05-01 23:26:31 +02003311EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
David Brownella1cdeda2008-07-14 22:38:24 +02003313/**
3314 * i2c_smbus_write_word_data - SMBus "write word" protocol
3315 * @client: Handle to slave device
3316 * @command: Byte interpreted by slave
3317 * @value: 16-bit "word" being written
3318 *
3319 * This executes the SMBus "write word" protocol, returning negative errno
3320 * else zero on success.
3321 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003322s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
3323 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
3325 union i2c_smbus_data data;
3326 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02003327 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3328 I2C_SMBUS_WRITE, command,
3329 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
David Brownellc0564602007-05-01 23:26:31 +02003331EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
David Brownella64ec072007-10-13 23:56:31 +02003333/**
David Brownella1cdeda2008-07-14 22:38:24 +02003334 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02003335 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02003336 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02003337 * @values: Byte array into which data will be read; big enough to hold
3338 * the data returned by the slave. SMBus allows at most 32 bytes.
3339 *
David Brownella1cdeda2008-07-14 22:38:24 +02003340 * This executes the SMBus "block read" protocol, returning negative errno
3341 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02003342 *
3343 * Note that using this function requires that the client's adapter support
3344 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
3345 * support this; its emulation through I2C messaging relies on a specific
3346 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
3347 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003348s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003349 u8 *values)
3350{
3351 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003352 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003353
David Brownell24a5bb72008-07-14 22:38:23 +02003354 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3355 I2C_SMBUS_READ, command,
3356 I2C_SMBUS_BLOCK_DATA, &data);
3357 if (status)
3358 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02003359
3360 memcpy(values, &data.block[1], data.block[0]);
3361 return data.block[0];
3362}
3363EXPORT_SYMBOL(i2c_smbus_read_block_data);
3364
David Brownella1cdeda2008-07-14 22:38:24 +02003365/**
3366 * i2c_smbus_write_block_data - SMBus "block write" protocol
3367 * @client: Handle to slave device
3368 * @command: Byte interpreted by slave
3369 * @length: Size of data block; SMBus allows at most 32 bytes
3370 * @values: Byte array which will be written.
3371 *
3372 * This executes the SMBus "block write" protocol, returning negative errno
3373 * else zero on success.
3374 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003375s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003376 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
3378 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (length > I2C_SMBUS_BLOCK_MAX)
3381 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01003383 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02003384 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3385 I2C_SMBUS_WRITE, command,
3386 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
David Brownellc0564602007-05-01 23:26:31 +02003388EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01003391s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02003392 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
3394 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02003395 int status;
Jean Delvare76560322006-01-18 23:14:55 +01003396
Jean Delvare4b2643d2007-07-12 14:12:29 +02003397 if (length > I2C_SMBUS_BLOCK_MAX)
3398 length = I2C_SMBUS_BLOCK_MAX;
3399 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02003400 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3401 I2C_SMBUS_READ, command,
3402 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3403 if (status < 0)
3404 return status;
Jean Delvare76560322006-01-18 23:14:55 +01003405
3406 memcpy(values, &data.block[1], data.block[0]);
3407 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
David Brownellc0564602007-05-01 23:26:31 +02003409EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Jean Delvare0cc43a12011-01-10 22:11:23 +01003411s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02003412 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11003413{
3414 union i2c_smbus_data data;
3415
3416 if (length > I2C_SMBUS_BLOCK_MAX)
3417 length = I2C_SMBUS_BLOCK_MAX;
3418 data.block[0] = length;
3419 memcpy(data.block + 1, values, length);
3420 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
3421 I2C_SMBUS_WRITE, command,
3422 I2C_SMBUS_I2C_BLOCK_DATA, &data);
3423}
David Brownellc0564602007-05-01 23:26:31 +02003424EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11003425
David Brownell438d6c22006-12-10 21:21:31 +01003426/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02003428static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
3429 unsigned short flags,
3430 char read_write, u8 command, int size,
3431 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 /* So we need to generate a series of msgs. In the case of writing, we
3434 need to use only one message; when reading, we need two. We initialize
3435 most things with sane defaults, to keep the code below somewhat
3436 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003437 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
3438 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02003439 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02003441 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02003442 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02003443 struct i2c_msg msg[2] = {
3444 {
3445 .addr = addr,
3446 .flags = flags,
3447 .len = 1,
3448 .buf = msgbuf0,
3449 }, {
3450 .addr = addr,
3451 .flags = flags | I2C_M_RD,
3452 .len = 0,
3453 .buf = msgbuf1,
3454 },
3455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02003458 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 case I2C_SMBUS_QUICK:
3460 msg[0].len = 0;
3461 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01003462 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
3463 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 num = 1;
3465 break;
3466 case I2C_SMBUS_BYTE:
3467 if (read_write == I2C_SMBUS_READ) {
3468 /* Special case: only a read! */
3469 msg[0].flags = I2C_M_RD | flags;
3470 num = 1;
3471 }
3472 break;
3473 case I2C_SMBUS_BYTE_DATA:
3474 if (read_write == I2C_SMBUS_READ)
3475 msg[1].len = 1;
3476 else {
3477 msg[0].len = 2;
3478 msgbuf0[1] = data->byte;
3479 }
3480 break;
3481 case I2C_SMBUS_WORD_DATA:
3482 if (read_write == I2C_SMBUS_READ)
3483 msg[1].len = 2;
3484 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02003485 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003487 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489 break;
3490 case I2C_SMBUS_PROC_CALL:
3491 num = 2; /* Special case */
3492 read_write = I2C_SMBUS_READ;
3493 msg[0].len = 3;
3494 msg[1].len = 2;
3495 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02003496 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 break;
3498 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02003500 msg[1].flags |= I2C_M_RECV_LEN;
3501 msg[1].len = 1; /* block length will be added by
3502 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 } else {
3504 msg[0].len = data->block[0] + 2;
3505 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02003506 dev_err(&adapter->dev,
3507 "Invalid block write size %d\n",
3508 data->block[0]);
3509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02003511 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 msgbuf0[i] = data->block[i-1];
3513 }
3514 break;
3515 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02003516 num = 2; /* Another special case */
3517 read_write = I2C_SMBUS_READ;
3518 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02003519 dev_err(&adapter->dev,
3520 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02003521 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02003522 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02003523 }
3524 msg[0].len = data->block[0] + 2;
3525 for (i = 1; i < msg[0].len; i++)
3526 msgbuf0[i] = data->block[i-1];
3527 msg[1].flags |= I2C_M_RECV_LEN;
3528 msg[1].len = 1; /* block length will be added by
3529 the underlying bus driver */
3530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 case I2C_SMBUS_I2C_BLOCK_DATA:
3532 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02003533 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 } else {
3535 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02003536 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02003537 dev_err(&adapter->dev,
3538 "Invalid block write size %d\n",
3539 data->block[0]);
3540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
3542 for (i = 1; i <= data->block[0]; i++)
3543 msgbuf0[i] = data->block[i];
3544 }
3545 break;
3546 default:
David Brownell24a5bb72008-07-14 22:38:23 +02003547 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
3548 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 }
3550
Jean Delvare421ef472005-10-26 21:28:55 +02003551 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
3552 && size != I2C_SMBUS_I2C_BLOCK_DATA);
3553 if (i) {
3554 /* Compute PEC if first message is a write */
3555 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01003556 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02003557 i2c_smbus_add_pec(&msg[0]);
3558 else /* Write followed by read */
3559 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
3560 }
3561 /* Ask for PEC if last message is a read */
3562 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01003563 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02003564 }
3565
David Brownell24a5bb72008-07-14 22:38:23 +02003566 status = i2c_transfer(adapter, msg, num);
3567 if (status < 0)
3568 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Jean Delvare421ef472005-10-26 21:28:55 +02003570 /* Check PEC if last message is a read */
3571 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02003572 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
3573 if (status < 0)
3574 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02003575 }
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02003578 switch (size) {
3579 case I2C_SMBUS_BYTE:
3580 data->byte = msgbuf0[0];
3581 break;
3582 case I2C_SMBUS_BYTE_DATA:
3583 data->byte = msgbuf1[0];
3584 break;
3585 case I2C_SMBUS_WORD_DATA:
3586 case I2C_SMBUS_PROC_CALL:
3587 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
3588 break;
3589 case I2C_SMBUS_I2C_BLOCK_DATA:
3590 for (i = 0; i < data->block[0]; i++)
3591 data->block[i+1] = msgbuf1[i];
3592 break;
3593 case I2C_SMBUS_BLOCK_DATA:
3594 case I2C_SMBUS_BLOCK_PROC_CALL:
3595 for (i = 0; i < msgbuf1[0] + 1; i++)
3596 data->block[i] = msgbuf1[i];
3597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 return 0;
3600}
3601
David Brownella1cdeda2008-07-14 22:38:24 +02003602/**
3603 * i2c_smbus_xfer - execute SMBus protocol operations
3604 * @adapter: Handle to I2C bus
3605 * @addr: Address of SMBus slave on that bus
3606 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3607 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3608 * @command: Byte interpreted by slave, for protocols which use such bytes
3609 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3610 * @data: Data to be read or written
3611 *
3612 * This executes an SMBus protocol operation, and returns a negative
3613 * errno code else zero on success.
3614 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003615s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02003616 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01003617 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Clifford Wolf66b650f2009-06-15 18:01:46 +02003619 unsigned long orig_jiffies;
3620 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
David Howells8a325992014-03-06 13:36:06 +00003623 /* If enabled, the following two tracepoints are conditional on
3624 * read_write and protocol.
3625 */
3626 trace_smbus_write(adapter, addr, flags, read_write,
3627 command, protocol, data);
3628 trace_smbus_read(adapter, addr, flags, read_write,
3629 command, protocol);
3630
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003631 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 if (adapter->algo->smbus_xfer) {
Peter Rosin8320f492016-05-04 22:15:27 +02003634 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003635
3636 /* Retry automatically on arbitration loss */
3637 orig_jiffies = jiffies;
3638 for (res = 0, try = 0; try <= adapter->retries; try++) {
3639 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3640 read_write, command,
3641 protocol, data);
3642 if (res != -EAGAIN)
3643 break;
3644 if (time_after(jiffies,
3645 orig_jiffies + adapter->timeout))
3646 break;
3647 }
Peter Rosin8320f492016-05-04 22:15:27 +02003648 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003650 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003651 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003652 /*
3653 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3654 * implement native support for the SMBus operation.
3655 */
3656 }
3657
David Howells8a325992014-03-06 13:36:06 +00003658 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3659 command, protocol, data);
3660
3661trace:
3662 /* If enabled, the reply tracepoint is conditional on read_write. */
3663 trace_smbus_reply(adapter, addr, flags, read_write,
3664 command, protocol, data);
3665 trace_smbus_result(adapter, addr, flags, read_write,
3666 command, protocol, res);
3667
3668 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Irina Tirdea01eef962015-08-12 17:31:33 +03003672/**
3673 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3674 * @client: Handle to slave device
3675 * @command: Byte interpreted by slave
3676 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3677 * @values: Byte array into which data will be read; big enough to hold
3678 * the data returned by the slave. SMBus allows at most
3679 * I2C_SMBUS_BLOCK_MAX bytes.
3680 *
3681 * This executes the SMBus "block read" protocol if supported by the adapter.
3682 * If block read is not supported, it emulates it using either word or byte
3683 * read protocols depending on availability.
3684 *
3685 * The addresses of the I2C slave device that are accessed with this function
3686 * must be mapped to a linear region, so that a block read will have the same
3687 * effect as a byte read. Before using this function you must double-check
3688 * if the I2C slave does support exchanging a block transfer with a byte
3689 * transfer.
3690 */
3691s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3692 u8 command, u8 length, u8 *values)
3693{
3694 u8 i = 0;
3695 int status;
3696
3697 if (length > I2C_SMBUS_BLOCK_MAX)
3698 length = I2C_SMBUS_BLOCK_MAX;
3699
3700 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3701 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3702
3703 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3704 return -EOPNOTSUPP;
3705
3706 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3707 while ((i + 2) <= length) {
3708 status = i2c_smbus_read_word_data(client, command + i);
3709 if (status < 0)
3710 return status;
3711 values[i] = status & 0xff;
3712 values[i + 1] = status >> 8;
3713 i += 2;
3714 }
3715 }
3716
3717 while (i < length) {
3718 status = i2c_smbus_read_byte_data(client, command + i);
3719 if (status < 0)
3720 return status;
3721 values[i] = status;
3722 i++;
3723 }
3724
3725 return i;
3726}
3727EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3730MODULE_DESCRIPTION("I2C-Bus main module");
3731MODULE_LICENSE("GPL");