blob: 5f89f1e3c2f24fc562a519eb173d33de8c280f42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020030#include <dt-bindings/i2c/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053033#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053035#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/i2c.h>
38#include <linux/init.h>
39#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010040#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010041#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060042#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010043#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020044#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020050#include <linux/pm_domain.h>
Dmitry Torokhov3fffd122015-08-17 23:52:51 -070051#include <linux/pm_wakeirq.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010052#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000053#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020055#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Brownell9c1600e2007-05-01 23:26:31 +020057#include "i2c-core.h"
58
David Howellsd9a83d62014-03-06 13:35:59 +000059#define CREATE_TRACE_POINTS
60#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Wolfram Sangda899f52015-05-18 21:09:12 +020062#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
63#define I2C_ADDR_OFFSET_SLAVE 0x1000
64
Jean Delvare6629dcf2010-05-04 11:09:28 +020065/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020066 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000067 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010068static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static DEFINE_IDR(i2c_adapter_idr);
70
Jean Delvare4f8cf822009-09-18 22:45:46 +020071static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020072static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010073
David Howellsd9a83d62014-03-06 13:35:59 +000074static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
75
76void i2c_transfer_trace_reg(void)
77{
78 static_key_slow_inc(&i2c_trace_msg);
79}
80
81void i2c_transfer_trace_unreg(void)
82{
83 static_key_slow_dec(&i2c_trace_msg);
84}
85
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020086#if defined(CONFIG_ACPI)
87struct acpi_i2c_handler_data {
88 struct acpi_connection_info info;
89 struct i2c_adapter *adapter;
90};
91
92struct gsb_buffer {
93 u8 status;
94 u8 len;
95 union {
96 u16 wdata;
97 u8 bdata;
98 u8 data[0];
99 };
100} __packed;
101
102static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
103{
104 struct i2c_board_info *info = data;
105
106 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
107 struct acpi_resource_i2c_serialbus *sb;
108
109 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1c2014-12-29 15:48:48 +0200110 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200111 info->addr = sb->slave_address;
112 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
113 info->flags |= I2C_CLIENT_TEN;
114 }
Mika Westerbergdab472e2015-05-06 13:29:07 +0300115 } else if (!info->irq) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200116 struct resource r;
117
118 if (acpi_dev_resource_interrupt(ares, 0, &r))
119 info->irq = r.start;
120 }
121
122 /* Tell the ACPI core to skip this resource */
123 return 1;
124}
125
126static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
127 void *data, void **return_value)
128{
129 struct i2c_adapter *adapter = data;
130 struct list_head resource_list;
131 struct i2c_board_info info;
132 struct acpi_device *adev;
133 int ret;
134
135 if (acpi_bus_get_device(handle, &adev))
136 return AE_OK;
137 if (acpi_bus_get_status(adev) || !adev->status.present)
138 return AE_OK;
139
140 memset(&info, 0, sizeof(info));
Rafael J. Wysockice793482015-03-16 23:49:03 +0100141 info.fwnode = acpi_fwnode_handle(adev);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200142
143 INIT_LIST_HEAD(&resource_list);
144 ret = acpi_dev_get_resources(adev, &resource_list,
145 acpi_i2c_add_resource, &info);
146 acpi_dev_free_resource_list(&resource_list);
147
148 if (ret < 0 || !info.addr)
149 return AE_OK;
150
151 adev->power.flags.ignore_parent = true;
152 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
153 if (!i2c_new_device(adapter, &info)) {
154 adev->power.flags.ignore_parent = false;
155 dev_err(&adapter->dev,
156 "failed to add I2C device %s from ACPI\n",
157 dev_name(&adev->dev));
158 }
159
160 return AE_OK;
161}
162
163/**
164 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
165 * @adap: pointer to adapter
166 *
167 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
168 * namespace. When a device is found it will be added to the Linux device
169 * model and bound to the corresponding ACPI handle.
170 */
171static void acpi_i2c_register_devices(struct i2c_adapter *adap)
172{
173 acpi_handle handle;
174 acpi_status status;
175
176 if (!adap->dev.parent)
177 return;
178
179 handle = ACPI_HANDLE(adap->dev.parent);
180 if (!handle)
181 return;
182
183 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
184 acpi_i2c_add_device, NULL,
185 adap, NULL);
186 if (ACPI_FAILURE(status))
187 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
188}
189
190#else /* CONFIG_ACPI */
191static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
192#endif /* CONFIG_ACPI */
193
194#ifdef CONFIG_ACPI_I2C_OPREGION
195static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
196 u8 cmd, u8 *data, u8 data_len)
197{
198
199 struct i2c_msg msgs[2];
200 int ret;
201 u8 *buffer;
202
203 buffer = kzalloc(data_len, GFP_KERNEL);
204 if (!buffer)
205 return AE_NO_MEMORY;
206
207 msgs[0].addr = client->addr;
208 msgs[0].flags = client->flags;
209 msgs[0].len = 1;
210 msgs[0].buf = &cmd;
211
212 msgs[1].addr = client->addr;
213 msgs[1].flags = client->flags | I2C_M_RD;
214 msgs[1].len = data_len;
215 msgs[1].buf = buffer;
216
217 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
218 if (ret < 0)
219 dev_err(&client->adapter->dev, "i2c read failed\n");
220 else
221 memcpy(data, buffer, data_len);
222
223 kfree(buffer);
224 return ret;
225}
226
227static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
228 u8 cmd, u8 *data, u8 data_len)
229{
230
231 struct i2c_msg msgs[1];
232 u8 *buffer;
233 int ret = AE_OK;
234
235 buffer = kzalloc(data_len + 1, GFP_KERNEL);
236 if (!buffer)
237 return AE_NO_MEMORY;
238
239 buffer[0] = cmd;
240 memcpy(buffer + 1, data, data_len);
241
242 msgs[0].addr = client->addr;
243 msgs[0].flags = client->flags;
244 msgs[0].len = data_len + 1;
245 msgs[0].buf = buffer;
246
247 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
248 if (ret < 0)
249 dev_err(&client->adapter->dev, "i2c write failed\n");
250
251 kfree(buffer);
252 return ret;
253}
254
255static acpi_status
256acpi_i2c_space_handler(u32 function, acpi_physical_address command,
257 u32 bits, u64 *value64,
258 void *handler_context, void *region_context)
259{
260 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
261 struct acpi_i2c_handler_data *data = handler_context;
262 struct acpi_connection_info *info = &data->info;
263 struct acpi_resource_i2c_serialbus *sb;
264 struct i2c_adapter *adapter = data->adapter;
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300265 struct i2c_client *client;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200266 struct acpi_resource *ares;
267 u32 accessor_type = function >> 16;
268 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100269 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200270 int status;
271
272 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
273 if (ACPI_FAILURE(ret))
274 return ret;
275
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300276 client = kzalloc(sizeof(*client), GFP_KERNEL);
277 if (!client) {
278 ret = AE_NO_MEMORY;
279 goto err;
280 }
281
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200282 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
283 ret = AE_BAD_PARAMETER;
284 goto err;
285 }
286
287 sb = &ares->data.i2c_serial_bus;
288 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
289 ret = AE_BAD_PARAMETER;
290 goto err;
291 }
292
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300293 client->adapter = adapter;
294 client->addr = sb->slave_address;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200295
296 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300297 client->flags |= I2C_CLIENT_TEN;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200298
299 switch (accessor_type) {
300 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
301 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300302 status = i2c_smbus_read_byte(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200303 if (status >= 0) {
304 gsb->bdata = status;
305 status = 0;
306 }
307 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300308 status = i2c_smbus_write_byte(client, gsb->bdata);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200309 }
310 break;
311
312 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
313 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300314 status = i2c_smbus_read_byte_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200315 if (status >= 0) {
316 gsb->bdata = status;
317 status = 0;
318 }
319 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300320 status = i2c_smbus_write_byte_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200321 gsb->bdata);
322 }
323 break;
324
325 case ACPI_GSB_ACCESS_ATTRIB_WORD:
326 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300327 status = i2c_smbus_read_word_data(client, command);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200328 if (status >= 0) {
329 gsb->wdata = status;
330 status = 0;
331 }
332 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300333 status = i2c_smbus_write_word_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200334 gsb->wdata);
335 }
336 break;
337
338 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
339 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300340 status = i2c_smbus_read_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200341 gsb->data);
342 if (status >= 0) {
343 gsb->len = status;
344 status = 0;
345 }
346 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300347 status = i2c_smbus_write_block_data(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200348 gsb->len, gsb->data);
349 }
350 break;
351
352 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
353 if (action == ACPI_READ) {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300354 status = acpi_gsb_i2c_read_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200355 gsb->data, info->access_length);
356 if (status > 0)
357 status = 0;
358 } else {
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300359 status = acpi_gsb_i2c_write_bytes(client, command,
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200360 gsb->data, info->access_length);
361 }
362 break;
363
364 default:
365 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
366 ret = AE_BAD_PARAMETER;
367 goto err;
368 }
369
370 gsb->status = status;
371
372 err:
Jarkko Nikula7ef85f52015-05-20 16:36:52 +0300373 kfree(client);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200374 ACPI_FREE(ares);
375 return ret;
376}
377
378
379static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
380{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200381 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200382 struct acpi_i2c_handler_data *data;
383 acpi_status status;
384
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200385 if (!adapter->dev.parent)
386 return -ENODEV;
387
388 handle = ACPI_HANDLE(adapter->dev.parent);
389
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200390 if (!handle)
391 return -ENODEV;
392
393 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
394 GFP_KERNEL);
395 if (!data)
396 return -ENOMEM;
397
398 data->adapter = adapter;
399 status = acpi_bus_attach_private_data(handle, (void *)data);
400 if (ACPI_FAILURE(status)) {
401 kfree(data);
402 return -ENOMEM;
403 }
404
405 status = acpi_install_address_space_handler(handle,
406 ACPI_ADR_SPACE_GSBUS,
407 &acpi_i2c_space_handler,
408 NULL,
409 data);
410 if (ACPI_FAILURE(status)) {
411 dev_err(&adapter->dev, "Error installing i2c space handler\n");
412 acpi_bus_detach_private_data(handle);
413 kfree(data);
414 return -ENOMEM;
415 }
416
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800417 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200418 return 0;
419}
420
421static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
422{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200423 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200424 struct acpi_i2c_handler_data *data;
425 acpi_status status;
426
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200427 if (!adapter->dev.parent)
428 return;
429
430 handle = ACPI_HANDLE(adapter->dev.parent);
431
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200432 if (!handle)
433 return;
434
435 acpi_remove_address_space_handler(handle,
436 ACPI_ADR_SPACE_GSBUS,
437 &acpi_i2c_space_handler);
438
439 status = acpi_bus_get_private_data(handle, (void **)&data);
440 if (ACPI_SUCCESS(status))
441 kfree(data);
442
443 acpi_bus_detach_private_data(handle);
444}
445#else /* CONFIG_ACPI_I2C_OPREGION */
446static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
447{ }
448
449static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
450{ return 0; }
451#endif /* CONFIG_ACPI_I2C_OPREGION */
452
David Brownellf37dd802007-02-13 22:09:00 +0100453/* ------------------------------------------------------------------------- */
454
Jean Delvared2653e92008-04-29 23:11:39 +0200455static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
456 const struct i2c_client *client)
457{
458 while (id->name[0]) {
459 if (strcmp(client->name, id->name) == 0)
460 return id;
461 id++;
462 }
463 return NULL;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static int i2c_device_match(struct device *dev, struct device_driver *drv)
467{
Jean Delvare51298d12009-09-18 22:45:45 +0200468 struct i2c_client *client = i2c_verify_client(dev);
469 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200470
Jean Delvare51298d12009-09-18 22:45:45 +0200471 if (!client)
472 return 0;
473
Grant Likely959e85f2010-06-08 07:48:19 -0600474 /* Attempt an OF style match */
475 if (of_driver_match_device(dev, drv))
476 return 1;
477
Mika Westerberg907ddf82012-11-23 12:23:40 +0100478 /* Then ACPI style match */
479 if (acpi_driver_match_device(dev, drv))
480 return 1;
481
Jean Delvare51298d12009-09-18 22:45:45 +0200482 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200483 /* match on an id table if there is one */
484 if (driver->id_table)
485 return i2c_match_id(driver->id_table, client) != NULL;
486
Jean Delvareeb8a7902008-05-18 20:49:41 +0200487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
David Brownell7b4fbc52007-05-01 23:26:30 +0200490
491/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200492static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200493{
494 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800495 int rc;
496
497 rc = acpi_device_uevent_modalias(dev, env);
498 if (rc != -ENODEV)
499 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200500
Jean Delvareeb8a7902008-05-18 20:49:41 +0200501 if (add_uevent_var(env, "MODALIAS=%s%s",
502 I2C_MODULE_PREFIX, client->name))
503 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200504 dev_dbg(dev, "uevent\n");
505 return 0;
506}
507
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530508/* i2c bus recovery routines */
509static int get_scl_gpio_value(struct i2c_adapter *adap)
510{
511 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
512}
513
514static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
515{
516 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
517}
518
519static int get_sda_gpio_value(struct i2c_adapter *adap)
520{
521 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
522}
523
524static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
525{
526 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
527 struct device *dev = &adap->dev;
528 int ret = 0;
529
530 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
531 GPIOF_OUT_INIT_HIGH, "i2c-scl");
532 if (ret) {
533 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
534 return ret;
535 }
536
537 if (bri->get_sda) {
538 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
539 /* work without SDA polling */
540 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
541 bri->sda_gpio);
542 bri->get_sda = NULL;
543 }
544 }
545
546 return ret;
547}
548
549static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
550{
551 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
552
553 if (bri->get_sda)
554 gpio_free(bri->sda_gpio);
555
556 gpio_free(bri->scl_gpio);
557}
558
559/*
560 * We are generating clock pulses. ndelay() determines durating of clk pulses.
561 * We will generate clock with rate 100 KHz and so duration of both clock levels
562 * is: delay in ns = (10^6 / 100) / 2
563 */
564#define RECOVERY_NDELAY 5000
565#define RECOVERY_CLK_CNT 9
566
567static int i2c_generic_recovery(struct i2c_adapter *adap)
568{
569 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
570 int i = 0, val = 1, ret = 0;
571
572 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300573 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530574
Jan Luebbe8b062602015-07-08 16:35:06 +0200575 bri->set_scl(adap, val);
576 ndelay(RECOVERY_NDELAY);
577
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530578 /*
579 * By this time SCL is high, as we need to give 9 falling-rising edges
580 */
581 while (i++ < RECOVERY_CLK_CNT * 2) {
582 if (val) {
583 /* Break if SDA is high */
584 if (bri->get_sda && bri->get_sda(adap))
585 break;
586 /* SCL shouldn't be low here */
587 if (!bri->get_scl(adap)) {
588 dev_err(&adap->dev,
589 "SCL is stuck low, exit recovery\n");
590 ret = -EBUSY;
591 break;
592 }
593 }
594
595 val = !val;
596 bri->set_scl(adap, val);
597 ndelay(RECOVERY_NDELAY);
598 }
599
600 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300601 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530602
603 return ret;
604}
605
606int i2c_generic_scl_recovery(struct i2c_adapter *adap)
607{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530608 return i2c_generic_recovery(adap);
609}
Mark Brownc1c21f42015-04-15 19:18:39 +0100610EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530611
612int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
613{
614 int ret;
615
616 ret = i2c_get_gpios_for_recovery(adap);
617 if (ret)
618 return ret;
619
620 ret = i2c_generic_recovery(adap);
621 i2c_put_gpios_for_recovery(adap);
622
623 return ret;
624}
Mark Brownc1c21f42015-04-15 19:18:39 +0100625EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530626
627int i2c_recover_bus(struct i2c_adapter *adap)
628{
629 if (!adap->bus_recovery_info)
630 return -EOPNOTSUPP;
631
632 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
633 return adap->bus_recovery_info->recover_bus(adap);
634}
Mark Brownc1c21f42015-04-15 19:18:39 +0100635EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static int i2c_device_probe(struct device *dev)
638{
Jean Delvare51298d12009-09-18 22:45:45 +0200639 struct i2c_client *client = i2c_verify_client(dev);
640 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100641 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200642
Jean Delvare51298d12009-09-18 22:45:45 +0200643 if (!client)
644 return 0;
645
Mika Westerberg845c8772015-05-06 13:29:08 +0300646 if (!client->irq) {
647 int irq = -ENOENT;
648
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700649 if (dev->of_node) {
650 irq = of_irq_get_byname(dev->of_node, "irq");
651 if (irq == -EINVAL || irq == -ENODATA)
652 irq = of_irq_get(dev->of_node, 0);
653 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300654 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700655 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100656 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200657 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100658 if (irq < 0)
659 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200660
661 client->irq = irq;
662 }
663
Jean Delvare51298d12009-09-18 22:45:45 +0200664 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200665 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200666 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200667
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700668 if (client->flags & I2C_CLIENT_WAKE) {
669 int wakeirq = -ENOENT;
670
671 if (dev->of_node) {
672 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
673 if (wakeirq == -EPROBE_DEFER)
674 return wakeirq;
675 }
676
677 device_init_wakeup(&client->dev, true);
678
679 if (wakeirq > 0 && wakeirq != client->irq)
680 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
681 else if (client->irq > 0)
682 status = dev_pm_set_wake_irq(dev, wakeirq);
683 else
684 status = 0;
685
686 if (status)
687 dev_warn(&client->dev, "failed to set up wakeup irq");
688 }
689
David Brownell7b4fbc52007-05-01 23:26:30 +0200690 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200691
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200692 status = of_clk_set_defaults(dev->of_node, false);
693 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700694 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200695
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200696 status = dev_pm_domain_attach(&client->dev, true);
697 if (status != -EPROBE_DEFER) {
698 status = driver->probe(client, i2c_match_id(driver->id_table,
699 client));
700 if (status)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700701 goto err_detach_pm_domain;
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200702 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100703
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700704 return 0;
705
706err_detach_pm_domain:
707 dev_pm_domain_detach(&client->dev, true);
708err_clear_wakeup_irq:
709 dev_pm_clear_wake_irq(&client->dev);
710 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static int i2c_device_remove(struct device *dev)
715{
Jean Delvare51298d12009-09-18 22:45:45 +0200716 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200717 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100718 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200719
Jean Delvare51298d12009-09-18 22:45:45 +0200720 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200721 return 0;
722
723 driver = to_i2c_driver(dev->driver);
724 if (driver->remove) {
725 dev_dbg(dev, "remove\n");
726 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200727 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100728
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200729 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700730
731 dev_pm_clear_wake_irq(&client->dev);
732 device_init_wakeup(&client->dev, false);
733
David Brownella1d9e6e2007-05-01 23:26:30 +0200734 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
David Brownellf37dd802007-02-13 22:09:00 +0100737static void i2c_device_shutdown(struct device *dev)
738{
Jean Delvare51298d12009-09-18 22:45:45 +0200739 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100740 struct i2c_driver *driver;
741
Jean Delvare51298d12009-09-18 22:45:45 +0200742 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100743 return;
744 driver = to_i2c_driver(dev->driver);
745 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200746 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100747}
748
David Brownell9c1600e2007-05-01 23:26:31 +0200749static void i2c_client_dev_release(struct device *dev)
750{
751 kfree(to_i2c_client(dev));
752}
753
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100754static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200755show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200756{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200757 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
758 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200759}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100760static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200761
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100762static ssize_t
763show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200764{
765 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800766 int len;
767
768 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
769 if (len != -ENODEV)
770 return len;
771
Jean Delvareeb8a7902008-05-18 20:49:41 +0200772 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200773}
Jean Delvare51298d12009-09-18 22:45:45 +0200774static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
775
776static struct attribute *i2c_dev_attrs[] = {
777 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200778 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200779 &dev_attr_modalias.attr,
780 NULL
781};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100782ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100783
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200784struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100785 .name = "i2c",
786 .match = i2c_device_match,
787 .probe = i2c_device_probe,
788 .remove = i2c_device_remove,
789 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000790};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200791EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000792
Jean Delvare51298d12009-09-18 22:45:45 +0200793static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100794 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200795 .uevent = i2c_device_uevent,
796 .release = i2c_client_dev_release,
797};
798
David Brownell9b766b82008-01-27 18:14:51 +0100799
800/**
801 * i2c_verify_client - return parameter as i2c_client, or NULL
802 * @dev: device, probably from some driver model iterator
803 *
804 * When traversing the driver model tree, perhaps using driver model
805 * iterators like @device_for_each_child(), you can't assume very much
806 * about the nodes you find. Use this function to avoid oopses caused
807 * by wrongly treating some non-I2C device as an i2c_client.
808 */
809struct i2c_client *i2c_verify_client(struct device *dev)
810{
Jean Delvare51298d12009-09-18 22:45:45 +0200811 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100812 ? to_i2c_client(dev)
813 : NULL;
814}
815EXPORT_SYMBOL(i2c_verify_client);
816
817
Wolfram Sangda899f52015-05-18 21:09:12 +0200818/* Return a unique address which takes the flags of the client into account */
819static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
820{
821 unsigned short addr = client->addr;
822
823 /* For some client flags, add an arbitrary offset to avoid collisions */
824 if (client->flags & I2C_CLIENT_TEN)
825 addr |= I2C_ADDR_OFFSET_TEN_BIT;
826
827 if (client->flags & I2C_CLIENT_SLAVE)
828 addr |= I2C_ADDR_OFFSET_SLAVE;
829
830 return addr;
831}
832
Jean Delvare3a89db52010-06-03 11:33:52 +0200833/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300834 * are purposely not enforced, except for the general call address. */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200835static int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200836{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200837 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200838 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200839 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200840 return -EINVAL;
841 } else {
842 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200843 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200844 return -EINVAL;
845 }
846 return 0;
847}
848
Jean Delvare656b8762010-06-03 11:33:53 +0200849/* And this is a strict address validity check, used when probing. If a
850 * device uses a reserved address, then it shouldn't be probed. 7-bit
851 * addressing is assumed, 10-bit address devices are rare and should be
852 * explicitly enumerated. */
Wolfram Sang66be60562015-07-17 12:43:22 +0200853static int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200854{
855 /*
856 * Reserved addresses per I2C specification:
857 * 0x00 General call address / START byte
858 * 0x01 CBUS address
859 * 0x02 Reserved for different bus format
860 * 0x03 Reserved for future purposes
861 * 0x04-0x07 Hs-mode master code
862 * 0x78-0x7b 10-bit slave addressing
863 * 0x7c-0x7f Reserved for future purposes
864 */
865 if (addr < 0x08 || addr > 0x77)
866 return -EINVAL;
867 return 0;
868}
869
Jean Delvare3b5f7942010-06-03 11:33:55 +0200870static int __i2c_check_addr_busy(struct device *dev, void *addrp)
871{
872 struct i2c_client *client = i2c_verify_client(dev);
873 int addr = *(int *)addrp;
874
Wolfram Sang9bccc702015-07-17 14:48:56 +0200875 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200876 return -EBUSY;
877 return 0;
878}
879
Michael Lawnick08263742010-08-11 18:21:02 +0200880/* walk up mux tree */
881static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
882{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200883 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200884 int result;
885
886 result = device_for_each_child(&adapter->dev, &addr,
887 __i2c_check_addr_busy);
888
Jean Delvare97cc4d42010-10-24 18:16:57 +0200889 if (!result && parent)
890 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200891
892 return result;
893}
894
895/* recurse down mux tree */
896static int i2c_check_mux_children(struct device *dev, void *addrp)
897{
898 int result;
899
900 if (dev->type == &i2c_adapter_type)
901 result = device_for_each_child(dev, addrp,
902 i2c_check_mux_children);
903 else
904 result = __i2c_check_addr_busy(dev, addrp);
905
906 return result;
907}
908
Jean Delvare3b5f7942010-06-03 11:33:55 +0200909static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
910{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200911 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200912 int result = 0;
913
Jean Delvare97cc4d42010-10-24 18:16:57 +0200914 if (parent)
915 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200916
917 if (!result)
918 result = device_for_each_child(&adapter->dev, &addr,
919 i2c_check_mux_children);
920
921 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200922}
923
David Brownell9c1600e2007-05-01 23:26:31 +0200924/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200925 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
926 * @adapter: Target I2C bus segment
927 */
928void i2c_lock_adapter(struct i2c_adapter *adapter)
929{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200930 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
931
932 if (parent)
933 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200934 else
935 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200936}
937EXPORT_SYMBOL_GPL(i2c_lock_adapter);
938
939/**
940 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
941 * @adapter: Target I2C bus segment
942 */
943static int i2c_trylock_adapter(struct i2c_adapter *adapter)
944{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200945 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
946
947 if (parent)
948 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200949 else
950 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200951}
952
953/**
954 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
955 * @adapter: Target I2C bus segment
956 */
957void i2c_unlock_adapter(struct i2c_adapter *adapter)
958{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200959 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
960
961 if (parent)
962 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200963 else
964 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200965}
966EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
967
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200968static void i2c_dev_set_name(struct i2c_adapter *adap,
969 struct i2c_client *client)
970{
971 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
972
973 if (adev) {
974 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
975 return;
976 }
977
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200978 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200979 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200980}
981
Jean Delvarefe61e072010-08-11 18:20:58 +0200982/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200983 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200984 * @adap: the adapter managing the device
985 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200986 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200987 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200988 * Create an i2c device. Binding is handled through driver model
989 * probe()/remove() methods. A driver may be bound to this device when we
990 * return from this function, or any later moment (e.g. maybe hotplugging will
991 * load the driver module). This call is not appropriate for use by mainboard
992 * initialization logic, which usually runs during an arch_initcall() long
993 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200994 *
995 * This returns the new i2c client, which may be saved for later use with
996 * i2c_unregister_device(); or NULL to indicate an error.
997 */
998struct i2c_client *
999i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1000{
1001 struct i2c_client *client;
1002 int status;
1003
1004 client = kzalloc(sizeof *client, GFP_KERNEL);
1005 if (!client)
1006 return NULL;
1007
1008 client->adapter = adap;
1009
1010 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001011
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001012 if (info->archdata)
1013 client->dev.archdata = *info->archdata;
1014
Marc Pignatee354252008-08-28 08:33:22 +02001015 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001016 client->addr = info->addr;
1017 client->irq = info->irq;
1018
David Brownell9c1600e2007-05-01 23:26:31 +02001019 strlcpy(client->name, info->type, sizeof(client->name));
1020
Wolfram Sangc4019b72015-07-17 12:50:06 +02001021 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +02001022 if (status) {
1023 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1024 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1025 goto out_err_silent;
1026 }
1027
Jean Delvaref8a227e2009-06-19 16:58:18 +02001028 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +02001029 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +02001030 if (status)
1031 goto out_err;
1032
1033 client->dev.parent = &client->adapter->dev;
1034 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001035 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001036 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +01001037 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001038
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001039 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001040 status = device_register(&client->dev);
1041 if (status)
1042 goto out_err;
1043
Jean Delvaref8a227e2009-06-19 16:58:18 +02001044 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1045 client->name, dev_name(&client->dev));
1046
David Brownell9c1600e2007-05-01 23:26:31 +02001047 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001048
1049out_err:
1050 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1051 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001052out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001053 kfree(client);
1054 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001055}
1056EXPORT_SYMBOL_GPL(i2c_new_device);
1057
1058
1059/**
1060 * i2c_unregister_device - reverse effect of i2c_new_device()
1061 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001062 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001063 */
1064void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001065{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001066 if (client->dev.of_node)
1067 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
David Brownella1d9e6e2007-05-01 23:26:30 +02001068 device_unregister(&client->dev);
1069}
David Brownell9c1600e2007-05-01 23:26:31 +02001070EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001071
1072
Jean Delvare60b129d2008-05-11 20:37:06 +02001073static const struct i2c_device_id dummy_id[] = {
1074 { "dummy", 0 },
1075 { },
1076};
1077
Jean Delvared2653e92008-04-29 23:11:39 +02001078static int dummy_probe(struct i2c_client *client,
1079 const struct i2c_device_id *id)
1080{
1081 return 0;
1082}
1083
1084static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001085{
1086 return 0;
1087}
1088
1089static struct i2c_driver dummy_driver = {
1090 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001091 .probe = dummy_probe,
1092 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001093 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001094};
1095
1096/**
1097 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1098 * @adapter: the adapter managing the device
1099 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001100 * Context: can sleep
1101 *
1102 * This returns an I2C client bound to the "dummy" driver, intended for use
1103 * with devices that consume multiple addresses. Examples of such chips
1104 * include various EEPROMS (like 24c04 and 24c08 models).
1105 *
1106 * These dummy devices have two main uses. First, most I2C and SMBus calls
1107 * except i2c_transfer() need a client handle; the dummy will be that handle.
1108 * And second, this prevents the specified address from being bound to a
1109 * different driver.
1110 *
1111 * This returns the new i2c client, which should be saved for later use with
1112 * i2c_unregister_device(); or NULL to indicate an error.
1113 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001114struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001115{
1116 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001117 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001118 };
1119
David Brownelle9f13732008-01-27 18:14:52 +01001120 return i2c_new_device(adapter, &info);
1121}
1122EXPORT_SYMBOL_GPL(i2c_new_dummy);
1123
David Brownellf37dd802007-02-13 22:09:00 +01001124/* ------------------------------------------------------------------------- */
1125
David Brownell16ffadf2007-05-01 23:26:28 +02001126/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1127
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001128static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
David Brownellef2c83212007-05-01 23:26:28 +02001130 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 complete(&adap->dev_released);
1132}
1133
Jean Delvare99cd8e22009-06-19 16:58:20 +02001134/*
Jean Delvare390946b2012-09-10 10:14:02 +02001135 * This function is only needed for mutex_lock_nested, so it is never
1136 * called unless locking correctness checking is enabled. Thus we
1137 * make it inline to avoid a compiler warning. That's what gcc ends up
1138 * doing anyway.
1139 */
1140static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1141{
1142 unsigned int depth = 0;
1143
1144 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1145 depth++;
1146
1147 return depth;
1148}
1149
1150/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001151 * Let users instantiate I2C devices through sysfs. This can be used when
1152 * platform initialization code doesn't contain the proper data for
1153 * whatever reason. Also useful for drivers that do device detection and
1154 * detection fails, either because the device uses an unexpected address,
1155 * or this is a compatible device with different ID register values.
1156 *
1157 * Parameter checking may look overzealous, but we really don't want
1158 * the user to provide incorrect parameters.
1159 */
1160static ssize_t
1161i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1162 const char *buf, size_t count)
1163{
1164 struct i2c_adapter *adap = to_i2c_adapter(dev);
1165 struct i2c_board_info info;
1166 struct i2c_client *client;
1167 char *blank, end;
1168 int res;
1169
Jean Delvare99cd8e22009-06-19 16:58:20 +02001170 memset(&info, 0, sizeof(struct i2c_board_info));
1171
1172 blank = strchr(buf, ' ');
1173 if (!blank) {
1174 dev_err(dev, "%s: Missing parameters\n", "new_device");
1175 return -EINVAL;
1176 }
1177 if (blank - buf > I2C_NAME_SIZE - 1) {
1178 dev_err(dev, "%s: Invalid device name\n", "new_device");
1179 return -EINVAL;
1180 }
1181 memcpy(info.type, buf, blank - buf);
1182
1183 /* Parse remaining parameters, reject extra parameters */
1184 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1185 if (res < 1) {
1186 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1187 return -EINVAL;
1188 }
1189 if (res > 1 && end != '\n') {
1190 dev_err(dev, "%s: Extra parameters\n", "new_device");
1191 return -EINVAL;
1192 }
1193
Wolfram Sangcfa03272015-07-27 14:03:38 +02001194 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1195 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1196 info.flags |= I2C_CLIENT_TEN;
1197 }
1198
1199 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1200 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1201 info.flags |= I2C_CLIENT_SLAVE;
1202 }
1203
Jean Delvare99cd8e22009-06-19 16:58:20 +02001204 client = i2c_new_device(adap, &info);
1205 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001206 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001207
1208 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001209 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001210 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001211 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001212 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1213 info.type, info.addr);
1214
1215 return count;
1216}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001217static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001218
1219/*
1220 * And of course let the users delete the devices they instantiated, if
1221 * they got it wrong. This interface can only be used to delete devices
1222 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1223 * don't delete devices to which some kernel code still has references.
1224 *
1225 * Parameter checking may look overzealous, but we really don't want
1226 * the user to delete the wrong device.
1227 */
1228static ssize_t
1229i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1230 const char *buf, size_t count)
1231{
1232 struct i2c_adapter *adap = to_i2c_adapter(dev);
1233 struct i2c_client *client, *next;
1234 unsigned short addr;
1235 char end;
1236 int res;
1237
1238 /* Parse parameters, reject extra parameters */
1239 res = sscanf(buf, "%hi%c", &addr, &end);
1240 if (res < 1) {
1241 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1242 return -EINVAL;
1243 }
1244 if (res > 1 && end != '\n') {
1245 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1246 return -EINVAL;
1247 }
1248
1249 /* Make sure the device was added through sysfs */
1250 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001251 mutex_lock_nested(&adap->userspace_clients_lock,
1252 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001253 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1254 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001255 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001256 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1257 "delete_device", client->name, client->addr);
1258
1259 list_del(&client->detected);
1260 i2c_unregister_device(client);
1261 res = count;
1262 break;
1263 }
1264 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001265 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001266
1267 if (res < 0)
1268 dev_err(dev, "%s: Can't find device in list\n",
1269 "delete_device");
1270 return res;
1271}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001272static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1273 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001274
1275static struct attribute *i2c_adapter_attrs[] = {
1276 &dev_attr_name.attr,
1277 &dev_attr_new_device.attr,
1278 &dev_attr_delete_device.attr,
1279 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001280};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001281ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001282
Michael Lawnick08263742010-08-11 18:21:02 +02001283struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001284 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001285 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001286};
Michael Lawnick08263742010-08-11 18:21:02 +02001287EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Stephen Warren643dd092012-04-17 12:43:33 -06001289/**
1290 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1291 * @dev: device, probably from some driver model iterator
1292 *
1293 * When traversing the driver model tree, perhaps using driver model
1294 * iterators like @device_for_each_child(), you can't assume very much
1295 * about the nodes you find. Use this function to avoid oopses caused
1296 * by wrongly treating some non-I2C device as an i2c_adapter.
1297 */
1298struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1299{
1300 return (dev->type == &i2c_adapter_type)
1301 ? to_i2c_adapter(dev)
1302 : NULL;
1303}
1304EXPORT_SYMBOL(i2c_verify_adapter);
1305
Jean Delvare2bb50952009-09-18 22:45:46 +02001306#ifdef CONFIG_I2C_COMPAT
1307static struct class_compat *i2c_adapter_compat_class;
1308#endif
1309
David Brownell9c1600e2007-05-01 23:26:31 +02001310static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1311{
1312 struct i2c_devinfo *devinfo;
1313
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001314 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001315 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1316 if (devinfo->busnum == adapter->nr
1317 && !i2c_new_device(adapter,
1318 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001319 dev_err(&adapter->dev,
1320 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001321 devinfo->board_info.addr);
1322 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001323 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001324}
1325
Wolfram Sang687b81d2013-07-11 12:56:15 +01001326/* OF support code */
1327
1328#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001329static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1330 struct device_node *node)
1331{
1332 struct i2c_client *result;
1333 struct i2c_board_info info = {};
1334 struct dev_archdata dev_ad = {};
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001335 const __be32 *addr_be;
1336 u32 addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001337 int len;
1338
1339 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1340
1341 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1342 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1343 node->full_name);
1344 return ERR_PTR(-EINVAL);
1345 }
1346
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001347 addr_be = of_get_property(node, "reg", &len);
1348 if (!addr_be || (len < sizeof(*addr_be))) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001349 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1350 node->full_name);
1351 return ERR_PTR(-EINVAL);
1352 }
1353
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001354 addr = be32_to_cpup(addr_be);
1355 if (addr & I2C_TEN_BIT_ADDRESS) {
1356 addr &= ~I2C_TEN_BIT_ADDRESS;
1357 info.flags |= I2C_CLIENT_TEN;
1358 }
1359
1360 if (addr & I2C_OWN_SLAVE_ADDRESS) {
1361 addr &= ~I2C_OWN_SLAVE_ADDRESS;
1362 info.flags |= I2C_CLIENT_SLAVE;
1363 }
1364
1365 if (i2c_check_addr_validity(addr, info.flags)) {
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001366 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1367 info.addr, node->full_name);
1368 return ERR_PTR(-EINVAL);
1369 }
1370
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +02001371 info.addr = addr;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001372 info.of_node = of_node_get(node);
1373 info.archdata = &dev_ad;
1374
1375 if (of_get_property(node, "wakeup-source", NULL))
1376 info.flags |= I2C_CLIENT_WAKE;
1377
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001378 result = i2c_new_device(adap, &info);
1379 if (result == NULL) {
1380 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1381 node->full_name);
1382 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001383 return ERR_PTR(-EINVAL);
1384 }
1385 return result;
1386}
1387
Wolfram Sang687b81d2013-07-11 12:56:15 +01001388static void of_i2c_register_devices(struct i2c_adapter *adap)
1389{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001390 struct device_node *node;
1391
1392 /* Only register child devices if the adapter has a node pointer set */
1393 if (!adap->dev.of_node)
1394 return;
1395
1396 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1397
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001398 for_each_available_child_of_node(adap->dev.of_node, node) {
1399 if (of_node_test_and_set_flag(node, OF_POPULATED))
1400 continue;
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001401 of_i2c_register_device(adap, node);
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001402 }
Wolfram Sang687b81d2013-07-11 12:56:15 +01001403}
1404
1405static int of_dev_node_match(struct device *dev, void *data)
1406{
1407 return dev->of_node == data;
1408}
1409
1410/* must call put_device() when done with returned i2c_client device */
1411struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1412{
1413 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001414 struct i2c_client *client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001415
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001416 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001417 if (!dev)
1418 return NULL;
1419
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001420 client = i2c_verify_client(dev);
1421 if (!client)
1422 put_device(dev);
1423
1424 return client;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001425}
1426EXPORT_SYMBOL(of_find_i2c_device_by_node);
1427
1428/* must call put_device() when done with returned i2c_adapter device */
1429struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1430{
1431 struct device *dev;
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001432 struct i2c_adapter *adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001433
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001434 dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001435 if (!dev)
1436 return NULL;
1437
Vladimir Zapolskiye3311462015-07-27 17:30:48 +03001438 adapter = i2c_verify_adapter(dev);
1439 if (!adapter)
1440 put_device(dev);
1441
1442 return adapter;
Wolfram Sang687b81d2013-07-11 12:56:15 +01001443}
1444EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
Vladimir Zapolskiy48e97432015-07-27 17:30:50 +03001445
1446/* must call i2c_put_adapter() when done with returned i2c_adapter device */
1447struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
1448{
1449 struct i2c_adapter *adapter;
1450
1451 adapter = of_find_i2c_adapter_by_node(node);
1452 if (!adapter)
1453 return NULL;
1454
1455 if (!try_module_get(adapter->owner)) {
1456 put_device(&adapter->dev);
1457 adapter = NULL;
1458 }
1459
1460 return adapter;
1461}
1462EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001463#else
1464static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1465#endif /* CONFIG_OF */
1466
Jean Delvare69b00892009-12-06 17:06:27 +01001467static int i2c_do_add_adapter(struct i2c_driver *driver,
1468 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001469{
Jean Delvare4735c982008-07-14 22:38:36 +02001470 /* Detect supported devices on that bus, and instantiate them */
1471 i2c_detect(adap, driver);
1472
1473 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001474 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001475 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1476 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001477 dev_warn(&adap->dev, "Please use another way to instantiate "
1478 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001479 /* We ignore the return code; if it fails, too bad */
1480 driver->attach_adapter(adap);
1481 }
1482 return 0;
1483}
1484
Jean Delvare69b00892009-12-06 17:06:27 +01001485static int __process_new_adapter(struct device_driver *d, void *data)
1486{
1487 return i2c_do_add_adapter(to_i2c_driver(d), data);
1488}
1489
David Brownell6e13e642007-05-01 23:26:31 +02001490static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Jean Delvared6703282010-08-11 18:20:59 +02001492 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
David Brownell1d0b19c2008-10-14 17:30:05 +02001494 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001495 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1496 res = -EAGAIN;
1497 goto out_list;
1498 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001499
Jean Delvare2236baa2010-11-15 22:40:38 +01001500 /* Sanity checks */
1501 if (unlikely(adap->name[0] == '\0')) {
1502 pr_err("i2c-core: Attempt to register an adapter with "
1503 "no name!\n");
1504 return -EINVAL;
1505 }
1506 if (unlikely(!adap->algo)) {
1507 pr_err("i2c-core: Attempt to register adapter '%s' with "
1508 "no algo!\n", adap->name);
1509 return -EINVAL;
1510 }
1511
Mika Kuoppala194684e2009-12-06 17:06:22 +01001512 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001513 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001514 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Jean Delvare8fcfef62009-03-28 21:34:43 +01001516 /* Set default timeout to 1 second if not already set */
1517 if (adap->timeout == 0)
1518 adap->timeout = HZ;
1519
Kay Sievers27d9c182009-01-07 14:29:16 +01001520 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001521 adap->dev.bus = &i2c_bus_type;
1522 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001523 res = device_register(&adap->dev);
1524 if (res)
1525 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001527 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1528
Charles Keepax6ada5c12015-04-16 13:05:19 +01001529 pm_runtime_no_callbacks(&adap->dev);
1530
Jean Delvare2bb50952009-09-18 22:45:46 +02001531#ifdef CONFIG_I2C_COMPAT
1532 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1533 adap->dev.parent);
1534 if (res)
1535 dev_warn(&adap->dev,
1536 "Failed to create compatibility class link\n");
1537#endif
1538
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301539 /* bus recovery specific initialization */
1540 if (adap->bus_recovery_info) {
1541 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1542
1543 if (!bri->recover_bus) {
1544 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1545 adap->bus_recovery_info = NULL;
1546 goto exit_recovery;
1547 }
1548
1549 /* Generic GPIO recovery */
1550 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1551 if (!gpio_is_valid(bri->scl_gpio)) {
1552 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1553 adap->bus_recovery_info = NULL;
1554 goto exit_recovery;
1555 }
1556
1557 if (gpio_is_valid(bri->sda_gpio))
1558 bri->get_sda = get_sda_gpio_value;
1559 else
1560 bri->get_sda = NULL;
1561
1562 bri->get_scl = get_scl_gpio_value;
1563 bri->set_scl = set_scl_gpio_value;
1564 } else if (!bri->set_scl || !bri->get_scl) {
1565 /* Generic SCL recovery */
1566 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1567 adap->bus_recovery_info = NULL;
1568 }
1569 }
1570
1571exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001572 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001573 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001574 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001575 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001576
David Brownell6e13e642007-05-01 23:26:31 +02001577 if (adap->nr < __i2c_first_dynamic_bus_num)
1578 i2c_scan_static_board_info(adap);
1579
Jean Delvare4735c982008-07-14 22:38:36 +02001580 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001581 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001582 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001583 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001584
1585 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001586
Jean Delvareb119c6c2006-08-15 18:26:30 +02001587out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001588 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001589 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001590 mutex_unlock(&core_lock);
1591 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
David Brownell6e13e642007-05-01 23:26:31 +02001594/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001595 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1596 * @adap: the adapter to register (with adap->nr initialized)
1597 * Context: can sleep
1598 *
1599 * See i2c_add_numbered_adapter() for details.
1600 */
1601static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1602{
1603 int id;
1604
1605 mutex_lock(&core_lock);
1606 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1607 GFP_KERNEL);
1608 mutex_unlock(&core_lock);
1609 if (id < 0)
1610 return id == -ENOSPC ? -EBUSY : id;
1611
1612 return i2c_register_adapter(adap);
1613}
1614
1615/**
David Brownell6e13e642007-05-01 23:26:31 +02001616 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1617 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001618 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001619 *
1620 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001621 * doesn't matter or when its bus number is specified by an dt alias.
1622 * Examples of bases when the bus number doesn't matter: I2C adapters
1623 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001624 *
1625 * When this returns zero, a new bus number was allocated and stored
1626 * in adap->nr, and the specified adapter became available for clients.
1627 * Otherwise, a negative errno value is returned.
1628 */
1629int i2c_add_adapter(struct i2c_adapter *adapter)
1630{
Doug Andersonee5c2742013-03-01 08:57:31 -08001631 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001632 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001633
Doug Andersonee5c2742013-03-01 08:57:31 -08001634 if (dev->of_node) {
1635 id = of_alias_get_id(dev->of_node, "i2c");
1636 if (id >= 0) {
1637 adapter->nr = id;
1638 return __i2c_add_numbered_adapter(adapter);
1639 }
1640 }
1641
Jean Delvarecaada322008-01-27 18:14:49 +01001642 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001643 id = idr_alloc(&i2c_adapter_idr, adapter,
1644 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001645 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001646 if (id < 0)
1647 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001648
1649 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001650
David Brownell6e13e642007-05-01 23:26:31 +02001651 return i2c_register_adapter(adapter);
1652}
1653EXPORT_SYMBOL(i2c_add_adapter);
1654
1655/**
1656 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1657 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001658 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001659 *
1660 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001661 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1662 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001663 * is used to properly configure I2C devices.
1664 *
Grant Likely488bf312011-07-25 17:49:43 +02001665 * If the requested bus number is set to -1, then this function will behave
1666 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1667 *
David Brownell6e13e642007-05-01 23:26:31 +02001668 * If no devices have pre-been declared for this bus, then be sure to
1669 * register the adapter before any dynamically allocated ones. Otherwise
1670 * the required bus ID may not be available.
1671 *
1672 * When this returns zero, the specified adapter became available for
1673 * clients using the bus number provided in adap->nr. Also, the table
1674 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1675 * and the appropriate driver model device nodes are created. Otherwise, a
1676 * negative errno value is returned.
1677 */
1678int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1679{
Grant Likely488bf312011-07-25 17:49:43 +02001680 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1681 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001682
Doug Andersonee5c2742013-03-01 08:57:31 -08001683 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001684}
1685EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1686
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001687static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001688 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001689{
Jean Delvare4735c982008-07-14 22:38:36 +02001690 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001691
Jean Delvareacec2112009-03-28 21:34:40 +01001692 /* Remove the devices we created ourselves as the result of hardware
1693 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001694 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1695 if (client->adapter == adapter) {
1696 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1697 client->name, client->addr);
1698 list_del(&client->detected);
1699 i2c_unregister_device(client);
1700 }
1701 }
Jean Delvare026526f2008-01-27 18:14:49 +01001702}
1703
Jean Delvaree549c2b2009-06-19 16:58:19 +02001704static int __unregister_client(struct device *dev, void *dummy)
1705{
1706 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001707 if (client && strcmp(client->name, "dummy"))
1708 i2c_unregister_device(client);
1709 return 0;
1710}
1711
1712static int __unregister_dummy(struct device *dev, void *dummy)
1713{
1714 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001715 if (client)
1716 i2c_unregister_device(client);
1717 return 0;
1718}
1719
Jean Delvare69b00892009-12-06 17:06:27 +01001720static int __process_removed_adapter(struct device_driver *d, void *data)
1721{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001722 i2c_do_del_adapter(to_i2c_driver(d), data);
1723 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001724}
1725
David Brownelld64f73b2007-07-12 14:12:28 +02001726/**
1727 * i2c_del_adapter - unregister I2C adapter
1728 * @adap: the adapter being unregistered
1729 * Context: can sleep
1730 *
1731 * This unregisters an I2C adapter which was previously registered
1732 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1733 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001734void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001736 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001737 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001740 mutex_lock(&core_lock);
1741 found = idr_find(&i2c_adapter_idr, adap->nr);
1742 mutex_unlock(&core_lock);
1743 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001744 pr_debug("i2c-core: attempting to delete unregistered "
1745 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001746 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
Lan Tianyu5d98e612014-05-20 20:59:23 +08001749 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001750 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001751 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001752 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001753 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001754 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001756 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001757 mutex_lock_nested(&adap->userspace_clients_lock,
1758 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001759 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1760 detected) {
1761 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1762 client->addr);
1763 list_del(&client->detected);
1764 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001765 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001766 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001767
Jean Delvaree549c2b2009-06-19 16:58:19 +02001768 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001769 * check the returned value. This is a two-pass process, because
1770 * we can't remove the dummy devices during the first pass: they
1771 * could have been instantiated by real devices wishing to clean
1772 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001773 device_for_each_child(&adap->dev, NULL, __unregister_client);
1774 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Jean Delvare2bb50952009-09-18 22:45:46 +02001776#ifdef CONFIG_I2C_COMPAT
1777 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1778 adap->dev.parent);
1779#endif
1780
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001781 /* device name is gone after device_unregister */
1782 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1783
Wolfram Sang26680ee2015-01-29 22:45:09 +01001784 /* wait until all references to the device are gone
1785 *
1786 * FIXME: This is old code and should ideally be replaced by an
1787 * alternative which results in decoupling the lifetime of the struct
1788 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301789 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
David Brownell6e13e642007-05-01 23:26:31 +02001795 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001796 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001798 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001800 /* Clear the device structure in case this adapter is ever going to be
1801 added again */
1802 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
David Brownellc0564602007-05-01 23:26:31 +02001804EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Brownell7b4fbc52007-05-01 23:26:30 +02001806/* ------------------------------------------------------------------------- */
1807
Jean Delvare7ae31482011-03-20 14:50:52 +01001808int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1809{
1810 int res;
1811
1812 mutex_lock(&core_lock);
1813 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1814 mutex_unlock(&core_lock);
1815
1816 return res;
1817}
1818EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1819
Jean Delvare69b00892009-12-06 17:06:27 +01001820static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001821{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001822 if (dev->type != &i2c_adapter_type)
1823 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001824 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001825}
1826
David Brownell7b4fbc52007-05-01 23:26:30 +02001827/*
1828 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001829 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
1831
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001832int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001834 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
David Brownell1d0b19c2008-10-14 17:30:05 +02001836 /* Can't register until after driver model init */
1837 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1838 return -EAGAIN;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001841 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jean Delvare729d6dd2009-06-19 16:58:18 +02001844 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001845 * will have called probe() for all matching-but-unbound devices.
1846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 res = driver_register(&driver->driver);
1848 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001849 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001850
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001851 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Jean Delvare4735c982008-07-14 22:38:36 +02001853 INIT_LIST_HEAD(&driver->clients);
1854 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001855 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001856
Jean Delvare7eebcb72006-02-05 23:28:21 +01001857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001859EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jean Delvare69b00892009-12-06 17:06:27 +01001861static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001862{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001863 if (dev->type == &i2c_adapter_type)
1864 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1865 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001866}
1867
David Brownella1d9e6e2007-05-01 23:26:30 +02001868/**
1869 * i2c_del_driver - unregister I2C driver
1870 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001871 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001872 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001873void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Jean Delvare7ae31482011-03-20 14:50:52 +01001875 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001878 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
David Brownellc0564602007-05-01 23:26:31 +02001880EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
David Brownell7b4fbc52007-05-01 23:26:30 +02001882/* ------------------------------------------------------------------------- */
1883
Jean Delvaree48d3312008-01-27 18:14:48 +01001884/**
1885 * i2c_use_client - increments the reference count of the i2c client structure
1886 * @client: the client being referenced
1887 *
1888 * Each live reference to a client should be refcounted. The driver model does
1889 * that automatically as part of driver binding, so that most drivers don't
1890 * need to do this explicitly: they hold a reference until they're unbound
1891 * from the device.
1892 *
1893 * A pointer to the client with the incremented reference counter is returned.
1894 */
1895struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
David Brownell6ea438e2008-07-14 22:38:24 +02001897 if (client && get_device(&client->dev))
1898 return client;
1899 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
David Brownellc0564602007-05-01 23:26:31 +02001901EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jean Delvaree48d3312008-01-27 18:14:48 +01001903/**
1904 * i2c_release_client - release a use of the i2c client structure
1905 * @client: the client being no longer referenced
1906 *
1907 * Must be called when a user of a client is finished with it.
1908 */
1909void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
David Brownell6ea438e2008-07-14 22:38:24 +02001911 if (client)
1912 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
David Brownellc0564602007-05-01 23:26:31 +02001914EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
David Brownell9b766b82008-01-27 18:14:51 +01001916struct i2c_cmd_arg {
1917 unsigned cmd;
1918 void *arg;
1919};
1920
1921static int i2c_cmd(struct device *dev, void *_arg)
1922{
1923 struct i2c_client *client = i2c_verify_client(dev);
1924 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001925 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001926
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001927 if (!client || !client->dev.driver)
1928 return 0;
1929
1930 driver = to_i2c_driver(client->dev.driver);
1931 if (driver->command)
1932 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001933 return 0;
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1937{
David Brownell9b766b82008-01-27 18:14:51 +01001938 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
David Brownell9b766b82008-01-27 18:14:51 +01001940 cmd_arg.cmd = cmd;
1941 cmd_arg.arg = arg;
1942 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
David Brownellc0564602007-05-01 23:26:31 +02001944EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001946#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1947static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1948 void *arg)
1949{
1950 struct of_reconfig_data *rd = arg;
1951 struct i2c_adapter *adap;
1952 struct i2c_client *client;
1953
1954 switch (of_reconfig_get_state_change(action, rd)) {
1955 case OF_RECONFIG_CHANGE_ADD:
1956 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1957 if (adap == NULL)
1958 return NOTIFY_OK; /* not for us */
1959
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001960 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
1961 put_device(&adap->dev);
1962 return NOTIFY_OK;
1963 }
1964
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001965 client = of_i2c_register_device(adap, rd->dn);
1966 put_device(&adap->dev);
1967
1968 if (IS_ERR(client)) {
1969 pr_err("%s: failed to create for '%s'\n",
1970 __func__, rd->dn->full_name);
1971 return notifier_from_errno(PTR_ERR(client));
1972 }
1973 break;
1974 case OF_RECONFIG_CHANGE_REMOVE:
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +02001975 /* already depopulated? */
1976 if (!of_node_check_flag(rd->dn, OF_POPULATED))
1977 return NOTIFY_OK;
1978
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001979 /* find our device by node */
1980 client = of_find_i2c_device_by_node(rd->dn);
1981 if (client == NULL)
1982 return NOTIFY_OK; /* no? not meant for us */
1983
1984 /* unregister takes one ref away */
1985 i2c_unregister_device(client);
1986
1987 /* and put the reference of the find */
1988 put_device(&client->dev);
1989 break;
1990 }
1991
1992 return NOTIFY_OK;
1993}
1994static struct notifier_block i2c_of_notifier = {
1995 .notifier_call = of_i2c_notify,
1996};
1997#else
1998extern struct notifier_block i2c_of_notifier;
1999#endif /* CONFIG_OF_DYNAMIC */
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001static int __init i2c_init(void)
2002{
2003 int retval;
2004
Wolfram Sang03bde7c2015-03-12 17:17:59 +01002005 retval = of_alias_get_highest_id("i2c");
2006
2007 down_write(&__i2c_board_lock);
2008 if (retval >= __i2c_first_dynamic_bus_num)
2009 __i2c_first_dynamic_bus_num = retval + 1;
2010 up_write(&__i2c_board_lock);
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 retval = bus_register(&i2c_bus_type);
2013 if (retval)
2014 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02002015#ifdef CONFIG_I2C_COMPAT
2016 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
2017 if (!i2c_adapter_compat_class) {
2018 retval = -ENOMEM;
2019 goto bus_err;
2020 }
2021#endif
David Brownelle9f13732008-01-27 18:14:52 +01002022 retval = i2c_add_driver(&dummy_driver);
2023 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02002024 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002025
2026 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2027 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
2028
David Brownelle9f13732008-01-27 18:14:52 +01002029 return 0;
2030
Jean Delvare2bb50952009-09-18 22:45:46 +02002031class_err:
2032#ifdef CONFIG_I2C_COMPAT
2033 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01002034bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02002035#endif
David Brownelle9f13732008-01-27 18:14:52 +01002036 bus_unregister(&i2c_bus_type);
2037 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040static void __exit i2c_exit(void)
2041{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02002042 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2043 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01002044 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02002045#ifdef CONFIG_I2C_COMPAT
2046 class_compat_unregister(i2c_adapter_compat_class);
2047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00002049 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
David Brownella10f9e72008-10-14 17:30:06 +02002052/* We must initialize early, because some subsystems register i2c drivers
2053 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2054 */
2055postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056module_exit(i2c_exit);
2057
2058/* ----------------------------------------------------
2059 * the functional interface to the i2c busses.
2060 * ----------------------------------------------------
2061 */
2062
Wolfram Sangb7f62582015-01-05 23:45:59 +01002063/* Check if val is exceeding the quirk IFF quirk is non 0 */
2064#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2065
2066static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
2067{
2068 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2069 err_msg, msg->addr, msg->len,
2070 msg->flags & I2C_M_RD ? "read" : "write");
2071 return -EOPNOTSUPP;
2072}
2073
2074static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2075{
2076 const struct i2c_adapter_quirks *q = adap->quirks;
2077 int max_num = q->max_num_msgs, i;
2078 bool do_len_check = true;
2079
2080 if (q->flags & I2C_AQ_COMB) {
2081 max_num = 2;
2082
2083 /* special checks for combined messages */
2084 if (num == 2) {
2085 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2086 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2087
2088 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2089 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2090
2091 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2092 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2093
2094 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2095 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2096
2097 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2098 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2099
2100 do_len_check = false;
2101 }
2102 }
2103
2104 if (i2c_quirk_exceeded(num, max_num))
2105 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2106
2107 for (i = 0; i < num; i++) {
2108 u16 len = msgs[i].len;
2109
2110 if (msgs[i].flags & I2C_M_RD) {
2111 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2112 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2113 } else {
2114 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2115 return i2c_quirk_error(adap, &msgs[i], "msg too long");
2116 }
2117 }
2118
2119 return 0;
2120}
2121
David Brownella1cdeda2008-07-14 22:38:24 +02002122/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002123 * __i2c_transfer - unlocked flavor of i2c_transfer
2124 * @adap: Handle to I2C bus
2125 * @msgs: One or more messages to execute before STOP is issued to
2126 * terminate the operation; each message begins with a START.
2127 * @num: Number of messages to be executed.
2128 *
2129 * Returns negative errno, else the number of messages executed.
2130 *
2131 * Adapter lock must be held when calling this function. No debug logging
2132 * takes place. adap->algo->master_xfer existence isn't checked.
2133 */
2134int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2135{
2136 unsigned long orig_jiffies;
2137 int ret, try;
2138
Wolfram Sangb7f62582015-01-05 23:45:59 +01002139 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2140 return -EOPNOTSUPP;
2141
David Howellsd9a83d62014-03-06 13:35:59 +00002142 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2143 * enabled. This is an efficient way of keeping the for-loop from
2144 * being executed when not needed.
2145 */
2146 if (static_key_false(&i2c_trace_msg)) {
2147 int i;
2148 for (i = 0; i < num; i++)
2149 if (msgs[i].flags & I2C_M_RD)
2150 trace_i2c_read(adap, &msgs[i], i);
2151 else
2152 trace_i2c_write(adap, &msgs[i], i);
2153 }
2154
Jean Delvareb37d2a32012-06-29 07:47:19 -03002155 /* Retry automatically on arbitration loss */
2156 orig_jiffies = jiffies;
2157 for (ret = 0, try = 0; try <= adap->retries; try++) {
2158 ret = adap->algo->master_xfer(adap, msgs, num);
2159 if (ret != -EAGAIN)
2160 break;
2161 if (time_after(jiffies, orig_jiffies + adap->timeout))
2162 break;
2163 }
2164
David Howellsd9a83d62014-03-06 13:35:59 +00002165 if (static_key_false(&i2c_trace_msg)) {
2166 int i;
2167 for (i = 0; i < ret; i++)
2168 if (msgs[i].flags & I2C_M_RD)
2169 trace_i2c_reply(adap, &msgs[i], i);
2170 trace_i2c_result(adap, i, ret);
2171 }
2172
Jean Delvareb37d2a32012-06-29 07:47:19 -03002173 return ret;
2174}
2175EXPORT_SYMBOL(__i2c_transfer);
2176
2177/**
David Brownella1cdeda2008-07-14 22:38:24 +02002178 * i2c_transfer - execute a single or combined I2C message
2179 * @adap: Handle to I2C bus
2180 * @msgs: One or more messages to execute before STOP is issued to
2181 * terminate the operation; each message begins with a START.
2182 * @num: Number of messages to be executed.
2183 *
2184 * Returns negative errno, else the number of messages executed.
2185 *
2186 * Note that there is no requirement that each message be sent to
2187 * the same slave address, although that is the most common model.
2188 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002189int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002191 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
David Brownella1cdeda2008-07-14 22:38:24 +02002193 /* REVISIT the fault reporting model here is weak:
2194 *
2195 * - When we get an error after receiving N bytes from a slave,
2196 * there is no way to report "N".
2197 *
2198 * - When we get a NAK after transmitting N bytes to a slave,
2199 * there is no way to report "N" ... or to let the master
2200 * continue executing the rest of this combined message, if
2201 * that's the appropriate response.
2202 *
2203 * - When for example "num" is two and we successfully complete
2204 * the first message but get an error part way through the
2205 * second, it's unclear whether that should be reported as
2206 * one (discarding status on the second message) or errno
2207 * (discarding status on the first one).
2208 */
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (adap->algo->master_xfer) {
2211#ifdef DEBUG
2212 for (ret = 0; ret < num; ret++) {
2213 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002214 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2215 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2216 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218#endif
2219
Mike Rapoportcea443a2008-01-27 18:14:50 +01002220 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002221 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002222 if (!ret)
2223 /* I2C activity is ongoing. */
2224 return -EAGAIN;
2225 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002226 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002227 }
2228
Jean Delvareb37d2a32012-06-29 07:47:19 -03002229 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002230 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 return ret;
2233 } else {
2234 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002235 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237}
David Brownellc0564602007-05-01 23:26:31 +02002238EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
David Brownella1cdeda2008-07-14 22:38:24 +02002240/**
2241 * i2c_master_send - issue a single I2C message in master transmit mode
2242 * @client: Handle to slave device
2243 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002244 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002245 *
2246 * Returns negative errno, or else the number of bytes written.
2247 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002248int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002251 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct i2c_msg msg;
2253
Jean Delvare815f55f2005-05-07 22:58:46 +02002254 msg.addr = client->addr;
2255 msg.flags = client->flags & I2C_M_TEN;
2256 msg.len = count;
2257 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002258
Jean Delvare815f55f2005-05-07 22:58:46 +02002259 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002261 /*
2262 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2263 * transmitted, else error code.
2264 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002265 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
David Brownellc0564602007-05-01 23:26:31 +02002267EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
David Brownella1cdeda2008-07-14 22:38:24 +02002269/**
2270 * i2c_master_recv - issue a single I2C message in master receive mode
2271 * @client: Handle to slave device
2272 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002273 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002274 *
2275 * Returns negative errno, or else the number of bytes read.
2276 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002277int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
Farid Hammane7225acf2010-05-21 18:40:58 +02002279 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct i2c_msg msg;
2281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Jean Delvare815f55f2005-05-07 22:58:46 +02002283 msg.addr = client->addr;
2284 msg.flags = client->flags & I2C_M_TEN;
2285 msg.flags |= I2C_M_RD;
2286 msg.len = count;
2287 msg.buf = buf;
2288
2289 ret = i2c_transfer(adap, &msg, 1);
2290
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002291 /*
2292 * If everything went ok (i.e. 1 msg received), return #bytes received,
2293 * else error code.
2294 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002295 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
David Brownellc0564602007-05-01 23:26:31 +02002297EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299/* ----------------------------------------------------
2300 * the i2c address scanning function
2301 * Will not work for 10-bit addresses!
2302 * ----------------------------------------------------
2303 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002304
Jean Delvare63e4e802010-06-03 11:33:51 +02002305/*
2306 * Legacy default probe function, mostly relevant for SMBus. The default
2307 * probe method is a quick write, but it is known to corrupt the 24RF08
2308 * EEPROMs due to a state machine bug, and could also irreversibly
2309 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2310 * we use a short byte read instead. Also, some bus drivers don't implement
2311 * quick write, so we fallback to a byte read in that case too.
2312 * On x86, there is another special case for FSC hardware monitoring chips,
2313 * which want regular byte reads (address 0x73.) Fortunately, these are the
2314 * only known chips using this I2C address on PC hardware.
2315 * Returns 1 if probe succeeded, 0 if not.
2316 */
2317static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2318{
2319 int err;
2320 union i2c_smbus_data dummy;
2321
2322#ifdef CONFIG_X86
2323 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2324 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2325 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2326 I2C_SMBUS_BYTE_DATA, &dummy);
2327 else
2328#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002329 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2330 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002331 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2332 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002333 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2334 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2335 I2C_SMBUS_BYTE, &dummy);
2336 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002337 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2338 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002339 err = -EOPNOTSUPP;
2340 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002341
2342 return err >= 0;
2343}
2344
Jean Delvareccfbbd02009-12-06 17:06:25 +01002345static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002346 struct i2c_driver *driver)
2347{
2348 struct i2c_board_info info;
2349 struct i2c_adapter *adapter = temp_client->adapter;
2350 int addr = temp_client->addr;
2351 int err;
2352
2353 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002354 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002355 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002356 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2357 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002358 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002359 }
2360
Wolfram Sang9bccc702015-07-17 14:48:56 +02002361 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002362 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002363 return 0;
2364
Jean Delvareccfbbd02009-12-06 17:06:25 +01002365 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002366 if (!i2c_default_probe(adapter, addr))
2367 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002368
2369 /* Finally call the custom detection function */
2370 memset(&info, 0, sizeof(struct i2c_board_info));
2371 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002372 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002373 if (err) {
2374 /* -ENODEV is returned if the detection fails. We catch it
2375 here as this isn't an error. */
2376 return err == -ENODEV ? 0 : err;
2377 }
2378
2379 /* Consistency check */
2380 if (info.type[0] == '\0') {
2381 dev_err(&adapter->dev, "%s detection function provided "
2382 "no name for 0x%x\n", driver->driver.name,
2383 addr);
2384 } else {
2385 struct i2c_client *client;
2386
2387 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002388 if (adapter->class & I2C_CLASS_DEPRECATED)
2389 dev_warn(&adapter->dev,
2390 "This adapter will soon drop class based instantiation of devices. "
2391 "Please make sure client 0x%02x gets instantiated by other means. "
2392 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2393 info.addr);
2394
Jean Delvare4735c982008-07-14 22:38:36 +02002395 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2396 info.type, info.addr);
2397 client = i2c_new_device(adapter, &info);
2398 if (client)
2399 list_add_tail(&client->detected, &driver->clients);
2400 else
2401 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2402 info.type, info.addr);
2403 }
2404 return 0;
2405}
2406
2407static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2408{
Jean Delvarec3813d62009-12-14 21:17:25 +01002409 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002410 struct i2c_client *temp_client;
2411 int i, err = 0;
2412 int adap_id = i2c_adapter_id(adapter);
2413
Jean Delvarec3813d62009-12-14 21:17:25 +01002414 address_list = driver->address_list;
2415 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002416 return 0;
2417
Wolfram Sang45552272014-07-10 13:46:21 +02002418 /* Warn that the adapter lost class based instantiation */
2419 if (adapter->class == I2C_CLASS_DEPRECATED) {
2420 dev_dbg(&adapter->dev,
2421 "This adapter dropped support for I2C classes and "
2422 "won't auto-detect %s devices anymore. If you need it, check "
2423 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2424 driver->driver.name);
2425 return 0;
2426 }
2427
Jean Delvare51b54ba2010-10-24 18:16:58 +02002428 /* Stop here if the classes do not match */
2429 if (!(adapter->class & driver->class))
2430 return 0;
2431
Jean Delvare4735c982008-07-14 22:38:36 +02002432 /* Set up a temporary client to help detect callback */
2433 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2434 if (!temp_client)
2435 return -ENOMEM;
2436 temp_client->adapter = adapter;
2437
Jean Delvarec3813d62009-12-14 21:17:25 +01002438 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002439 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002440 "addr 0x%02x\n", adap_id, address_list[i]);
2441 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002442 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002443 if (unlikely(err))
2444 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002445 }
2446
Jean Delvare4735c982008-07-14 22:38:36 +02002447 kfree(temp_client);
2448 return err;
2449}
2450
Jean Delvared44f19d2010-08-11 18:20:57 +02002451int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2452{
2453 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2454 I2C_SMBUS_QUICK, NULL) >= 0;
2455}
2456EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2457
Jean Delvare12b5053a2007-05-01 23:26:31 +02002458struct i2c_client *
2459i2c_new_probed_device(struct i2c_adapter *adap,
2460 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002461 unsigned short const *addr_list,
2462 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002463{
2464 int i;
2465
Jean Delvare8031d792010-08-11 18:21:00 +02002466 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002467 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002468
Jean Delvare12b5053a2007-05-01 23:26:31 +02002469 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2470 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002471 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002472 dev_warn(&adap->dev, "Invalid 7-bit address "
2473 "0x%02x\n", addr_list[i]);
2474 continue;
2475 }
2476
Wolfram Sang9bccc702015-07-17 14:48:56 +02002477 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002478 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002479 dev_dbg(&adap->dev, "Address 0x%02x already in "
2480 "use, not probing\n", addr_list[i]);
2481 continue;
2482 }
2483
Jean Delvare63e4e802010-06-03 11:33:51 +02002484 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002485 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002486 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002487 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002488
2489 if (addr_list[i] == I2C_CLIENT_END) {
2490 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2491 return NULL;
2492 }
2493
2494 info->addr = addr_list[i];
2495 return i2c_new_device(adap, info);
2496}
2497EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2498
Jean Delvared735b342011-03-20 14:50:52 +01002499struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002502
Jean Delvarecaada322008-01-27 18:14:49 +01002503 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002504 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002505 if (!adapter)
2506 goto exit;
2507
2508 if (try_module_get(adapter->owner))
2509 get_device(&adapter->dev);
2510 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002511 adapter = NULL;
2512
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002513 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002514 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002515 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
David Brownellc0564602007-05-01 23:26:31 +02002517EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519void i2c_put_adapter(struct i2c_adapter *adap)
2520{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002521 if (!adap)
2522 return;
2523
2524 put_device(&adap->dev);
2525 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
David Brownellc0564602007-05-01 23:26:31 +02002527EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529/* The SMBus parts */
2530
David Brownell438d6c22006-12-10 21:21:31 +01002531#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002532static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002535
Farid Hammane7225acf2010-05-21 18:40:58 +02002536 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002537 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 data = data ^ POLY;
2539 data = data << 1;
2540 }
2541 return (u8)(data >> 8);
2542}
2543
Jean Delvare421ef472005-10-26 21:28:55 +02002544/* Incremental CRC8 over count bytes in the array pointed to by p */
2545static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 int i;
2548
Farid Hammane7225acf2010-05-21 18:40:58 +02002549 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002550 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return crc;
2552}
2553
Jean Delvare421ef472005-10-26 21:28:55 +02002554/* Assume a 7-bit address, which is reasonable for SMBus */
2555static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Jean Delvare421ef472005-10-26 21:28:55 +02002557 /* The address will be sent first */
2558 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2559 pec = i2c_smbus_pec(pec, &addr, 1);
2560
2561 /* The data buffer follows */
2562 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Jean Delvare421ef472005-10-26 21:28:55 +02002565/* Used for write only transactions */
2566static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Jean Delvare421ef472005-10-26 21:28:55 +02002568 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2569 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Jean Delvare421ef472005-10-26 21:28:55 +02002572/* Return <0 on CRC error
2573 If there was a write before this read (most cases) we need to take the
2574 partial CRC from the write part into account.
2575 Note that this function does modify the message (we need to decrease the
2576 message length to hide the CRC byte from the caller). */
2577static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Jean Delvare421ef472005-10-26 21:28:55 +02002579 u8 rpec = msg->buf[--msg->len];
2580 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (rpec != cpec) {
2583 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2584 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002585 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
David Brownell438d6c22006-12-10 21:21:31 +01002587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
David Brownella1cdeda2008-07-14 22:38:24 +02002590/**
2591 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2592 * @client: Handle to slave device
2593 *
2594 * This executes the SMBus "receive byte" protocol, returning negative errno
2595 * else the byte received from the device.
2596 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002597s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002600 int status;
2601
2602 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2603 I2C_SMBUS_READ, 0,
2604 I2C_SMBUS_BYTE, &data);
2605 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
David Brownellc0564602007-05-01 23:26:31 +02002607EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
David Brownella1cdeda2008-07-14 22:38:24 +02002609/**
2610 * i2c_smbus_write_byte - SMBus "send byte" protocol
2611 * @client: Handle to slave device
2612 * @value: Byte to be sent
2613 *
2614 * This executes the SMBus "send byte" protocol, returning negative errno
2615 * else zero on success.
2616 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002617s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Farid Hammane7225acf2010-05-21 18:40:58 +02002619 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002620 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
David Brownellc0564602007-05-01 23:26:31 +02002622EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
David Brownella1cdeda2008-07-14 22:38:24 +02002624/**
2625 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2626 * @client: Handle to slave device
2627 * @command: Byte interpreted by slave
2628 *
2629 * This executes the SMBus "read byte" protocol, returning negative errno
2630 * else a data byte received from the device.
2631 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002632s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002635 int status;
2636
2637 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2638 I2C_SMBUS_READ, command,
2639 I2C_SMBUS_BYTE_DATA, &data);
2640 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
David Brownellc0564602007-05-01 23:26:31 +02002642EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
David Brownella1cdeda2008-07-14 22:38:24 +02002644/**
2645 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2646 * @client: Handle to slave device
2647 * @command: Byte interpreted by slave
2648 * @value: Byte being written
2649 *
2650 * This executes the SMBus "write byte" protocol, returning negative errno
2651 * else zero on success.
2652 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002653s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2654 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 union i2c_smbus_data data;
2657 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002658 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2659 I2C_SMBUS_WRITE, command,
2660 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
David Brownellc0564602007-05-01 23:26:31 +02002662EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
David Brownella1cdeda2008-07-14 22:38:24 +02002664/**
2665 * i2c_smbus_read_word_data - SMBus "read word" protocol
2666 * @client: Handle to slave device
2667 * @command: Byte interpreted by slave
2668 *
2669 * This executes the SMBus "read word" protocol, returning negative errno
2670 * else a 16-bit unsigned "word" received from the device.
2671 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002672s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002675 int status;
2676
2677 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2678 I2C_SMBUS_READ, command,
2679 I2C_SMBUS_WORD_DATA, &data);
2680 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
David Brownellc0564602007-05-01 23:26:31 +02002682EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
David Brownella1cdeda2008-07-14 22:38:24 +02002684/**
2685 * i2c_smbus_write_word_data - SMBus "write word" protocol
2686 * @client: Handle to slave device
2687 * @command: Byte interpreted by slave
2688 * @value: 16-bit "word" being written
2689 *
2690 * This executes the SMBus "write word" protocol, returning negative errno
2691 * else zero on success.
2692 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002693s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2694 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 union i2c_smbus_data data;
2697 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002698 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2699 I2C_SMBUS_WRITE, command,
2700 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
David Brownellc0564602007-05-01 23:26:31 +02002702EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
David Brownella64ec072007-10-13 23:56:31 +02002704/**
David Brownella1cdeda2008-07-14 22:38:24 +02002705 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002706 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002707 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002708 * @values: Byte array into which data will be read; big enough to hold
2709 * the data returned by the slave. SMBus allows at most 32 bytes.
2710 *
David Brownella1cdeda2008-07-14 22:38:24 +02002711 * This executes the SMBus "block read" protocol, returning negative errno
2712 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002713 *
2714 * Note that using this function requires that the client's adapter support
2715 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2716 * support this; its emulation through I2C messaging relies on a specific
2717 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2718 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002719s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002720 u8 *values)
2721{
2722 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002723 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002724
David Brownell24a5bb72008-07-14 22:38:23 +02002725 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2726 I2C_SMBUS_READ, command,
2727 I2C_SMBUS_BLOCK_DATA, &data);
2728 if (status)
2729 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002730
2731 memcpy(values, &data.block[1], data.block[0]);
2732 return data.block[0];
2733}
2734EXPORT_SYMBOL(i2c_smbus_read_block_data);
2735
David Brownella1cdeda2008-07-14 22:38:24 +02002736/**
2737 * i2c_smbus_write_block_data - SMBus "block write" protocol
2738 * @client: Handle to slave device
2739 * @command: Byte interpreted by slave
2740 * @length: Size of data block; SMBus allows at most 32 bytes
2741 * @values: Byte array which will be written.
2742 *
2743 * This executes the SMBus "block write" protocol, returning negative errno
2744 * else zero on success.
2745 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002746s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002747 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (length > I2C_SMBUS_BLOCK_MAX)
2752 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002754 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002755 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2756 I2C_SMBUS_WRITE, command,
2757 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
David Brownellc0564602007-05-01 23:26:31 +02002759EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002762s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002763 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002766 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002767
Jean Delvare4b2643d2007-07-12 14:12:29 +02002768 if (length > I2C_SMBUS_BLOCK_MAX)
2769 length = I2C_SMBUS_BLOCK_MAX;
2770 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002771 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2772 I2C_SMBUS_READ, command,
2773 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2774 if (status < 0)
2775 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002776
2777 memcpy(values, &data.block[1], data.block[0]);
2778 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
David Brownellc0564602007-05-01 23:26:31 +02002780EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Jean Delvare0cc43a12011-01-10 22:11:23 +01002782s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002783 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002784{
2785 union i2c_smbus_data data;
2786
2787 if (length > I2C_SMBUS_BLOCK_MAX)
2788 length = I2C_SMBUS_BLOCK_MAX;
2789 data.block[0] = length;
2790 memcpy(data.block + 1, values, length);
2791 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2792 I2C_SMBUS_WRITE, command,
2793 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2794}
David Brownellc0564602007-05-01 23:26:31 +02002795EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002796
David Brownell438d6c22006-12-10 21:21:31 +01002797/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002799static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2800 unsigned short flags,
2801 char read_write, u8 command, int size,
2802 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
2804 /* So we need to generate a series of msgs. In the case of writing, we
2805 need to use only one message; when reading, we need two. We initialize
2806 most things with sane defaults, to keep the code below somewhat
2807 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002808 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2809 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002810 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002812 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002813 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002814 struct i2c_msg msg[2] = {
2815 {
2816 .addr = addr,
2817 .flags = flags,
2818 .len = 1,
2819 .buf = msgbuf0,
2820 }, {
2821 .addr = addr,
2822 .flags = flags | I2C_M_RD,
2823 .len = 0,
2824 .buf = msgbuf1,
2825 },
2826 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002829 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 case I2C_SMBUS_QUICK:
2831 msg[0].len = 0;
2832 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002833 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2834 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 num = 1;
2836 break;
2837 case I2C_SMBUS_BYTE:
2838 if (read_write == I2C_SMBUS_READ) {
2839 /* Special case: only a read! */
2840 msg[0].flags = I2C_M_RD | flags;
2841 num = 1;
2842 }
2843 break;
2844 case I2C_SMBUS_BYTE_DATA:
2845 if (read_write == I2C_SMBUS_READ)
2846 msg[1].len = 1;
2847 else {
2848 msg[0].len = 2;
2849 msgbuf0[1] = data->byte;
2850 }
2851 break;
2852 case I2C_SMBUS_WORD_DATA:
2853 if (read_write == I2C_SMBUS_READ)
2854 msg[1].len = 2;
2855 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002856 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002858 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
2860 break;
2861 case I2C_SMBUS_PROC_CALL:
2862 num = 2; /* Special case */
2863 read_write = I2C_SMBUS_READ;
2864 msg[0].len = 3;
2865 msg[1].len = 2;
2866 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002867 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 break;
2869 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002871 msg[1].flags |= I2C_M_RECV_LEN;
2872 msg[1].len = 1; /* block length will be added by
2873 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 } else {
2875 msg[0].len = data->block[0] + 2;
2876 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002877 dev_err(&adapter->dev,
2878 "Invalid block write size %d\n",
2879 data->block[0]);
2880 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002882 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 msgbuf0[i] = data->block[i-1];
2884 }
2885 break;
2886 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002887 num = 2; /* Another special case */
2888 read_write = I2C_SMBUS_READ;
2889 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002890 dev_err(&adapter->dev,
2891 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002892 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002893 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002894 }
2895 msg[0].len = data->block[0] + 2;
2896 for (i = 1; i < msg[0].len; i++)
2897 msgbuf0[i] = data->block[i-1];
2898 msg[1].flags |= I2C_M_RECV_LEN;
2899 msg[1].len = 1; /* block length will be added by
2900 the underlying bus driver */
2901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 case I2C_SMBUS_I2C_BLOCK_DATA:
2903 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002904 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 } else {
2906 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002907 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002908 dev_err(&adapter->dev,
2909 "Invalid block write size %d\n",
2910 data->block[0]);
2911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913 for (i = 1; i <= data->block[0]; i++)
2914 msgbuf0[i] = data->block[i];
2915 }
2916 break;
2917 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002918 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2919 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921
Jean Delvare421ef472005-10-26 21:28:55 +02002922 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2923 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2924 if (i) {
2925 /* Compute PEC if first message is a write */
2926 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002927 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002928 i2c_smbus_add_pec(&msg[0]);
2929 else /* Write followed by read */
2930 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2931 }
2932 /* Ask for PEC if last message is a read */
2933 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002934 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002935 }
2936
David Brownell24a5bb72008-07-14 22:38:23 +02002937 status = i2c_transfer(adapter, msg, num);
2938 if (status < 0)
2939 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Jean Delvare421ef472005-10-26 21:28:55 +02002941 /* Check PEC if last message is a read */
2942 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002943 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2944 if (status < 0)
2945 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002946 }
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002949 switch (size) {
2950 case I2C_SMBUS_BYTE:
2951 data->byte = msgbuf0[0];
2952 break;
2953 case I2C_SMBUS_BYTE_DATA:
2954 data->byte = msgbuf1[0];
2955 break;
2956 case I2C_SMBUS_WORD_DATA:
2957 case I2C_SMBUS_PROC_CALL:
2958 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2959 break;
2960 case I2C_SMBUS_I2C_BLOCK_DATA:
2961 for (i = 0; i < data->block[0]; i++)
2962 data->block[i+1] = msgbuf1[i];
2963 break;
2964 case I2C_SMBUS_BLOCK_DATA:
2965 case I2C_SMBUS_BLOCK_PROC_CALL:
2966 for (i = 0; i < msgbuf1[0] + 1; i++)
2967 data->block[i] = msgbuf1[i];
2968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 return 0;
2971}
2972
David Brownella1cdeda2008-07-14 22:38:24 +02002973/**
2974 * i2c_smbus_xfer - execute SMBus protocol operations
2975 * @adapter: Handle to I2C bus
2976 * @addr: Address of SMBus slave on that bus
2977 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2978 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2979 * @command: Byte interpreted by slave, for protocols which use such bytes
2980 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2981 * @data: Data to be read or written
2982 *
2983 * This executes an SMBus protocol operation, and returns a negative
2984 * errno code else zero on success.
2985 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002986s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002987 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002988 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002990 unsigned long orig_jiffies;
2991 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
David Howells8a325992014-03-06 13:36:06 +00002994 /* If enabled, the following two tracepoints are conditional on
2995 * read_write and protocol.
2996 */
2997 trace_smbus_write(adapter, addr, flags, read_write,
2998 command, protocol, data);
2999 trace_smbus_read(adapter, addr, flags, read_write,
3000 command, protocol);
3001
Laurent Pinchartd47726c2012-07-24 14:13:59 +02003002 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02003005 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02003006
3007 /* Retry automatically on arbitration loss */
3008 orig_jiffies = jiffies;
3009 for (res = 0, try = 0; try <= adapter->retries; try++) {
3010 res = adapter->algo->smbus_xfer(adapter, addr, flags,
3011 read_write, command,
3012 protocol, data);
3013 if (res != -EAGAIN)
3014 break;
3015 if (time_after(jiffies,
3016 orig_jiffies + adapter->timeout))
3017 break;
3018 }
Jean Delvarefe61e072010-08-11 18:20:58 +02003019 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003021 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00003022 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02003023 /*
3024 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3025 * implement native support for the SMBus operation.
3026 */
3027 }
3028
David Howells8a325992014-03-06 13:36:06 +00003029 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
3030 command, protocol, data);
3031
3032trace:
3033 /* If enabled, the reply tracepoint is conditional on read_write. */
3034 trace_smbus_reply(adapter, addr, flags, read_write,
3035 command, protocol, data);
3036 trace_smbus_result(adapter, addr, flags, read_write,
3037 command, protocol, res);
3038
3039 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Irina Tirdea01eef962015-08-12 17:31:33 +03003043/**
3044 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3045 * @client: Handle to slave device
3046 * @command: Byte interpreted by slave
3047 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3048 * @values: Byte array into which data will be read; big enough to hold
3049 * the data returned by the slave. SMBus allows at most
3050 * I2C_SMBUS_BLOCK_MAX bytes.
3051 *
3052 * This executes the SMBus "block read" protocol if supported by the adapter.
3053 * If block read is not supported, it emulates it using either word or byte
3054 * read protocols depending on availability.
3055 *
3056 * The addresses of the I2C slave device that are accessed with this function
3057 * must be mapped to a linear region, so that a block read will have the same
3058 * effect as a byte read. Before using this function you must double-check
3059 * if the I2C slave does support exchanging a block transfer with a byte
3060 * transfer.
3061 */
3062s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
3063 u8 command, u8 length, u8 *values)
3064{
3065 u8 i = 0;
3066 int status;
3067
3068 if (length > I2C_SMBUS_BLOCK_MAX)
3069 length = I2C_SMBUS_BLOCK_MAX;
3070
3071 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
3072 return i2c_smbus_read_i2c_block_data(client, command, length, values);
3073
3074 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
3075 return -EOPNOTSUPP;
3076
3077 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
3078 while ((i + 2) <= length) {
3079 status = i2c_smbus_read_word_data(client, command + i);
3080 if (status < 0)
3081 return status;
3082 values[i] = status & 0xff;
3083 values[i + 1] = status >> 8;
3084 i += 2;
3085 }
3086 }
3087
3088 while (i < length) {
3089 status = i2c_smbus_read_byte_data(client, command + i);
3090 if (status < 0)
3091 return status;
3092 values[i] = status;
3093 i++;
3094 }
3095
3096 return i;
3097}
3098EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
3099
Jean Delvared5fd1202015-01-26 20:59:31 +01003100#if IS_ENABLED(CONFIG_I2C_SLAVE)
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003101int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
3102{
3103 int ret;
3104
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003105 if (!client || !slave_cb) {
3106 WARN(1, "insufficent data\n");
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003107 return -EINVAL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003108 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003109
Wolfram Sangc6909d62015-08-05 21:12:54 +02003110 if (!(client->flags & I2C_CLIENT_SLAVE))
3111 dev_warn(&client->dev, "%s: client slave flag not set. You might see address collisions\n",
3112 __func__);
3113
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003114 if (!(client->flags & I2C_CLIENT_TEN)) {
3115 /* Enforce stricter address checking */
Wolfram Sang66be60562015-07-17 12:43:22 +02003116 ret = i2c_check_7bit_addr_validity_strict(client->addr);
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003117 if (ret) {
3118 dev_err(&client->dev, "%s: invalid address\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003119 return ret;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003120 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003121 }
3122
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003123 if (!client->adapter->algo->reg_slave) {
3124 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003125 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003126 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003127
3128 client->slave_cb = slave_cb;
3129
3130 i2c_lock_adapter(client->adapter);
3131 ret = client->adapter->algo->reg_slave(client);
3132 i2c_unlock_adapter(client->adapter);
3133
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003134 if (ret) {
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003135 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003136 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
3137 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003138
3139 return ret;
3140}
3141EXPORT_SYMBOL_GPL(i2c_slave_register);
3142
3143int i2c_slave_unregister(struct i2c_client *client)
3144{
3145 int ret;
3146
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003147 if (!client->adapter->algo->unreg_slave) {
3148 dev_err(&client->dev, "%s: not supported by adapter\n", __func__);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003149 return -EOPNOTSUPP;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003150 }
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003151
3152 i2c_lock_adapter(client->adapter);
3153 ret = client->adapter->algo->unreg_slave(client);
3154 i2c_unlock_adapter(client->adapter);
3155
3156 if (ret == 0)
3157 client->slave_cb = NULL;
Wolfram Sang0c7cab92015-05-14 14:40:05 +02003158 else
3159 dev_err(&client->dev, "%s: adapter returned error %d\n", __func__, ret);
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003160
3161 return ret;
3162}
3163EXPORT_SYMBOL_GPL(i2c_slave_unregister);
Jean Delvared5fd1202015-01-26 20:59:31 +01003164#endif
Wolfram Sang4b1acc42014-11-18 17:04:53 +01003165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3167MODULE_DESCRIPTION("I2C-Bus main module");
3168MODULE_LICENSE("GPL");