blob: c09d06bf4d9b03cbe939350bf23606bbc557f828 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfram Sangca1f8da2014-11-04 23:46:27 +010013 GNU General Public License for more details. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* ------------------------------------------------------------------------- */
15
Jan Engelhardt96de0e22007-10-19 23:21:04 +020016/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020018 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010019 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020020 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010021 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020025 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang4b1acc42014-11-18 17:04:53 +010027 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
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 Rapoportcea443a2008-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>
53
David Brownell9c1600e2007-05-01 23:26:31 +020054#include "i2c-core.h"
55
David Howellsd9a83d62014-03-06 13:35:59 +000056#define CREATE_TRACE_POINTS
57#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jean Delvare6629dcf2010-05-04 11:09:28 +020059/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020060 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000061 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010062static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_IDR(i2c_adapter_idr);
64
Jean Delvare4f8cf822009-09-18 22:45:46 +020065static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020066static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010067
David Howellsd9a83d62014-03-06 13:35:59 +000068static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
69
70void i2c_transfer_trace_reg(void)
71{
72 static_key_slow_inc(&i2c_trace_msg);
73}
74
75void i2c_transfer_trace_unreg(void)
76{
77 static_key_slow_dec(&i2c_trace_msg);
78}
79
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020080#if defined(CONFIG_ACPI)
81struct acpi_i2c_handler_data {
82 struct acpi_connection_info info;
83 struct i2c_adapter *adapter;
84};
85
86struct gsb_buffer {
87 u8 status;
88 u8 len;
89 union {
90 u16 wdata;
91 u8 bdata;
92 u8 data[0];
93 };
94} __packed;
95
96static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
97{
98 struct i2c_board_info *info = data;
99
100 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
101 struct acpi_resource_i2c_serialbus *sb;
102
103 sb = &ares->data.i2c_serial_bus;
104 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
105 info->addr = sb->slave_address;
106 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
107 info->flags |= I2C_CLIENT_TEN;
108 }
109 } else if (info->irq < 0) {
110 struct resource r;
111
112 if (acpi_dev_resource_interrupt(ares, 0, &r))
113 info->irq = r.start;
114 }
115
116 /* Tell the ACPI core to skip this resource */
117 return 1;
118}
119
120static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
121 void *data, void **return_value)
122{
123 struct i2c_adapter *adapter = data;
124 struct list_head resource_list;
125 struct i2c_board_info info;
126 struct acpi_device *adev;
127 int ret;
128
129 if (acpi_bus_get_device(handle, &adev))
130 return AE_OK;
131 if (acpi_bus_get_status(adev) || !adev->status.present)
132 return AE_OK;
133
134 memset(&info, 0, sizeof(info));
135 info.acpi_node.companion = adev;
136 info.irq = -1;
137
138 INIT_LIST_HEAD(&resource_list);
139 ret = acpi_dev_get_resources(adev, &resource_list,
140 acpi_i2c_add_resource, &info);
141 acpi_dev_free_resource_list(&resource_list);
142
143 if (ret < 0 || !info.addr)
144 return AE_OK;
145
146 adev->power.flags.ignore_parent = true;
147 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
148 if (!i2c_new_device(adapter, &info)) {
149 adev->power.flags.ignore_parent = false;
150 dev_err(&adapter->dev,
151 "failed to add I2C device %s from ACPI\n",
152 dev_name(&adev->dev));
153 }
154
155 return AE_OK;
156}
157
158/**
159 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
160 * @adap: pointer to adapter
161 *
162 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
163 * namespace. When a device is found it will be added to the Linux device
164 * model and bound to the corresponding ACPI handle.
165 */
166static void acpi_i2c_register_devices(struct i2c_adapter *adap)
167{
168 acpi_handle handle;
169 acpi_status status;
170
171 if (!adap->dev.parent)
172 return;
173
174 handle = ACPI_HANDLE(adap->dev.parent);
175 if (!handle)
176 return;
177
178 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
179 acpi_i2c_add_device, NULL,
180 adap, NULL);
181 if (ACPI_FAILURE(status))
182 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
183}
184
185#else /* CONFIG_ACPI */
186static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
187#endif /* CONFIG_ACPI */
188
189#ifdef CONFIG_ACPI_I2C_OPREGION
190static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
191 u8 cmd, u8 *data, u8 data_len)
192{
193
194 struct i2c_msg msgs[2];
195 int ret;
196 u8 *buffer;
197
198 buffer = kzalloc(data_len, GFP_KERNEL);
199 if (!buffer)
200 return AE_NO_MEMORY;
201
202 msgs[0].addr = client->addr;
203 msgs[0].flags = client->flags;
204 msgs[0].len = 1;
205 msgs[0].buf = &cmd;
206
207 msgs[1].addr = client->addr;
208 msgs[1].flags = client->flags | I2C_M_RD;
209 msgs[1].len = data_len;
210 msgs[1].buf = buffer;
211
212 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
213 if (ret < 0)
214 dev_err(&client->adapter->dev, "i2c read failed\n");
215 else
216 memcpy(data, buffer, data_len);
217
218 kfree(buffer);
219 return ret;
220}
221
222static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
223 u8 cmd, u8 *data, u8 data_len)
224{
225
226 struct i2c_msg msgs[1];
227 u8 *buffer;
228 int ret = AE_OK;
229
230 buffer = kzalloc(data_len + 1, GFP_KERNEL);
231 if (!buffer)
232 return AE_NO_MEMORY;
233
234 buffer[0] = cmd;
235 memcpy(buffer + 1, data, data_len);
236
237 msgs[0].addr = client->addr;
238 msgs[0].flags = client->flags;
239 msgs[0].len = data_len + 1;
240 msgs[0].buf = buffer;
241
242 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
243 if (ret < 0)
244 dev_err(&client->adapter->dev, "i2c write failed\n");
245
246 kfree(buffer);
247 return ret;
248}
249
250static acpi_status
251acpi_i2c_space_handler(u32 function, acpi_physical_address command,
252 u32 bits, u64 *value64,
253 void *handler_context, void *region_context)
254{
255 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
256 struct acpi_i2c_handler_data *data = handler_context;
257 struct acpi_connection_info *info = &data->info;
258 struct acpi_resource_i2c_serialbus *sb;
259 struct i2c_adapter *adapter = data->adapter;
260 struct i2c_client client;
261 struct acpi_resource *ares;
262 u32 accessor_type = function >> 16;
263 u8 action = function & ACPI_IO_MASK;
Wolfram Sang4470c722014-11-18 15:12:43 +0100264 acpi_status ret;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200265 int status;
266
267 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
268 if (ACPI_FAILURE(ret))
269 return ret;
270
271 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
272 ret = AE_BAD_PARAMETER;
273 goto err;
274 }
275
276 sb = &ares->data.i2c_serial_bus;
277 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
278 ret = AE_BAD_PARAMETER;
279 goto err;
280 }
281
282 memset(&client, 0, sizeof(client));
283 client.adapter = adapter;
284 client.addr = sb->slave_address;
285 client.flags = 0;
286
287 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
288 client.flags |= I2C_CLIENT_TEN;
289
290 switch (accessor_type) {
291 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
292 if (action == ACPI_READ) {
293 status = i2c_smbus_read_byte(&client);
294 if (status >= 0) {
295 gsb->bdata = status;
296 status = 0;
297 }
298 } else {
299 status = i2c_smbus_write_byte(&client, gsb->bdata);
300 }
301 break;
302
303 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
304 if (action == ACPI_READ) {
305 status = i2c_smbus_read_byte_data(&client, command);
306 if (status >= 0) {
307 gsb->bdata = status;
308 status = 0;
309 }
310 } else {
311 status = i2c_smbus_write_byte_data(&client, command,
312 gsb->bdata);
313 }
314 break;
315
316 case ACPI_GSB_ACCESS_ATTRIB_WORD:
317 if (action == ACPI_READ) {
318 status = i2c_smbus_read_word_data(&client, command);
319 if (status >= 0) {
320 gsb->wdata = status;
321 status = 0;
322 }
323 } else {
324 status = i2c_smbus_write_word_data(&client, command,
325 gsb->wdata);
326 }
327 break;
328
329 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
330 if (action == ACPI_READ) {
331 status = i2c_smbus_read_block_data(&client, command,
332 gsb->data);
333 if (status >= 0) {
334 gsb->len = status;
335 status = 0;
336 }
337 } else {
338 status = i2c_smbus_write_block_data(&client, command,
339 gsb->len, gsb->data);
340 }
341 break;
342
343 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
344 if (action == ACPI_READ) {
345 status = acpi_gsb_i2c_read_bytes(&client, command,
346 gsb->data, info->access_length);
347 if (status > 0)
348 status = 0;
349 } else {
350 status = acpi_gsb_i2c_write_bytes(&client, command,
351 gsb->data, info->access_length);
352 }
353 break;
354
355 default:
356 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
357 ret = AE_BAD_PARAMETER;
358 goto err;
359 }
360
361 gsb->status = status;
362
363 err:
364 ACPI_FREE(ares);
365 return ret;
366}
367
368
369static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
370{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200371 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200372 struct acpi_i2c_handler_data *data;
373 acpi_status status;
374
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200375 if (!adapter->dev.parent)
376 return -ENODEV;
377
378 handle = ACPI_HANDLE(adapter->dev.parent);
379
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200380 if (!handle)
381 return -ENODEV;
382
383 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
384 GFP_KERNEL);
385 if (!data)
386 return -ENOMEM;
387
388 data->adapter = adapter;
389 status = acpi_bus_attach_private_data(handle, (void *)data);
390 if (ACPI_FAILURE(status)) {
391 kfree(data);
392 return -ENOMEM;
393 }
394
395 status = acpi_install_address_space_handler(handle,
396 ACPI_ADR_SPACE_GSBUS,
397 &acpi_i2c_space_handler,
398 NULL,
399 data);
400 if (ACPI_FAILURE(status)) {
401 dev_err(&adapter->dev, "Error installing i2c space handler\n");
402 acpi_bus_detach_private_data(handle);
403 kfree(data);
404 return -ENOMEM;
405 }
406
407 return 0;
408}
409
410static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
411{
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200412 acpi_handle handle;
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200413 struct acpi_i2c_handler_data *data;
414 acpi_status status;
415
Peter Hüwe0aef44e2014-09-12 21:09:47 +0200416 if (!adapter->dev.parent)
417 return;
418
419 handle = ACPI_HANDLE(adapter->dev.parent);
420
Wolfram Sang17f4a5c2014-09-22 19:41:00 +0200421 if (!handle)
422 return;
423
424 acpi_remove_address_space_handler(handle,
425 ACPI_ADR_SPACE_GSBUS,
426 &acpi_i2c_space_handler);
427
428 status = acpi_bus_get_private_data(handle, (void **)&data);
429 if (ACPI_SUCCESS(status))
430 kfree(data);
431
432 acpi_bus_detach_private_data(handle);
433}
434#else /* CONFIG_ACPI_I2C_OPREGION */
435static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
436{ }
437
438static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
439{ return 0; }
440#endif /* CONFIG_ACPI_I2C_OPREGION */
441
David Brownellf37dd802007-02-13 22:09:00 +0100442/* ------------------------------------------------------------------------- */
443
Jean Delvared2653e92008-04-29 23:11:39 +0200444static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
445 const struct i2c_client *client)
446{
447 while (id->name[0]) {
448 if (strcmp(client->name, id->name) == 0)
449 return id;
450 id++;
451 }
452 return NULL;
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static int i2c_device_match(struct device *dev, struct device_driver *drv)
456{
Jean Delvare51298d12009-09-18 22:45:45 +0200457 struct i2c_client *client = i2c_verify_client(dev);
458 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200459
Jean Delvare51298d12009-09-18 22:45:45 +0200460 if (!client)
461 return 0;
462
Grant Likely959e85f2010-06-08 07:48:19 -0600463 /* Attempt an OF style match */
464 if (of_driver_match_device(dev, drv))
465 return 1;
466
Mika Westerberg907ddf82012-11-23 12:23:40 +0100467 /* Then ACPI style match */
468 if (acpi_driver_match_device(dev, drv))
469 return 1;
470
Jean Delvare51298d12009-09-18 22:45:45 +0200471 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200472 /* match on an id table if there is one */
473 if (driver->id_table)
474 return i2c_match_id(driver->id_table, client) != NULL;
475
Jean Delvareeb8a7902008-05-18 20:49:41 +0200476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
David Brownell7b4fbc52007-05-01 23:26:30 +0200479
480/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200481static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200482{
483 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800484 int rc;
485
486 rc = acpi_device_uevent_modalias(dev, env);
487 if (rc != -ENODEV)
488 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200489
Jean Delvareeb8a7902008-05-18 20:49:41 +0200490 if (add_uevent_var(env, "MODALIAS=%s%s",
491 I2C_MODULE_PREFIX, client->name))
492 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200493 dev_dbg(dev, "uevent\n");
494 return 0;
495}
496
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530497/* i2c bus recovery routines */
498static int get_scl_gpio_value(struct i2c_adapter *adap)
499{
500 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
501}
502
503static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
504{
505 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
506}
507
508static int get_sda_gpio_value(struct i2c_adapter *adap)
509{
510 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
511}
512
513static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
514{
515 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
516 struct device *dev = &adap->dev;
517 int ret = 0;
518
519 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
520 GPIOF_OUT_INIT_HIGH, "i2c-scl");
521 if (ret) {
522 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
523 return ret;
524 }
525
526 if (bri->get_sda) {
527 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
528 /* work without SDA polling */
529 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
530 bri->sda_gpio);
531 bri->get_sda = NULL;
532 }
533 }
534
535 return ret;
536}
537
538static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
539{
540 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
541
542 if (bri->get_sda)
543 gpio_free(bri->sda_gpio);
544
545 gpio_free(bri->scl_gpio);
546}
547
548/*
549 * We are generating clock pulses. ndelay() determines durating of clk pulses.
550 * We will generate clock with rate 100 KHz and so duration of both clock levels
551 * is: delay in ns = (10^6 / 100) / 2
552 */
553#define RECOVERY_NDELAY 5000
554#define RECOVERY_CLK_CNT 9
555
556static int i2c_generic_recovery(struct i2c_adapter *adap)
557{
558 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
559 int i = 0, val = 1, ret = 0;
560
561 if (bri->prepare_recovery)
562 bri->prepare_recovery(bri);
563
564 /*
565 * By this time SCL is high, as we need to give 9 falling-rising edges
566 */
567 while (i++ < RECOVERY_CLK_CNT * 2) {
568 if (val) {
569 /* Break if SDA is high */
570 if (bri->get_sda && bri->get_sda(adap))
571 break;
572 /* SCL shouldn't be low here */
573 if (!bri->get_scl(adap)) {
574 dev_err(&adap->dev,
575 "SCL is stuck low, exit recovery\n");
576 ret = -EBUSY;
577 break;
578 }
579 }
580
581 val = !val;
582 bri->set_scl(adap, val);
583 ndelay(RECOVERY_NDELAY);
584 }
585
586 if (bri->unprepare_recovery)
587 bri->unprepare_recovery(bri);
588
589 return ret;
590}
591
592int i2c_generic_scl_recovery(struct i2c_adapter *adap)
593{
594 adap->bus_recovery_info->set_scl(adap, 1);
595 return i2c_generic_recovery(adap);
596}
597
598int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
599{
600 int ret;
601
602 ret = i2c_get_gpios_for_recovery(adap);
603 if (ret)
604 return ret;
605
606 ret = i2c_generic_recovery(adap);
607 i2c_put_gpios_for_recovery(adap);
608
609 return ret;
610}
611
612int i2c_recover_bus(struct i2c_adapter *adap)
613{
614 if (!adap->bus_recovery_info)
615 return -EOPNOTSUPP;
616
617 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
618 return adap->bus_recovery_info->recover_bus(adap);
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static int i2c_device_probe(struct device *dev)
622{
Jean Delvare51298d12009-09-18 22:45:45 +0200623 struct i2c_client *client = i2c_verify_client(dev);
624 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100625 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200626
Jean Delvare51298d12009-09-18 22:45:45 +0200627 if (!client)
628 return 0;
629
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200630 if (!client->irq && dev->of_node) {
631 int irq = of_irq_get(dev->of_node, 0);
632
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100633 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200634 return irq;
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100635 if (irq < 0)
636 irq = 0;
Laurent Pinchart2fd36c552014-10-30 15:59:38 +0200637
638 client->irq = irq;
639 }
640
Jean Delvare51298d12009-09-18 22:45:45 +0200641 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200642 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200643 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200644
Marc Pignatee354252008-08-28 08:33:22 +0200645 if (!device_can_wakeup(&client->dev))
646 device_init_wakeup(&client->dev,
647 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200648 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200649
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200650 status = of_clk_set_defaults(dev->of_node, false);
651 if (status < 0)
652 return status;
653
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200654 status = dev_pm_domain_attach(&client->dev, true);
655 if (status != -EPROBE_DEFER) {
656 status = driver->probe(client, i2c_match_id(driver->id_table,
657 client));
658 if (status)
659 dev_pm_domain_detach(&client->dev, true);
660 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100661
Hans Verkuil50c33042008-03-12 14:15:00 +0100662 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665static int i2c_device_remove(struct device *dev)
666{
Jean Delvare51298d12009-09-18 22:45:45 +0200667 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200668 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100669 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200670
Jean Delvare51298d12009-09-18 22:45:45 +0200671 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200672 return 0;
673
674 driver = to_i2c_driver(dev->driver);
675 if (driver->remove) {
676 dev_dbg(dev, "remove\n");
677 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200678 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100679
Laurent Pincharte4df3a02014-10-30 15:59:37 +0200680 if (dev->of_node)
681 irq_dispose_mapping(client->irq);
682
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200683 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200684 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
David Brownellf37dd802007-02-13 22:09:00 +0100687static void i2c_device_shutdown(struct device *dev)
688{
Jean Delvare51298d12009-09-18 22:45:45 +0200689 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100690 struct i2c_driver *driver;
691
Jean Delvare51298d12009-09-18 22:45:45 +0200692 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100693 return;
694 driver = to_i2c_driver(dev->driver);
695 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200696 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100697}
698
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200699#ifdef CONFIG_PM_SLEEP
700static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100701{
Jean Delvare51298d12009-09-18 22:45:45 +0200702 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100703 struct i2c_driver *driver;
704
Jean Delvare51298d12009-09-18 22:45:45 +0200705 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100706 return 0;
707 driver = to_i2c_driver(dev->driver);
708 if (!driver->suspend)
709 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200710 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100711}
712
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200713static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100714{
Jean Delvare51298d12009-09-18 22:45:45 +0200715 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100716 struct i2c_driver *driver;
717
Jean Delvare51298d12009-09-18 22:45:45 +0200718 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100719 return 0;
720 driver = to_i2c_driver(dev->driver);
721 if (!driver->resume)
722 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200723 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100724}
725
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200726static int i2c_device_pm_suspend(struct device *dev)
727{
728 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
729
Mark Brownd529de22011-01-14 22:03:49 +0100730 if (pm)
731 return pm_generic_suspend(dev);
732 else
733 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200734}
735
736static int i2c_device_pm_resume(struct device *dev)
737{
738 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200739
740 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100741 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200742 else
Mark Brownd529de22011-01-14 22:03:49 +0100743 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200744}
745
746static int i2c_device_pm_freeze(struct device *dev)
747{
748 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
749
Mark Brownd529de22011-01-14 22:03:49 +0100750 if (pm)
751 return pm_generic_freeze(dev);
752 else
753 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200754}
755
756static int i2c_device_pm_thaw(struct device *dev)
757{
758 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
759
Mark Brownd529de22011-01-14 22:03:49 +0100760 if (pm)
761 return pm_generic_thaw(dev);
762 else
763 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200764}
765
766static int i2c_device_pm_poweroff(struct device *dev)
767{
768 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
769
Mark Brownd529de22011-01-14 22:03:49 +0100770 if (pm)
771 return pm_generic_poweroff(dev);
772 else
773 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200774}
775
776static int i2c_device_pm_restore(struct device *dev)
777{
778 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200779
780 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100781 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200782 else
Mark Brownd529de22011-01-14 22:03:49 +0100783 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200784}
785#else /* !CONFIG_PM_SLEEP */
786#define i2c_device_pm_suspend NULL
787#define i2c_device_pm_resume NULL
788#define i2c_device_pm_freeze NULL
789#define i2c_device_pm_thaw NULL
790#define i2c_device_pm_poweroff NULL
791#define i2c_device_pm_restore NULL
792#endif /* !CONFIG_PM_SLEEP */
793
David Brownell9c1600e2007-05-01 23:26:31 +0200794static void i2c_client_dev_release(struct device *dev)
795{
796 kfree(to_i2c_client(dev));
797}
798
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100799static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200800show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200801{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200802 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
803 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200804}
805
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100806static ssize_t
807show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200808{
809 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800810 int len;
811
812 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
813 if (len != -ENODEV)
814 return len;
815
Jean Delvareeb8a7902008-05-18 20:49:41 +0200816 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200817}
818
Jean Delvare4f8cf822009-09-18 22:45:46 +0200819static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200820static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
821
822static struct attribute *i2c_dev_attrs[] = {
823 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200824 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200825 &dev_attr_modalias.attr,
826 NULL
827};
828
829static struct attribute_group i2c_dev_attr_group = {
830 .attrs = i2c_dev_attrs,
831};
832
833static const struct attribute_group *i2c_dev_attr_groups[] = {
834 &i2c_dev_attr_group,
835 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200836};
837
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100838static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100839 .suspend = i2c_device_pm_suspend,
840 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200841 .freeze = i2c_device_pm_freeze,
842 .thaw = i2c_device_pm_thaw,
843 .poweroff = i2c_device_pm_poweroff,
844 .restore = i2c_device_pm_restore,
845 SET_RUNTIME_PM_OPS(
846 pm_generic_runtime_suspend,
847 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200848 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200849 )
sonic zhang54067ee2009-12-14 21:17:30 +0100850};
851
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200852struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100853 .name = "i2c",
854 .match = i2c_device_match,
855 .probe = i2c_device_probe,
856 .remove = i2c_device_remove,
857 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100858 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000859};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200860EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000861
Jean Delvare51298d12009-09-18 22:45:45 +0200862static struct device_type i2c_client_type = {
863 .groups = i2c_dev_attr_groups,
864 .uevent = i2c_device_uevent,
865 .release = i2c_client_dev_release,
866};
867
David Brownell9b766b82008-01-27 18:14:51 +0100868
869/**
870 * i2c_verify_client - return parameter as i2c_client, or NULL
871 * @dev: device, probably from some driver model iterator
872 *
873 * When traversing the driver model tree, perhaps using driver model
874 * iterators like @device_for_each_child(), you can't assume very much
875 * about the nodes you find. Use this function to avoid oopses caused
876 * by wrongly treating some non-I2C device as an i2c_client.
877 */
878struct i2c_client *i2c_verify_client(struct device *dev)
879{
Jean Delvare51298d12009-09-18 22:45:45 +0200880 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100881 ? to_i2c_client(dev)
882 : NULL;
883}
884EXPORT_SYMBOL(i2c_verify_client);
885
886
Jean Delvare3a89db52010-06-03 11:33:52 +0200887/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300888 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200889static int i2c_check_client_addr_validity(const struct i2c_client *client)
890{
891 if (client->flags & I2C_CLIENT_TEN) {
892 /* 10-bit address, all values are valid */
893 if (client->addr > 0x3ff)
894 return -EINVAL;
895 } else {
896 /* 7-bit address, reject the general call address */
897 if (client->addr == 0x00 || client->addr > 0x7f)
898 return -EINVAL;
899 }
900 return 0;
901}
902
Jean Delvare656b8762010-06-03 11:33:53 +0200903/* And this is a strict address validity check, used when probing. If a
904 * device uses a reserved address, then it shouldn't be probed. 7-bit
905 * addressing is assumed, 10-bit address devices are rare and should be
906 * explicitly enumerated. */
907static int i2c_check_addr_validity(unsigned short addr)
908{
909 /*
910 * Reserved addresses per I2C specification:
911 * 0x00 General call address / START byte
912 * 0x01 CBUS address
913 * 0x02 Reserved for different bus format
914 * 0x03 Reserved for future purposes
915 * 0x04-0x07 Hs-mode master code
916 * 0x78-0x7b 10-bit slave addressing
917 * 0x7c-0x7f Reserved for future purposes
918 */
919 if (addr < 0x08 || addr > 0x77)
920 return -EINVAL;
921 return 0;
922}
923
Jean Delvare3b5f7942010-06-03 11:33:55 +0200924static int __i2c_check_addr_busy(struct device *dev, void *addrp)
925{
926 struct i2c_client *client = i2c_verify_client(dev);
927 int addr = *(int *)addrp;
928
929 if (client && client->addr == addr)
930 return -EBUSY;
931 return 0;
932}
933
Michael Lawnick08263742010-08-11 18:21:02 +0200934/* walk up mux tree */
935static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
936{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200937 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200938 int result;
939
940 result = device_for_each_child(&adapter->dev, &addr,
941 __i2c_check_addr_busy);
942
Jean Delvare97cc4d42010-10-24 18:16:57 +0200943 if (!result && parent)
944 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200945
946 return result;
947}
948
949/* recurse down mux tree */
950static int i2c_check_mux_children(struct device *dev, void *addrp)
951{
952 int result;
953
954 if (dev->type == &i2c_adapter_type)
955 result = device_for_each_child(dev, addrp,
956 i2c_check_mux_children);
957 else
958 result = __i2c_check_addr_busy(dev, addrp);
959
960 return result;
961}
962
Jean Delvare3b5f7942010-06-03 11:33:55 +0200963static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
964{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200965 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200966 int result = 0;
967
Jean Delvare97cc4d42010-10-24 18:16:57 +0200968 if (parent)
969 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200970
971 if (!result)
972 result = device_for_each_child(&adapter->dev, &addr,
973 i2c_check_mux_children);
974
975 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200976}
977
David Brownell9c1600e2007-05-01 23:26:31 +0200978/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200979 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
980 * @adapter: Target I2C bus segment
981 */
982void i2c_lock_adapter(struct i2c_adapter *adapter)
983{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200984 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
985
986 if (parent)
987 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200988 else
989 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200990}
991EXPORT_SYMBOL_GPL(i2c_lock_adapter);
992
993/**
994 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
995 * @adapter: Target I2C bus segment
996 */
997static int i2c_trylock_adapter(struct i2c_adapter *adapter)
998{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200999 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1000
1001 if (parent)
1002 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001003 else
1004 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001005}
1006
1007/**
1008 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
1009 * @adapter: Target I2C bus segment
1010 */
1011void i2c_unlock_adapter(struct i2c_adapter *adapter)
1012{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001013 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1014
1015 if (parent)
1016 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001017 else
1018 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001019}
1020EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1021
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001022static void i2c_dev_set_name(struct i2c_adapter *adap,
1023 struct i2c_client *client)
1024{
1025 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1026
1027 if (adev) {
1028 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1029 return;
1030 }
1031
1032 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1033 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1034 client->addr | ((client->flags & I2C_CLIENT_TEN)
1035 ? 0xa000 : 0));
1036}
1037
Jean Delvarefe61e072010-08-11 18:20:58 +02001038/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001039 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001040 * @adap: the adapter managing the device
1041 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001042 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001043 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001044 * Create an i2c device. Binding is handled through driver model
1045 * probe()/remove() methods. A driver may be bound to this device when we
1046 * return from this function, or any later moment (e.g. maybe hotplugging will
1047 * load the driver module). This call is not appropriate for use by mainboard
1048 * initialization logic, which usually runs during an arch_initcall() long
1049 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001050 *
1051 * This returns the new i2c client, which may be saved for later use with
1052 * i2c_unregister_device(); or NULL to indicate an error.
1053 */
1054struct i2c_client *
1055i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1056{
1057 struct i2c_client *client;
1058 int status;
1059
1060 client = kzalloc(sizeof *client, GFP_KERNEL);
1061 if (!client)
1062 return NULL;
1063
1064 client->adapter = adap;
1065
1066 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001067
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001068 if (info->archdata)
1069 client->dev.archdata = *info->archdata;
1070
Marc Pignatee354252008-08-28 08:33:22 +02001071 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001072 client->addr = info->addr;
1073 client->irq = info->irq;
1074
David Brownell9c1600e2007-05-01 23:26:31 +02001075 strlcpy(client->name, info->type, sizeof(client->name));
1076
Jean Delvare3a89db52010-06-03 11:33:52 +02001077 /* Check for address validity */
1078 status = i2c_check_client_addr_validity(client);
1079 if (status) {
1080 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1081 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1082 goto out_err_silent;
1083 }
1084
Jean Delvaref8a227e2009-06-19 16:58:18 +02001085 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001086 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001087 if (status)
1088 goto out_err;
1089
1090 client->dev.parent = &client->adapter->dev;
1091 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001092 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001093 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001094 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001095
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001096 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001097 status = device_register(&client->dev);
1098 if (status)
1099 goto out_err;
1100
Jean Delvaref8a227e2009-06-19 16:58:18 +02001101 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1102 client->name, dev_name(&client->dev));
1103
David Brownell9c1600e2007-05-01 23:26:31 +02001104 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001105
1106out_err:
1107 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1108 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001109out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001110 kfree(client);
1111 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001112}
1113EXPORT_SYMBOL_GPL(i2c_new_device);
1114
1115
1116/**
1117 * i2c_unregister_device - reverse effect of i2c_new_device()
1118 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001119 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001120 */
1121void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001122{
David Brownella1d9e6e2007-05-01 23:26:30 +02001123 device_unregister(&client->dev);
1124}
David Brownell9c1600e2007-05-01 23:26:31 +02001125EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001126
1127
Jean Delvare60b129d2008-05-11 20:37:06 +02001128static const struct i2c_device_id dummy_id[] = {
1129 { "dummy", 0 },
1130 { },
1131};
1132
Jean Delvared2653e92008-04-29 23:11:39 +02001133static int dummy_probe(struct i2c_client *client,
1134 const struct i2c_device_id *id)
1135{
1136 return 0;
1137}
1138
1139static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001140{
1141 return 0;
1142}
1143
1144static struct i2c_driver dummy_driver = {
1145 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001146 .probe = dummy_probe,
1147 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001148 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001149};
1150
1151/**
1152 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1153 * @adapter: the adapter managing the device
1154 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001155 * Context: can sleep
1156 *
1157 * This returns an I2C client bound to the "dummy" driver, intended for use
1158 * with devices that consume multiple addresses. Examples of such chips
1159 * include various EEPROMS (like 24c04 and 24c08 models).
1160 *
1161 * These dummy devices have two main uses. First, most I2C and SMBus calls
1162 * except i2c_transfer() need a client handle; the dummy will be that handle.
1163 * And second, this prevents the specified address from being bound to a
1164 * different driver.
1165 *
1166 * This returns the new i2c client, which should be saved for later use with
1167 * i2c_unregister_device(); or NULL to indicate an error.
1168 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001169struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001170{
1171 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001172 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001173 };
1174
David Brownelle9f13732008-01-27 18:14:52 +01001175 return i2c_new_device(adapter, &info);
1176}
1177EXPORT_SYMBOL_GPL(i2c_new_dummy);
1178
David Brownellf37dd802007-02-13 22:09:00 +01001179/* ------------------------------------------------------------------------- */
1180
David Brownell16ffadf2007-05-01 23:26:28 +02001181/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1182
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001183static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
David Brownellef2c83212007-05-01 23:26:28 +02001185 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 complete(&adap->dev_released);
1187}
1188
Jean Delvare99cd8e22009-06-19 16:58:20 +02001189/*
Jean Delvare390946b2012-09-10 10:14:02 +02001190 * This function is only needed for mutex_lock_nested, so it is never
1191 * called unless locking correctness checking is enabled. Thus we
1192 * make it inline to avoid a compiler warning. That's what gcc ends up
1193 * doing anyway.
1194 */
1195static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1196{
1197 unsigned int depth = 0;
1198
1199 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1200 depth++;
1201
1202 return depth;
1203}
1204
1205/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001206 * Let users instantiate I2C devices through sysfs. This can be used when
1207 * platform initialization code doesn't contain the proper data for
1208 * whatever reason. Also useful for drivers that do device detection and
1209 * detection fails, either because the device uses an unexpected address,
1210 * or this is a compatible device with different ID register values.
1211 *
1212 * Parameter checking may look overzealous, but we really don't want
1213 * the user to provide incorrect parameters.
1214 */
1215static ssize_t
1216i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1217 const char *buf, size_t count)
1218{
1219 struct i2c_adapter *adap = to_i2c_adapter(dev);
1220 struct i2c_board_info info;
1221 struct i2c_client *client;
1222 char *blank, end;
1223 int res;
1224
Jean Delvare99cd8e22009-06-19 16:58:20 +02001225 memset(&info, 0, sizeof(struct i2c_board_info));
1226
1227 blank = strchr(buf, ' ');
1228 if (!blank) {
1229 dev_err(dev, "%s: Missing parameters\n", "new_device");
1230 return -EINVAL;
1231 }
1232 if (blank - buf > I2C_NAME_SIZE - 1) {
1233 dev_err(dev, "%s: Invalid device name\n", "new_device");
1234 return -EINVAL;
1235 }
1236 memcpy(info.type, buf, blank - buf);
1237
1238 /* Parse remaining parameters, reject extra parameters */
1239 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1240 if (res < 1) {
1241 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1242 return -EINVAL;
1243 }
1244 if (res > 1 && end != '\n') {
1245 dev_err(dev, "%s: Extra parameters\n", "new_device");
1246 return -EINVAL;
1247 }
1248
Jean Delvare99cd8e22009-06-19 16:58:20 +02001249 client = i2c_new_device(adap, &info);
1250 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001251 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001252
1253 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001254 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001255 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001256 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001257 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1258 info.type, info.addr);
1259
1260 return count;
1261}
1262
1263/*
1264 * And of course let the users delete the devices they instantiated, if
1265 * they got it wrong. This interface can only be used to delete devices
1266 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1267 * don't delete devices to which some kernel code still has references.
1268 *
1269 * Parameter checking may look overzealous, but we really don't want
1270 * the user to delete the wrong device.
1271 */
1272static ssize_t
1273i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1274 const char *buf, size_t count)
1275{
1276 struct i2c_adapter *adap = to_i2c_adapter(dev);
1277 struct i2c_client *client, *next;
1278 unsigned short addr;
1279 char end;
1280 int res;
1281
1282 /* Parse parameters, reject extra parameters */
1283 res = sscanf(buf, "%hi%c", &addr, &end);
1284 if (res < 1) {
1285 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1286 return -EINVAL;
1287 }
1288 if (res > 1 && end != '\n') {
1289 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1290 return -EINVAL;
1291 }
1292
1293 /* Make sure the device was added through sysfs */
1294 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001295 mutex_lock_nested(&adap->userspace_clients_lock,
1296 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001297 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1298 detected) {
1299 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001300 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1301 "delete_device", client->name, client->addr);
1302
1303 list_del(&client->detected);
1304 i2c_unregister_device(client);
1305 res = count;
1306 break;
1307 }
1308 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001309 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001310
1311 if (res < 0)
1312 dev_err(dev, "%s: Can't find device in list\n",
1313 "delete_device");
1314 return res;
1315}
1316
Jean Delvare4f8cf822009-09-18 22:45:46 +02001317static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001318static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1319 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001320
1321static struct attribute *i2c_adapter_attrs[] = {
1322 &dev_attr_name.attr,
1323 &dev_attr_new_device.attr,
1324 &dev_attr_delete_device.attr,
1325 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001326};
1327
Jean Delvare4f8cf822009-09-18 22:45:46 +02001328static struct attribute_group i2c_adapter_attr_group = {
1329 .attrs = i2c_adapter_attrs,
1330};
1331
1332static const struct attribute_group *i2c_adapter_attr_groups[] = {
1333 &i2c_adapter_attr_group,
1334 NULL
1335};
1336
Michael Lawnick08263742010-08-11 18:21:02 +02001337struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001338 .groups = i2c_adapter_attr_groups,
1339 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001340};
Michael Lawnick08263742010-08-11 18:21:02 +02001341EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Stephen Warren643dd092012-04-17 12:43:33 -06001343/**
1344 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1345 * @dev: device, probably from some driver model iterator
1346 *
1347 * When traversing the driver model tree, perhaps using driver model
1348 * iterators like @device_for_each_child(), you can't assume very much
1349 * about the nodes you find. Use this function to avoid oopses caused
1350 * by wrongly treating some non-I2C device as an i2c_adapter.
1351 */
1352struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1353{
1354 return (dev->type == &i2c_adapter_type)
1355 ? to_i2c_adapter(dev)
1356 : NULL;
1357}
1358EXPORT_SYMBOL(i2c_verify_adapter);
1359
Jean Delvare2bb50952009-09-18 22:45:46 +02001360#ifdef CONFIG_I2C_COMPAT
1361static struct class_compat *i2c_adapter_compat_class;
1362#endif
1363
David Brownell9c1600e2007-05-01 23:26:31 +02001364static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1365{
1366 struct i2c_devinfo *devinfo;
1367
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001368 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001369 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1370 if (devinfo->busnum == adapter->nr
1371 && !i2c_new_device(adapter,
1372 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001373 dev_err(&adapter->dev,
1374 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001375 devinfo->board_info.addr);
1376 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001377 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001378}
1379
Wolfram Sang687b81d2013-07-11 12:56:15 +01001380/* OF support code */
1381
1382#if IS_ENABLED(CONFIG_OF)
1383static void of_i2c_register_devices(struct i2c_adapter *adap)
1384{
1385 void *result;
1386 struct device_node *node;
1387
1388 /* Only register child devices if the adapter has a node pointer set */
1389 if (!adap->dev.of_node)
1390 return;
1391
1392 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1393
1394 for_each_available_child_of_node(adap->dev.of_node, node) {
1395 struct i2c_board_info info = {};
1396 struct dev_archdata dev_ad = {};
1397 const __be32 *addr;
1398 int len;
1399
1400 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1401
1402 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1403 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1404 node->full_name);
1405 continue;
1406 }
1407
1408 addr = of_get_property(node, "reg", &len);
1409 if (!addr || (len < sizeof(int))) {
1410 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1411 node->full_name);
1412 continue;
1413 }
1414
1415 info.addr = be32_to_cpup(addr);
1416 if (info.addr > (1 << 10) - 1) {
1417 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1418 info.addr, node->full_name);
1419 continue;
1420 }
1421
Wolfram Sang687b81d2013-07-11 12:56:15 +01001422 info.of_node = of_node_get(node);
1423 info.archdata = &dev_ad;
1424
1425 if (of_get_property(node, "wakeup-source", NULL))
1426 info.flags |= I2C_CLIENT_WAKE;
1427
1428 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1429
1430 result = i2c_new_device(adap, &info);
1431 if (result == NULL) {
1432 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1433 node->full_name);
1434 of_node_put(node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001435 continue;
1436 }
1437 }
1438}
1439
1440static int of_dev_node_match(struct device *dev, void *data)
1441{
1442 return dev->of_node == data;
1443}
1444
1445/* must call put_device() when done with returned i2c_client device */
1446struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1447{
1448 struct device *dev;
1449
1450 dev = bus_find_device(&i2c_bus_type, NULL, node,
1451 of_dev_node_match);
1452 if (!dev)
1453 return NULL;
1454
1455 return i2c_verify_client(dev);
1456}
1457EXPORT_SYMBOL(of_find_i2c_device_by_node);
1458
1459/* must call put_device() when done with returned i2c_adapter device */
1460struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1461{
1462 struct device *dev;
1463
1464 dev = bus_find_device(&i2c_bus_type, NULL, node,
1465 of_dev_node_match);
1466 if (!dev)
1467 return NULL;
1468
1469 return i2c_verify_adapter(dev);
1470}
1471EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1472#else
1473static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1474#endif /* CONFIG_OF */
1475
Jean Delvare69b00892009-12-06 17:06:27 +01001476static int i2c_do_add_adapter(struct i2c_driver *driver,
1477 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001478{
Jean Delvare4735c982008-07-14 22:38:36 +02001479 /* Detect supported devices on that bus, and instantiate them */
1480 i2c_detect(adap, driver);
1481
1482 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001483 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001484 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1485 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001486 dev_warn(&adap->dev, "Please use another way to instantiate "
1487 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001488 /* We ignore the return code; if it fails, too bad */
1489 driver->attach_adapter(adap);
1490 }
1491 return 0;
1492}
1493
Jean Delvare69b00892009-12-06 17:06:27 +01001494static int __process_new_adapter(struct device_driver *d, void *data)
1495{
1496 return i2c_do_add_adapter(to_i2c_driver(d), data);
1497}
1498
David Brownell6e13e642007-05-01 23:26:31 +02001499static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Jean Delvared6703282010-08-11 18:20:59 +02001501 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David Brownell1d0b19c2008-10-14 17:30:05 +02001503 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001504 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1505 res = -EAGAIN;
1506 goto out_list;
1507 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001508
Jean Delvare2236baa2010-11-15 22:40:38 +01001509 /* Sanity checks */
1510 if (unlikely(adap->name[0] == '\0')) {
1511 pr_err("i2c-core: Attempt to register an adapter with "
1512 "no name!\n");
1513 return -EINVAL;
1514 }
1515 if (unlikely(!adap->algo)) {
1516 pr_err("i2c-core: Attempt to register adapter '%s' with "
1517 "no algo!\n", adap->name);
1518 return -EINVAL;
1519 }
1520
Mika Kuoppala194684e2009-12-06 17:06:22 +01001521 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001522 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001523 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Jean Delvare8fcfef62009-03-28 21:34:43 +01001525 /* Set default timeout to 1 second if not already set */
1526 if (adap->timeout == 0)
1527 adap->timeout = HZ;
1528
Kay Sievers27d9c182009-01-07 14:29:16 +01001529 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001530 adap->dev.bus = &i2c_bus_type;
1531 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001532 res = device_register(&adap->dev);
1533 if (res)
1534 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001536 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1537
Jean Delvare2bb50952009-09-18 22:45:46 +02001538#ifdef CONFIG_I2C_COMPAT
1539 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1540 adap->dev.parent);
1541 if (res)
1542 dev_warn(&adap->dev,
1543 "Failed to create compatibility class link\n");
1544#endif
1545
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301546 /* bus recovery specific initialization */
1547 if (adap->bus_recovery_info) {
1548 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1549
1550 if (!bri->recover_bus) {
1551 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1552 adap->bus_recovery_info = NULL;
1553 goto exit_recovery;
1554 }
1555
1556 /* Generic GPIO recovery */
1557 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1558 if (!gpio_is_valid(bri->scl_gpio)) {
1559 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1560 adap->bus_recovery_info = NULL;
1561 goto exit_recovery;
1562 }
1563
1564 if (gpio_is_valid(bri->sda_gpio))
1565 bri->get_sda = get_sda_gpio_value;
1566 else
1567 bri->get_sda = NULL;
1568
1569 bri->get_scl = get_scl_gpio_value;
1570 bri->set_scl = set_scl_gpio_value;
1571 } else if (!bri->set_scl || !bri->get_scl) {
1572 /* Generic SCL recovery */
1573 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1574 adap->bus_recovery_info = NULL;
1575 }
1576 }
1577
1578exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001579 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001580 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001581 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001582 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001583
David Brownell6e13e642007-05-01 23:26:31 +02001584 if (adap->nr < __i2c_first_dynamic_bus_num)
1585 i2c_scan_static_board_info(adap);
1586
Jean Delvare4735c982008-07-14 22:38:36 +02001587 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001588 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001589 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001590 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001591
1592 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001593
Jean Delvareb119c6c2006-08-15 18:26:30 +02001594out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001595 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001596 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001597 mutex_unlock(&core_lock);
1598 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
David Brownell6e13e642007-05-01 23:26:31 +02001601/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001602 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1603 * @adap: the adapter to register (with adap->nr initialized)
1604 * Context: can sleep
1605 *
1606 * See i2c_add_numbered_adapter() for details.
1607 */
1608static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1609{
1610 int id;
1611
1612 mutex_lock(&core_lock);
1613 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1614 GFP_KERNEL);
1615 mutex_unlock(&core_lock);
1616 if (id < 0)
1617 return id == -ENOSPC ? -EBUSY : id;
1618
1619 return i2c_register_adapter(adap);
1620}
1621
1622/**
David Brownell6e13e642007-05-01 23:26:31 +02001623 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1624 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001625 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001626 *
1627 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001628 * doesn't matter or when its bus number is specified by an dt alias.
1629 * Examples of bases when the bus number doesn't matter: I2C adapters
1630 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001631 *
1632 * When this returns zero, a new bus number was allocated and stored
1633 * in adap->nr, and the specified adapter became available for clients.
1634 * Otherwise, a negative errno value is returned.
1635 */
1636int i2c_add_adapter(struct i2c_adapter *adapter)
1637{
Doug Andersonee5c2742013-03-01 08:57:31 -08001638 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001639 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001640
Doug Andersonee5c2742013-03-01 08:57:31 -08001641 if (dev->of_node) {
1642 id = of_alias_get_id(dev->of_node, "i2c");
1643 if (id >= 0) {
1644 adapter->nr = id;
1645 return __i2c_add_numbered_adapter(adapter);
1646 }
1647 }
1648
Jean Delvarecaada322008-01-27 18:14:49 +01001649 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001650 id = idr_alloc(&i2c_adapter_idr, adapter,
1651 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001652 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001653 if (id < 0)
1654 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001655
1656 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001657
David Brownell6e13e642007-05-01 23:26:31 +02001658 return i2c_register_adapter(adapter);
1659}
1660EXPORT_SYMBOL(i2c_add_adapter);
1661
1662/**
1663 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1664 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001665 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001666 *
1667 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001668 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1669 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001670 * is used to properly configure I2C devices.
1671 *
Grant Likely488bf312011-07-25 17:49:43 +02001672 * If the requested bus number is set to -1, then this function will behave
1673 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1674 *
David Brownell6e13e642007-05-01 23:26:31 +02001675 * If no devices have pre-been declared for this bus, then be sure to
1676 * register the adapter before any dynamically allocated ones. Otherwise
1677 * the required bus ID may not be available.
1678 *
1679 * When this returns zero, the specified adapter became available for
1680 * clients using the bus number provided in adap->nr. Also, the table
1681 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1682 * and the appropriate driver model device nodes are created. Otherwise, a
1683 * negative errno value is returned.
1684 */
1685int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1686{
Grant Likely488bf312011-07-25 17:49:43 +02001687 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1688 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001689
Doug Andersonee5c2742013-03-01 08:57:31 -08001690 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001691}
1692EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1693
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001694static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001695 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001696{
Jean Delvare4735c982008-07-14 22:38:36 +02001697 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001698
Jean Delvareacec2112009-03-28 21:34:40 +01001699 /* Remove the devices we created ourselves as the result of hardware
1700 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001701 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1702 if (client->adapter == adapter) {
1703 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1704 client->name, client->addr);
1705 list_del(&client->detected);
1706 i2c_unregister_device(client);
1707 }
1708 }
Jean Delvare026526f2008-01-27 18:14:49 +01001709}
1710
Jean Delvaree549c2b2009-06-19 16:58:19 +02001711static int __unregister_client(struct device *dev, void *dummy)
1712{
1713 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001714 if (client && strcmp(client->name, "dummy"))
1715 i2c_unregister_device(client);
1716 return 0;
1717}
1718
1719static int __unregister_dummy(struct device *dev, void *dummy)
1720{
1721 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001722 if (client)
1723 i2c_unregister_device(client);
1724 return 0;
1725}
1726
Jean Delvare69b00892009-12-06 17:06:27 +01001727static int __process_removed_adapter(struct device_driver *d, void *data)
1728{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001729 i2c_do_del_adapter(to_i2c_driver(d), data);
1730 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001731}
1732
David Brownelld64f73b2007-07-12 14:12:28 +02001733/**
1734 * i2c_del_adapter - unregister I2C adapter
1735 * @adap: the adapter being unregistered
1736 * Context: can sleep
1737 *
1738 * This unregisters an I2C adapter which was previously registered
1739 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1740 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001741void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001743 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001744 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001747 mutex_lock(&core_lock);
1748 found = idr_find(&i2c_adapter_idr, adap->nr);
1749 mutex_unlock(&core_lock);
1750 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001751 pr_debug("i2c-core: attempting to delete unregistered "
1752 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001753 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Lan Tianyu5d98e612014-05-20 20:59:23 +08001756 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001757 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001758 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001759 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001760 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001761 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001763 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001764 mutex_lock_nested(&adap->userspace_clients_lock,
1765 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001766 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1767 detected) {
1768 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1769 client->addr);
1770 list_del(&client->detected);
1771 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001772 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001773 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001774
Jean Delvaree549c2b2009-06-19 16:58:19 +02001775 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001776 * check the returned value. This is a two-pass process, because
1777 * we can't remove the dummy devices during the first pass: they
1778 * could have been instantiated by real devices wishing to clean
1779 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001780 device_for_each_child(&adap->dev, NULL, __unregister_client);
1781 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Jean Delvare2bb50952009-09-18 22:45:46 +02001783#ifdef CONFIG_I2C_COMPAT
1784 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1785 adap->dev.parent);
1786#endif
1787
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001788 /* device name is gone after device_unregister */
1789 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /* clean up the sysfs representation */
1792 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* wait for sysfs to drop all references */
1796 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David Brownell6e13e642007-05-01 23:26:31 +02001798 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001799 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001801 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001803 /* Clear the device structure in case this adapter is ever going to be
1804 added again */
1805 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
David Brownellc0564602007-05-01 23:26:31 +02001807EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
David Brownell7b4fbc52007-05-01 23:26:30 +02001809/* ------------------------------------------------------------------------- */
1810
Jean Delvare7ae31482011-03-20 14:50:52 +01001811int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1812{
1813 int res;
1814
1815 mutex_lock(&core_lock);
1816 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1817 mutex_unlock(&core_lock);
1818
1819 return res;
1820}
1821EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1822
Jean Delvare69b00892009-12-06 17:06:27 +01001823static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001824{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001825 if (dev->type != &i2c_adapter_type)
1826 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001827 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001828}
1829
David Brownell7b4fbc52007-05-01 23:26:30 +02001830/*
1831 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001832 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
1834
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001835int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001837 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David Brownell1d0b19c2008-10-14 17:30:05 +02001839 /* Can't register until after driver model init */
1840 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1841 return -EAGAIN;
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001844 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Jean Delvare729d6dd2009-06-19 16:58:18 +02001847 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001848 * will have called probe() for all matching-but-unbound devices.
1849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 res = driver_register(&driver->driver);
1851 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001852 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001853
Mark Brownf4e8db32011-01-14 22:03:50 +01001854 /* Drivers should switch to dev_pm_ops instead. */
1855 if (driver->suspend)
1856 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1857 driver->driver.name);
1858 if (driver->resume)
1859 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1860 driver->driver.name);
1861
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001862 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Jean Delvare4735c982008-07-14 22:38:36 +02001864 INIT_LIST_HEAD(&driver->clients);
1865 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001866 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001867
Jean Delvare7eebcb72006-02-05 23:28:21 +01001868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001870EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Jean Delvare69b00892009-12-06 17:06:27 +01001872static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001873{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001874 if (dev->type == &i2c_adapter_type)
1875 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1876 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001877}
1878
David Brownella1d9e6e2007-05-01 23:26:30 +02001879/**
1880 * i2c_del_driver - unregister I2C driver
1881 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001882 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001883 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001884void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Jean Delvare7ae31482011-03-20 14:50:52 +01001886 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001889 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
David Brownellc0564602007-05-01 23:26:31 +02001891EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
David Brownell7b4fbc52007-05-01 23:26:30 +02001893/* ------------------------------------------------------------------------- */
1894
Jean Delvaree48d3312008-01-27 18:14:48 +01001895/**
1896 * i2c_use_client - increments the reference count of the i2c client structure
1897 * @client: the client being referenced
1898 *
1899 * Each live reference to a client should be refcounted. The driver model does
1900 * that automatically as part of driver binding, so that most drivers don't
1901 * need to do this explicitly: they hold a reference until they're unbound
1902 * from the device.
1903 *
1904 * A pointer to the client with the incremented reference counter is returned.
1905 */
1906struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
David Brownell6ea438e2008-07-14 22:38:24 +02001908 if (client && get_device(&client->dev))
1909 return client;
1910 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
David Brownellc0564602007-05-01 23:26:31 +02001912EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Jean Delvaree48d3312008-01-27 18:14:48 +01001914/**
1915 * i2c_release_client - release a use of the i2c client structure
1916 * @client: the client being no longer referenced
1917 *
1918 * Must be called when a user of a client is finished with it.
1919 */
1920void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
David Brownell6ea438e2008-07-14 22:38:24 +02001922 if (client)
1923 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
David Brownellc0564602007-05-01 23:26:31 +02001925EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
David Brownell9b766b82008-01-27 18:14:51 +01001927struct i2c_cmd_arg {
1928 unsigned cmd;
1929 void *arg;
1930};
1931
1932static int i2c_cmd(struct device *dev, void *_arg)
1933{
1934 struct i2c_client *client = i2c_verify_client(dev);
1935 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001936 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001937
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001938 if (!client || !client->dev.driver)
1939 return 0;
1940
1941 driver = to_i2c_driver(client->dev.driver);
1942 if (driver->command)
1943 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001944 return 0;
1945}
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1948{
David Brownell9b766b82008-01-27 18:14:51 +01001949 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
David Brownell9b766b82008-01-27 18:14:51 +01001951 cmd_arg.cmd = cmd;
1952 cmd_arg.arg = arg;
1953 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
David Brownellc0564602007-05-01 23:26:31 +02001955EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957static int __init i2c_init(void)
1958{
1959 int retval;
1960
1961 retval = bus_register(&i2c_bus_type);
1962 if (retval)
1963 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001964#ifdef CONFIG_I2C_COMPAT
1965 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1966 if (!i2c_adapter_compat_class) {
1967 retval = -ENOMEM;
1968 goto bus_err;
1969 }
1970#endif
David Brownelle9f13732008-01-27 18:14:52 +01001971 retval = i2c_add_driver(&dummy_driver);
1972 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001973 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001974 return 0;
1975
Jean Delvare2bb50952009-09-18 22:45:46 +02001976class_err:
1977#ifdef CONFIG_I2C_COMPAT
1978 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001979bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001980#endif
David Brownelle9f13732008-01-27 18:14:52 +01001981 bus_unregister(&i2c_bus_type);
1982 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985static void __exit i2c_exit(void)
1986{
David Brownelle9f13732008-01-27 18:14:52 +01001987 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001988#ifdef CONFIG_I2C_COMPAT
1989 class_compat_unregister(i2c_adapter_compat_class);
1990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001992 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
David Brownella10f9e72008-10-14 17:30:06 +02001995/* We must initialize early, because some subsystems register i2c drivers
1996 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1997 */
1998postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999module_exit(i2c_exit);
2000
2001/* ----------------------------------------------------
2002 * the functional interface to the i2c busses.
2003 * ----------------------------------------------------
2004 */
2005
David Brownella1cdeda2008-07-14 22:38:24 +02002006/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002007 * __i2c_transfer - unlocked flavor of i2c_transfer
2008 * @adap: Handle to I2C bus
2009 * @msgs: One or more messages to execute before STOP is issued to
2010 * terminate the operation; each message begins with a START.
2011 * @num: Number of messages to be executed.
2012 *
2013 * Returns negative errno, else the number of messages executed.
2014 *
2015 * Adapter lock must be held when calling this function. No debug logging
2016 * takes place. adap->algo->master_xfer existence isn't checked.
2017 */
2018int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2019{
2020 unsigned long orig_jiffies;
2021 int ret, try;
2022
David Howellsd9a83d62014-03-06 13:35:59 +00002023 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2024 * enabled. This is an efficient way of keeping the for-loop from
2025 * being executed when not needed.
2026 */
2027 if (static_key_false(&i2c_trace_msg)) {
2028 int i;
2029 for (i = 0; i < num; i++)
2030 if (msgs[i].flags & I2C_M_RD)
2031 trace_i2c_read(adap, &msgs[i], i);
2032 else
2033 trace_i2c_write(adap, &msgs[i], i);
2034 }
2035
Jean Delvareb37d2a32012-06-29 07:47:19 -03002036 /* Retry automatically on arbitration loss */
2037 orig_jiffies = jiffies;
2038 for (ret = 0, try = 0; try <= adap->retries; try++) {
2039 ret = adap->algo->master_xfer(adap, msgs, num);
2040 if (ret != -EAGAIN)
2041 break;
2042 if (time_after(jiffies, orig_jiffies + adap->timeout))
2043 break;
2044 }
2045
David Howellsd9a83d62014-03-06 13:35:59 +00002046 if (static_key_false(&i2c_trace_msg)) {
2047 int i;
2048 for (i = 0; i < ret; i++)
2049 if (msgs[i].flags & I2C_M_RD)
2050 trace_i2c_reply(adap, &msgs[i], i);
2051 trace_i2c_result(adap, i, ret);
2052 }
2053
Jean Delvareb37d2a32012-06-29 07:47:19 -03002054 return ret;
2055}
2056EXPORT_SYMBOL(__i2c_transfer);
2057
2058/**
David Brownella1cdeda2008-07-14 22:38:24 +02002059 * i2c_transfer - execute a single or combined I2C message
2060 * @adap: Handle to I2C bus
2061 * @msgs: One or more messages to execute before STOP is issued to
2062 * terminate the operation; each message begins with a START.
2063 * @num: Number of messages to be executed.
2064 *
2065 * Returns negative errno, else the number of messages executed.
2066 *
2067 * Note that there is no requirement that each message be sent to
2068 * the same slave address, although that is the most common model.
2069 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002070int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002072 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
David Brownella1cdeda2008-07-14 22:38:24 +02002074 /* REVISIT the fault reporting model here is weak:
2075 *
2076 * - When we get an error after receiving N bytes from a slave,
2077 * there is no way to report "N".
2078 *
2079 * - When we get a NAK after transmitting N bytes to a slave,
2080 * there is no way to report "N" ... or to let the master
2081 * continue executing the rest of this combined message, if
2082 * that's the appropriate response.
2083 *
2084 * - When for example "num" is two and we successfully complete
2085 * the first message but get an error part way through the
2086 * second, it's unclear whether that should be reported as
2087 * one (discarding status on the second message) or errno
2088 * (discarding status on the first one).
2089 */
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (adap->algo->master_xfer) {
2092#ifdef DEBUG
2093 for (ret = 0; ret < num; ret++) {
2094 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002095 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2096 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2097 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099#endif
2100
Mike Rapoportcea443a2008-01-27 18:14:50 +01002101 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002102 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002103 if (!ret)
2104 /* I2C activity is ongoing. */
2105 return -EAGAIN;
2106 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002107 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002108 }
2109
Jean Delvareb37d2a32012-06-29 07:47:19 -03002110 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002111 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 return ret;
2114 } else {
2115 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002116 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118}
David Brownellc0564602007-05-01 23:26:31 +02002119EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
David Brownella1cdeda2008-07-14 22:38:24 +02002121/**
2122 * i2c_master_send - issue a single I2C message in master transmit mode
2123 * @client: Handle to slave device
2124 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002125 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002126 *
2127 * Returns negative errno, or else the number of bytes written.
2128 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002129int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002132 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 struct i2c_msg msg;
2134
Jean Delvare815f55f2005-05-07 22:58:46 +02002135 msg.addr = client->addr;
2136 msg.flags = client->flags & I2C_M_TEN;
2137 msg.len = count;
2138 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002139
Jean Delvare815f55f2005-05-07 22:58:46 +02002140 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002142 /*
2143 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2144 * transmitted, else error code.
2145 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002146 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
David Brownellc0564602007-05-01 23:26:31 +02002148EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
David Brownella1cdeda2008-07-14 22:38:24 +02002150/**
2151 * i2c_master_recv - issue a single I2C message in master receive mode
2152 * @client: Handle to slave device
2153 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002154 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002155 *
2156 * Returns negative errno, or else the number of bytes read.
2157 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002158int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Farid Hammane7225acf2010-05-21 18:40:58 +02002160 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct i2c_msg msg;
2162 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jean Delvare815f55f2005-05-07 22:58:46 +02002164 msg.addr = client->addr;
2165 msg.flags = client->flags & I2C_M_TEN;
2166 msg.flags |= I2C_M_RD;
2167 msg.len = count;
2168 msg.buf = buf;
2169
2170 ret = i2c_transfer(adap, &msg, 1);
2171
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002172 /*
2173 * If everything went ok (i.e. 1 msg received), return #bytes received,
2174 * else error code.
2175 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002176 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
David Brownellc0564602007-05-01 23:26:31 +02002178EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180/* ----------------------------------------------------
2181 * the i2c address scanning function
2182 * Will not work for 10-bit addresses!
2183 * ----------------------------------------------------
2184 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002185
Jean Delvare63e4e802010-06-03 11:33:51 +02002186/*
2187 * Legacy default probe function, mostly relevant for SMBus. The default
2188 * probe method is a quick write, but it is known to corrupt the 24RF08
2189 * EEPROMs due to a state machine bug, and could also irreversibly
2190 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2191 * we use a short byte read instead. Also, some bus drivers don't implement
2192 * quick write, so we fallback to a byte read in that case too.
2193 * On x86, there is another special case for FSC hardware monitoring chips,
2194 * which want regular byte reads (address 0x73.) Fortunately, these are the
2195 * only known chips using this I2C address on PC hardware.
2196 * Returns 1 if probe succeeded, 0 if not.
2197 */
2198static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2199{
2200 int err;
2201 union i2c_smbus_data dummy;
2202
2203#ifdef CONFIG_X86
2204 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2205 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2206 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2207 I2C_SMBUS_BYTE_DATA, &dummy);
2208 else
2209#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002210 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2211 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002212 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2213 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002214 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2215 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2216 I2C_SMBUS_BYTE, &dummy);
2217 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002218 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2219 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002220 err = -EOPNOTSUPP;
2221 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002222
2223 return err >= 0;
2224}
2225
Jean Delvareccfbbd02009-12-06 17:06:25 +01002226static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002227 struct i2c_driver *driver)
2228{
2229 struct i2c_board_info info;
2230 struct i2c_adapter *adapter = temp_client->adapter;
2231 int addr = temp_client->addr;
2232 int err;
2233
2234 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002235 err = i2c_check_addr_validity(addr);
2236 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002237 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2238 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002239 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002240 }
2241
2242 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002243 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002244 return 0;
2245
Jean Delvareccfbbd02009-12-06 17:06:25 +01002246 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002247 if (!i2c_default_probe(adapter, addr))
2248 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002249
2250 /* Finally call the custom detection function */
2251 memset(&info, 0, sizeof(struct i2c_board_info));
2252 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002253 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002254 if (err) {
2255 /* -ENODEV is returned if the detection fails. We catch it
2256 here as this isn't an error. */
2257 return err == -ENODEV ? 0 : err;
2258 }
2259
2260 /* Consistency check */
2261 if (info.type[0] == '\0') {
2262 dev_err(&adapter->dev, "%s detection function provided "
2263 "no name for 0x%x\n", driver->driver.name,
2264 addr);
2265 } else {
2266 struct i2c_client *client;
2267
2268 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002269 if (adapter->class & I2C_CLASS_DEPRECATED)
2270 dev_warn(&adapter->dev,
2271 "This adapter will soon drop class based instantiation of devices. "
2272 "Please make sure client 0x%02x gets instantiated by other means. "
2273 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2274 info.addr);
2275
Jean Delvare4735c982008-07-14 22:38:36 +02002276 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2277 info.type, info.addr);
2278 client = i2c_new_device(adapter, &info);
2279 if (client)
2280 list_add_tail(&client->detected, &driver->clients);
2281 else
2282 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2283 info.type, info.addr);
2284 }
2285 return 0;
2286}
2287
2288static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2289{
Jean Delvarec3813d62009-12-14 21:17:25 +01002290 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002291 struct i2c_client *temp_client;
2292 int i, err = 0;
2293 int adap_id = i2c_adapter_id(adapter);
2294
Jean Delvarec3813d62009-12-14 21:17:25 +01002295 address_list = driver->address_list;
2296 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002297 return 0;
2298
Wolfram Sang45552272014-07-10 13:46:21 +02002299 /* Warn that the adapter lost class based instantiation */
2300 if (adapter->class == I2C_CLASS_DEPRECATED) {
2301 dev_dbg(&adapter->dev,
2302 "This adapter dropped support for I2C classes and "
2303 "won't auto-detect %s devices anymore. If you need it, check "
2304 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2305 driver->driver.name);
2306 return 0;
2307 }
2308
Jean Delvare51b54ba2010-10-24 18:16:58 +02002309 /* Stop here if the classes do not match */
2310 if (!(adapter->class & driver->class))
2311 return 0;
2312
Jean Delvare4735c982008-07-14 22:38:36 +02002313 /* Set up a temporary client to help detect callback */
2314 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2315 if (!temp_client)
2316 return -ENOMEM;
2317 temp_client->adapter = adapter;
2318
Jean Delvarec3813d62009-12-14 21:17:25 +01002319 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002320 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002321 "addr 0x%02x\n", adap_id, address_list[i]);
2322 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002323 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002324 if (unlikely(err))
2325 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002326 }
2327
Jean Delvare4735c982008-07-14 22:38:36 +02002328 kfree(temp_client);
2329 return err;
2330}
2331
Jean Delvared44f19d2010-08-11 18:20:57 +02002332int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2333{
2334 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2335 I2C_SMBUS_QUICK, NULL) >= 0;
2336}
2337EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2338
Jean Delvare12b5053a2007-05-01 23:26:31 +02002339struct i2c_client *
2340i2c_new_probed_device(struct i2c_adapter *adap,
2341 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002342 unsigned short const *addr_list,
2343 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002344{
2345 int i;
2346
Jean Delvare8031d792010-08-11 18:21:00 +02002347 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002348 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002349
Jean Delvare12b5053a2007-05-01 23:26:31 +02002350 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2351 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002352 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002353 dev_warn(&adap->dev, "Invalid 7-bit address "
2354 "0x%02x\n", addr_list[i]);
2355 continue;
2356 }
2357
2358 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002359 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002360 dev_dbg(&adap->dev, "Address 0x%02x already in "
2361 "use, not probing\n", addr_list[i]);
2362 continue;
2363 }
2364
Jean Delvare63e4e802010-06-03 11:33:51 +02002365 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002366 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002367 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002368 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002369
2370 if (addr_list[i] == I2C_CLIENT_END) {
2371 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2372 return NULL;
2373 }
2374
2375 info->addr = addr_list[i];
2376 return i2c_new_device(adap, info);
2377}
2378EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2379
Jean Delvared735b342011-03-20 14:50:52 +01002380struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002383
Jean Delvarecaada322008-01-27 18:14:49 +01002384 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002385 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002386 if (adapter && !try_module_get(adapter->owner))
2387 adapter = NULL;
2388
Jean Delvarecaada322008-01-27 18:14:49 +01002389 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002390 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
David Brownellc0564602007-05-01 23:26:31 +02002392EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394void i2c_put_adapter(struct i2c_adapter *adap)
2395{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002396 if (adap)
2397 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
David Brownellc0564602007-05-01 23:26:31 +02002399EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401/* The SMBus parts */
2402
David Brownell438d6c22006-12-10 21:21:31 +01002403#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002404static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002407
Farid Hammane7225acf2010-05-21 18:40:58 +02002408 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002409 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 data = data ^ POLY;
2411 data = data << 1;
2412 }
2413 return (u8)(data >> 8);
2414}
2415
Jean Delvare421ef472005-10-26 21:28:55 +02002416/* Incremental CRC8 over count bytes in the array pointed to by p */
2417static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 int i;
2420
Farid Hammane7225acf2010-05-21 18:40:58 +02002421 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002422 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return crc;
2424}
2425
Jean Delvare421ef472005-10-26 21:28:55 +02002426/* Assume a 7-bit address, which is reasonable for SMBus */
2427static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Jean Delvare421ef472005-10-26 21:28:55 +02002429 /* The address will be sent first */
2430 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2431 pec = i2c_smbus_pec(pec, &addr, 1);
2432
2433 /* The data buffer follows */
2434 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Jean Delvare421ef472005-10-26 21:28:55 +02002437/* Used for write only transactions */
2438static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Jean Delvare421ef472005-10-26 21:28:55 +02002440 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2441 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
Jean Delvare421ef472005-10-26 21:28:55 +02002444/* Return <0 on CRC error
2445 If there was a write before this read (most cases) we need to take the
2446 partial CRC from the write part into account.
2447 Note that this function does modify the message (we need to decrease the
2448 message length to hide the CRC byte from the caller). */
2449static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Jean Delvare421ef472005-10-26 21:28:55 +02002451 u8 rpec = msg->buf[--msg->len];
2452 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (rpec != cpec) {
2455 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2456 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002457 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
David Brownell438d6c22006-12-10 21:21:31 +01002459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
David Brownella1cdeda2008-07-14 22:38:24 +02002462/**
2463 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2464 * @client: Handle to slave device
2465 *
2466 * This executes the SMBus "receive byte" protocol, returning negative errno
2467 * else the byte received from the device.
2468 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002469s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002472 int status;
2473
2474 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2475 I2C_SMBUS_READ, 0,
2476 I2C_SMBUS_BYTE, &data);
2477 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
David Brownellc0564602007-05-01 23:26:31 +02002479EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
David Brownella1cdeda2008-07-14 22:38:24 +02002481/**
2482 * i2c_smbus_write_byte - SMBus "send byte" protocol
2483 * @client: Handle to slave device
2484 * @value: Byte to be sent
2485 *
2486 * This executes the SMBus "send byte" protocol, returning negative errno
2487 * else zero on success.
2488 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002489s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Farid Hammane7225acf2010-05-21 18:40:58 +02002491 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002492 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
David Brownellc0564602007-05-01 23:26:31 +02002494EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
David Brownella1cdeda2008-07-14 22:38:24 +02002496/**
2497 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2498 * @client: Handle to slave device
2499 * @command: Byte interpreted by slave
2500 *
2501 * This executes the SMBus "read byte" protocol, returning negative errno
2502 * else a data byte received from the device.
2503 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002504s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002507 int status;
2508
2509 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2510 I2C_SMBUS_READ, command,
2511 I2C_SMBUS_BYTE_DATA, &data);
2512 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
David Brownellc0564602007-05-01 23:26:31 +02002514EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
David Brownella1cdeda2008-07-14 22:38:24 +02002516/**
2517 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2518 * @client: Handle to slave device
2519 * @command: Byte interpreted by slave
2520 * @value: Byte being written
2521 *
2522 * This executes the SMBus "write byte" protocol, returning negative errno
2523 * else zero on success.
2524 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002525s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2526 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
2528 union i2c_smbus_data data;
2529 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002530 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2531 I2C_SMBUS_WRITE, command,
2532 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
David Brownellc0564602007-05-01 23:26:31 +02002534EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
David Brownella1cdeda2008-07-14 22:38:24 +02002536/**
2537 * i2c_smbus_read_word_data - SMBus "read word" protocol
2538 * @client: Handle to slave device
2539 * @command: Byte interpreted by slave
2540 *
2541 * This executes the SMBus "read word" protocol, returning negative errno
2542 * else a 16-bit unsigned "word" received from the device.
2543 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002544s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002547 int status;
2548
2549 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2550 I2C_SMBUS_READ, command,
2551 I2C_SMBUS_WORD_DATA, &data);
2552 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
David Brownellc0564602007-05-01 23:26:31 +02002554EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
David Brownella1cdeda2008-07-14 22:38:24 +02002556/**
2557 * i2c_smbus_write_word_data - SMBus "write word" protocol
2558 * @client: Handle to slave device
2559 * @command: Byte interpreted by slave
2560 * @value: 16-bit "word" being written
2561 *
2562 * This executes the SMBus "write word" protocol, returning negative errno
2563 * else zero on success.
2564 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002565s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2566 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 union i2c_smbus_data data;
2569 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002570 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2571 I2C_SMBUS_WRITE, command,
2572 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
David Brownellc0564602007-05-01 23:26:31 +02002574EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
David Brownella64ec072007-10-13 23:56:31 +02002576/**
David Brownella1cdeda2008-07-14 22:38:24 +02002577 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002578 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002579 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002580 * @values: Byte array into which data will be read; big enough to hold
2581 * the data returned by the slave. SMBus allows at most 32 bytes.
2582 *
David Brownella1cdeda2008-07-14 22:38:24 +02002583 * This executes the SMBus "block read" protocol, returning negative errno
2584 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002585 *
2586 * Note that using this function requires that the client's adapter support
2587 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2588 * support this; its emulation through I2C messaging relies on a specific
2589 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2590 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002591s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002592 u8 *values)
2593{
2594 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002595 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002596
David Brownell24a5bb72008-07-14 22:38:23 +02002597 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2598 I2C_SMBUS_READ, command,
2599 I2C_SMBUS_BLOCK_DATA, &data);
2600 if (status)
2601 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002602
2603 memcpy(values, &data.block[1], data.block[0]);
2604 return data.block[0];
2605}
2606EXPORT_SYMBOL(i2c_smbus_read_block_data);
2607
David Brownella1cdeda2008-07-14 22:38:24 +02002608/**
2609 * i2c_smbus_write_block_data - SMBus "block write" protocol
2610 * @client: Handle to slave device
2611 * @command: Byte interpreted by slave
2612 * @length: Size of data block; SMBus allows at most 32 bytes
2613 * @values: Byte array which will be written.
2614 *
2615 * This executes the SMBus "block write" protocol, returning negative errno
2616 * else zero on success.
2617 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002618s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002619 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
2621 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (length > I2C_SMBUS_BLOCK_MAX)
2624 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002626 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002627 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2628 I2C_SMBUS_WRITE, command,
2629 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
David Brownellc0564602007-05-01 23:26:31 +02002631EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002634s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002635 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002638 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002639
Jean Delvare4b2643d2007-07-12 14:12:29 +02002640 if (length > I2C_SMBUS_BLOCK_MAX)
2641 length = I2C_SMBUS_BLOCK_MAX;
2642 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002643 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2644 I2C_SMBUS_READ, command,
2645 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2646 if (status < 0)
2647 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002648
2649 memcpy(values, &data.block[1], data.block[0]);
2650 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
David Brownellc0564602007-05-01 23:26:31 +02002652EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Jean Delvare0cc43a12011-01-10 22:11:23 +01002654s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002655 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002656{
2657 union i2c_smbus_data data;
2658
2659 if (length > I2C_SMBUS_BLOCK_MAX)
2660 length = I2C_SMBUS_BLOCK_MAX;
2661 data.block[0] = length;
2662 memcpy(data.block + 1, values, length);
2663 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2664 I2C_SMBUS_WRITE, command,
2665 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2666}
David Brownellc0564602007-05-01 23:26:31 +02002667EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002668
David Brownell438d6c22006-12-10 21:21:31 +01002669/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002671static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2672 unsigned short flags,
2673 char read_write, u8 command, int size,
2674 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 /* So we need to generate a series of msgs. In the case of writing, we
2677 need to use only one message; when reading, we need two. We initialize
2678 most things with sane defaults, to keep the code below somewhat
2679 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002680 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2681 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002682 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002684 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002685 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002686 struct i2c_msg msg[2] = {
2687 {
2688 .addr = addr,
2689 .flags = flags,
2690 .len = 1,
2691 .buf = msgbuf0,
2692 }, {
2693 .addr = addr,
2694 .flags = flags | I2C_M_RD,
2695 .len = 0,
2696 .buf = msgbuf1,
2697 },
2698 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002701 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 case I2C_SMBUS_QUICK:
2703 msg[0].len = 0;
2704 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002705 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2706 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 num = 1;
2708 break;
2709 case I2C_SMBUS_BYTE:
2710 if (read_write == I2C_SMBUS_READ) {
2711 /* Special case: only a read! */
2712 msg[0].flags = I2C_M_RD | flags;
2713 num = 1;
2714 }
2715 break;
2716 case I2C_SMBUS_BYTE_DATA:
2717 if (read_write == I2C_SMBUS_READ)
2718 msg[1].len = 1;
2719 else {
2720 msg[0].len = 2;
2721 msgbuf0[1] = data->byte;
2722 }
2723 break;
2724 case I2C_SMBUS_WORD_DATA:
2725 if (read_write == I2C_SMBUS_READ)
2726 msg[1].len = 2;
2727 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002728 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002730 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732 break;
2733 case I2C_SMBUS_PROC_CALL:
2734 num = 2; /* Special case */
2735 read_write = I2C_SMBUS_READ;
2736 msg[0].len = 3;
2737 msg[1].len = 2;
2738 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002739 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 break;
2741 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002743 msg[1].flags |= I2C_M_RECV_LEN;
2744 msg[1].len = 1; /* block length will be added by
2745 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 } else {
2747 msg[0].len = data->block[0] + 2;
2748 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002749 dev_err(&adapter->dev,
2750 "Invalid block write size %d\n",
2751 data->block[0]);
2752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002754 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 msgbuf0[i] = data->block[i-1];
2756 }
2757 break;
2758 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002759 num = 2; /* Another special case */
2760 read_write = I2C_SMBUS_READ;
2761 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002762 dev_err(&adapter->dev,
2763 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002764 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002765 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002766 }
2767 msg[0].len = data->block[0] + 2;
2768 for (i = 1; i < msg[0].len; i++)
2769 msgbuf0[i] = data->block[i-1];
2770 msg[1].flags |= I2C_M_RECV_LEN;
2771 msg[1].len = 1; /* block length will be added by
2772 the underlying bus driver */
2773 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 case I2C_SMBUS_I2C_BLOCK_DATA:
2775 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002776 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 } else {
2778 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002779 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002780 dev_err(&adapter->dev,
2781 "Invalid block write size %d\n",
2782 data->block[0]);
2783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785 for (i = 1; i <= data->block[0]; i++)
2786 msgbuf0[i] = data->block[i];
2787 }
2788 break;
2789 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002790 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2791 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Jean Delvare421ef472005-10-26 21:28:55 +02002794 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2795 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2796 if (i) {
2797 /* Compute PEC if first message is a write */
2798 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002799 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002800 i2c_smbus_add_pec(&msg[0]);
2801 else /* Write followed by read */
2802 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2803 }
2804 /* Ask for PEC if last message is a read */
2805 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002806 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002807 }
2808
David Brownell24a5bb72008-07-14 22:38:23 +02002809 status = i2c_transfer(adapter, msg, num);
2810 if (status < 0)
2811 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Jean Delvare421ef472005-10-26 21:28:55 +02002813 /* Check PEC if last message is a read */
2814 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002815 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2816 if (status < 0)
2817 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002818 }
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002821 switch (size) {
2822 case I2C_SMBUS_BYTE:
2823 data->byte = msgbuf0[0];
2824 break;
2825 case I2C_SMBUS_BYTE_DATA:
2826 data->byte = msgbuf1[0];
2827 break;
2828 case I2C_SMBUS_WORD_DATA:
2829 case I2C_SMBUS_PROC_CALL:
2830 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2831 break;
2832 case I2C_SMBUS_I2C_BLOCK_DATA:
2833 for (i = 0; i < data->block[0]; i++)
2834 data->block[i+1] = msgbuf1[i];
2835 break;
2836 case I2C_SMBUS_BLOCK_DATA:
2837 case I2C_SMBUS_BLOCK_PROC_CALL:
2838 for (i = 0; i < msgbuf1[0] + 1; i++)
2839 data->block[i] = msgbuf1[i];
2840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842 return 0;
2843}
2844
David Brownella1cdeda2008-07-14 22:38:24 +02002845/**
2846 * i2c_smbus_xfer - execute SMBus protocol operations
2847 * @adapter: Handle to I2C bus
2848 * @addr: Address of SMBus slave on that bus
2849 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2850 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2851 * @command: Byte interpreted by slave, for protocols which use such bytes
2852 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2853 * @data: Data to be read or written
2854 *
2855 * This executes an SMBus protocol operation, and returns a negative
2856 * errno code else zero on success.
2857 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002858s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002859 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002860 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002862 unsigned long orig_jiffies;
2863 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
David Howells8a325992014-03-06 13:36:06 +00002866 /* If enabled, the following two tracepoints are conditional on
2867 * read_write and protocol.
2868 */
2869 trace_smbus_write(adapter, addr, flags, read_write,
2870 command, protocol, data);
2871 trace_smbus_read(adapter, addr, flags, read_write,
2872 command, protocol);
2873
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002874 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002877 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002878
2879 /* Retry automatically on arbitration loss */
2880 orig_jiffies = jiffies;
2881 for (res = 0, try = 0; try <= adapter->retries; try++) {
2882 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2883 read_write, command,
2884 protocol, data);
2885 if (res != -EAGAIN)
2886 break;
2887 if (time_after(jiffies,
2888 orig_jiffies + adapter->timeout))
2889 break;
2890 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002891 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002893 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002894 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002895 /*
2896 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2897 * implement native support for the SMBus operation.
2898 */
2899 }
2900
David Howells8a325992014-03-06 13:36:06 +00002901 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2902 command, protocol, data);
2903
2904trace:
2905 /* If enabled, the reply tracepoint is conditional on read_write. */
2906 trace_smbus_reply(adapter, addr, flags, read_write,
2907 command, protocol, data);
2908 trace_smbus_result(adapter, addr, flags, read_write,
2909 command, protocol, res);
2910
2911 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Wolfram Sang4b1acc42014-11-18 17:04:53 +01002915int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb)
2916{
2917 int ret;
2918
2919 if (!client || !slave_cb)
2920 return -EINVAL;
2921
2922 if (!(client->flags & I2C_CLIENT_TEN)) {
2923 /* Enforce stricter address checking */
2924 ret = i2c_check_addr_validity(client->addr);
2925 if (ret)
2926 return ret;
2927 }
2928
2929 if (!client->adapter->algo->reg_slave)
2930 return -EOPNOTSUPP;
2931
2932 client->slave_cb = slave_cb;
2933
2934 i2c_lock_adapter(client->adapter);
2935 ret = client->adapter->algo->reg_slave(client);
2936 i2c_unlock_adapter(client->adapter);
2937
2938 if (ret)
2939 client->slave_cb = NULL;
2940
2941 return ret;
2942}
2943EXPORT_SYMBOL_GPL(i2c_slave_register);
2944
2945int i2c_slave_unregister(struct i2c_client *client)
2946{
2947 int ret;
2948
2949 if (!client->adapter->algo->unreg_slave)
2950 return -EOPNOTSUPP;
2951
2952 i2c_lock_adapter(client->adapter);
2953 ret = client->adapter->algo->unreg_slave(client);
2954 i2c_unlock_adapter(client->adapter);
2955
2956 if (ret == 0)
2957 client->slave_cb = NULL;
2958
2959 return ret;
2960}
2961EXPORT_SYMBOL_GPL(i2c_slave_unregister);
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2964MODULE_DESCRIPTION("I2C-Bus main module");
2965MODULE_LICENSE("GPL");