blob: 7730a68fa2d0f0ddb4bd00b59d2f8a0334855377 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c60f2014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/init.h>
38#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010039#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010040#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060041#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020043#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010044#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010045#include <linux/hardirq.h>
46#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020047#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010048#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020049#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000051#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020053#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare6629dcf2010-05-04 11:09:28 +020060/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020061 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000062 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010063static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_IDR(i2c_adapter_idr);
65
Jean Delvare4f8cf822009-09-18 22:45:46 +020066static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
70
71void i2c_transfer_trace_reg(void)
72{
73 static_key_slow_inc(&i2c_trace_msg);
74}
75
76void i2c_transfer_trace_unreg(void)
77{
78 static_key_slow_dec(&i2c_trace_msg);
79}
80
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020081#if defined(CONFIG_ACPI)
82struct acpi_i2c_handler_data {
83 struct acpi_connection_info info;
84 struct i2c_adapter *adapter;
85};
86
87struct gsb_buffer {
88 u8 status;
89 u8 len;
90 union {
91 u16 wdata;
92 u8 bdata;
93 u8 data[0];
94 };
95} __packed;
96
97static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
98{
99 struct i2c_board_info *info = data;
100
101 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
102 struct acpi_resource_i2c_serialbus *sb;
103
104 sb = &ares->data.i2c_serial_bus;
Mika Westerberg393cc1ce2014-12-29 15:48:48 +0200105 if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200106 info->addr = sb->slave_address;
107 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
108 info->flags |= I2C_CLIENT_TEN;
109 }
110 } else if (info->irq < 0) {
111 struct resource r;
112
113 if (acpi_dev_resource_interrupt(ares, 0, &r))
114 info->irq = r.start;
115 }
116
117 /* Tell the ACPI core to skip this resource */
118 return 1;
119}
120
121static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
122 void *data, void **return_value)
123{
124 struct i2c_adapter *adapter = data;
125 struct list_head resource_list;
126 struct i2c_board_info info;
127 struct acpi_device *adev;
128 int ret;
129
130 if (acpi_bus_get_device(handle, &adev))
131 return AE_OK;
132 if (acpi_bus_get_status(adev) || !adev->status.present)
133 return AE_OK;
134
135 memset(&info, 0, sizeof(info));
136 info.acpi_node.companion = adev;
137 info.irq = -1;
138
139 INIT_LIST_HEAD(&resource_list);
140 ret = acpi_dev_get_resources(adev, &resource_list,
141 acpi_i2c_add_resource, &info);
142 acpi_dev_free_resource_list(&resource_list);
143
144 if (ret < 0 || !info.addr)
145 return AE_OK;
146
147 adev->power.flags.ignore_parent = true;
148 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
149 if (!i2c_new_device(adapter, &info)) {
150 adev->power.flags.ignore_parent = false;
151 dev_err(&adapter->dev,
152 "failed to add I2C device %s from ACPI\n",
153 dev_name(&adev->dev));
154 }
155
156 return AE_OK;
157}
158
159/**
160 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
161 * @adap: pointer to adapter
162 *
163 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
164 * namespace. When a device is found it will be added to the Linux device
165 * model and bound to the corresponding ACPI handle.
166 */
167static void acpi_i2c_register_devices(struct i2c_adapter *adap)
168{
169 acpi_handle handle;
170 acpi_status status;
171
172 if (!adap->dev.parent)
173 return;
174
175 handle = ACPI_HANDLE(adap->dev.parent);
176 if (!handle)
177 return;
178
179 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
180 acpi_i2c_add_device, NULL,
181 adap, NULL);
182 if (ACPI_FAILURE(status))
183 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
184}
185
186#else /* CONFIG_ACPI */
187static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
188#endif /* CONFIG_ACPI */
189
190#ifdef CONFIG_ACPI_I2C_OPREGION
191static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
192 u8 cmd, u8 *data, u8 data_len)
193{
194
195 struct i2c_msg msgs[2];
196 int ret;
197 u8 *buffer;
198
199 buffer = kzalloc(data_len, GFP_KERNEL);
200 if (!buffer)
201 return AE_NO_MEMORY;
202
203 msgs[0].addr = client->addr;
204 msgs[0].flags = client->flags;
205 msgs[0].len = 1;
206 msgs[0].buf = &cmd;
207
208 msgs[1].addr = client->addr;
209 msgs[1].flags = client->flags | I2C_M_RD;
210 msgs[1].len = data_len;
211 msgs[1].buf = buffer;
212
213 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
214 if (ret < 0)
215 dev_err(&client->adapter->dev, "i2c read failed\n");
216 else
217 memcpy(data, buffer, data_len);
218
219 kfree(buffer);
220 return ret;
221}
222
223static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
224 u8 cmd, u8 *data, u8 data_len)
225{
226
227 struct i2c_msg msgs[1];
228 u8 *buffer;
229 int ret = AE_OK;
230
231 buffer = kzalloc(data_len + 1, GFP_KERNEL);
232 if (!buffer)
233 return AE_NO_MEMORY;
234
235 buffer[0] = cmd;
236 memcpy(buffer + 1, data, data_len);
237
238 msgs[0].addr = client->addr;
239 msgs[0].flags = client->flags;
240 msgs[0].len = data_len + 1;
241 msgs[0].buf = buffer;
242
243 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
244 if (ret < 0)
245 dev_err(&client->adapter->dev, "i2c write failed\n");
246
247 kfree(buffer);
248 return ret;
249}
250
251static acpi_status
252acpi_i2c_space_handler(u32 function, acpi_physical_address command,
253 u32 bits, u64 *value64,
254 void *handler_context, void *region_context)
255{
256 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
257 struct acpi_i2c_handler_data *data = handler_context;
258 struct acpi_connection_info *info = &data->info;
259 struct acpi_resource_i2c_serialbus *sb;
260 struct i2c_adapter *adapter = data->adapter;
261 struct i2c_client client;
262 struct acpi_resource *ares;
263 u32 accessor_type = function >> 16;
264 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100265 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200266 int status;
267
268 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
269 if (ACPI_FAILURE(ret))
270 return ret;
271
272 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
273 ret = AE_BAD_PARAMETER;
274 goto err;
275 }
276
277 sb = &ares->data.i2c_serial_bus;
278 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
279 ret = AE_BAD_PARAMETER;
280 goto err;
281 }
282
283 memset(&client, 0, sizeof(client));
284 client.adapter = adapter;
285 client.addr = sb->slave_address;
286 client.flags = 0;
287
288 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
289 client.flags |= I2C_CLIENT_TEN;
290
291 switch (accessor_type) {
292 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
293 if (action == ACPI_READ) {
294 status = i2c_smbus_read_byte(&client);
295 if (status >= 0) {
296 gsb->bdata = status;
297 status = 0;
298 }
299 } else {
300 status = i2c_smbus_write_byte(&client, gsb->bdata);
301 }
302 break;
303
304 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
305 if (action == ACPI_READ) {
306 status = i2c_smbus_read_byte_data(&client, command);
307 if (status >= 0) {
308 gsb->bdata = status;
309 status = 0;
310 }
311 } else {
312 status = i2c_smbus_write_byte_data(&client, command,
313 gsb->bdata);
314 }
315 break;
316
317 case ACPI_GSB_ACCESS_ATTRIB_WORD:
318 if (action == ACPI_READ) {
319 status = i2c_smbus_read_word_data(&client, command);
320 if (status >= 0) {
321 gsb->wdata = status;
322 status = 0;
323 }
324 } else {
325 status = i2c_smbus_write_word_data(&client, command,
326 gsb->wdata);
327 }
328 break;
329
330 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
331 if (action == ACPI_READ) {
332 status = i2c_smbus_read_block_data(&client, command,
333 gsb->data);
334 if (status >= 0) {
335 gsb->len = status;
336 status = 0;
337 }
338 } else {
339 status = i2c_smbus_write_block_data(&client, command,
340 gsb->len, gsb->data);
341 }
342 break;
343
344 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
345 if (action == ACPI_READ) {
346 status = acpi_gsb_i2c_read_bytes(&client, command,
347 gsb->data, info->access_length);
348 if (status > 0)
349 status = 0;
350 } else {
351 status = acpi_gsb_i2c_write_bytes(&client, command,
352 gsb->data, info->access_length);
353 }
354 break;
355
356 default:
357 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
358 ret = AE_BAD_PARAMETER;
359 goto err;
360 }
361
362 gsb->status = status;
363
364 err:
365 ACPI_FREE(ares);
366 return ret;
367}
368
369
370static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
371{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200372 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200373 struct acpi_i2c_handler_data *data;
374 acpi_status status;
375
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200376 if (!adapter->dev.parent)
377 return -ENODEV;
378
379 handle = ACPI_HANDLE(adapter->dev.parent);
380
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200381 if (!handle)
382 return -ENODEV;
383
384 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
385 GFP_KERNEL);
386 if (!data)
387 return -ENOMEM;
388
389 data->adapter = adapter;
390 status = acpi_bus_attach_private_data(handle, (void *)data);
391 if (ACPI_FAILURE(status)) {
392 kfree(data);
393 return -ENOMEM;
394 }
395
396 status = acpi_install_address_space_handler(handle,
397 ACPI_ADR_SPACE_GSBUS,
398 &acpi_i2c_space_handler,
399 NULL,
400 data);
401 if (ACPI_FAILURE(status)) {
402 dev_err(&adapter->dev, "Error installing i2c space handler\n");
403 acpi_bus_detach_private_data(handle);
404 kfree(data);
405 return -ENOMEM;
406 }
407
Lan Tianyu40e7fcb2014-11-23 21:22:54 +0800408 acpi_walk_dep_device_list(handle);
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200409 return 0;
410}
411
412static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
413{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200414 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200415 struct acpi_i2c_handler_data *data;
416 acpi_status status;
417
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200418 if (!adapter->dev.parent)
419 return;
420
421 handle = ACPI_HANDLE(adapter->dev.parent);
422
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200423 if (!handle)
424 return;
425
426 acpi_remove_address_space_handler(handle,
427 ACPI_ADR_SPACE_GSBUS,
428 &acpi_i2c_space_handler);
429
430 status = acpi_bus_get_private_data(handle, (void **)&data);
431 if (ACPI_SUCCESS(status))
432 kfree(data);
433
434 acpi_bus_detach_private_data(handle);
435}
436#else /* CONFIG_ACPI_I2C_OPREGION */
437static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
438{ }
439
440static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
441{ return 0; }
442#endif /* CONFIG_ACPI_I2C_OPREGION */
443
David Brownellf37dd802007-02-13 22:09:00 +0100444/* ------------------------------------------------------------------------- */
445
Jean Delvared2653e92008-04-29 23:11:39 +0200446static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
447 const struct i2c_client *client)
448{
449 while (id->name[0]) {
450 if (strcmp(client->name, id->name) == 0)
451 return id;
452 id++;
453 }
454 return NULL;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static int i2c_device_match(struct device *dev, struct device_driver *drv)
458{
Jean Delvare51298d12009-09-18 22:45:45 +0200459 struct i2c_client *client = i2c_verify_client(dev);
460 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200461
Jean Delvare51298d12009-09-18 22:45:45 +0200462 if (!client)
463 return 0;
464
Grant Likely959e85f2010-06-08 07:48:19 -0600465 /* Attempt an OF style match */
466 if (of_driver_match_device(dev, drv))
467 return 1;
468
Mika Westerberg907ddf82012-11-23 12:23:40 +0100469 /* Then ACPI style match */
470 if (acpi_driver_match_device(dev, drv))
471 return 1;
472
Jean Delvare51298d12009-09-18 22:45:45 +0200473 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200474 /* match on an id table if there is one */
475 if (driver->id_table)
476 return i2c_match_id(driver->id_table, client) != NULL;
477
Jean Delvareeb8a7902008-05-18 20:49:41 +0200478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David Brownell7b4fbc52007-05-01 23:26:30 +0200481
482/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200483static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200484{
485 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800486 int rc;
487
488 rc = acpi_device_uevent_modalias(dev, env);
489 if (rc != -ENODEV)
490 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200491
Jean Delvareeb8a7902008-05-18 20:49:41 +0200492 if (add_uevent_var(env, "MODALIAS=%s%s",
493 I2C_MODULE_PREFIX, client->name))
494 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200495 dev_dbg(dev, "uevent\n");
496 return 0;
497}
498
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530499/* i2c bus recovery routines */
500static int get_scl_gpio_value(struct i2c_adapter *adap)
501{
502 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
503}
504
505static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
506{
507 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
508}
509
510static int get_sda_gpio_value(struct i2c_adapter *adap)
511{
512 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
513}
514
515static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
516{
517 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
518 struct device *dev = &adap->dev;
519 int ret = 0;
520
521 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
522 GPIOF_OUT_INIT_HIGH, "i2c-scl");
523 if (ret) {
524 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
525 return ret;
526 }
527
528 if (bri->get_sda) {
529 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
530 /* work without SDA polling */
531 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
532 bri->sda_gpio);
533 bri->get_sda = NULL;
534 }
535 }
536
537 return ret;
538}
539
540static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
541{
542 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
543
544 if (bri->get_sda)
545 gpio_free(bri->sda_gpio);
546
547 gpio_free(bri->scl_gpio);
548}
549
550/*
551 * We are generating clock pulses. ndelay() determines durating of clk pulses.
552 * We will generate clock with rate 100 KHz and so duration of both clock levels
553 * is: delay in ns = (10^6 / 100) / 2
554 */
555#define RECOVERY_NDELAY 5000
556#define RECOVERY_CLK_CNT 9
557
558static int i2c_generic_recovery(struct i2c_adapter *adap)
559{
560 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
561 int i = 0, val = 1, ret = 0;
562
563 if (bri->prepare_recovery)
564 bri->prepare_recovery(bri);
565
566 /*
567 * By this time SCL is high, as we need to give 9 falling-rising edges
568 */
569 while (i++ < RECOVERY_CLK_CNT * 2) {
570 if (val) {
571 /* Break if SDA is high */
572 if (bri->get_sda && bri->get_sda(adap))
573 break;
574 /* SCL shouldn't be low here */
575 if (!bri->get_scl(adap)) {
576 dev_err(&adap->dev,
577 "SCL is stuck low, exit recovery\n");
578 ret = -EBUSY;
579 break;
580 }
581 }
582
583 val = !val;
584 bri->set_scl(adap, val);
585 ndelay(RECOVERY_NDELAY);
586 }
587
588 if (bri->unprepare_recovery)
589 bri->unprepare_recovery(bri);
590
591 return ret;
592}
593
594int i2c_generic_scl_recovery(struct i2c_adapter *adap)
595{
596 adap->bus_recovery_info->set_scl(adap, 1);
597 return i2c_generic_recovery(adap);
598}
599
600int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
601{
602 int ret;
603
604 ret = i2c_get_gpios_for_recovery(adap);
605 if (ret)
606 return ret;
607
608 ret = i2c_generic_recovery(adap);
609 i2c_put_gpios_for_recovery(adap);
610
611 return ret;
612}
613
614int i2c_recover_bus(struct i2c_adapter *adap)
615{
616 if (!adap->bus_recovery_info)
617 return -EOPNOTSUPP;
618
619 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
620 return adap->bus_recovery_info->recover_bus(adap);
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int i2c_device_probe(struct device *dev)
624{
Jean Delvare51298d12009-09-18 22:45:45 +0200625 struct i2c_client *client = i2c_verify_client(dev);
626 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100627 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200628
Jean Delvare51298d12009-09-18 22:45:45 +0200629 if (!client)
630 return 0;
631
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200632 if (!client->irq && dev->of_node) {
633 int irq = of_irq_get(dev->of_node, 0);
634
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100635 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200636 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100637 if (irq < 0)
638 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200639
640 client->irq = irq;
641 }
642
Jean Delvare51298d12009-09-18 22:45:45 +0200643 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200644 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200645 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200646
Marc Pignatee354252008-08-28 08:33:22 +0200647 if (!device_can_wakeup(&client->dev))
648 device_init_wakeup(&client->dev,
649 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200650 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200651
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200652 status = of_clk_set_defaults(dev->of_node, false);
653 if (status < 0)
654 return status;
655
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200656 status = dev_pm_domain_attach(&client->dev, true);
657 if (status != -EPROBE_DEFER) {
658 status = driver->probe(client, i2c_match_id(driver->id_table,
659 client));
660 if (status)
661 dev_pm_domain_detach(&client->dev, true);
662 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100663
Hans Verkuil50c33042008-03-12 14:15:00 +0100664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static int i2c_device_remove(struct device *dev)
668{
Jean Delvare51298d12009-09-18 22:45:45 +0200669 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200670 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100671 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200672
Jean Delvare51298d12009-09-18 22:45:45 +0200673 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200674 return 0;
675
676 driver = to_i2c_driver(dev->driver);
677 if (driver->remove) {
678 dev_dbg(dev, "remove\n");
679 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200680 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100681
Laurent Pincharte4df3a02014-10-30 15:59:37 +0200682 if (dev->of_node)
683 irq_dispose_mapping(client->irq);
684
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200685 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200686 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
David Brownellf37dd802007-02-13 22:09:00 +0100689static void i2c_device_shutdown(struct device *dev)
690{
Jean Delvare51298d12009-09-18 22:45:45 +0200691 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100692 struct i2c_driver *driver;
693
Jean Delvare51298d12009-09-18 22:45:45 +0200694 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100695 return;
696 driver = to_i2c_driver(dev->driver);
697 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200698 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100699}
700
David Brownell9c1600e2007-05-01 23:26:31 +0200701static void i2c_client_dev_release(struct device *dev)
702{
703 kfree(to_i2c_client(dev));
704}
705
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100706static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200707show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200708{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200709 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
710 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200711}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100712static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200713
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100714static ssize_t
715show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200716{
717 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800718 int len;
719
720 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
721 if (len != -ENODEV)
722 return len;
723
Jean Delvareeb8a7902008-05-18 20:49:41 +0200724 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200725}
Jean Delvare51298d12009-09-18 22:45:45 +0200726static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
727
728static struct attribute *i2c_dev_attrs[] = {
729 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200730 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200731 &dev_attr_modalias.attr,
732 NULL
733};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100734ATTRIBUTE_GROUPS(i2c_dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200735
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200736struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100737 .name = "i2c",
738 .match = i2c_device_match,
739 .probe = i2c_device_probe,
740 .remove = i2c_device_remove,
741 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000742};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200743EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000744
Jean Delvare51298d12009-09-18 22:45:45 +0200745static struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100746 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200747 .uevent = i2c_device_uevent,
748 .release = i2c_client_dev_release,
749};
750
David Brownell9b766b82008-01-27 18:14:51 +0100751
752/**
753 * i2c_verify_client - return parameter as i2c_client, or NULL
754 * @dev: device, probably from some driver model iterator
755 *
756 * When traversing the driver model tree, perhaps using driver model
757 * iterators like @device_for_each_child(), you can't assume very much
758 * about the nodes you find. Use this function to avoid oopses caused
759 * by wrongly treating some non-I2C device as an i2c_client.
760 */
761struct i2c_client *i2c_verify_client(struct device *dev)
762{
Jean Delvare51298d12009-09-18 22:45:45 +0200763 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100764 ? to_i2c_client(dev)
765 : NULL;
766}
767EXPORT_SYMBOL(i2c_verify_client);
768
769
Jean Delvare3a89db52010-06-03 11:33:52 +0200770/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300771 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200772static int i2c_check_client_addr_validity(const struct i2c_client *client)
773{
774 if (client->flags & I2C_CLIENT_TEN) {
775 /* 10-bit address, all values are valid */
776 if (client->addr > 0x3ff)
777 return -EINVAL;
778 } else {
779 /* 7-bit address, reject the general call address */
780 if (client->addr == 0x00 || client->addr > 0x7f)
781 return -EINVAL;
782 }
783 return 0;
784}
785
Jean Delvare656b8762010-06-03 11:33:53 +0200786/* And this is a strict address validity check, used when probing. If a
787 * device uses a reserved address, then it shouldn't be probed. 7-bit
788 * addressing is assumed, 10-bit address devices are rare and should be
789 * explicitly enumerated. */
790static int i2c_check_addr_validity(unsigned short addr)
791{
792 /*
793 * Reserved addresses per I2C specification:
794 * 0x00 General call address / START byte
795 * 0x01 CBUS address
796 * 0x02 Reserved for different bus format
797 * 0x03 Reserved for future purposes
798 * 0x04-0x07 Hs-mode master code
799 * 0x78-0x7b 10-bit slave addressing
800 * 0x7c-0x7f Reserved for future purposes
801 */
802 if (addr < 0x08 || addr > 0x77)
803 return -EINVAL;
804 return 0;
805}
806
Jean Delvare3b5f7942010-06-03 11:33:55 +0200807static int __i2c_check_addr_busy(struct device *dev, void *addrp)
808{
809 struct i2c_client *client = i2c_verify_client(dev);
810 int addr = *(int *)addrp;
811
812 if (client && client->addr == addr)
813 return -EBUSY;
814 return 0;
815}
816
Michael Lawnick08263742010-08-11 18:21:02 +0200817/* walk up mux tree */
818static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
819{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200820 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200821 int result;
822
823 result = device_for_each_child(&adapter->dev, &addr,
824 __i2c_check_addr_busy);
825
Jean Delvare97cc4d42010-10-24 18:16:57 +0200826 if (!result && parent)
827 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200828
829 return result;
830}
831
832/* recurse down mux tree */
833static int i2c_check_mux_children(struct device *dev, void *addrp)
834{
835 int result;
836
837 if (dev->type == &i2c_adapter_type)
838 result = device_for_each_child(dev, addrp,
839 i2c_check_mux_children);
840 else
841 result = __i2c_check_addr_busy(dev, addrp);
842
843 return result;
844}
845
Jean Delvare3b5f7942010-06-03 11:33:55 +0200846static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
847{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200848 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200849 int result = 0;
850
Jean Delvare97cc4d42010-10-24 18:16:57 +0200851 if (parent)
852 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200853
854 if (!result)
855 result = device_for_each_child(&adapter->dev, &addr,
856 i2c_check_mux_children);
857
858 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200859}
860
David Brownell9c1600e2007-05-01 23:26:31 +0200861/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200862 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
863 * @adapter: Target I2C bus segment
864 */
865void i2c_lock_adapter(struct i2c_adapter *adapter)
866{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200867 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
868
869 if (parent)
870 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200871 else
872 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200873}
874EXPORT_SYMBOL_GPL(i2c_lock_adapter);
875
876/**
877 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
878 * @adapter: Target I2C bus segment
879 */
880static int i2c_trylock_adapter(struct i2c_adapter *adapter)
881{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200882 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
883
884 if (parent)
885 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200886 else
887 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200888}
889
890/**
891 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
892 * @adapter: Target I2C bus segment
893 */
894void i2c_unlock_adapter(struct i2c_adapter *adapter)
895{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200896 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
897
898 if (parent)
899 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200900 else
901 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200902}
903EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
904
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200905static void i2c_dev_set_name(struct i2c_adapter *adap,
906 struct i2c_client *client)
907{
908 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
909
910 if (adev) {
911 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
912 return;
913 }
914
915 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
916 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
917 client->addr | ((client->flags & I2C_CLIENT_TEN)
918 ? 0xa000 : 0));
919}
920
Jean Delvarefe61e072010-08-11 18:20:58 +0200921/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200922 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200923 * @adap: the adapter managing the device
924 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200925 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200926 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200927 * Create an i2c device. Binding is handled through driver model
928 * probe()/remove() methods. A driver may be bound to this device when we
929 * return from this function, or any later moment (e.g. maybe hotplugging will
930 * load the driver module). This call is not appropriate for use by mainboard
931 * initialization logic, which usually runs during an arch_initcall() long
932 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200933 *
934 * This returns the new i2c client, which may be saved for later use with
935 * i2c_unregister_device(); or NULL to indicate an error.
936 */
937struct i2c_client *
938i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
939{
940 struct i2c_client *client;
941 int status;
942
943 client = kzalloc(sizeof *client, GFP_KERNEL);
944 if (!client)
945 return NULL;
946
947 client->adapter = adap;
948
949 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200950
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200951 if (info->archdata)
952 client->dev.archdata = *info->archdata;
953
Marc Pignatee354252008-08-28 08:33:22 +0200954 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200955 client->addr = info->addr;
956 client->irq = info->irq;
957
David Brownell9c1600e2007-05-01 23:26:31 +0200958 strlcpy(client->name, info->type, sizeof(client->name));
959
Jean Delvare3a89db52010-06-03 11:33:52 +0200960 /* Check for address validity */
961 status = i2c_check_client_addr_validity(client);
962 if (status) {
963 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
964 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
965 goto out_err_silent;
966 }
967
Jean Delvaref8a227e2009-06-19 16:58:18 +0200968 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200969 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200970 if (status)
971 goto out_err;
972
973 client->dev.parent = &client->adapter->dev;
974 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200975 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700976 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100977 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200978
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200979 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200980 status = device_register(&client->dev);
981 if (status)
982 goto out_err;
983
Jean Delvaref8a227e2009-06-19 16:58:18 +0200984 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
985 client->name, dev_name(&client->dev));
986
David Brownell9c1600e2007-05-01 23:26:31 +0200987 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200988
989out_err:
990 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
991 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200992out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200993 kfree(client);
994 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200995}
996EXPORT_SYMBOL_GPL(i2c_new_device);
997
998
999/**
1000 * i2c_unregister_device - reverse effect of i2c_new_device()
1001 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001002 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001003 */
1004void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001005{
David Brownella1d9e6e2007-05-01 23:26:30 +02001006 device_unregister(&client->dev);
1007}
David Brownell9c1600e2007-05-01 23:26:31 +02001008EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001009
1010
Jean Delvare60b129d2008-05-11 20:37:06 +02001011static const struct i2c_device_id dummy_id[] = {
1012 { "dummy", 0 },
1013 { },
1014};
1015
Jean Delvared2653e92008-04-29 23:11:39 +02001016static int dummy_probe(struct i2c_client *client,
1017 const struct i2c_device_id *id)
1018{
1019 return 0;
1020}
1021
1022static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001023{
1024 return 0;
1025}
1026
1027static struct i2c_driver dummy_driver = {
1028 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001029 .probe = dummy_probe,
1030 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001031 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001032};
1033
1034/**
1035 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1036 * @adapter: the adapter managing the device
1037 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001038 * Context: can sleep
1039 *
1040 * This returns an I2C client bound to the "dummy" driver, intended for use
1041 * with devices that consume multiple addresses. Examples of such chips
1042 * include various EEPROMS (like 24c04 and 24c08 models).
1043 *
1044 * These dummy devices have two main uses. First, most I2C and SMBus calls
1045 * except i2c_transfer() need a client handle; the dummy will be that handle.
1046 * And second, this prevents the specified address from being bound to a
1047 * different driver.
1048 *
1049 * This returns the new i2c client, which should be saved for later use with
1050 * i2c_unregister_device(); or NULL to indicate an error.
1051 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001052struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001053{
1054 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001055 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001056 };
1057
David Brownelle9f13732008-01-27 18:14:52 +01001058 return i2c_new_device(adapter, &info);
1059}
1060EXPORT_SYMBOL_GPL(i2c_new_dummy);
1061
David Brownellf37dd802007-02-13 22:09:00 +01001062/* ------------------------------------------------------------------------- */
1063
David Brownell16ffadf2007-05-01 23:26:28 +02001064/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1065
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001066static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
David Brownellef2c83212007-05-01 23:26:28 +02001068 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 complete(&adap->dev_released);
1070}
1071
Jean Delvare99cd8e22009-06-19 16:58:20 +02001072/*
Jean Delvare390946b2012-09-10 10:14:02 +02001073 * This function is only needed for mutex_lock_nested, so it is never
1074 * called unless locking correctness checking is enabled. Thus we
1075 * make it inline to avoid a compiler warning. That's what gcc ends up
1076 * doing anyway.
1077 */
1078static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1079{
1080 unsigned int depth = 0;
1081
1082 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1083 depth++;
1084
1085 return depth;
1086}
1087
1088/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001089 * Let users instantiate I2C devices through sysfs. This can be used when
1090 * platform initialization code doesn't contain the proper data for
1091 * whatever reason. Also useful for drivers that do device detection and
1092 * detection fails, either because the device uses an unexpected address,
1093 * or this is a compatible device with different ID register values.
1094 *
1095 * Parameter checking may look overzealous, but we really don't want
1096 * the user to provide incorrect parameters.
1097 */
1098static ssize_t
1099i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1100 const char *buf, size_t count)
1101{
1102 struct i2c_adapter *adap = to_i2c_adapter(dev);
1103 struct i2c_board_info info;
1104 struct i2c_client *client;
1105 char *blank, end;
1106 int res;
1107
Jean Delvare99cd8e22009-06-19 16:58:20 +02001108 memset(&info, 0, sizeof(struct i2c_board_info));
1109
1110 blank = strchr(buf, ' ');
1111 if (!blank) {
1112 dev_err(dev, "%s: Missing parameters\n", "new_device");
1113 return -EINVAL;
1114 }
1115 if (blank - buf > I2C_NAME_SIZE - 1) {
1116 dev_err(dev, "%s: Invalid device name\n", "new_device");
1117 return -EINVAL;
1118 }
1119 memcpy(info.type, buf, blank - buf);
1120
1121 /* Parse remaining parameters, reject extra parameters */
1122 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1123 if (res < 1) {
1124 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1125 return -EINVAL;
1126 }
1127 if (res > 1 && end != '\n') {
1128 dev_err(dev, "%s: Extra parameters\n", "new_device");
1129 return -EINVAL;
1130 }
1131
Jean Delvare99cd8e22009-06-19 16:58:20 +02001132 client = i2c_new_device(adap, &info);
1133 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001134 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001135
1136 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001137 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001138 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001139 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001140 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1141 info.type, info.addr);
1142
1143 return count;
1144}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001145static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001146
1147/*
1148 * And of course let the users delete the devices they instantiated, if
1149 * they got it wrong. This interface can only be used to delete devices
1150 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1151 * don't delete devices to which some kernel code still has references.
1152 *
1153 * Parameter checking may look overzealous, but we really don't want
1154 * the user to delete the wrong device.
1155 */
1156static ssize_t
1157i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1158 const char *buf, size_t count)
1159{
1160 struct i2c_adapter *adap = to_i2c_adapter(dev);
1161 struct i2c_client *client, *next;
1162 unsigned short addr;
1163 char end;
1164 int res;
1165
1166 /* Parse parameters, reject extra parameters */
1167 res = sscanf(buf, "%hi%c", &addr, &end);
1168 if (res < 1) {
1169 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1170 return -EINVAL;
1171 }
1172 if (res > 1 && end != '\n') {
1173 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1174 return -EINVAL;
1175 }
1176
1177 /* Make sure the device was added through sysfs */
1178 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001179 mutex_lock_nested(&adap->userspace_clients_lock,
1180 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001181 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1182 detected) {
1183 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001184 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1185 "delete_device", client->name, client->addr);
1186
1187 list_del(&client->detected);
1188 i2c_unregister_device(client);
1189 res = count;
1190 break;
1191 }
1192 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001193 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001194
1195 if (res < 0)
1196 dev_err(dev, "%s: Can't find device in list\n",
1197 "delete_device");
1198 return res;
1199}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001200static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1201 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001202
1203static struct attribute *i2c_adapter_attrs[] = {
1204 &dev_attr_name.attr,
1205 &dev_attr_new_device.attr,
1206 &dev_attr_delete_device.attr,
1207 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001208};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001209ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001210
Michael Lawnick08263742010-08-11 18:21:02 +02001211struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001212 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001213 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001214};
Michael Lawnick08263742010-08-11 18:21:02 +02001215EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Stephen Warren643dd092012-04-17 12:43:33 -06001217/**
1218 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1219 * @dev: device, probably from some driver model iterator
1220 *
1221 * When traversing the driver model tree, perhaps using driver model
1222 * iterators like @device_for_each_child(), you can't assume very much
1223 * about the nodes you find. Use this function to avoid oopses caused
1224 * by wrongly treating some non-I2C device as an i2c_adapter.
1225 */
1226struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1227{
1228 return (dev->type == &i2c_adapter_type)
1229 ? to_i2c_adapter(dev)
1230 : NULL;
1231}
1232EXPORT_SYMBOL(i2c_verify_adapter);
1233
Jean Delvare2bb50952009-09-18 22:45:46 +02001234#ifdef CONFIG_I2C_COMPAT
1235static struct class_compat *i2c_adapter_compat_class;
1236#endif
1237
David Brownell9c1600e2007-05-01 23:26:31 +02001238static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1239{
1240 struct i2c_devinfo *devinfo;
1241
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001242 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001243 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1244 if (devinfo->busnum == adapter->nr
1245 && !i2c_new_device(adapter,
1246 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001247 dev_err(&adapter->dev,
1248 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001249 devinfo->board_info.addr);
1250 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001251 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001252}
1253
Wolfram Sang687b81d2013-07-11 12:56:15 +01001254/* OF support code */
1255
1256#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001257static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1258 struct device_node *node)
1259{
1260 struct i2c_client *result;
1261 struct i2c_board_info info = {};
1262 struct dev_archdata dev_ad = {};
1263 const __be32 *addr;
1264 int len;
1265
1266 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1267
1268 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1269 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1270 node->full_name);
1271 return ERR_PTR(-EINVAL);
1272 }
1273
1274 addr = of_get_property(node, "reg", &len);
1275 if (!addr || (len < sizeof(int))) {
1276 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1277 node->full_name);
1278 return ERR_PTR(-EINVAL);
1279 }
1280
1281 info.addr = be32_to_cpup(addr);
1282 if (info.addr > (1 << 10) - 1) {
1283 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1284 info.addr, node->full_name);
1285 return ERR_PTR(-EINVAL);
1286 }
1287
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001288 info.of_node = of_node_get(node);
1289 info.archdata = &dev_ad;
1290
1291 if (of_get_property(node, "wakeup-source", NULL))
1292 info.flags |= I2C_CLIENT_WAKE;
1293
1294 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1295
1296 result = i2c_new_device(adap, &info);
1297 if (result == NULL) {
1298 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1299 node->full_name);
1300 of_node_put(node);
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001301 return ERR_PTR(-EINVAL);
1302 }
1303 return result;
1304}
1305
Wolfram Sang687b81d2013-07-11 12:56:15 +01001306static void of_i2c_register_devices(struct i2c_adapter *adap)
1307{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001308 struct device_node *node;
1309
1310 /* Only register child devices if the adapter has a node pointer set */
1311 if (!adap->dev.of_node)
1312 return;
1313
1314 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1315
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001316 for_each_available_child_of_node(adap->dev.of_node, node)
1317 of_i2c_register_device(adap, node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001318}
1319
1320static int of_dev_node_match(struct device *dev, void *data)
1321{
1322 return dev->of_node == data;
1323}
1324
1325/* must call put_device() when done with returned i2c_client device */
1326struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1327{
1328 struct device *dev;
1329
1330 dev = bus_find_device(&i2c_bus_type, NULL, node,
1331 of_dev_node_match);
1332 if (!dev)
1333 return NULL;
1334
1335 return i2c_verify_client(dev);
1336}
1337EXPORT_SYMBOL(of_find_i2c_device_by_node);
1338
1339/* must call put_device() when done with returned i2c_adapter device */
1340struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1341{
1342 struct device *dev;
1343
1344 dev = bus_find_device(&i2c_bus_type, NULL, node,
1345 of_dev_node_match);
1346 if (!dev)
1347 return NULL;
1348
1349 return i2c_verify_adapter(dev);
1350}
1351EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1352#else
1353static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1354#endif /* CONFIG_OF */
1355
Jean Delvare69b00892009-12-06 17:06:27 +01001356static int i2c_do_add_adapter(struct i2c_driver *driver,
1357 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001358{
Jean Delvare4735c982008-07-14 22:38:36 +02001359 /* Detect supported devices on that bus, and instantiate them */
1360 i2c_detect(adap, driver);
1361
1362 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001363 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001364 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1365 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001366 dev_warn(&adap->dev, "Please use another way to instantiate "
1367 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001368 /* We ignore the return code; if it fails, too bad */
1369 driver->attach_adapter(adap);
1370 }
1371 return 0;
1372}
1373
Jean Delvare69b00892009-12-06 17:06:27 +01001374static int __process_new_adapter(struct device_driver *d, void *data)
1375{
1376 return i2c_do_add_adapter(to_i2c_driver(d), data);
1377}
1378
David Brownell6e13e642007-05-01 23:26:31 +02001379static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Jean Delvared6703282010-08-11 18:20:59 +02001381 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David Brownell1d0b19c2008-10-14 17:30:05 +02001383 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001384 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1385 res = -EAGAIN;
1386 goto out_list;
1387 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001388
Jean Delvare2236baa2010-11-15 22:40:38 +01001389 /* Sanity checks */
1390 if (unlikely(adap->name[0] == '\0')) {
1391 pr_err("i2c-core: Attempt to register an adapter with "
1392 "no name!\n");
1393 return -EINVAL;
1394 }
1395 if (unlikely(!adap->algo)) {
1396 pr_err("i2c-core: Attempt to register adapter '%s' with "
1397 "no algo!\n", adap->name);
1398 return -EINVAL;
1399 }
1400
Mika Kuoppala194684e2009-12-06 17:06:22 +01001401 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001402 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001403 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jean Delvare8fcfef62009-03-28 21:34:43 +01001405 /* Set default timeout to 1 second if not already set */
1406 if (adap->timeout == 0)
1407 adap->timeout = HZ;
1408
Kay Sievers27d9c182009-01-07 14:29:16 +01001409 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001410 adap->dev.bus = &i2c_bus_type;
1411 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001412 res = device_register(&adap->dev);
1413 if (res)
1414 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001416 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1417
Jean Delvare2bb50952009-09-18 22:45:46 +02001418#ifdef CONFIG_I2C_COMPAT
1419 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1420 adap->dev.parent);
1421 if (res)
1422 dev_warn(&adap->dev,
1423 "Failed to create compatibility class link\n");
1424#endif
1425
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301426 /* bus recovery specific initialization */
1427 if (adap->bus_recovery_info) {
1428 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1429
1430 if (!bri->recover_bus) {
1431 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1432 adap->bus_recovery_info = NULL;
1433 goto exit_recovery;
1434 }
1435
1436 /* Generic GPIO recovery */
1437 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1438 if (!gpio_is_valid(bri->scl_gpio)) {
1439 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1440 adap->bus_recovery_info = NULL;
1441 goto exit_recovery;
1442 }
1443
1444 if (gpio_is_valid(bri->sda_gpio))
1445 bri->get_sda = get_sda_gpio_value;
1446 else
1447 bri->get_sda = NULL;
1448
1449 bri->get_scl = get_scl_gpio_value;
1450 bri->set_scl = set_scl_gpio_value;
1451 } else if (!bri->set_scl || !bri->get_scl) {
1452 /* Generic SCL recovery */
1453 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1454 adap->bus_recovery_info = NULL;
1455 }
1456 }
1457
1458exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001459 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001460 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001461 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001462 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001463
David Brownell6e13e642007-05-01 23:26:31 +02001464 if (adap->nr < __i2c_first_dynamic_bus_num)
1465 i2c_scan_static_board_info(adap);
1466
Jean Delvare4735c982008-07-14 22:38:36 +02001467 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001468 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001469 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001470 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001471
1472 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001473
Jean Delvareb119c6c2006-08-15 18:26:30 +02001474out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001475 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001476 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001477 mutex_unlock(&core_lock);
1478 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
David Brownell6e13e642007-05-01 23:26:31 +02001481/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001482 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1483 * @adap: the adapter to register (with adap->nr initialized)
1484 * Context: can sleep
1485 *
1486 * See i2c_add_numbered_adapter() for details.
1487 */
1488static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1489{
1490 int id;
1491
1492 mutex_lock(&core_lock);
1493 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1494 GFP_KERNEL);
1495 mutex_unlock(&core_lock);
1496 if (id < 0)
1497 return id == -ENOSPC ? -EBUSY : id;
1498
1499 return i2c_register_adapter(adap);
1500}
1501
1502/**
David Brownell6e13e642007-05-01 23:26:31 +02001503 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1504 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001505 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001506 *
1507 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001508 * doesn't matter or when its bus number is specified by an dt alias.
1509 * Examples of bases when the bus number doesn't matter: I2C adapters
1510 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001511 *
1512 * When this returns zero, a new bus number was allocated and stored
1513 * in adap->nr, and the specified adapter became available for clients.
1514 * Otherwise, a negative errno value is returned.
1515 */
1516int i2c_add_adapter(struct i2c_adapter *adapter)
1517{
Doug Andersonee5c2742013-03-01 08:57:31 -08001518 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001519 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001520
Doug Andersonee5c2742013-03-01 08:57:31 -08001521 if (dev->of_node) {
1522 id = of_alias_get_id(dev->of_node, "i2c");
1523 if (id >= 0) {
1524 adapter->nr = id;
1525 return __i2c_add_numbered_adapter(adapter);
1526 }
1527 }
1528
Jean Delvarecaada322008-01-27 18:14:49 +01001529 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001530 id = idr_alloc(&i2c_adapter_idr, adapter,
1531 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001532 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001533 if (id < 0)
1534 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001535
1536 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001537
David Brownell6e13e642007-05-01 23:26:31 +02001538 return i2c_register_adapter(adapter);
1539}
1540EXPORT_SYMBOL(i2c_add_adapter);
1541
1542/**
1543 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1544 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001545 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001546 *
1547 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001548 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1549 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001550 * is used to properly configure I2C devices.
1551 *
Grant Likely488bf312011-07-25 17:49:43 +02001552 * If the requested bus number is set to -1, then this function will behave
1553 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1554 *
David Brownell6e13e642007-05-01 23:26:31 +02001555 * If no devices have pre-been declared for this bus, then be sure to
1556 * register the adapter before any dynamically allocated ones. Otherwise
1557 * the required bus ID may not be available.
1558 *
1559 * When this returns zero, the specified adapter became available for
1560 * clients using the bus number provided in adap->nr. Also, the table
1561 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1562 * and the appropriate driver model device nodes are created. Otherwise, a
1563 * negative errno value is returned.
1564 */
1565int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1566{
Grant Likely488bf312011-07-25 17:49:43 +02001567 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1568 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001569
Doug Andersonee5c2742013-03-01 08:57:31 -08001570 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001571}
1572EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1573
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001574static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001575 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001576{
Jean Delvare4735c982008-07-14 22:38:36 +02001577 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001578
Jean Delvareacec2112009-03-28 21:34:40 +01001579 /* Remove the devices we created ourselves as the result of hardware
1580 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001581 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1582 if (client->adapter == adapter) {
1583 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1584 client->name, client->addr);
1585 list_del(&client->detected);
1586 i2c_unregister_device(client);
1587 }
1588 }
Jean Delvare026526f2008-01-27 18:14:49 +01001589}
1590
Jean Delvaree549c2b2009-06-19 16:58:19 +02001591static int __unregister_client(struct device *dev, void *dummy)
1592{
1593 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001594 if (client && strcmp(client->name, "dummy"))
1595 i2c_unregister_device(client);
1596 return 0;
1597}
1598
1599static int __unregister_dummy(struct device *dev, void *dummy)
1600{
1601 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001602 if (client)
1603 i2c_unregister_device(client);
1604 return 0;
1605}
1606
Jean Delvare69b00892009-12-06 17:06:27 +01001607static int __process_removed_adapter(struct device_driver *d, void *data)
1608{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001609 i2c_do_del_adapter(to_i2c_driver(d), data);
1610 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001611}
1612
David Brownelld64f73b2007-07-12 14:12:28 +02001613/**
1614 * i2c_del_adapter - unregister I2C adapter
1615 * @adap: the adapter being unregistered
1616 * Context: can sleep
1617 *
1618 * This unregisters an I2C adapter which was previously registered
1619 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1620 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001621void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001623 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001624 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001627 mutex_lock(&core_lock);
1628 found = idr_find(&i2c_adapter_idr, adap->nr);
1629 mutex_unlock(&core_lock);
1630 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001631 pr_debug("i2c-core: attempting to delete unregistered "
1632 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635
Lan Tianyu5d98e612014-05-20 20:59:23 +08001636 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001637 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001638 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001639 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001640 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001641 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001643 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001644 mutex_lock_nested(&adap->userspace_clients_lock,
1645 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001646 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1647 detected) {
1648 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1649 client->addr);
1650 list_del(&client->detected);
1651 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001652 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001653 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001654
Jean Delvaree549c2b2009-06-19 16:58:19 +02001655 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001656 * check the returned value. This is a two-pass process, because
1657 * we can't remove the dummy devices during the first pass: they
1658 * could have been instantiated by real devices wishing to clean
1659 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001660 device_for_each_child(&adap->dev, NULL, __unregister_client);
1661 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Jean Delvare2bb50952009-09-18 22:45:46 +02001663#ifdef CONFIG_I2C_COMPAT
1664 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1665 adap->dev.parent);
1666#endif
1667
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001668 /* device name is gone after device_unregister */
1669 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* clean up the sysfs representation */
1672 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /* wait for sysfs to drop all references */
1676 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
David Brownell6e13e642007-05-01 23:26:31 +02001678 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001679 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001681 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001683 /* Clear the device structure in case this adapter is ever going to be
1684 added again */
1685 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
David Brownellc0564602007-05-01 23:26:31 +02001687EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
David Brownell7b4fbc52007-05-01 23:26:30 +02001689/* ------------------------------------------------------------------------- */
1690
Jean Delvare7ae31482011-03-20 14:50:52 +01001691int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1692{
1693 int res;
1694
1695 mutex_lock(&core_lock);
1696 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1697 mutex_unlock(&core_lock);
1698
1699 return res;
1700}
1701EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1702
Jean Delvare69b00892009-12-06 17:06:27 +01001703static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001704{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001705 if (dev->type != &i2c_adapter_type)
1706 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001707 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001708}
1709
David Brownell7b4fbc52007-05-01 23:26:30 +02001710/*
1711 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001712 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
1714
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001715int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001717 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
David Brownell1d0b19c2008-10-14 17:30:05 +02001719 /* Can't register until after driver model init */
1720 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1721 return -EAGAIN;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001724 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Jean Delvare729d6dd2009-06-19 16:58:18 +02001727 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001728 * will have called probe() for all matching-but-unbound devices.
1729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 res = driver_register(&driver->driver);
1731 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001732 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001733
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001734 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Jean Delvare4735c982008-07-14 22:38:36 +02001736 INIT_LIST_HEAD(&driver->clients);
1737 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001738 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001739
Jean Delvare7eebcb72006-02-05 23:28:21 +01001740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001742EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jean Delvare69b00892009-12-06 17:06:27 +01001744static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001745{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001746 if (dev->type == &i2c_adapter_type)
1747 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1748 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001749}
1750
David Brownella1d9e6e2007-05-01 23:26:30 +02001751/**
1752 * i2c_del_driver - unregister I2C driver
1753 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001754 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001755 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001756void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Jean Delvare7ae31482011-03-20 14:50:52 +01001758 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001761 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
David Brownellc0564602007-05-01 23:26:31 +02001763EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
David Brownell7b4fbc52007-05-01 23:26:30 +02001765/* ------------------------------------------------------------------------- */
1766
Jean Delvaree48d3312008-01-27 18:14:48 +01001767/**
1768 * i2c_use_client - increments the reference count of the i2c client structure
1769 * @client: the client being referenced
1770 *
1771 * Each live reference to a client should be refcounted. The driver model does
1772 * that automatically as part of driver binding, so that most drivers don't
1773 * need to do this explicitly: they hold a reference until they're unbound
1774 * from the device.
1775 *
1776 * A pointer to the client with the incremented reference counter is returned.
1777 */
1778struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
David Brownell6ea438e2008-07-14 22:38:24 +02001780 if (client && get_device(&client->dev))
1781 return client;
1782 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
David Brownellc0564602007-05-01 23:26:31 +02001784EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Jean Delvaree48d3312008-01-27 18:14:48 +01001786/**
1787 * i2c_release_client - release a use of the i2c client structure
1788 * @client: the client being no longer referenced
1789 *
1790 * Must be called when a user of a client is finished with it.
1791 */
1792void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
David Brownell6ea438e2008-07-14 22:38:24 +02001794 if (client)
1795 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
David Brownellc0564602007-05-01 23:26:31 +02001797EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
David Brownell9b766b82008-01-27 18:14:51 +01001799struct i2c_cmd_arg {
1800 unsigned cmd;
1801 void *arg;
1802};
1803
1804static int i2c_cmd(struct device *dev, void *_arg)
1805{
1806 struct i2c_client *client = i2c_verify_client(dev);
1807 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001808 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001809
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001810 if (!client || !client->dev.driver)
1811 return 0;
1812
1813 driver = to_i2c_driver(client->dev.driver);
1814 if (driver->command)
1815 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001816 return 0;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1820{
David Brownell9b766b82008-01-27 18:14:51 +01001821 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
David Brownell9b766b82008-01-27 18:14:51 +01001823 cmd_arg.cmd = cmd;
1824 cmd_arg.arg = arg;
1825 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
David Brownellc0564602007-05-01 23:26:31 +02001827EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001829#if IS_ENABLED(CONFIG_OF_DYNAMIC)
1830static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
1831 void *arg)
1832{
1833 struct of_reconfig_data *rd = arg;
1834 struct i2c_adapter *adap;
1835 struct i2c_client *client;
1836
1837 switch (of_reconfig_get_state_change(action, rd)) {
1838 case OF_RECONFIG_CHANGE_ADD:
1839 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
1840 if (adap == NULL)
1841 return NOTIFY_OK; /* not for us */
1842
1843 client = of_i2c_register_device(adap, rd->dn);
1844 put_device(&adap->dev);
1845
1846 if (IS_ERR(client)) {
1847 pr_err("%s: failed to create for '%s'\n",
1848 __func__, rd->dn->full_name);
1849 return notifier_from_errno(PTR_ERR(client));
1850 }
1851 break;
1852 case OF_RECONFIG_CHANGE_REMOVE:
1853 /* find our device by node */
1854 client = of_find_i2c_device_by_node(rd->dn);
1855 if (client == NULL)
1856 return NOTIFY_OK; /* no? not meant for us */
1857
1858 /* unregister takes one ref away */
1859 i2c_unregister_device(client);
1860
1861 /* and put the reference of the find */
1862 put_device(&client->dev);
1863 break;
1864 }
1865
1866 return NOTIFY_OK;
1867}
1868static struct notifier_block i2c_of_notifier = {
1869 .notifier_call = of_i2c_notify,
1870};
1871#else
1872extern struct notifier_block i2c_of_notifier;
1873#endif /* CONFIG_OF_DYNAMIC */
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875static int __init i2c_init(void)
1876{
1877 int retval;
1878
1879 retval = bus_register(&i2c_bus_type);
1880 if (retval)
1881 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001882#ifdef CONFIG_I2C_COMPAT
1883 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1884 if (!i2c_adapter_compat_class) {
1885 retval = -ENOMEM;
1886 goto bus_err;
1887 }
1888#endif
David Brownelle9f13732008-01-27 18:14:52 +01001889 retval = i2c_add_driver(&dummy_driver);
1890 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001891 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001892
1893 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1894 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
1895
David Brownelle9f13732008-01-27 18:14:52 +01001896 return 0;
1897
Jean Delvare2bb50952009-09-18 22:45:46 +02001898class_err:
1899#ifdef CONFIG_I2C_COMPAT
1900 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001901bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001902#endif
David Brownelle9f13732008-01-27 18:14:52 +01001903 bus_unregister(&i2c_bus_type);
1904 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
1907static void __exit i2c_exit(void)
1908{
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001909 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1910 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001911 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001912#ifdef CONFIG_I2C_COMPAT
1913 class_compat_unregister(i2c_adapter_compat_class);
1914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001916 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
David Brownella10f9e72008-10-14 17:30:06 +02001919/* We must initialize early, because some subsystems register i2c drivers
1920 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1921 */
1922postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923module_exit(i2c_exit);
1924
1925/* ----------------------------------------------------
1926 * the functional interface to the i2c busses.
1927 * ----------------------------------------------------
1928 */
1929
David Brownella1cdeda2008-07-14 22:38:24 +02001930/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001931 * __i2c_transfer - unlocked flavor of i2c_transfer
1932 * @adap: Handle to I2C bus
1933 * @msgs: One or more messages to execute before STOP is issued to
1934 * terminate the operation; each message begins with a START.
1935 * @num: Number of messages to be executed.
1936 *
1937 * Returns negative errno, else the number of messages executed.
1938 *
1939 * Adapter lock must be held when calling this function. No debug logging
1940 * takes place. adap->algo->master_xfer existence isn't checked.
1941 */
1942int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1943{
1944 unsigned long orig_jiffies;
1945 int ret, try;
1946
David Howellsd9a83d62014-03-06 13:35:59 +00001947 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1948 * enabled. This is an efficient way of keeping the for-loop from
1949 * being executed when not needed.
1950 */
1951 if (static_key_false(&i2c_trace_msg)) {
1952 int i;
1953 for (i = 0; i < num; i++)
1954 if (msgs[i].flags & I2C_M_RD)
1955 trace_i2c_read(adap, &msgs[i], i);
1956 else
1957 trace_i2c_write(adap, &msgs[i], i);
1958 }
1959
Jean Delvareb37d2a32012-06-29 07:47:19 -03001960 /* Retry automatically on arbitration loss */
1961 orig_jiffies = jiffies;
1962 for (ret = 0, try = 0; try <= adap->retries; try++) {
1963 ret = adap->algo->master_xfer(adap, msgs, num);
1964 if (ret != -EAGAIN)
1965 break;
1966 if (time_after(jiffies, orig_jiffies + adap->timeout))
1967 break;
1968 }
1969
David Howellsd9a83d62014-03-06 13:35:59 +00001970 if (static_key_false(&i2c_trace_msg)) {
1971 int i;
1972 for (i = 0; i < ret; i++)
1973 if (msgs[i].flags & I2C_M_RD)
1974 trace_i2c_reply(adap, &msgs[i], i);
1975 trace_i2c_result(adap, i, ret);
1976 }
1977
Jean Delvareb37d2a32012-06-29 07:47:19 -03001978 return ret;
1979}
1980EXPORT_SYMBOL(__i2c_transfer);
1981
1982/**
David Brownella1cdeda2008-07-14 22:38:24 +02001983 * i2c_transfer - execute a single or combined I2C message
1984 * @adap: Handle to I2C bus
1985 * @msgs: One or more messages to execute before STOP is issued to
1986 * terminate the operation; each message begins with a START.
1987 * @num: Number of messages to be executed.
1988 *
1989 * Returns negative errno, else the number of messages executed.
1990 *
1991 * Note that there is no requirement that each message be sent to
1992 * the same slave address, although that is the most common model.
1993 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001994int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001996 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
David Brownella1cdeda2008-07-14 22:38:24 +02001998 /* REVISIT the fault reporting model here is weak:
1999 *
2000 * - When we get an error after receiving N bytes from a slave,
2001 * there is no way to report "N".
2002 *
2003 * - When we get a NAK after transmitting N bytes to a slave,
2004 * there is no way to report "N" ... or to let the master
2005 * continue executing the rest of this combined message, if
2006 * that's the appropriate response.
2007 *
2008 * - When for example "num" is two and we successfully complete
2009 * the first message but get an error part way through the
2010 * second, it's unclear whether that should be reported as
2011 * one (discarding status on the second message) or errno
2012 * (discarding status on the first one).
2013 */
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (adap->algo->master_xfer) {
2016#ifdef DEBUG
2017 for (ret = 0; ret < num; ret++) {
2018 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002019 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2020 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2021 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023#endif
2024
Mike Rapoportcea443a82008-01-27 18:14:50 +01002025 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002026 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002027 if (!ret)
2028 /* I2C activity is ongoing. */
2029 return -EAGAIN;
2030 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002031 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01002032 }
2033
Jean Delvareb37d2a32012-06-29 07:47:19 -03002034 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002035 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 return ret;
2038 } else {
2039 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002040 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042}
David Brownellc0564602007-05-01 23:26:31 +02002043EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
David Brownella1cdeda2008-07-14 22:38:24 +02002045/**
2046 * i2c_master_send - issue a single I2C message in master transmit mode
2047 * @client: Handle to slave device
2048 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002049 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002050 *
2051 * Returns negative errno, or else the number of bytes written.
2052 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002053int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002056 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct i2c_msg msg;
2058
Jean Delvare815f55f2005-05-07 22:58:46 +02002059 msg.addr = client->addr;
2060 msg.flags = client->flags & I2C_M_TEN;
2061 msg.len = count;
2062 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002063
Jean Delvare815f55f2005-05-07 22:58:46 +02002064 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002066 /*
2067 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2068 * transmitted, else error code.
2069 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002070 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
David Brownellc0564602007-05-01 23:26:31 +02002072EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
David Brownella1cdeda2008-07-14 22:38:24 +02002074/**
2075 * i2c_master_recv - issue a single I2C message in master receive mode
2076 * @client: Handle to slave device
2077 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002078 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002079 *
2080 * Returns negative errno, or else the number of bytes read.
2081 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002082int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Farid Hammane7225acf2010-05-21 18:40:58 +02002084 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct i2c_msg msg;
2086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jean Delvare815f55f2005-05-07 22:58:46 +02002088 msg.addr = client->addr;
2089 msg.flags = client->flags & I2C_M_TEN;
2090 msg.flags |= I2C_M_RD;
2091 msg.len = count;
2092 msg.buf = buf;
2093
2094 ret = i2c_transfer(adap, &msg, 1);
2095
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002096 /*
2097 * If everything went ok (i.e. 1 msg received), return #bytes received,
2098 * else error code.
2099 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002100 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
David Brownellc0564602007-05-01 23:26:31 +02002102EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/* ----------------------------------------------------
2105 * the i2c address scanning function
2106 * Will not work for 10-bit addresses!
2107 * ----------------------------------------------------
2108 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002109
Jean Delvare63e4e802010-06-03 11:33:51 +02002110/*
2111 * Legacy default probe function, mostly relevant for SMBus. The default
2112 * probe method is a quick write, but it is known to corrupt the 24RF08
2113 * EEPROMs due to a state machine bug, and could also irreversibly
2114 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2115 * we use a short byte read instead. Also, some bus drivers don't implement
2116 * quick write, so we fallback to a byte read in that case too.
2117 * On x86, there is another special case for FSC hardware monitoring chips,
2118 * which want regular byte reads (address 0x73.) Fortunately, these are the
2119 * only known chips using this I2C address on PC hardware.
2120 * Returns 1 if probe succeeded, 0 if not.
2121 */
2122static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2123{
2124 int err;
2125 union i2c_smbus_data dummy;
2126
2127#ifdef CONFIG_X86
2128 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2129 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2130 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2131 I2C_SMBUS_BYTE_DATA, &dummy);
2132 else
2133#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002134 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2135 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002136 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2137 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002138 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2139 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2140 I2C_SMBUS_BYTE, &dummy);
2141 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002142 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2143 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002144 err = -EOPNOTSUPP;
2145 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002146
2147 return err >= 0;
2148}
2149
Jean Delvareccfbbd02009-12-06 17:06:25 +01002150static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002151 struct i2c_driver *driver)
2152{
2153 struct i2c_board_info info;
2154 struct i2c_adapter *adapter = temp_client->adapter;
2155 int addr = temp_client->addr;
2156 int err;
2157
2158 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002159 err = i2c_check_addr_validity(addr);
2160 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002161 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2162 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002163 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002164 }
2165
2166 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002167 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002168 return 0;
2169
Jean Delvareccfbbd02009-12-06 17:06:25 +01002170 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002171 if (!i2c_default_probe(adapter, addr))
2172 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002173
2174 /* Finally call the custom detection function */
2175 memset(&info, 0, sizeof(struct i2c_board_info));
2176 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002177 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002178 if (err) {
2179 /* -ENODEV is returned if the detection fails. We catch it
2180 here as this isn't an error. */
2181 return err == -ENODEV ? 0 : err;
2182 }
2183
2184 /* Consistency check */
2185 if (info.type[0] == '\0') {
2186 dev_err(&adapter->dev, "%s detection function provided "
2187 "no name for 0x%x\n", driver->driver.name,
2188 addr);
2189 } else {
2190 struct i2c_client *client;
2191
2192 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002193 if (adapter->class & I2C_CLASS_DEPRECATED)
2194 dev_warn(&adapter->dev,
2195 "This adapter will soon drop class based instantiation of devices. "
2196 "Please make sure client 0x%02x gets instantiated by other means. "
2197 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2198 info.addr);
2199
Jean Delvare4735c982008-07-14 22:38:36 +02002200 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2201 info.type, info.addr);
2202 client = i2c_new_device(adapter, &info);
2203 if (client)
2204 list_add_tail(&client->detected, &driver->clients);
2205 else
2206 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2207 info.type, info.addr);
2208 }
2209 return 0;
2210}
2211
2212static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2213{
Jean Delvarec3813d62009-12-14 21:17:25 +01002214 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002215 struct i2c_client *temp_client;
2216 int i, err = 0;
2217 int adap_id = i2c_adapter_id(adapter);
2218
Jean Delvarec3813d62009-12-14 21:17:25 +01002219 address_list = driver->address_list;
2220 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002221 return 0;
2222
Wolfram Sang45552272014-07-10 13:46:21 +02002223 /* Warn that the adapter lost class based instantiation */
2224 if (adapter->class == I2C_CLASS_DEPRECATED) {
2225 dev_dbg(&adapter->dev,
2226 "This adapter dropped support for I2C classes and "
2227 "won't auto-detect %s devices anymore. If you need it, check "
2228 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2229 driver->driver.name);
2230 return 0;
2231 }
2232
Jean Delvare51b54ba2010-10-24 18:16:58 +02002233 /* Stop here if the classes do not match */
2234 if (!(adapter->class & driver->class))
2235 return 0;
2236
Jean Delvare4735c982008-07-14 22:38:36 +02002237 /* Set up a temporary client to help detect callback */
2238 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2239 if (!temp_client)
2240 return -ENOMEM;
2241 temp_client->adapter = adapter;
2242
Jean Delvarec3813d62009-12-14 21:17:25 +01002243 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002244 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002245 "addr 0x%02x\n", adap_id, address_list[i]);
2246 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002247 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002248 if (unlikely(err))
2249 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002250 }
2251
Jean Delvare4735c982008-07-14 22:38:36 +02002252 kfree(temp_client);
2253 return err;
2254}
2255
Jean Delvared44f19d2010-08-11 18:20:57 +02002256int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2257{
2258 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2259 I2C_SMBUS_QUICK, NULL) >= 0;
2260}
2261EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2262
Jean Delvare12b5053a2007-05-01 23:26:31 +02002263struct i2c_client *
2264i2c_new_probed_device(struct i2c_adapter *adap,
2265 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002266 unsigned short const *addr_list,
2267 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002268{
2269 int i;
2270
Jean Delvare8031d792010-08-11 18:21:00 +02002271 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002272 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002273
Jean Delvare12b5053a2007-05-01 23:26:31 +02002274 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2275 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002276 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002277 dev_warn(&adap->dev, "Invalid 7-bit address "
2278 "0x%02x\n", addr_list[i]);
2279 continue;
2280 }
2281
2282 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002283 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002284 dev_dbg(&adap->dev, "Address 0x%02x already in "
2285 "use, not probing\n", addr_list[i]);
2286 continue;
2287 }
2288
Jean Delvare63e4e802010-06-03 11:33:51 +02002289 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002290 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002291 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002292 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002293
2294 if (addr_list[i] == I2C_CLIENT_END) {
2295 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2296 return NULL;
2297 }
2298
2299 info->addr = addr_list[i];
2300 return i2c_new_device(adap, info);
2301}
2302EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2303
Jean Delvared735b342011-03-20 14:50:52 +01002304struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002307
Jean Delvarecaada322008-01-27 18:14:49 +01002308 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002309 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002310 if (adapter && !try_module_get(adapter->owner))
2311 adapter = NULL;
2312
Jean Delvarecaada322008-01-27 18:14:49 +01002313 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002314 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
David Brownellc0564602007-05-01 23:26:31 +02002316EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318void i2c_put_adapter(struct i2c_adapter *adap)
2319{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002320 if (adap)
2321 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
David Brownellc0564602007-05-01 23:26:31 +02002323EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325/* The SMBus parts */
2326
David Brownell438d6c22006-12-10 21:21:31 +01002327#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002328static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002331
Farid Hammane7225acf2010-05-21 18:40:58 +02002332 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002333 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 data = data ^ POLY;
2335 data = data << 1;
2336 }
2337 return (u8)(data >> 8);
2338}
2339
Jean Delvare421ef472005-10-26 21:28:55 +02002340/* Incremental CRC8 over count bytes in the array pointed to by p */
2341static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 int i;
2344
Farid Hammane7225acf2010-05-21 18:40:58 +02002345 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002346 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return crc;
2348}
2349
Jean Delvare421ef472005-10-26 21:28:55 +02002350/* Assume a 7-bit address, which is reasonable for SMBus */
2351static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Jean Delvare421ef472005-10-26 21:28:55 +02002353 /* The address will be sent first */
2354 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2355 pec = i2c_smbus_pec(pec, &addr, 1);
2356
2357 /* The data buffer follows */
2358 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Jean Delvare421ef472005-10-26 21:28:55 +02002361/* Used for write only transactions */
2362static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Jean Delvare421ef472005-10-26 21:28:55 +02002364 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2365 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Jean Delvare421ef472005-10-26 21:28:55 +02002368/* Return <0 on CRC error
2369 If there was a write before this read (most cases) we need to take the
2370 partial CRC from the write part into account.
2371 Note that this function does modify the message (we need to decrease the
2372 message length to hide the CRC byte from the caller). */
2373static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Jean Delvare421ef472005-10-26 21:28:55 +02002375 u8 rpec = msg->buf[--msg->len];
2376 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (rpec != cpec) {
2379 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2380 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002381 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
David Brownell438d6c22006-12-10 21:21:31 +01002383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
David Brownella1cdeda2008-07-14 22:38:24 +02002386/**
2387 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2388 * @client: Handle to slave device
2389 *
2390 * This executes the SMBus "receive byte" protocol, returning negative errno
2391 * else the byte received from the device.
2392 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002393s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002396 int status;
2397
2398 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2399 I2C_SMBUS_READ, 0,
2400 I2C_SMBUS_BYTE, &data);
2401 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
David Brownellc0564602007-05-01 23:26:31 +02002403EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
David Brownella1cdeda2008-07-14 22:38:24 +02002405/**
2406 * i2c_smbus_write_byte - SMBus "send byte" protocol
2407 * @client: Handle to slave device
2408 * @value: Byte to be sent
2409 *
2410 * This executes the SMBus "send byte" protocol, returning negative errno
2411 * else zero on success.
2412 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002413s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Farid Hammane7225acf2010-05-21 18:40:58 +02002415 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002416 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
David Brownellc0564602007-05-01 23:26:31 +02002418EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
David Brownella1cdeda2008-07-14 22:38:24 +02002420/**
2421 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2422 * @client: Handle to slave device
2423 * @command: Byte interpreted by slave
2424 *
2425 * This executes the SMBus "read byte" protocol, returning negative errno
2426 * else a data byte received from the device.
2427 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002428s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002431 int status;
2432
2433 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2434 I2C_SMBUS_READ, command,
2435 I2C_SMBUS_BYTE_DATA, &data);
2436 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
David Brownellc0564602007-05-01 23:26:31 +02002438EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
David Brownella1cdeda2008-07-14 22:38:24 +02002440/**
2441 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2442 * @client: Handle to slave device
2443 * @command: Byte interpreted by slave
2444 * @value: Byte being written
2445 *
2446 * This executes the SMBus "write byte" protocol, returning negative errno
2447 * else zero on success.
2448 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002449s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2450 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 union i2c_smbus_data data;
2453 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002454 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2455 I2C_SMBUS_WRITE, command,
2456 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
David Brownellc0564602007-05-01 23:26:31 +02002458EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
David Brownella1cdeda2008-07-14 22:38:24 +02002460/**
2461 * i2c_smbus_read_word_data - SMBus "read word" protocol
2462 * @client: Handle to slave device
2463 * @command: Byte interpreted by slave
2464 *
2465 * This executes the SMBus "read word" protocol, returning negative errno
2466 * else a 16-bit unsigned "word" received from the device.
2467 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002468s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002471 int status;
2472
2473 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2474 I2C_SMBUS_READ, command,
2475 I2C_SMBUS_WORD_DATA, &data);
2476 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
David Brownellc0564602007-05-01 23:26:31 +02002478EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
David Brownella1cdeda2008-07-14 22:38:24 +02002480/**
2481 * i2c_smbus_write_word_data - SMBus "write word" protocol
2482 * @client: Handle to slave device
2483 * @command: Byte interpreted by slave
2484 * @value: 16-bit "word" being written
2485 *
2486 * This executes the SMBus "write word" protocol, returning negative errno
2487 * else zero on success.
2488 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002489s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2490 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 union i2c_smbus_data data;
2493 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002494 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2495 I2C_SMBUS_WRITE, command,
2496 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
David Brownellc0564602007-05-01 23:26:31 +02002498EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
David Brownella64ec072007-10-13 23:56:31 +02002500/**
David Brownella1cdeda2008-07-14 22:38:24 +02002501 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002502 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002503 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002504 * @values: Byte array into which data will be read; big enough to hold
2505 * the data returned by the slave. SMBus allows at most 32 bytes.
2506 *
David Brownella1cdeda2008-07-14 22:38:24 +02002507 * This executes the SMBus "block read" protocol, returning negative errno
2508 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002509 *
2510 * Note that using this function requires that the client's adapter support
2511 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2512 * support this; its emulation through I2C messaging relies on a specific
2513 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2514 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002515s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002516 u8 *values)
2517{
2518 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002519 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002520
David Brownell24a5bb72008-07-14 22:38:23 +02002521 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2522 I2C_SMBUS_READ, command,
2523 I2C_SMBUS_BLOCK_DATA, &data);
2524 if (status)
2525 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002526
2527 memcpy(values, &data.block[1], data.block[0]);
2528 return data.block[0];
2529}
2530EXPORT_SYMBOL(i2c_smbus_read_block_data);
2531
David Brownella1cdeda2008-07-14 22:38:24 +02002532/**
2533 * i2c_smbus_write_block_data - SMBus "block write" protocol
2534 * @client: Handle to slave device
2535 * @command: Byte interpreted by slave
2536 * @length: Size of data block; SMBus allows at most 32 bytes
2537 * @values: Byte array which will be written.
2538 *
2539 * This executes the SMBus "block write" protocol, returning negative errno
2540 * else zero on success.
2541 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002542s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002543 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (length > I2C_SMBUS_BLOCK_MAX)
2548 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002550 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002551 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2552 I2C_SMBUS_WRITE, command,
2553 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
David Brownellc0564602007-05-01 23:26:31 +02002555EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002558s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002559 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002562 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002563
Jean Delvare4b2643d2007-07-12 14:12:29 +02002564 if (length > I2C_SMBUS_BLOCK_MAX)
2565 length = I2C_SMBUS_BLOCK_MAX;
2566 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002567 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2568 I2C_SMBUS_READ, command,
2569 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2570 if (status < 0)
2571 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002572
2573 memcpy(values, &data.block[1], data.block[0]);
2574 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
David Brownellc0564602007-05-01 23:26:31 +02002576EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Jean Delvare0cc43a12011-01-10 22:11:23 +01002578s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002579 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002580{
2581 union i2c_smbus_data data;
2582
2583 if (length > I2C_SMBUS_BLOCK_MAX)
2584 length = I2C_SMBUS_BLOCK_MAX;
2585 data.block[0] = length;
2586 memcpy(data.block + 1, values, length);
2587 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2588 I2C_SMBUS_WRITE, command,
2589 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2590}
David Brownellc0564602007-05-01 23:26:31 +02002591EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002592
David Brownell438d6c22006-12-10 21:21:31 +01002593/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002595static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2596 unsigned short flags,
2597 char read_write, u8 command, int size,
2598 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 /* So we need to generate a series of msgs. In the case of writing, we
2601 need to use only one message; when reading, we need two. We initialize
2602 most things with sane defaults, to keep the code below somewhat
2603 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002604 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2605 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002606 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002608 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002609 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002610 struct i2c_msg msg[2] = {
2611 {
2612 .addr = addr,
2613 .flags = flags,
2614 .len = 1,
2615 .buf = msgbuf0,
2616 }, {
2617 .addr = addr,
2618 .flags = flags | I2C_M_RD,
2619 .len = 0,
2620 .buf = msgbuf1,
2621 },
2622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002625 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 case I2C_SMBUS_QUICK:
2627 msg[0].len = 0;
2628 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002629 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2630 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 num = 1;
2632 break;
2633 case I2C_SMBUS_BYTE:
2634 if (read_write == I2C_SMBUS_READ) {
2635 /* Special case: only a read! */
2636 msg[0].flags = I2C_M_RD | flags;
2637 num = 1;
2638 }
2639 break;
2640 case I2C_SMBUS_BYTE_DATA:
2641 if (read_write == I2C_SMBUS_READ)
2642 msg[1].len = 1;
2643 else {
2644 msg[0].len = 2;
2645 msgbuf0[1] = data->byte;
2646 }
2647 break;
2648 case I2C_SMBUS_WORD_DATA:
2649 if (read_write == I2C_SMBUS_READ)
2650 msg[1].len = 2;
2651 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002652 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002654 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 break;
2657 case I2C_SMBUS_PROC_CALL:
2658 num = 2; /* Special case */
2659 read_write = I2C_SMBUS_READ;
2660 msg[0].len = 3;
2661 msg[1].len = 2;
2662 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002663 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 break;
2665 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002667 msg[1].flags |= I2C_M_RECV_LEN;
2668 msg[1].len = 1; /* block length will be added by
2669 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } else {
2671 msg[0].len = data->block[0] + 2;
2672 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002673 dev_err(&adapter->dev,
2674 "Invalid block write size %d\n",
2675 data->block[0]);
2676 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002678 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 msgbuf0[i] = data->block[i-1];
2680 }
2681 break;
2682 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002683 num = 2; /* Another special case */
2684 read_write = I2C_SMBUS_READ;
2685 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002686 dev_err(&adapter->dev,
2687 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002688 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002689 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002690 }
2691 msg[0].len = data->block[0] + 2;
2692 for (i = 1; i < msg[0].len; i++)
2693 msgbuf0[i] = data->block[i-1];
2694 msg[1].flags |= I2C_M_RECV_LEN;
2695 msg[1].len = 1; /* block length will be added by
2696 the underlying bus driver */
2697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 case I2C_SMBUS_I2C_BLOCK_DATA:
2699 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002700 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 } else {
2702 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002703 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002704 dev_err(&adapter->dev,
2705 "Invalid block write size %d\n",
2706 data->block[0]);
2707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709 for (i = 1; i <= data->block[0]; i++)
2710 msgbuf0[i] = data->block[i];
2711 }
2712 break;
2713 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002714 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2715 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717
Jean Delvare421ef472005-10-26 21:28:55 +02002718 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2719 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2720 if (i) {
2721 /* Compute PEC if first message is a write */
2722 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002723 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002724 i2c_smbus_add_pec(&msg[0]);
2725 else /* Write followed by read */
2726 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2727 }
2728 /* Ask for PEC if last message is a read */
2729 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002730 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002731 }
2732
David Brownell24a5bb72008-07-14 22:38:23 +02002733 status = i2c_transfer(adapter, msg, num);
2734 if (status < 0)
2735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Jean Delvare421ef472005-10-26 21:28:55 +02002737 /* Check PEC if last message is a read */
2738 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002739 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2740 if (status < 0)
2741 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002742 }
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002745 switch (size) {
2746 case I2C_SMBUS_BYTE:
2747 data->byte = msgbuf0[0];
2748 break;
2749 case I2C_SMBUS_BYTE_DATA:
2750 data->byte = msgbuf1[0];
2751 break;
2752 case I2C_SMBUS_WORD_DATA:
2753 case I2C_SMBUS_PROC_CALL:
2754 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2755 break;
2756 case I2C_SMBUS_I2C_BLOCK_DATA:
2757 for (i = 0; i < data->block[0]; i++)
2758 data->block[i+1] = msgbuf1[i];
2759 break;
2760 case I2C_SMBUS_BLOCK_DATA:
2761 case I2C_SMBUS_BLOCK_PROC_CALL:
2762 for (i = 0; i < msgbuf1[0] + 1; i++)
2763 data->block[i] = msgbuf1[i];
2764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
2766 return 0;
2767}
2768
David Brownella1cdeda2008-07-14 22:38:24 +02002769/**
2770 * i2c_smbus_xfer - execute SMBus protocol operations
2771 * @adapter: Handle to I2C bus
2772 * @addr: Address of SMBus slave on that bus
2773 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2774 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2775 * @command: Byte interpreted by slave, for protocols which use such bytes
2776 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2777 * @data: Data to be read or written
2778 *
2779 * This executes an SMBus protocol operation, and returns a negative
2780 * errno code else zero on success.
2781 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002782s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002783 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002784 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002786 unsigned long orig_jiffies;
2787 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
David Howells8a325992014-03-06 13:36:06 +00002790 /* If enabled, the following two tracepoints are conditional on
2791 * read_write and protocol.
2792 */
2793 trace_smbus_write(adapter, addr, flags, read_write,
2794 command, protocol, data);
2795 trace_smbus_read(adapter, addr, flags, read_write,
2796 command, protocol);
2797
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002798 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002801 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002802
2803 /* Retry automatically on arbitration loss */
2804 orig_jiffies = jiffies;
2805 for (res = 0, try = 0; try <= adapter->retries; try++) {
2806 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2807 read_write, command,
2808 protocol, data);
2809 if (res != -EAGAIN)
2810 break;
2811 if (time_after(jiffies,
2812 orig_jiffies + adapter->timeout))
2813 break;
2814 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002815 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002817 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002818 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002819 /*
2820 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2821 * implement native support for the SMBus operation.
2822 */
2823 }
2824
David Howells8a325992014-03-06 13:36:06 +00002825 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2826 command, protocol, data);
2827
2828trace:
2829 /* If enabled, the reply tracepoint is conditional on read_write. */
2830 trace_smbus_reply(adapter, addr, flags, read_write,
2831 command, protocol, data);
2832 trace_smbus_result(adapter, addr, flags, read_write,
2833 command, protocol, res);
2834
2835 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002839int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2840{
2841 int ret;
2842
2843 if (!client || !slave_cb)
2844 return -EINVAL;
2845
2846 if (!(client->flags & I2C_CLIENT_TEN)) {
2847 /* Enforce stricter address checking */
2848 ret = i2c_check_addr_validity(client->addr);
2849 if (ret)
2850 return ret;
2851 }
2852
2853 if (!client->adapter->algo->reg_slave)
2854 return -EOPNOTSUPP;
2855
2856 client->slave_cb = slave_cb;
2857
2858 i2c_lock_adapter(client->adapter);
2859 ret = client->adapter->algo->reg_slave(client);
2860 i2c_unlock_adapter(client->adapter);
2861
2862 if (ret)
2863 client->slave_cb = NULL;
2864
2865 return ret;
2866}
2867EXPORT_SYMBOL_GPL(i2c_slave_register);
2868
2869int i2c_slave_unregister(struct i2c_client *client)
2870{
2871 int ret;
2872
2873 if (!client->adapter->algo->unreg_slave)
2874 return -EOPNOTSUPP;
2875
2876 i2c_lock_adapter(client->adapter);
2877 ret = client->adapter->algo->unreg_slave(client);
2878 i2c_unlock_adapter(client->adapter);
2879
2880 if (ret == 0)
2881 client->slave_cb = NULL;
2882
2883 return ret;
2884}
2885EXPORT_SYMBOL_GPL(i2c_slave_unregister);
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2888MODULE_DESCRIPTION("I2C-Bus main module");
2889MODULE_LICENSE("GPL");