blob: 15ba6185dba585edd8ccb24efda0173a227d8431 [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 Sang687b81d2013-07-11 12:56:15 +010027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053031#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053033#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/i2c.h>
36#include <linux/init.h>
37#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010038#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010039#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060040#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010041#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020042#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010043#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010044#include <linux/hardirq.h>
45#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020046#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010047#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020048#include <linux/pm_domain.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010049#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000050#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020052#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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;
264 acpi_status ret = AE_OK;
265 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
630 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200631 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200632 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200633
Marc Pignatee354252008-08-28 08:33:22 +0200634 if (!device_can_wakeup(&client->dev))
635 device_init_wakeup(&client->dev,
636 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200637 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200638
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200639 status = of_clk_set_defaults(dev->of_node, false);
640 if (status < 0)
641 return status;
642
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200643 status = dev_pm_domain_attach(&client->dev, true);
644 if (status != -EPROBE_DEFER) {
645 status = driver->probe(client, i2c_match_id(driver->id_table,
646 client));
647 if (status)
648 dev_pm_domain_detach(&client->dev, true);
649 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100650
Hans Verkuil50c33042008-03-12 14:15:00 +0100651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static int i2c_device_remove(struct device *dev)
655{
Jean Delvare51298d12009-09-18 22:45:45 +0200656 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200657 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100658 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200659
Jean Delvare51298d12009-09-18 22:45:45 +0200660 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200661 return 0;
662
663 driver = to_i2c_driver(dev->driver);
664 if (driver->remove) {
665 dev_dbg(dev, "remove\n");
666 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200667 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100668
Laurent Pincharte4df3a02014-10-30 15:59:37 +0200669 if (dev->of_node)
670 irq_dispose_mapping(client->irq);
671
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200672 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200673 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
David Brownellf37dd802007-02-13 22:09:00 +0100676static void i2c_device_shutdown(struct device *dev)
677{
Jean Delvare51298d12009-09-18 22:45:45 +0200678 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100679 struct i2c_driver *driver;
680
Jean Delvare51298d12009-09-18 22:45:45 +0200681 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100682 return;
683 driver = to_i2c_driver(dev->driver);
684 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200685 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100686}
687
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200688#ifdef CONFIG_PM_SLEEP
689static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100690{
Jean Delvare51298d12009-09-18 22:45:45 +0200691 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100692 struct i2c_driver *driver;
693
Jean Delvare51298d12009-09-18 22:45:45 +0200694 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100695 return 0;
696 driver = to_i2c_driver(dev->driver);
697 if (!driver->suspend)
698 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200699 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100700}
701
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200702static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100703{
Jean Delvare51298d12009-09-18 22:45:45 +0200704 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100705 struct i2c_driver *driver;
706
Jean Delvare51298d12009-09-18 22:45:45 +0200707 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100708 return 0;
709 driver = to_i2c_driver(dev->driver);
710 if (!driver->resume)
711 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200712 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100713}
714
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200715static int i2c_device_pm_suspend(struct device *dev)
716{
717 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
718
Mark Brownd529de22011-01-14 22:03:49 +0100719 if (pm)
720 return pm_generic_suspend(dev);
721 else
722 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200723}
724
725static int i2c_device_pm_resume(struct device *dev)
726{
727 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200728
729 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100730 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200731 else
Mark Brownd529de22011-01-14 22:03:49 +0100732 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200733}
734
735static int i2c_device_pm_freeze(struct device *dev)
736{
737 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
738
Mark Brownd529de22011-01-14 22:03:49 +0100739 if (pm)
740 return pm_generic_freeze(dev);
741 else
742 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200743}
744
745static int i2c_device_pm_thaw(struct device *dev)
746{
747 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
748
Mark Brownd529de22011-01-14 22:03:49 +0100749 if (pm)
750 return pm_generic_thaw(dev);
751 else
752 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200753}
754
755static int i2c_device_pm_poweroff(struct device *dev)
756{
757 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
758
Mark Brownd529de22011-01-14 22:03:49 +0100759 if (pm)
760 return pm_generic_poweroff(dev);
761 else
762 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200763}
764
765static int i2c_device_pm_restore(struct device *dev)
766{
767 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200768
769 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100770 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200771 else
Mark Brownd529de22011-01-14 22:03:49 +0100772 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200773}
774#else /* !CONFIG_PM_SLEEP */
775#define i2c_device_pm_suspend NULL
776#define i2c_device_pm_resume NULL
777#define i2c_device_pm_freeze NULL
778#define i2c_device_pm_thaw NULL
779#define i2c_device_pm_poweroff NULL
780#define i2c_device_pm_restore NULL
781#endif /* !CONFIG_PM_SLEEP */
782
David Brownell9c1600e2007-05-01 23:26:31 +0200783static void i2c_client_dev_release(struct device *dev)
784{
785 kfree(to_i2c_client(dev));
786}
787
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100788static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200789show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200790{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200791 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
792 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200793}
794
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100795static ssize_t
796show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200797{
798 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800799 int len;
800
801 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
802 if (len != -ENODEV)
803 return len;
804
Jean Delvareeb8a7902008-05-18 20:49:41 +0200805 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200806}
807
Jean Delvare4f8cf822009-09-18 22:45:46 +0200808static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200809static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
810
811static struct attribute *i2c_dev_attrs[] = {
812 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200813 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200814 &dev_attr_modalias.attr,
815 NULL
816};
817
818static struct attribute_group i2c_dev_attr_group = {
819 .attrs = i2c_dev_attrs,
820};
821
822static const struct attribute_group *i2c_dev_attr_groups[] = {
823 &i2c_dev_attr_group,
824 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200825};
826
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100827static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100828 .suspend = i2c_device_pm_suspend,
829 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200830 .freeze = i2c_device_pm_freeze,
831 .thaw = i2c_device_pm_thaw,
832 .poweroff = i2c_device_pm_poweroff,
833 .restore = i2c_device_pm_restore,
834 SET_RUNTIME_PM_OPS(
835 pm_generic_runtime_suspend,
836 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200837 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200838 )
sonic zhang54067ee2009-12-14 21:17:30 +0100839};
840
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200841struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100842 .name = "i2c",
843 .match = i2c_device_match,
844 .probe = i2c_device_probe,
845 .remove = i2c_device_remove,
846 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100847 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000848};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200849EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000850
Jean Delvare51298d12009-09-18 22:45:45 +0200851static struct device_type i2c_client_type = {
852 .groups = i2c_dev_attr_groups,
853 .uevent = i2c_device_uevent,
854 .release = i2c_client_dev_release,
855};
856
David Brownell9b766b82008-01-27 18:14:51 +0100857
858/**
859 * i2c_verify_client - return parameter as i2c_client, or NULL
860 * @dev: device, probably from some driver model iterator
861 *
862 * When traversing the driver model tree, perhaps using driver model
863 * iterators like @device_for_each_child(), you can't assume very much
864 * about the nodes you find. Use this function to avoid oopses caused
865 * by wrongly treating some non-I2C device as an i2c_client.
866 */
867struct i2c_client *i2c_verify_client(struct device *dev)
868{
Jean Delvare51298d12009-09-18 22:45:45 +0200869 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100870 ? to_i2c_client(dev)
871 : NULL;
872}
873EXPORT_SYMBOL(i2c_verify_client);
874
875
Jean Delvare3a89db52010-06-03 11:33:52 +0200876/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300877 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200878static int i2c_check_client_addr_validity(const struct i2c_client *client)
879{
880 if (client->flags & I2C_CLIENT_TEN) {
881 /* 10-bit address, all values are valid */
882 if (client->addr > 0x3ff)
883 return -EINVAL;
884 } else {
885 /* 7-bit address, reject the general call address */
886 if (client->addr == 0x00 || client->addr > 0x7f)
887 return -EINVAL;
888 }
889 return 0;
890}
891
Jean Delvare656b8762010-06-03 11:33:53 +0200892/* And this is a strict address validity check, used when probing. If a
893 * device uses a reserved address, then it shouldn't be probed. 7-bit
894 * addressing is assumed, 10-bit address devices are rare and should be
895 * explicitly enumerated. */
896static int i2c_check_addr_validity(unsigned short addr)
897{
898 /*
899 * Reserved addresses per I2C specification:
900 * 0x00 General call address / START byte
901 * 0x01 CBUS address
902 * 0x02 Reserved for different bus format
903 * 0x03 Reserved for future purposes
904 * 0x04-0x07 Hs-mode master code
905 * 0x78-0x7b 10-bit slave addressing
906 * 0x7c-0x7f Reserved for future purposes
907 */
908 if (addr < 0x08 || addr > 0x77)
909 return -EINVAL;
910 return 0;
911}
912
Jean Delvare3b5f7942010-06-03 11:33:55 +0200913static int __i2c_check_addr_busy(struct device *dev, void *addrp)
914{
915 struct i2c_client *client = i2c_verify_client(dev);
916 int addr = *(int *)addrp;
917
918 if (client && client->addr == addr)
919 return -EBUSY;
920 return 0;
921}
922
Michael Lawnick08263742010-08-11 18:21:02 +0200923/* walk up mux tree */
924static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
925{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200926 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200927 int result;
928
929 result = device_for_each_child(&adapter->dev, &addr,
930 __i2c_check_addr_busy);
931
Jean Delvare97cc4d42010-10-24 18:16:57 +0200932 if (!result && parent)
933 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200934
935 return result;
936}
937
938/* recurse down mux tree */
939static int i2c_check_mux_children(struct device *dev, void *addrp)
940{
941 int result;
942
943 if (dev->type == &i2c_adapter_type)
944 result = device_for_each_child(dev, addrp,
945 i2c_check_mux_children);
946 else
947 result = __i2c_check_addr_busy(dev, addrp);
948
949 return result;
950}
951
Jean Delvare3b5f7942010-06-03 11:33:55 +0200952static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
953{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200954 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200955 int result = 0;
956
Jean Delvare97cc4d42010-10-24 18:16:57 +0200957 if (parent)
958 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200959
960 if (!result)
961 result = device_for_each_child(&adapter->dev, &addr,
962 i2c_check_mux_children);
963
964 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200965}
966
David Brownell9c1600e2007-05-01 23:26:31 +0200967/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200968 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
969 * @adapter: Target I2C bus segment
970 */
971void i2c_lock_adapter(struct i2c_adapter *adapter)
972{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200973 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
974
975 if (parent)
976 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200977 else
978 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200979}
980EXPORT_SYMBOL_GPL(i2c_lock_adapter);
981
982/**
983 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
984 * @adapter: Target I2C bus segment
985 */
986static int i2c_trylock_adapter(struct i2c_adapter *adapter)
987{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200988 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
989
990 if (parent)
991 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200992 else
993 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200994}
995
996/**
997 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
998 * @adapter: Target I2C bus segment
999 */
1000void i2c_unlock_adapter(struct i2c_adapter *adapter)
1001{
Jean Delvare97cc4d42010-10-24 18:16:57 +02001002 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
1003
1004 if (parent)
1005 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +02001006 else
1007 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +02001008}
1009EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
1010
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001011static void i2c_dev_set_name(struct i2c_adapter *adap,
1012 struct i2c_client *client)
1013{
1014 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1015
1016 if (adev) {
1017 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1018 return;
1019 }
1020
1021 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1022 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1023 client->addr | ((client->flags & I2C_CLIENT_TEN)
1024 ? 0xa000 : 0));
1025}
1026
Jean Delvarefe61e072010-08-11 18:20:58 +02001027/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001028 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001029 * @adap: the adapter managing the device
1030 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001031 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001032 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001033 * Create an i2c device. Binding is handled through driver model
1034 * probe()/remove() methods. A driver may be bound to this device when we
1035 * return from this function, or any later moment (e.g. maybe hotplugging will
1036 * load the driver module). This call is not appropriate for use by mainboard
1037 * initialization logic, which usually runs during an arch_initcall() long
1038 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001039 *
1040 * This returns the new i2c client, which may be saved for later use with
1041 * i2c_unregister_device(); or NULL to indicate an error.
1042 */
1043struct i2c_client *
1044i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1045{
1046 struct i2c_client *client;
1047 int status;
1048
1049 client = kzalloc(sizeof *client, GFP_KERNEL);
1050 if (!client)
1051 return NULL;
1052
1053 client->adapter = adap;
1054
1055 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001056
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001057 if (info->archdata)
1058 client->dev.archdata = *info->archdata;
1059
Marc Pignatee354252008-08-28 08:33:22 +02001060 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001061 client->addr = info->addr;
1062 client->irq = info->irq;
1063
David Brownell9c1600e2007-05-01 23:26:31 +02001064 strlcpy(client->name, info->type, sizeof(client->name));
1065
Jean Delvare3a89db52010-06-03 11:33:52 +02001066 /* Check for address validity */
1067 status = i2c_check_client_addr_validity(client);
1068 if (status) {
1069 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1070 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1071 goto out_err_silent;
1072 }
1073
Jean Delvaref8a227e2009-06-19 16:58:18 +02001074 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001075 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001076 if (status)
1077 goto out_err;
1078
1079 client->dev.parent = &client->adapter->dev;
1080 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001081 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001082 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001083 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001084
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001085 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001086 status = device_register(&client->dev);
1087 if (status)
1088 goto out_err;
1089
Jean Delvaref8a227e2009-06-19 16:58:18 +02001090 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1091 client->name, dev_name(&client->dev));
1092
David Brownell9c1600e2007-05-01 23:26:31 +02001093 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001094
1095out_err:
1096 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1097 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001098out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001099 kfree(client);
1100 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001101}
1102EXPORT_SYMBOL_GPL(i2c_new_device);
1103
1104
1105/**
1106 * i2c_unregister_device - reverse effect of i2c_new_device()
1107 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001108 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001109 */
1110void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001111{
David Brownella1d9e6e2007-05-01 23:26:30 +02001112 device_unregister(&client->dev);
1113}
David Brownell9c1600e2007-05-01 23:26:31 +02001114EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001115
1116
Jean Delvare60b129d2008-05-11 20:37:06 +02001117static const struct i2c_device_id dummy_id[] = {
1118 { "dummy", 0 },
1119 { },
1120};
1121
Jean Delvared2653e92008-04-29 23:11:39 +02001122static int dummy_probe(struct i2c_client *client,
1123 const struct i2c_device_id *id)
1124{
1125 return 0;
1126}
1127
1128static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001129{
1130 return 0;
1131}
1132
1133static struct i2c_driver dummy_driver = {
1134 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001135 .probe = dummy_probe,
1136 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001137 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001138};
1139
1140/**
1141 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1142 * @adapter: the adapter managing the device
1143 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001144 * Context: can sleep
1145 *
1146 * This returns an I2C client bound to the "dummy" driver, intended for use
1147 * with devices that consume multiple addresses. Examples of such chips
1148 * include various EEPROMS (like 24c04 and 24c08 models).
1149 *
1150 * These dummy devices have two main uses. First, most I2C and SMBus calls
1151 * except i2c_transfer() need a client handle; the dummy will be that handle.
1152 * And second, this prevents the specified address from being bound to a
1153 * different driver.
1154 *
1155 * This returns the new i2c client, which should be saved for later use with
1156 * i2c_unregister_device(); or NULL to indicate an error.
1157 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001158struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001159{
1160 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001161 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001162 };
1163
David Brownelle9f13732008-01-27 18:14:52 +01001164 return i2c_new_device(adapter, &info);
1165}
1166EXPORT_SYMBOL_GPL(i2c_new_dummy);
1167
David Brownellf37dd802007-02-13 22:09:00 +01001168/* ------------------------------------------------------------------------- */
1169
David Brownell16ffadf2007-05-01 23:26:28 +02001170/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1171
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001172static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
David Brownellef2c83212007-05-01 23:26:28 +02001174 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 complete(&adap->dev_released);
1176}
1177
Jean Delvare99cd8e22009-06-19 16:58:20 +02001178/*
Jean Delvare390946b2012-09-10 10:14:02 +02001179 * This function is only needed for mutex_lock_nested, so it is never
1180 * called unless locking correctness checking is enabled. Thus we
1181 * make it inline to avoid a compiler warning. That's what gcc ends up
1182 * doing anyway.
1183 */
1184static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1185{
1186 unsigned int depth = 0;
1187
1188 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1189 depth++;
1190
1191 return depth;
1192}
1193
1194/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001195 * Let users instantiate I2C devices through sysfs. This can be used when
1196 * platform initialization code doesn't contain the proper data for
1197 * whatever reason. Also useful for drivers that do device detection and
1198 * detection fails, either because the device uses an unexpected address,
1199 * or this is a compatible device with different ID register values.
1200 *
1201 * Parameter checking may look overzealous, but we really don't want
1202 * the user to provide incorrect parameters.
1203 */
1204static ssize_t
1205i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1206 const char *buf, size_t count)
1207{
1208 struct i2c_adapter *adap = to_i2c_adapter(dev);
1209 struct i2c_board_info info;
1210 struct i2c_client *client;
1211 char *blank, end;
1212 int res;
1213
Jean Delvare99cd8e22009-06-19 16:58:20 +02001214 memset(&info, 0, sizeof(struct i2c_board_info));
1215
1216 blank = strchr(buf, ' ');
1217 if (!blank) {
1218 dev_err(dev, "%s: Missing parameters\n", "new_device");
1219 return -EINVAL;
1220 }
1221 if (blank - buf > I2C_NAME_SIZE - 1) {
1222 dev_err(dev, "%s: Invalid device name\n", "new_device");
1223 return -EINVAL;
1224 }
1225 memcpy(info.type, buf, blank - buf);
1226
1227 /* Parse remaining parameters, reject extra parameters */
1228 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1229 if (res < 1) {
1230 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1231 return -EINVAL;
1232 }
1233 if (res > 1 && end != '\n') {
1234 dev_err(dev, "%s: Extra parameters\n", "new_device");
1235 return -EINVAL;
1236 }
1237
Jean Delvare99cd8e22009-06-19 16:58:20 +02001238 client = i2c_new_device(adap, &info);
1239 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001240 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001241
1242 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001243 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001244 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001245 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001246 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1247 info.type, info.addr);
1248
1249 return count;
1250}
1251
1252/*
1253 * And of course let the users delete the devices they instantiated, if
1254 * they got it wrong. This interface can only be used to delete devices
1255 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1256 * don't delete devices to which some kernel code still has references.
1257 *
1258 * Parameter checking may look overzealous, but we really don't want
1259 * the user to delete the wrong device.
1260 */
1261static ssize_t
1262i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1263 const char *buf, size_t count)
1264{
1265 struct i2c_adapter *adap = to_i2c_adapter(dev);
1266 struct i2c_client *client, *next;
1267 unsigned short addr;
1268 char end;
1269 int res;
1270
1271 /* Parse parameters, reject extra parameters */
1272 res = sscanf(buf, "%hi%c", &addr, &end);
1273 if (res < 1) {
1274 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1275 return -EINVAL;
1276 }
1277 if (res > 1 && end != '\n') {
1278 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1279 return -EINVAL;
1280 }
1281
1282 /* Make sure the device was added through sysfs */
1283 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001284 mutex_lock_nested(&adap->userspace_clients_lock,
1285 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001286 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1287 detected) {
1288 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001289 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1290 "delete_device", client->name, client->addr);
1291
1292 list_del(&client->detected);
1293 i2c_unregister_device(client);
1294 res = count;
1295 break;
1296 }
1297 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001298 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001299
1300 if (res < 0)
1301 dev_err(dev, "%s: Can't find device in list\n",
1302 "delete_device");
1303 return res;
1304}
1305
Jean Delvare4f8cf822009-09-18 22:45:46 +02001306static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001307static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1308 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001309
1310static struct attribute *i2c_adapter_attrs[] = {
1311 &dev_attr_name.attr,
1312 &dev_attr_new_device.attr,
1313 &dev_attr_delete_device.attr,
1314 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001315};
1316
Jean Delvare4f8cf822009-09-18 22:45:46 +02001317static struct attribute_group i2c_adapter_attr_group = {
1318 .attrs = i2c_adapter_attrs,
1319};
1320
1321static const struct attribute_group *i2c_adapter_attr_groups[] = {
1322 &i2c_adapter_attr_group,
1323 NULL
1324};
1325
Michael Lawnick08263742010-08-11 18:21:02 +02001326struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001327 .groups = i2c_adapter_attr_groups,
1328 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001329};
Michael Lawnick08263742010-08-11 18:21:02 +02001330EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Stephen Warren643dd092012-04-17 12:43:33 -06001332/**
1333 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1334 * @dev: device, probably from some driver model iterator
1335 *
1336 * When traversing the driver model tree, perhaps using driver model
1337 * iterators like @device_for_each_child(), you can't assume very much
1338 * about the nodes you find. Use this function to avoid oopses caused
1339 * by wrongly treating some non-I2C device as an i2c_adapter.
1340 */
1341struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1342{
1343 return (dev->type == &i2c_adapter_type)
1344 ? to_i2c_adapter(dev)
1345 : NULL;
1346}
1347EXPORT_SYMBOL(i2c_verify_adapter);
1348
Jean Delvare2bb50952009-09-18 22:45:46 +02001349#ifdef CONFIG_I2C_COMPAT
1350static struct class_compat *i2c_adapter_compat_class;
1351#endif
1352
David Brownell9c1600e2007-05-01 23:26:31 +02001353static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1354{
1355 struct i2c_devinfo *devinfo;
1356
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001357 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001358 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1359 if (devinfo->busnum == adapter->nr
1360 && !i2c_new_device(adapter,
1361 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001362 dev_err(&adapter->dev,
1363 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001364 devinfo->board_info.addr);
1365 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001366 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001367}
1368
Wolfram Sang687b81d2013-07-11 12:56:15 +01001369/* OF support code */
1370
1371#if IS_ENABLED(CONFIG_OF)
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001372static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
1373 struct device_node *node)
1374{
1375 struct i2c_client *result;
1376 struct i2c_board_info info = {};
1377 struct dev_archdata dev_ad = {};
1378 const __be32 *addr;
1379 int len;
1380
1381 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1382
1383 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1384 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1385 node->full_name);
1386 return ERR_PTR(-EINVAL);
1387 }
1388
1389 addr = of_get_property(node, "reg", &len);
1390 if (!addr || (len < sizeof(int))) {
1391 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1392 node->full_name);
1393 return ERR_PTR(-EINVAL);
1394 }
1395
1396 info.addr = be32_to_cpup(addr);
1397 if (info.addr > (1 << 10) - 1) {
1398 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1399 info.addr, node->full_name);
1400 return ERR_PTR(-EINVAL);
1401 }
1402
1403 info.irq = irq_of_parse_and_map(node, 0);
1404 info.of_node = of_node_get(node);
1405 info.archdata = &dev_ad;
1406
1407 if (of_get_property(node, "wakeup-source", NULL))
1408 info.flags |= I2C_CLIENT_WAKE;
1409
1410 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1411
1412 result = i2c_new_device(adap, &info);
1413 if (result == NULL) {
1414 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1415 node->full_name);
1416 of_node_put(node);
1417 irq_dispose_mapping(info.irq);
1418 return ERR_PTR(-EINVAL);
1419 }
1420 return result;
1421}
1422
Wolfram Sang687b81d2013-07-11 12:56:15 +01001423static void of_i2c_register_devices(struct i2c_adapter *adap)
1424{
Wolfram Sang687b81d2013-07-11 12:56:15 +01001425 struct device_node *node;
1426
1427 /* Only register child devices if the adapter has a node pointer set */
1428 if (!adap->dev.of_node)
1429 return;
1430
1431 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1432
Pantelis Antonioua430a3452014-10-28 22:36:02 +02001433 for_each_available_child_of_node(adap->dev.of_node, node)
1434 of_i2c_register_device(adap, node);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001435}
1436
1437static int of_dev_node_match(struct device *dev, void *data)
1438{
1439 return dev->of_node == data;
1440}
1441
1442/* must call put_device() when done with returned i2c_client device */
1443struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1444{
1445 struct device *dev;
1446
1447 dev = bus_find_device(&i2c_bus_type, NULL, node,
1448 of_dev_node_match);
1449 if (!dev)
1450 return NULL;
1451
1452 return i2c_verify_client(dev);
1453}
1454EXPORT_SYMBOL(of_find_i2c_device_by_node);
1455
1456/* must call put_device() when done with returned i2c_adapter device */
1457struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1458{
1459 struct device *dev;
1460
1461 dev = bus_find_device(&i2c_bus_type, NULL, node,
1462 of_dev_node_match);
1463 if (!dev)
1464 return NULL;
1465
1466 return i2c_verify_adapter(dev);
1467}
1468EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1469#else
1470static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1471#endif /* CONFIG_OF */
1472
Jean Delvare69b00892009-12-06 17:06:27 +01001473static int i2c_do_add_adapter(struct i2c_driver *driver,
1474 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001475{
Jean Delvare4735c982008-07-14 22:38:36 +02001476 /* Detect supported devices on that bus, and instantiate them */
1477 i2c_detect(adap, driver);
1478
1479 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001480 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001481 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1482 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001483 dev_warn(&adap->dev, "Please use another way to instantiate "
1484 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001485 /* We ignore the return code; if it fails, too bad */
1486 driver->attach_adapter(adap);
1487 }
1488 return 0;
1489}
1490
Jean Delvare69b00892009-12-06 17:06:27 +01001491static int __process_new_adapter(struct device_driver *d, void *data)
1492{
1493 return i2c_do_add_adapter(to_i2c_driver(d), data);
1494}
1495
David Brownell6e13e642007-05-01 23:26:31 +02001496static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Jean Delvared6703282010-08-11 18:20:59 +02001498 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
David Brownell1d0b19c2008-10-14 17:30:05 +02001500 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001501 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1502 res = -EAGAIN;
1503 goto out_list;
1504 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001505
Jean Delvare2236baa2010-11-15 22:40:38 +01001506 /* Sanity checks */
1507 if (unlikely(adap->name[0] == '\0')) {
1508 pr_err("i2c-core: Attempt to register an adapter with "
1509 "no name!\n");
1510 return -EINVAL;
1511 }
1512 if (unlikely(!adap->algo)) {
1513 pr_err("i2c-core: Attempt to register adapter '%s' with "
1514 "no algo!\n", adap->name);
1515 return -EINVAL;
1516 }
1517
Mika Kuoppala194684e2009-12-06 17:06:22 +01001518 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001519 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001520 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jean Delvare8fcfef62009-03-28 21:34:43 +01001522 /* Set default timeout to 1 second if not already set */
1523 if (adap->timeout == 0)
1524 adap->timeout = HZ;
1525
Kay Sievers27d9c182009-01-07 14:29:16 +01001526 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001527 adap->dev.bus = &i2c_bus_type;
1528 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001529 res = device_register(&adap->dev);
1530 if (res)
1531 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001533 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1534
Jean Delvare2bb50952009-09-18 22:45:46 +02001535#ifdef CONFIG_I2C_COMPAT
1536 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1537 adap->dev.parent);
1538 if (res)
1539 dev_warn(&adap->dev,
1540 "Failed to create compatibility class link\n");
1541#endif
1542
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301543 /* bus recovery specific initialization */
1544 if (adap->bus_recovery_info) {
1545 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1546
1547 if (!bri->recover_bus) {
1548 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1549 adap->bus_recovery_info = NULL;
1550 goto exit_recovery;
1551 }
1552
1553 /* Generic GPIO recovery */
1554 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1555 if (!gpio_is_valid(bri->scl_gpio)) {
1556 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1557 adap->bus_recovery_info = NULL;
1558 goto exit_recovery;
1559 }
1560
1561 if (gpio_is_valid(bri->sda_gpio))
1562 bri->get_sda = get_sda_gpio_value;
1563 else
1564 bri->get_sda = NULL;
1565
1566 bri->get_scl = get_scl_gpio_value;
1567 bri->set_scl = set_scl_gpio_value;
1568 } else if (!bri->set_scl || !bri->get_scl) {
1569 /* Generic SCL recovery */
1570 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1571 adap->bus_recovery_info = NULL;
1572 }
1573 }
1574
1575exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001576 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001577 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001578 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001579 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001580
David Brownell6e13e642007-05-01 23:26:31 +02001581 if (adap->nr < __i2c_first_dynamic_bus_num)
1582 i2c_scan_static_board_info(adap);
1583
Jean Delvare4735c982008-07-14 22:38:36 +02001584 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001585 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001586 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001587 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001588
1589 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001590
Jean Delvareb119c6c2006-08-15 18:26:30 +02001591out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001592 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001593 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001594 mutex_unlock(&core_lock);
1595 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
David Brownell6e13e642007-05-01 23:26:31 +02001598/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001599 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1600 * @adap: the adapter to register (with adap->nr initialized)
1601 * Context: can sleep
1602 *
1603 * See i2c_add_numbered_adapter() for details.
1604 */
1605static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1606{
1607 int id;
1608
1609 mutex_lock(&core_lock);
1610 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1611 GFP_KERNEL);
1612 mutex_unlock(&core_lock);
1613 if (id < 0)
1614 return id == -ENOSPC ? -EBUSY : id;
1615
1616 return i2c_register_adapter(adap);
1617}
1618
1619/**
David Brownell6e13e642007-05-01 23:26:31 +02001620 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1621 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001622 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001623 *
1624 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001625 * doesn't matter or when its bus number is specified by an dt alias.
1626 * Examples of bases when the bus number doesn't matter: I2C adapters
1627 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001628 *
1629 * When this returns zero, a new bus number was allocated and stored
1630 * in adap->nr, and the specified adapter became available for clients.
1631 * Otherwise, a negative errno value is returned.
1632 */
1633int i2c_add_adapter(struct i2c_adapter *adapter)
1634{
Doug Andersonee5c2742013-03-01 08:57:31 -08001635 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001636 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001637
Doug Andersonee5c2742013-03-01 08:57:31 -08001638 if (dev->of_node) {
1639 id = of_alias_get_id(dev->of_node, "i2c");
1640 if (id >= 0) {
1641 adapter->nr = id;
1642 return __i2c_add_numbered_adapter(adapter);
1643 }
1644 }
1645
Jean Delvarecaada322008-01-27 18:14:49 +01001646 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001647 id = idr_alloc(&i2c_adapter_idr, adapter,
1648 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001649 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001650 if (id < 0)
1651 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001652
1653 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001654
David Brownell6e13e642007-05-01 23:26:31 +02001655 return i2c_register_adapter(adapter);
1656}
1657EXPORT_SYMBOL(i2c_add_adapter);
1658
1659/**
1660 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1661 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001662 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001663 *
1664 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001665 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1666 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001667 * is used to properly configure I2C devices.
1668 *
Grant Likely488bf312011-07-25 17:49:43 +02001669 * If the requested bus number is set to -1, then this function will behave
1670 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1671 *
David Brownell6e13e642007-05-01 23:26:31 +02001672 * If no devices have pre-been declared for this bus, then be sure to
1673 * register the adapter before any dynamically allocated ones. Otherwise
1674 * the required bus ID may not be available.
1675 *
1676 * When this returns zero, the specified adapter became available for
1677 * clients using the bus number provided in adap->nr. Also, the table
1678 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1679 * and the appropriate driver model device nodes are created. Otherwise, a
1680 * negative errno value is returned.
1681 */
1682int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1683{
Grant Likely488bf312011-07-25 17:49:43 +02001684 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1685 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001686
Doug Andersonee5c2742013-03-01 08:57:31 -08001687 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001688}
1689EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1690
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001691static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001692 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001693{
Jean Delvare4735c982008-07-14 22:38:36 +02001694 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001695
Jean Delvareacec2112009-03-28 21:34:40 +01001696 /* Remove the devices we created ourselves as the result of hardware
1697 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001698 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1699 if (client->adapter == adapter) {
1700 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1701 client->name, client->addr);
1702 list_del(&client->detected);
1703 i2c_unregister_device(client);
1704 }
1705 }
Jean Delvare026526f2008-01-27 18:14:49 +01001706}
1707
Jean Delvaree549c2b2009-06-19 16:58:19 +02001708static int __unregister_client(struct device *dev, void *dummy)
1709{
1710 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001711 if (client && strcmp(client->name, "dummy"))
1712 i2c_unregister_device(client);
1713 return 0;
1714}
1715
1716static int __unregister_dummy(struct device *dev, void *dummy)
1717{
1718 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001719 if (client)
1720 i2c_unregister_device(client);
1721 return 0;
1722}
1723
Jean Delvare69b00892009-12-06 17:06:27 +01001724static int __process_removed_adapter(struct device_driver *d, void *data)
1725{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001726 i2c_do_del_adapter(to_i2c_driver(d), data);
1727 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001728}
1729
David Brownelld64f73b2007-07-12 14:12:28 +02001730/**
1731 * i2c_del_adapter - unregister I2C adapter
1732 * @adap: the adapter being unregistered
1733 * Context: can sleep
1734 *
1735 * This unregisters an I2C adapter which was previously registered
1736 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1737 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001738void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001740 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001741 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001744 mutex_lock(&core_lock);
1745 found = idr_find(&i2c_adapter_idr, adap->nr);
1746 mutex_unlock(&core_lock);
1747 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001748 pr_debug("i2c-core: attempting to delete unregistered "
1749 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001750 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
Lan Tianyu5d98e612014-05-20 20:59:23 +08001753 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001754 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001755 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001756 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001757 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001758 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001760 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001761 mutex_lock_nested(&adap->userspace_clients_lock,
1762 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001763 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1764 detected) {
1765 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1766 client->addr);
1767 list_del(&client->detected);
1768 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001769 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001770 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001771
Jean Delvaree549c2b2009-06-19 16:58:19 +02001772 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001773 * check the returned value. This is a two-pass process, because
1774 * we can't remove the dummy devices during the first pass: they
1775 * could have been instantiated by real devices wishing to clean
1776 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001777 device_for_each_child(&adap->dev, NULL, __unregister_client);
1778 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jean Delvare2bb50952009-09-18 22:45:46 +02001780#ifdef CONFIG_I2C_COMPAT
1781 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1782 adap->dev.parent);
1783#endif
1784
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001785 /* device name is gone after device_unregister */
1786 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /* clean up the sysfs representation */
1789 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /* wait for sysfs to drop all references */
1793 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
David Brownell6e13e642007-05-01 23:26:31 +02001795 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001796 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001798 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001800 /* Clear the device structure in case this adapter is ever going to be
1801 added again */
1802 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
David Brownellc0564602007-05-01 23:26:31 +02001804EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Brownell7b4fbc52007-05-01 23:26:30 +02001806/* ------------------------------------------------------------------------- */
1807
Jean Delvare7ae31482011-03-20 14:50:52 +01001808int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1809{
1810 int res;
1811
1812 mutex_lock(&core_lock);
1813 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1814 mutex_unlock(&core_lock);
1815
1816 return res;
1817}
1818EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1819
Jean Delvare69b00892009-12-06 17:06:27 +01001820static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001821{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001822 if (dev->type != &i2c_adapter_type)
1823 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001824 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001825}
1826
David Brownell7b4fbc52007-05-01 23:26:30 +02001827/*
1828 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001829 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
1831
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001832int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001834 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
David Brownell1d0b19c2008-10-14 17:30:05 +02001836 /* Can't register until after driver model init */
1837 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1838 return -EAGAIN;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001841 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Jean Delvare729d6dd2009-06-19 16:58:18 +02001844 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001845 * will have called probe() for all matching-but-unbound devices.
1846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 res = driver_register(&driver->driver);
1848 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001849 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001850
Mark Brownf4e8db32011-01-14 22:03:50 +01001851 /* Drivers should switch to dev_pm_ops instead. */
1852 if (driver->suspend)
1853 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1854 driver->driver.name);
1855 if (driver->resume)
1856 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1857 driver->driver.name);
1858
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001859 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Jean Delvare4735c982008-07-14 22:38:36 +02001861 INIT_LIST_HEAD(&driver->clients);
1862 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001863 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001864
Jean Delvare7eebcb72006-02-05 23:28:21 +01001865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001867EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jean Delvare69b00892009-12-06 17:06:27 +01001869static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001870{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001871 if (dev->type == &i2c_adapter_type)
1872 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1873 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001874}
1875
David Brownella1d9e6e2007-05-01 23:26:30 +02001876/**
1877 * i2c_del_driver - unregister I2C driver
1878 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001879 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001880 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001881void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Jean Delvare7ae31482011-03-20 14:50:52 +01001883 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001886 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
David Brownellc0564602007-05-01 23:26:31 +02001888EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
David Brownell7b4fbc52007-05-01 23:26:30 +02001890/* ------------------------------------------------------------------------- */
1891
Jean Delvaree48d3312008-01-27 18:14:48 +01001892/**
1893 * i2c_use_client - increments the reference count of the i2c client structure
1894 * @client: the client being referenced
1895 *
1896 * Each live reference to a client should be refcounted. The driver model does
1897 * that automatically as part of driver binding, so that most drivers don't
1898 * need to do this explicitly: they hold a reference until they're unbound
1899 * from the device.
1900 *
1901 * A pointer to the client with the incremented reference counter is returned.
1902 */
1903struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
David Brownell6ea438e2008-07-14 22:38:24 +02001905 if (client && get_device(&client->dev))
1906 return client;
1907 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
David Brownellc0564602007-05-01 23:26:31 +02001909EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Jean Delvaree48d3312008-01-27 18:14:48 +01001911/**
1912 * i2c_release_client - release a use of the i2c client structure
1913 * @client: the client being no longer referenced
1914 *
1915 * Must be called when a user of a client is finished with it.
1916 */
1917void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
David Brownell6ea438e2008-07-14 22:38:24 +02001919 if (client)
1920 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
David Brownellc0564602007-05-01 23:26:31 +02001922EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
David Brownell9b766b82008-01-27 18:14:51 +01001924struct i2c_cmd_arg {
1925 unsigned cmd;
1926 void *arg;
1927};
1928
1929static int i2c_cmd(struct device *dev, void *_arg)
1930{
1931 struct i2c_client *client = i2c_verify_client(dev);
1932 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001933 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001934
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001935 if (!client || !client->dev.driver)
1936 return 0;
1937
1938 driver = to_i2c_driver(client->dev.driver);
1939 if (driver->command)
1940 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001941 return 0;
1942}
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1945{
David Brownell9b766b82008-01-27 18:14:51 +01001946 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
David Brownell9b766b82008-01-27 18:14:51 +01001948 cmd_arg.cmd = cmd;
1949 cmd_arg.arg = arg;
1950 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
David Brownellc0564602007-05-01 23:26:31 +02001952EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954static int __init i2c_init(void)
1955{
1956 int retval;
1957
1958 retval = bus_register(&i2c_bus_type);
1959 if (retval)
1960 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001961#ifdef CONFIG_I2C_COMPAT
1962 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1963 if (!i2c_adapter_compat_class) {
1964 retval = -ENOMEM;
1965 goto bus_err;
1966 }
1967#endif
David Brownelle9f13732008-01-27 18:14:52 +01001968 retval = i2c_add_driver(&dummy_driver);
1969 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001970 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001971 return 0;
1972
Jean Delvare2bb50952009-09-18 22:45:46 +02001973class_err:
1974#ifdef CONFIG_I2C_COMPAT
1975 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001976bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001977#endif
David Brownelle9f13732008-01-27 18:14:52 +01001978 bus_unregister(&i2c_bus_type);
1979 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982static void __exit i2c_exit(void)
1983{
David Brownelle9f13732008-01-27 18:14:52 +01001984 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001985#ifdef CONFIG_I2C_COMPAT
1986 class_compat_unregister(i2c_adapter_compat_class);
1987#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001989 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
David Brownella10f9e72008-10-14 17:30:06 +02001992/* We must initialize early, because some subsystems register i2c drivers
1993 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1994 */
1995postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996module_exit(i2c_exit);
1997
1998/* ----------------------------------------------------
1999 * the functional interface to the i2c busses.
2000 * ----------------------------------------------------
2001 */
2002
David Brownella1cdeda2008-07-14 22:38:24 +02002003/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002004 * __i2c_transfer - unlocked flavor of i2c_transfer
2005 * @adap: Handle to I2C bus
2006 * @msgs: One or more messages to execute before STOP is issued to
2007 * terminate the operation; each message begins with a START.
2008 * @num: Number of messages to be executed.
2009 *
2010 * Returns negative errno, else the number of messages executed.
2011 *
2012 * Adapter lock must be held when calling this function. No debug logging
2013 * takes place. adap->algo->master_xfer existence isn't checked.
2014 */
2015int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2016{
2017 unsigned long orig_jiffies;
2018 int ret, try;
2019
David Howellsd9a83d62014-03-06 13:35:59 +00002020 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2021 * enabled. This is an efficient way of keeping the for-loop from
2022 * being executed when not needed.
2023 */
2024 if (static_key_false(&i2c_trace_msg)) {
2025 int i;
2026 for (i = 0; i < num; i++)
2027 if (msgs[i].flags & I2C_M_RD)
2028 trace_i2c_read(adap, &msgs[i], i);
2029 else
2030 trace_i2c_write(adap, &msgs[i], i);
2031 }
2032
Jean Delvareb37d2a32012-06-29 07:47:19 -03002033 /* Retry automatically on arbitration loss */
2034 orig_jiffies = jiffies;
2035 for (ret = 0, try = 0; try <= adap->retries; try++) {
2036 ret = adap->algo->master_xfer(adap, msgs, num);
2037 if (ret != -EAGAIN)
2038 break;
2039 if (time_after(jiffies, orig_jiffies + adap->timeout))
2040 break;
2041 }
2042
David Howellsd9a83d62014-03-06 13:35:59 +00002043 if (static_key_false(&i2c_trace_msg)) {
2044 int i;
2045 for (i = 0; i < ret; i++)
2046 if (msgs[i].flags & I2C_M_RD)
2047 trace_i2c_reply(adap, &msgs[i], i);
2048 trace_i2c_result(adap, i, ret);
2049 }
2050
Jean Delvareb37d2a32012-06-29 07:47:19 -03002051 return ret;
2052}
2053EXPORT_SYMBOL(__i2c_transfer);
2054
2055/**
David Brownella1cdeda2008-07-14 22:38:24 +02002056 * i2c_transfer - execute a single or combined I2C message
2057 * @adap: Handle to I2C bus
2058 * @msgs: One or more messages to execute before STOP is issued to
2059 * terminate the operation; each message begins with a START.
2060 * @num: Number of messages to be executed.
2061 *
2062 * Returns negative errno, else the number of messages executed.
2063 *
2064 * Note that there is no requirement that each message be sent to
2065 * the same slave address, although that is the most common model.
2066 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002067int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002069 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
David Brownella1cdeda2008-07-14 22:38:24 +02002071 /* REVISIT the fault reporting model here is weak:
2072 *
2073 * - When we get an error after receiving N bytes from a slave,
2074 * there is no way to report "N".
2075 *
2076 * - When we get a NAK after transmitting N bytes to a slave,
2077 * there is no way to report "N" ... or to let the master
2078 * continue executing the rest of this combined message, if
2079 * that's the appropriate response.
2080 *
2081 * - When for example "num" is two and we successfully complete
2082 * the first message but get an error part way through the
2083 * second, it's unclear whether that should be reported as
2084 * one (discarding status on the second message) or errno
2085 * (discarding status on the first one).
2086 */
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (adap->algo->master_xfer) {
2089#ifdef DEBUG
2090 for (ret = 0; ret < num; ret++) {
2091 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002092 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2093 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2094 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096#endif
2097
Mike Rapoportcea443a2008-01-27 18:14:50 +01002098 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002099 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002100 if (!ret)
2101 /* I2C activity is ongoing. */
2102 return -EAGAIN;
2103 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002104 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002105 }
2106
Jean Delvareb37d2a32012-06-29 07:47:19 -03002107 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002108 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 return ret;
2111 } else {
2112 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002113 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115}
David Brownellc0564602007-05-01 23:26:31 +02002116EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
David Brownella1cdeda2008-07-14 22:38:24 +02002118/**
2119 * i2c_master_send - issue a single I2C message in master transmit mode
2120 * @client: Handle to slave device
2121 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002122 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002123 *
2124 * Returns negative errno, or else the number of bytes written.
2125 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002126int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002129 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct i2c_msg msg;
2131
Jean Delvare815f55f2005-05-07 22:58:46 +02002132 msg.addr = client->addr;
2133 msg.flags = client->flags & I2C_M_TEN;
2134 msg.len = count;
2135 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002136
Jean Delvare815f55f2005-05-07 22:58:46 +02002137 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002139 /*
2140 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2141 * transmitted, else error code.
2142 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002143 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
David Brownellc0564602007-05-01 23:26:31 +02002145EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
David Brownella1cdeda2008-07-14 22:38:24 +02002147/**
2148 * i2c_master_recv - issue a single I2C message in master receive mode
2149 * @client: Handle to slave device
2150 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002151 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002152 *
2153 * Returns negative errno, or else the number of bytes read.
2154 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002155int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Farid Hammane7225acf2010-05-21 18:40:58 +02002157 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct i2c_msg msg;
2159 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Jean Delvare815f55f2005-05-07 22:58:46 +02002161 msg.addr = client->addr;
2162 msg.flags = client->flags & I2C_M_TEN;
2163 msg.flags |= I2C_M_RD;
2164 msg.len = count;
2165 msg.buf = buf;
2166
2167 ret = i2c_transfer(adap, &msg, 1);
2168
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002169 /*
2170 * If everything went ok (i.e. 1 msg received), return #bytes received,
2171 * else error code.
2172 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002173 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
David Brownellc0564602007-05-01 23:26:31 +02002175EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177/* ----------------------------------------------------
2178 * the i2c address scanning function
2179 * Will not work for 10-bit addresses!
2180 * ----------------------------------------------------
2181 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002182
Jean Delvare63e4e802010-06-03 11:33:51 +02002183/*
2184 * Legacy default probe function, mostly relevant for SMBus. The default
2185 * probe method is a quick write, but it is known to corrupt the 24RF08
2186 * EEPROMs due to a state machine bug, and could also irreversibly
2187 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2188 * we use a short byte read instead. Also, some bus drivers don't implement
2189 * quick write, so we fallback to a byte read in that case too.
2190 * On x86, there is another special case for FSC hardware monitoring chips,
2191 * which want regular byte reads (address 0x73.) Fortunately, these are the
2192 * only known chips using this I2C address on PC hardware.
2193 * Returns 1 if probe succeeded, 0 if not.
2194 */
2195static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2196{
2197 int err;
2198 union i2c_smbus_data dummy;
2199
2200#ifdef CONFIG_X86
2201 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2202 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2203 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2204 I2C_SMBUS_BYTE_DATA, &dummy);
2205 else
2206#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002207 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2208 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002209 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2210 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002211 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2212 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2213 I2C_SMBUS_BYTE, &dummy);
2214 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002215 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2216 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002217 err = -EOPNOTSUPP;
2218 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002219
2220 return err >= 0;
2221}
2222
Jean Delvareccfbbd02009-12-06 17:06:25 +01002223static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002224 struct i2c_driver *driver)
2225{
2226 struct i2c_board_info info;
2227 struct i2c_adapter *adapter = temp_client->adapter;
2228 int addr = temp_client->addr;
2229 int err;
2230
2231 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002232 err = i2c_check_addr_validity(addr);
2233 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002234 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2235 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002236 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002237 }
2238
2239 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002240 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002241 return 0;
2242
Jean Delvareccfbbd02009-12-06 17:06:25 +01002243 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002244 if (!i2c_default_probe(adapter, addr))
2245 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002246
2247 /* Finally call the custom detection function */
2248 memset(&info, 0, sizeof(struct i2c_board_info));
2249 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002250 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002251 if (err) {
2252 /* -ENODEV is returned if the detection fails. We catch it
2253 here as this isn't an error. */
2254 return err == -ENODEV ? 0 : err;
2255 }
2256
2257 /* Consistency check */
2258 if (info.type[0] == '\0') {
2259 dev_err(&adapter->dev, "%s detection function provided "
2260 "no name for 0x%x\n", driver->driver.name,
2261 addr);
2262 } else {
2263 struct i2c_client *client;
2264
2265 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002266 if (adapter->class & I2C_CLASS_DEPRECATED)
2267 dev_warn(&adapter->dev,
2268 "This adapter will soon drop class based instantiation of devices. "
2269 "Please make sure client 0x%02x gets instantiated by other means. "
2270 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2271 info.addr);
2272
Jean Delvare4735c982008-07-14 22:38:36 +02002273 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2274 info.type, info.addr);
2275 client = i2c_new_device(adapter, &info);
2276 if (client)
2277 list_add_tail(&client->detected, &driver->clients);
2278 else
2279 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2280 info.type, info.addr);
2281 }
2282 return 0;
2283}
2284
2285static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2286{
Jean Delvarec3813d62009-12-14 21:17:25 +01002287 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002288 struct i2c_client *temp_client;
2289 int i, err = 0;
2290 int adap_id = i2c_adapter_id(adapter);
2291
Jean Delvarec3813d62009-12-14 21:17:25 +01002292 address_list = driver->address_list;
2293 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002294 return 0;
2295
Wolfram Sang45552272014-07-10 13:46:21 +02002296 /* Warn that the adapter lost class based instantiation */
2297 if (adapter->class == I2C_CLASS_DEPRECATED) {
2298 dev_dbg(&adapter->dev,
2299 "This adapter dropped support for I2C classes and "
2300 "won't auto-detect %s devices anymore. If you need it, check "
2301 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2302 driver->driver.name);
2303 return 0;
2304 }
2305
Jean Delvare51b54ba2010-10-24 18:16:58 +02002306 /* Stop here if the classes do not match */
2307 if (!(adapter->class & driver->class))
2308 return 0;
2309
Jean Delvare4735c982008-07-14 22:38:36 +02002310 /* Set up a temporary client to help detect callback */
2311 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2312 if (!temp_client)
2313 return -ENOMEM;
2314 temp_client->adapter = adapter;
2315
Jean Delvarec3813d62009-12-14 21:17:25 +01002316 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002317 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002318 "addr 0x%02x\n", adap_id, address_list[i]);
2319 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002320 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002321 if (unlikely(err))
2322 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002323 }
2324
Jean Delvare4735c982008-07-14 22:38:36 +02002325 kfree(temp_client);
2326 return err;
2327}
2328
Jean Delvared44f19d2010-08-11 18:20:57 +02002329int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2330{
2331 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2332 I2C_SMBUS_QUICK, NULL) >= 0;
2333}
2334EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2335
Jean Delvare12b5053a2007-05-01 23:26:31 +02002336struct i2c_client *
2337i2c_new_probed_device(struct i2c_adapter *adap,
2338 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002339 unsigned short const *addr_list,
2340 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002341{
2342 int i;
2343
Jean Delvare8031d792010-08-11 18:21:00 +02002344 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002345 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002346
Jean Delvare12b5053a2007-05-01 23:26:31 +02002347 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2348 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002349 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002350 dev_warn(&adap->dev, "Invalid 7-bit address "
2351 "0x%02x\n", addr_list[i]);
2352 continue;
2353 }
2354
2355 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002356 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002357 dev_dbg(&adap->dev, "Address 0x%02x already in "
2358 "use, not probing\n", addr_list[i]);
2359 continue;
2360 }
2361
Jean Delvare63e4e802010-06-03 11:33:51 +02002362 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002363 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002364 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002365 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002366
2367 if (addr_list[i] == I2C_CLIENT_END) {
2368 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2369 return NULL;
2370 }
2371
2372 info->addr = addr_list[i];
2373 return i2c_new_device(adap, info);
2374}
2375EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2376
Jean Delvared735b342011-03-20 14:50:52 +01002377struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002380
Jean Delvarecaada322008-01-27 18:14:49 +01002381 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002382 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002383 if (adapter && !try_module_get(adapter->owner))
2384 adapter = NULL;
2385
Jean Delvarecaada322008-01-27 18:14:49 +01002386 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002387 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
David Brownellc0564602007-05-01 23:26:31 +02002389EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391void i2c_put_adapter(struct i2c_adapter *adap)
2392{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002393 if (adap)
2394 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
David Brownellc0564602007-05-01 23:26:31 +02002396EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398/* The SMBus parts */
2399
David Brownell438d6c22006-12-10 21:21:31 +01002400#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002401static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
2403 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002404
Farid Hammane7225acf2010-05-21 18:40:58 +02002405 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002406 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 data = data ^ POLY;
2408 data = data << 1;
2409 }
2410 return (u8)(data >> 8);
2411}
2412
Jean Delvare421ef472005-10-26 21:28:55 +02002413/* Incremental CRC8 over count bytes in the array pointed to by p */
2414static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
2416 int i;
2417
Farid Hammane7225acf2010-05-21 18:40:58 +02002418 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002419 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return crc;
2421}
2422
Jean Delvare421ef472005-10-26 21:28:55 +02002423/* Assume a 7-bit address, which is reasonable for SMBus */
2424static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Jean Delvare421ef472005-10-26 21:28:55 +02002426 /* The address will be sent first */
2427 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2428 pec = i2c_smbus_pec(pec, &addr, 1);
2429
2430 /* The data buffer follows */
2431 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Jean Delvare421ef472005-10-26 21:28:55 +02002434/* Used for write only transactions */
2435static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Jean Delvare421ef472005-10-26 21:28:55 +02002437 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2438 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Jean Delvare421ef472005-10-26 21:28:55 +02002441/* Return <0 on CRC error
2442 If there was a write before this read (most cases) we need to take the
2443 partial CRC from the write part into account.
2444 Note that this function does modify the message (we need to decrease the
2445 message length to hide the CRC byte from the caller). */
2446static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Jean Delvare421ef472005-10-26 21:28:55 +02002448 u8 rpec = msg->buf[--msg->len];
2449 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (rpec != cpec) {
2452 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2453 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002454 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
David Brownell438d6c22006-12-10 21:21:31 +01002456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
David Brownella1cdeda2008-07-14 22:38:24 +02002459/**
2460 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2461 * @client: Handle to slave device
2462 *
2463 * This executes the SMBus "receive byte" protocol, returning negative errno
2464 * else the byte received from the device.
2465 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002466s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002469 int status;
2470
2471 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2472 I2C_SMBUS_READ, 0,
2473 I2C_SMBUS_BYTE, &data);
2474 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
David Brownellc0564602007-05-01 23:26:31 +02002476EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
David Brownella1cdeda2008-07-14 22:38:24 +02002478/**
2479 * i2c_smbus_write_byte - SMBus "send byte" protocol
2480 * @client: Handle to slave device
2481 * @value: Byte to be sent
2482 *
2483 * This executes the SMBus "send byte" protocol, returning negative errno
2484 * else zero on success.
2485 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002486s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Farid Hammane7225acf2010-05-21 18:40:58 +02002488 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002489 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
David Brownellc0564602007-05-01 23:26:31 +02002491EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
David Brownella1cdeda2008-07-14 22:38:24 +02002493/**
2494 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2495 * @client: Handle to slave device
2496 * @command: Byte interpreted by slave
2497 *
2498 * This executes the SMBus "read byte" protocol, returning negative errno
2499 * else a data byte received from the device.
2500 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002501s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002504 int status;
2505
2506 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2507 I2C_SMBUS_READ, command,
2508 I2C_SMBUS_BYTE_DATA, &data);
2509 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
David Brownellc0564602007-05-01 23:26:31 +02002511EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
David Brownella1cdeda2008-07-14 22:38:24 +02002513/**
2514 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2515 * @client: Handle to slave device
2516 * @command: Byte interpreted by slave
2517 * @value: Byte being written
2518 *
2519 * This executes the SMBus "write byte" protocol, returning negative errno
2520 * else zero on success.
2521 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002522s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2523 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 union i2c_smbus_data data;
2526 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002527 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2528 I2C_SMBUS_WRITE, command,
2529 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
David Brownellc0564602007-05-01 23:26:31 +02002531EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
David Brownella1cdeda2008-07-14 22:38:24 +02002533/**
2534 * i2c_smbus_read_word_data - SMBus "read word" protocol
2535 * @client: Handle to slave device
2536 * @command: Byte interpreted by slave
2537 *
2538 * This executes the SMBus "read word" protocol, returning negative errno
2539 * else a 16-bit unsigned "word" received from the device.
2540 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002541s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002544 int status;
2545
2546 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2547 I2C_SMBUS_READ, command,
2548 I2C_SMBUS_WORD_DATA, &data);
2549 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
David Brownellc0564602007-05-01 23:26:31 +02002551EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
David Brownella1cdeda2008-07-14 22:38:24 +02002553/**
2554 * i2c_smbus_write_word_data - SMBus "write word" protocol
2555 * @client: Handle to slave device
2556 * @command: Byte interpreted by slave
2557 * @value: 16-bit "word" being written
2558 *
2559 * This executes the SMBus "write word" protocol, returning negative errno
2560 * else zero on success.
2561 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002562s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2563 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 union i2c_smbus_data data;
2566 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002567 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2568 I2C_SMBUS_WRITE, command,
2569 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
David Brownellc0564602007-05-01 23:26:31 +02002571EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
David Brownella64ec072007-10-13 23:56:31 +02002573/**
David Brownella1cdeda2008-07-14 22:38:24 +02002574 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002575 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002576 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002577 * @values: Byte array into which data will be read; big enough to hold
2578 * the data returned by the slave. SMBus allows at most 32 bytes.
2579 *
David Brownella1cdeda2008-07-14 22:38:24 +02002580 * This executes the SMBus "block read" protocol, returning negative errno
2581 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002582 *
2583 * Note that using this function requires that the client's adapter support
2584 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2585 * support this; its emulation through I2C messaging relies on a specific
2586 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2587 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002588s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002589 u8 *values)
2590{
2591 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002592 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002593
David Brownell24a5bb72008-07-14 22:38:23 +02002594 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2595 I2C_SMBUS_READ, command,
2596 I2C_SMBUS_BLOCK_DATA, &data);
2597 if (status)
2598 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002599
2600 memcpy(values, &data.block[1], data.block[0]);
2601 return data.block[0];
2602}
2603EXPORT_SYMBOL(i2c_smbus_read_block_data);
2604
David Brownella1cdeda2008-07-14 22:38:24 +02002605/**
2606 * i2c_smbus_write_block_data - SMBus "block write" protocol
2607 * @client: Handle to slave device
2608 * @command: Byte interpreted by slave
2609 * @length: Size of data block; SMBus allows at most 32 bytes
2610 * @values: Byte array which will be written.
2611 *
2612 * This executes the SMBus "block write" protocol, returning negative errno
2613 * else zero on success.
2614 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002615s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002616 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (length > I2C_SMBUS_BLOCK_MAX)
2621 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002623 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002624 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2625 I2C_SMBUS_WRITE, command,
2626 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
David Brownellc0564602007-05-01 23:26:31 +02002628EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002631s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002632 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002635 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002636
Jean Delvare4b2643d2007-07-12 14:12:29 +02002637 if (length > I2C_SMBUS_BLOCK_MAX)
2638 length = I2C_SMBUS_BLOCK_MAX;
2639 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002640 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2641 I2C_SMBUS_READ, command,
2642 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2643 if (status < 0)
2644 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002645
2646 memcpy(values, &data.block[1], data.block[0]);
2647 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
David Brownellc0564602007-05-01 23:26:31 +02002649EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Jean Delvare0cc43a12011-01-10 22:11:23 +01002651s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002652 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002653{
2654 union i2c_smbus_data data;
2655
2656 if (length > I2C_SMBUS_BLOCK_MAX)
2657 length = I2C_SMBUS_BLOCK_MAX;
2658 data.block[0] = length;
2659 memcpy(data.block + 1, values, length);
2660 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2661 I2C_SMBUS_WRITE, command,
2662 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2663}
David Brownellc0564602007-05-01 23:26:31 +02002664EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002665
David Brownell438d6c22006-12-10 21:21:31 +01002666/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002668static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2669 unsigned short flags,
2670 char read_write, u8 command, int size,
2671 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
2673 /* So we need to generate a series of msgs. In the case of writing, we
2674 need to use only one message; when reading, we need two. We initialize
2675 most things with sane defaults, to keep the code below somewhat
2676 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002677 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2678 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002679 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002681 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002682 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002683 struct i2c_msg msg[2] = {
2684 {
2685 .addr = addr,
2686 .flags = flags,
2687 .len = 1,
2688 .buf = msgbuf0,
2689 }, {
2690 .addr = addr,
2691 .flags = flags | I2C_M_RD,
2692 .len = 0,
2693 .buf = msgbuf1,
2694 },
2695 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002698 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 case I2C_SMBUS_QUICK:
2700 msg[0].len = 0;
2701 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002702 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2703 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 num = 1;
2705 break;
2706 case I2C_SMBUS_BYTE:
2707 if (read_write == I2C_SMBUS_READ) {
2708 /* Special case: only a read! */
2709 msg[0].flags = I2C_M_RD | flags;
2710 num = 1;
2711 }
2712 break;
2713 case I2C_SMBUS_BYTE_DATA:
2714 if (read_write == I2C_SMBUS_READ)
2715 msg[1].len = 1;
2716 else {
2717 msg[0].len = 2;
2718 msgbuf0[1] = data->byte;
2719 }
2720 break;
2721 case I2C_SMBUS_WORD_DATA:
2722 if (read_write == I2C_SMBUS_READ)
2723 msg[1].len = 2;
2724 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002725 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002727 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729 break;
2730 case I2C_SMBUS_PROC_CALL:
2731 num = 2; /* Special case */
2732 read_write = I2C_SMBUS_READ;
2733 msg[0].len = 3;
2734 msg[1].len = 2;
2735 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002736 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 break;
2738 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002740 msg[1].flags |= I2C_M_RECV_LEN;
2741 msg[1].len = 1; /* block length will be added by
2742 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 } else {
2744 msg[0].len = data->block[0] + 2;
2745 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002746 dev_err(&adapter->dev,
2747 "Invalid block write size %d\n",
2748 data->block[0]);
2749 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002751 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 msgbuf0[i] = data->block[i-1];
2753 }
2754 break;
2755 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002756 num = 2; /* Another special case */
2757 read_write = I2C_SMBUS_READ;
2758 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002759 dev_err(&adapter->dev,
2760 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002761 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002762 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002763 }
2764 msg[0].len = data->block[0] + 2;
2765 for (i = 1; i < msg[0].len; i++)
2766 msgbuf0[i] = data->block[i-1];
2767 msg[1].flags |= I2C_M_RECV_LEN;
2768 msg[1].len = 1; /* block length will be added by
2769 the underlying bus driver */
2770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 case I2C_SMBUS_I2C_BLOCK_DATA:
2772 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002773 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 } else {
2775 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002776 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002777 dev_err(&adapter->dev,
2778 "Invalid block write size %d\n",
2779 data->block[0]);
2780 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782 for (i = 1; i <= data->block[0]; i++)
2783 msgbuf0[i] = data->block[i];
2784 }
2785 break;
2786 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002787 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2788 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
2790
Jean Delvare421ef472005-10-26 21:28:55 +02002791 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2792 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2793 if (i) {
2794 /* Compute PEC if first message is a write */
2795 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002796 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002797 i2c_smbus_add_pec(&msg[0]);
2798 else /* Write followed by read */
2799 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2800 }
2801 /* Ask for PEC if last message is a read */
2802 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002803 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002804 }
2805
David Brownell24a5bb72008-07-14 22:38:23 +02002806 status = i2c_transfer(adapter, msg, num);
2807 if (status < 0)
2808 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Jean Delvare421ef472005-10-26 21:28:55 +02002810 /* Check PEC if last message is a read */
2811 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002812 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2813 if (status < 0)
2814 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002815 }
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002818 switch (size) {
2819 case I2C_SMBUS_BYTE:
2820 data->byte = msgbuf0[0];
2821 break;
2822 case I2C_SMBUS_BYTE_DATA:
2823 data->byte = msgbuf1[0];
2824 break;
2825 case I2C_SMBUS_WORD_DATA:
2826 case I2C_SMBUS_PROC_CALL:
2827 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2828 break;
2829 case I2C_SMBUS_I2C_BLOCK_DATA:
2830 for (i = 0; i < data->block[0]; i++)
2831 data->block[i+1] = msgbuf1[i];
2832 break;
2833 case I2C_SMBUS_BLOCK_DATA:
2834 case I2C_SMBUS_BLOCK_PROC_CALL:
2835 for (i = 0; i < msgbuf1[0] + 1; i++)
2836 data->block[i] = msgbuf1[i];
2837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
2839 return 0;
2840}
2841
David Brownella1cdeda2008-07-14 22:38:24 +02002842/**
2843 * i2c_smbus_xfer - execute SMBus protocol operations
2844 * @adapter: Handle to I2C bus
2845 * @addr: Address of SMBus slave on that bus
2846 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2847 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2848 * @command: Byte interpreted by slave, for protocols which use such bytes
2849 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2850 * @data: Data to be read or written
2851 *
2852 * This executes an SMBus protocol operation, and returns a negative
2853 * errno code else zero on success.
2854 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002855s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002856 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002857 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002859 unsigned long orig_jiffies;
2860 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
David Howells8a325992014-03-06 13:36:06 +00002863 /* If enabled, the following two tracepoints are conditional on
2864 * read_write and protocol.
2865 */
2866 trace_smbus_write(adapter, addr, flags, read_write,
2867 command, protocol, data);
2868 trace_smbus_read(adapter, addr, flags, read_write,
2869 command, protocol);
2870
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002871 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002874 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002875
2876 /* Retry automatically on arbitration loss */
2877 orig_jiffies = jiffies;
2878 for (res = 0, try = 0; try <= adapter->retries; try++) {
2879 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2880 read_write, command,
2881 protocol, data);
2882 if (res != -EAGAIN)
2883 break;
2884 if (time_after(jiffies,
2885 orig_jiffies + adapter->timeout))
2886 break;
2887 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002888 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002890 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002891 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002892 /*
2893 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2894 * implement native support for the SMBus operation.
2895 */
2896 }
2897
David Howells8a325992014-03-06 13:36:06 +00002898 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2899 command, protocol, data);
2900
2901trace:
2902 /* If enabled, the reply tracepoint is conditional on read_write. */
2903 trace_smbus_reply(adapter, addr, flags, read_write,
2904 command, protocol, data);
2905 trace_smbus_result(adapter, addr, flags, read_write,
2906 command, protocol, res);
2907
2908 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2913MODULE_DESCRIPTION("I2C-Bus main module");
2914MODULE_LICENSE("GPL");