blob: b696ac7e6d867645dc15b18a4793c9b05905289a [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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
Jean Delvare5694f8a2012-03-26 21:47:19 +020017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* ------------------------------------------------------------------------- */
20
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020023 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Jean Delvare7c81c602014-01-29 20:40:08 +010024 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020025 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010026 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020030 I2C ACPI code Copyright (C) 2014 Intel Corp
31 Author: Lan Tianyu <tianyu.lan@intel.com>
Wolfram Sang687b81d2013-07-11 12:56:15 +010032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053036#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053038#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/slab.h>
40#include <linux/i2c.h>
41#include <linux/init.h>
42#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010043#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010044#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060045#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010046#include <linux/of_irq.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020047#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010048#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010049#include <linux/hardirq.h>
50#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020051#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010052#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010053#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000054#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/uaccess.h>
56
David Brownell9c1600e2007-05-01 23:26:31 +020057#include "i2c-core.h"
58
David Howellsd9a83d62014-03-06 13:35:59 +000059#define CREATE_TRACE_POINTS
60#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jean Delvare6629dcf2010-05-04 11:09:28 +020062/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020063 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000064 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4f8cf822009-09-18 22:45:46 +020068static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020069static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010070
David Howellsd9a83d62014-03-06 13:35:59 +000071static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
72
73void i2c_transfer_trace_reg(void)
74{
75 static_key_slow_inc(&i2c_trace_msg);
76}
77
78void i2c_transfer_trace_unreg(void)
79{
80 static_key_slow_dec(&i2c_trace_msg);
81}
82
Wolfram Sang17f4a5c2014-09-22 19:41:00 +020083#if defined(CONFIG_ACPI)
84struct acpi_i2c_handler_data {
85 struct acpi_connection_info info;
86 struct i2c_adapter *adapter;
87};
88
89struct gsb_buffer {
90 u8 status;
91 u8 len;
92 union {
93 u16 wdata;
94 u8 bdata;
95 u8 data[0];
96 };
97} __packed;
98
99static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
100{
101 struct i2c_board_info *info = data;
102
103 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
104 struct acpi_resource_i2c_serialbus *sb;
105
106 sb = &ares->data.i2c_serial_bus;
107 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
108 info->addr = sb->slave_address;
109 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
110 info->flags |= I2C_CLIENT_TEN;
111 }
112 } else if (info->irq < 0) {
113 struct resource r;
114
115 if (acpi_dev_resource_interrupt(ares, 0, &r))
116 info->irq = r.start;
117 }
118
119 /* Tell the ACPI core to skip this resource */
120 return 1;
121}
122
123static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
124 void *data, void **return_value)
125{
126 struct i2c_adapter *adapter = data;
127 struct list_head resource_list;
128 struct i2c_board_info info;
129 struct acpi_device *adev;
130 int ret;
131
132 if (acpi_bus_get_device(handle, &adev))
133 return AE_OK;
134 if (acpi_bus_get_status(adev) || !adev->status.present)
135 return AE_OK;
136
137 memset(&info, 0, sizeof(info));
138 info.acpi_node.companion = adev;
139 info.irq = -1;
140
141 INIT_LIST_HEAD(&resource_list);
142 ret = acpi_dev_get_resources(adev, &resource_list,
143 acpi_i2c_add_resource, &info);
144 acpi_dev_free_resource_list(&resource_list);
145
146 if (ret < 0 || !info.addr)
147 return AE_OK;
148
149 adev->power.flags.ignore_parent = true;
150 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
151 if (!i2c_new_device(adapter, &info)) {
152 adev->power.flags.ignore_parent = false;
153 dev_err(&adapter->dev,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev->dev));
156 }
157
158 return AE_OK;
159}
160
161/**
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
164 *
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
168 */
169static void acpi_i2c_register_devices(struct i2c_adapter *adap)
170{
171 acpi_handle handle;
172 acpi_status status;
173
174 if (!adap->dev.parent)
175 return;
176
177 handle = ACPI_HANDLE(adap->dev.parent);
178 if (!handle)
179 return;
180
181 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
182 acpi_i2c_add_device, NULL,
183 adap, NULL);
184 if (ACPI_FAILURE(status))
185 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
186}
187
188#else /* CONFIG_ACPI */
189static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { }
190#endif /* CONFIG_ACPI */
191
192#ifdef CONFIG_ACPI_I2C_OPREGION
193static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
194 u8 cmd, u8 *data, u8 data_len)
195{
196
197 struct i2c_msg msgs[2];
198 int ret;
199 u8 *buffer;
200
201 buffer = kzalloc(data_len, GFP_KERNEL);
202 if (!buffer)
203 return AE_NO_MEMORY;
204
205 msgs[0].addr = client->addr;
206 msgs[0].flags = client->flags;
207 msgs[0].len = 1;
208 msgs[0].buf = &cmd;
209
210 msgs[1].addr = client->addr;
211 msgs[1].flags = client->flags | I2C_M_RD;
212 msgs[1].len = data_len;
213 msgs[1].buf = buffer;
214
215 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
216 if (ret < 0)
217 dev_err(&client->adapter->dev, "i2c read failed\n");
218 else
219 memcpy(data, buffer, data_len);
220
221 kfree(buffer);
222 return ret;
223}
224
225static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
226 u8 cmd, u8 *data, u8 data_len)
227{
228
229 struct i2c_msg msgs[1];
230 u8 *buffer;
231 int ret = AE_OK;
232
233 buffer = kzalloc(data_len + 1, GFP_KERNEL);
234 if (!buffer)
235 return AE_NO_MEMORY;
236
237 buffer[0] = cmd;
238 memcpy(buffer + 1, data, data_len);
239
240 msgs[0].addr = client->addr;
241 msgs[0].flags = client->flags;
242 msgs[0].len = data_len + 1;
243 msgs[0].buf = buffer;
244
245 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
246 if (ret < 0)
247 dev_err(&client->adapter->dev, "i2c write failed\n");
248
249 kfree(buffer);
250 return ret;
251}
252
253static acpi_status
254acpi_i2c_space_handler(u32 function, acpi_physical_address command,
255 u32 bits, u64 *value64,
256 void *handler_context, void *region_context)
257{
258 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
259 struct acpi_i2c_handler_data *data = handler_context;
260 struct acpi_connection_info *info = &data->info;
261 struct acpi_resource_i2c_serialbus *sb;
262 struct i2c_adapter *adapter = data->adapter;
263 struct i2c_client client;
264 struct acpi_resource *ares;
265 u32 accessor_type = function >> 16;
266 u8 action = function & ACPI_IO_MASK;
267 acpi_status ret = AE_OK;
268 int status;
269
270 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
271 if (ACPI_FAILURE(ret))
272 return ret;
273
274 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
275 ret = AE_BAD_PARAMETER;
276 goto err;
277 }
278
279 sb = &ares->data.i2c_serial_bus;
280 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
281 ret = AE_BAD_PARAMETER;
282 goto err;
283 }
284
285 memset(&client, 0, sizeof(client));
286 client.adapter = adapter;
287 client.addr = sb->slave_address;
288 client.flags = 0;
289
290 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
291 client.flags |= I2C_CLIENT_TEN;
292
293 switch (accessor_type) {
294 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
295 if (action == ACPI_READ) {
296 status = i2c_smbus_read_byte(&client);
297 if (status >= 0) {
298 gsb->bdata = status;
299 status = 0;
300 }
301 } else {
302 status = i2c_smbus_write_byte(&client, gsb->bdata);
303 }
304 break;
305
306 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
307 if (action == ACPI_READ) {
308 status = i2c_smbus_read_byte_data(&client, command);
309 if (status >= 0) {
310 gsb->bdata = status;
311 status = 0;
312 }
313 } else {
314 status = i2c_smbus_write_byte_data(&client, command,
315 gsb->bdata);
316 }
317 break;
318
319 case ACPI_GSB_ACCESS_ATTRIB_WORD:
320 if (action == ACPI_READ) {
321 status = i2c_smbus_read_word_data(&client, command);
322 if (status >= 0) {
323 gsb->wdata = status;
324 status = 0;
325 }
326 } else {
327 status = i2c_smbus_write_word_data(&client, command,
328 gsb->wdata);
329 }
330 break;
331
332 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
333 if (action == ACPI_READ) {
334 status = i2c_smbus_read_block_data(&client, command,
335 gsb->data);
336 if (status >= 0) {
337 gsb->len = status;
338 status = 0;
339 }
340 } else {
341 status = i2c_smbus_write_block_data(&client, command,
342 gsb->len, gsb->data);
343 }
344 break;
345
346 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
347 if (action == ACPI_READ) {
348 status = acpi_gsb_i2c_read_bytes(&client, command,
349 gsb->data, info->access_length);
350 if (status > 0)
351 status = 0;
352 } else {
353 status = acpi_gsb_i2c_write_bytes(&client, command,
354 gsb->data, info->access_length);
355 }
356 break;
357
358 default:
359 pr_info("protocol(0x%02x) is not supported.\n", accessor_type);
360 ret = AE_BAD_PARAMETER;
361 goto err;
362 }
363
364 gsb->status = status;
365
366 err:
367 ACPI_FREE(ares);
368 return ret;
369}
370
371
372static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
373{
374 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
375 struct acpi_i2c_handler_data *data;
376 acpi_status status;
377
378 if (!handle)
379 return -ENODEV;
380
381 data = kzalloc(sizeof(struct acpi_i2c_handler_data),
382 GFP_KERNEL);
383 if (!data)
384 return -ENOMEM;
385
386 data->adapter = adapter;
387 status = acpi_bus_attach_private_data(handle, (void *)data);
388 if (ACPI_FAILURE(status)) {
389 kfree(data);
390 return -ENOMEM;
391 }
392
393 status = acpi_install_address_space_handler(handle,
394 ACPI_ADR_SPACE_GSBUS,
395 &acpi_i2c_space_handler,
396 NULL,
397 data);
398 if (ACPI_FAILURE(status)) {
399 dev_err(&adapter->dev, "Error installing i2c space handler\n");
400 acpi_bus_detach_private_data(handle);
401 kfree(data);
402 return -ENOMEM;
403 }
404
405 return 0;
406}
407
408static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
409{
410 acpi_handle handle = ACPI_HANDLE(adapter->dev.parent);
411 struct acpi_i2c_handler_data *data;
412 acpi_status status;
413
414 if (!handle)
415 return;
416
417 acpi_remove_address_space_handler(handle,
418 ACPI_ADR_SPACE_GSBUS,
419 &acpi_i2c_space_handler);
420
421 status = acpi_bus_get_private_data(handle, (void **)&data);
422 if (ACPI_SUCCESS(status))
423 kfree(data);
424
425 acpi_bus_detach_private_data(handle);
426}
427#else /* CONFIG_ACPI_I2C_OPREGION */
428static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter)
429{ }
430
431static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter)
432{ return 0; }
433#endif /* CONFIG_ACPI_I2C_OPREGION */
434
David Brownellf37dd802007-02-13 22:09:00 +0100435/* ------------------------------------------------------------------------- */
436
Jean Delvared2653e92008-04-29 23:11:39 +0200437static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
438 const struct i2c_client *client)
439{
440 while (id->name[0]) {
441 if (strcmp(client->name, id->name) == 0)
442 return id;
443 id++;
444 }
445 return NULL;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int i2c_device_match(struct device *dev, struct device_driver *drv)
449{
Jean Delvare51298d12009-09-18 22:45:45 +0200450 struct i2c_client *client = i2c_verify_client(dev);
451 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200452
Jean Delvare51298d12009-09-18 22:45:45 +0200453 if (!client)
454 return 0;
455
Grant Likely959e85f2010-06-08 07:48:19 -0600456 /* Attempt an OF style match */
457 if (of_driver_match_device(dev, drv))
458 return 1;
459
Mika Westerberg907ddf82012-11-23 12:23:40 +0100460 /* Then ACPI style match */
461 if (acpi_driver_match_device(dev, drv))
462 return 1;
463
Jean Delvare51298d12009-09-18 22:45:45 +0200464 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200465 /* match on an id table if there is one */
466 if (driver->id_table)
467 return i2c_match_id(driver->id_table, client) != NULL;
468
Jean Delvareeb8a7902008-05-18 20:49:41 +0200469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
David Brownell7b4fbc52007-05-01 23:26:30 +0200472
473/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200474static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200475{
476 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800477 int rc;
478
479 rc = acpi_device_uevent_modalias(dev, env);
480 if (rc != -ENODEV)
481 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200482
Jean Delvareeb8a7902008-05-18 20:49:41 +0200483 if (add_uevent_var(env, "MODALIAS=%s%s",
484 I2C_MODULE_PREFIX, client->name))
485 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200486 dev_dbg(dev, "uevent\n");
487 return 0;
488}
489
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530490/* i2c bus recovery routines */
491static int get_scl_gpio_value(struct i2c_adapter *adap)
492{
493 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
494}
495
496static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
497{
498 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
499}
500
501static int get_sda_gpio_value(struct i2c_adapter *adap)
502{
503 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
504}
505
506static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
507{
508 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
509 struct device *dev = &adap->dev;
510 int ret = 0;
511
512 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
513 GPIOF_OUT_INIT_HIGH, "i2c-scl");
514 if (ret) {
515 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
516 return ret;
517 }
518
519 if (bri->get_sda) {
520 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
521 /* work without SDA polling */
522 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
523 bri->sda_gpio);
524 bri->get_sda = NULL;
525 }
526 }
527
528 return ret;
529}
530
531static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
532{
533 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
534
535 if (bri->get_sda)
536 gpio_free(bri->sda_gpio);
537
538 gpio_free(bri->scl_gpio);
539}
540
541/*
542 * We are generating clock pulses. ndelay() determines durating of clk pulses.
543 * We will generate clock with rate 100 KHz and so duration of both clock levels
544 * is: delay in ns = (10^6 / 100) / 2
545 */
546#define RECOVERY_NDELAY 5000
547#define RECOVERY_CLK_CNT 9
548
549static int i2c_generic_recovery(struct i2c_adapter *adap)
550{
551 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
552 int i = 0, val = 1, ret = 0;
553
554 if (bri->prepare_recovery)
555 bri->prepare_recovery(bri);
556
557 /*
558 * By this time SCL is high, as we need to give 9 falling-rising edges
559 */
560 while (i++ < RECOVERY_CLK_CNT * 2) {
561 if (val) {
562 /* Break if SDA is high */
563 if (bri->get_sda && bri->get_sda(adap))
564 break;
565 /* SCL shouldn't be low here */
566 if (!bri->get_scl(adap)) {
567 dev_err(&adap->dev,
568 "SCL is stuck low, exit recovery\n");
569 ret = -EBUSY;
570 break;
571 }
572 }
573
574 val = !val;
575 bri->set_scl(adap, val);
576 ndelay(RECOVERY_NDELAY);
577 }
578
579 if (bri->unprepare_recovery)
580 bri->unprepare_recovery(bri);
581
582 return ret;
583}
584
585int i2c_generic_scl_recovery(struct i2c_adapter *adap)
586{
587 adap->bus_recovery_info->set_scl(adap, 1);
588 return i2c_generic_recovery(adap);
589}
590
591int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
592{
593 int ret;
594
595 ret = i2c_get_gpios_for_recovery(adap);
596 if (ret)
597 return ret;
598
599 ret = i2c_generic_recovery(adap);
600 i2c_put_gpios_for_recovery(adap);
601
602 return ret;
603}
604
605int i2c_recover_bus(struct i2c_adapter *adap)
606{
607 if (!adap->bus_recovery_info)
608 return -EOPNOTSUPP;
609
610 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
611 return adap->bus_recovery_info->recover_bus(adap);
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static int i2c_device_probe(struct device *dev)
615{
Jean Delvare51298d12009-09-18 22:45:45 +0200616 struct i2c_client *client = i2c_verify_client(dev);
617 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100618 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200619
Jean Delvare51298d12009-09-18 22:45:45 +0200620 if (!client)
621 return 0;
622
623 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200624 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200625 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200626
Marc Pignatee354252008-08-28 08:33:22 +0200627 if (!device_can_wakeup(&client->dev))
628 device_init_wakeup(&client->dev,
629 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200630 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200631
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200632 status = of_clk_set_defaults(dev->of_node, false);
633 if (status < 0)
634 return status;
635
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300636 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200637 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sang72fa8182014-01-21 17:48:34 +0100638 if (status)
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300639 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sang72fa8182014-01-21 17:48:34 +0100640
Hans Verkuil50c33042008-03-12 14:15:00 +0100641 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644static int i2c_device_remove(struct device *dev)
645{
Jean Delvare51298d12009-09-18 22:45:45 +0200646 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200647 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100648 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200649
Jean Delvare51298d12009-09-18 22:45:45 +0200650 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200651 return 0;
652
653 driver = to_i2c_driver(dev->driver);
654 if (driver->remove) {
655 dev_dbg(dev, "remove\n");
656 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200657 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100658
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300659 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200660 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
David Brownellf37dd802007-02-13 22:09:00 +0100663static void i2c_device_shutdown(struct device *dev)
664{
Jean Delvare51298d12009-09-18 22:45:45 +0200665 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100666 struct i2c_driver *driver;
667
Jean Delvare51298d12009-09-18 22:45:45 +0200668 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100669 return;
670 driver = to_i2c_driver(dev->driver);
671 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200672 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100673}
674
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200675#ifdef CONFIG_PM_SLEEP
676static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100677{
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 0;
683 driver = to_i2c_driver(dev->driver);
684 if (!driver->suspend)
685 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200686 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100687}
688
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200689static int i2c_legacy_resume(struct device *dev)
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->resume)
698 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200699 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100700}
701
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200702static int i2c_device_pm_suspend(struct device *dev)
703{
704 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
705
Mark Brownd529de22011-01-14 22:03:49 +0100706 if (pm)
707 return pm_generic_suspend(dev);
708 else
709 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200710}
711
712static int i2c_device_pm_resume(struct device *dev)
713{
714 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200715
716 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100717 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200718 else
Mark Brownd529de22011-01-14 22:03:49 +0100719 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200720}
721
722static int i2c_device_pm_freeze(struct device *dev)
723{
724 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
725
Mark Brownd529de22011-01-14 22:03:49 +0100726 if (pm)
727 return pm_generic_freeze(dev);
728 else
729 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200730}
731
732static int i2c_device_pm_thaw(struct device *dev)
733{
734 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
735
Mark Brownd529de22011-01-14 22:03:49 +0100736 if (pm)
737 return pm_generic_thaw(dev);
738 else
739 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200740}
741
742static int i2c_device_pm_poweroff(struct device *dev)
743{
744 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
745
Mark Brownd529de22011-01-14 22:03:49 +0100746 if (pm)
747 return pm_generic_poweroff(dev);
748 else
749 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200750}
751
752static int i2c_device_pm_restore(struct device *dev)
753{
754 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200755
756 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100757 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200758 else
Mark Brownd529de22011-01-14 22:03:49 +0100759 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200760}
761#else /* !CONFIG_PM_SLEEP */
762#define i2c_device_pm_suspend NULL
763#define i2c_device_pm_resume NULL
764#define i2c_device_pm_freeze NULL
765#define i2c_device_pm_thaw NULL
766#define i2c_device_pm_poweroff NULL
767#define i2c_device_pm_restore NULL
768#endif /* !CONFIG_PM_SLEEP */
769
David Brownell9c1600e2007-05-01 23:26:31 +0200770static void i2c_client_dev_release(struct device *dev)
771{
772 kfree(to_i2c_client(dev));
773}
774
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100775static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200776show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200777{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200778 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
779 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200780}
781
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100782static ssize_t
783show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200784{
785 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800786 int len;
787
788 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
789 if (len != -ENODEV)
790 return len;
791
Jean Delvareeb8a7902008-05-18 20:49:41 +0200792 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200793}
794
Jean Delvare4f8cf822009-09-18 22:45:46 +0200795static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200796static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
797
798static struct attribute *i2c_dev_attrs[] = {
799 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200800 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200801 &dev_attr_modalias.attr,
802 NULL
803};
804
805static struct attribute_group i2c_dev_attr_group = {
806 .attrs = i2c_dev_attrs,
807};
808
809static const struct attribute_group *i2c_dev_attr_groups[] = {
810 &i2c_dev_attr_group,
811 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200812};
813
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100814static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100815 .suspend = i2c_device_pm_suspend,
816 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200817 .freeze = i2c_device_pm_freeze,
818 .thaw = i2c_device_pm_thaw,
819 .poweroff = i2c_device_pm_poweroff,
820 .restore = i2c_device_pm_restore,
821 SET_RUNTIME_PM_OPS(
822 pm_generic_runtime_suspend,
823 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200824 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200825 )
sonic zhang54067ee2009-12-14 21:17:30 +0100826};
827
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200828struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100829 .name = "i2c",
830 .match = i2c_device_match,
831 .probe = i2c_device_probe,
832 .remove = i2c_device_remove,
833 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100834 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000835};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200836EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000837
Jean Delvare51298d12009-09-18 22:45:45 +0200838static struct device_type i2c_client_type = {
839 .groups = i2c_dev_attr_groups,
840 .uevent = i2c_device_uevent,
841 .release = i2c_client_dev_release,
842};
843
David Brownell9b766b82008-01-27 18:14:51 +0100844
845/**
846 * i2c_verify_client - return parameter as i2c_client, or NULL
847 * @dev: device, probably from some driver model iterator
848 *
849 * When traversing the driver model tree, perhaps using driver model
850 * iterators like @device_for_each_child(), you can't assume very much
851 * about the nodes you find. Use this function to avoid oopses caused
852 * by wrongly treating some non-I2C device as an i2c_client.
853 */
854struct i2c_client *i2c_verify_client(struct device *dev)
855{
Jean Delvare51298d12009-09-18 22:45:45 +0200856 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100857 ? to_i2c_client(dev)
858 : NULL;
859}
860EXPORT_SYMBOL(i2c_verify_client);
861
862
Jean Delvare3a89db52010-06-03 11:33:52 +0200863/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300864 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200865static int i2c_check_client_addr_validity(const struct i2c_client *client)
866{
867 if (client->flags & I2C_CLIENT_TEN) {
868 /* 10-bit address, all values are valid */
869 if (client->addr > 0x3ff)
870 return -EINVAL;
871 } else {
872 /* 7-bit address, reject the general call address */
873 if (client->addr == 0x00 || client->addr > 0x7f)
874 return -EINVAL;
875 }
876 return 0;
877}
878
Jean Delvare656b8762010-06-03 11:33:53 +0200879/* And this is a strict address validity check, used when probing. If a
880 * device uses a reserved address, then it shouldn't be probed. 7-bit
881 * addressing is assumed, 10-bit address devices are rare and should be
882 * explicitly enumerated. */
883static int i2c_check_addr_validity(unsigned short addr)
884{
885 /*
886 * Reserved addresses per I2C specification:
887 * 0x00 General call address / START byte
888 * 0x01 CBUS address
889 * 0x02 Reserved for different bus format
890 * 0x03 Reserved for future purposes
891 * 0x04-0x07 Hs-mode master code
892 * 0x78-0x7b 10-bit slave addressing
893 * 0x7c-0x7f Reserved for future purposes
894 */
895 if (addr < 0x08 || addr > 0x77)
896 return -EINVAL;
897 return 0;
898}
899
Jean Delvare3b5f7942010-06-03 11:33:55 +0200900static int __i2c_check_addr_busy(struct device *dev, void *addrp)
901{
902 struct i2c_client *client = i2c_verify_client(dev);
903 int addr = *(int *)addrp;
904
905 if (client && client->addr == addr)
906 return -EBUSY;
907 return 0;
908}
909
Michael Lawnick08263742010-08-11 18:21:02 +0200910/* walk up mux tree */
911static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
912{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200913 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200914 int result;
915
916 result = device_for_each_child(&adapter->dev, &addr,
917 __i2c_check_addr_busy);
918
Jean Delvare97cc4d42010-10-24 18:16:57 +0200919 if (!result && parent)
920 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200921
922 return result;
923}
924
925/* recurse down mux tree */
926static int i2c_check_mux_children(struct device *dev, void *addrp)
927{
928 int result;
929
930 if (dev->type == &i2c_adapter_type)
931 result = device_for_each_child(dev, addrp,
932 i2c_check_mux_children);
933 else
934 result = __i2c_check_addr_busy(dev, addrp);
935
936 return result;
937}
938
Jean Delvare3b5f7942010-06-03 11:33:55 +0200939static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
940{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200941 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200942 int result = 0;
943
Jean Delvare97cc4d42010-10-24 18:16:57 +0200944 if (parent)
945 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200946
947 if (!result)
948 result = device_for_each_child(&adapter->dev, &addr,
949 i2c_check_mux_children);
950
951 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200952}
953
David Brownell9c1600e2007-05-01 23:26:31 +0200954/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200955 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
956 * @adapter: Target I2C bus segment
957 */
958void i2c_lock_adapter(struct i2c_adapter *adapter)
959{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200960 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
961
962 if (parent)
963 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200964 else
965 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200966}
967EXPORT_SYMBOL_GPL(i2c_lock_adapter);
968
969/**
970 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
971 * @adapter: Target I2C bus segment
972 */
973static int i2c_trylock_adapter(struct i2c_adapter *adapter)
974{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200975 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
976
977 if (parent)
978 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200979 else
980 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200981}
982
983/**
984 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
985 * @adapter: Target I2C bus segment
986 */
987void i2c_unlock_adapter(struct i2c_adapter *adapter)
988{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200989 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
990
991 if (parent)
992 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200993 else
994 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200995}
996EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
997
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200998static void i2c_dev_set_name(struct i2c_adapter *adap,
999 struct i2c_client *client)
1000{
1001 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
1002
1003 if (adev) {
1004 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
1005 return;
1006 }
1007
1008 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1009 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
1010 client->addr | ((client->flags & I2C_CLIENT_TEN)
1011 ? 0xa000 : 0));
1012}
1013
Jean Delvarefe61e072010-08-11 18:20:58 +02001014/**
Jean Delvaref8a227e2009-06-19 16:58:18 +02001015 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +02001016 * @adap: the adapter managing the device
1017 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +02001018 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001019 *
Jean Delvaref8a227e2009-06-19 16:58:18 +02001020 * Create an i2c device. Binding is handled through driver model
1021 * probe()/remove() methods. A driver may be bound to this device when we
1022 * return from this function, or any later moment (e.g. maybe hotplugging will
1023 * load the driver module). This call is not appropriate for use by mainboard
1024 * initialization logic, which usually runs during an arch_initcall() long
1025 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +02001026 *
1027 * This returns the new i2c client, which may be saved for later use with
1028 * i2c_unregister_device(); or NULL to indicate an error.
1029 */
1030struct i2c_client *
1031i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
1032{
1033 struct i2c_client *client;
1034 int status;
1035
1036 client = kzalloc(sizeof *client, GFP_KERNEL);
1037 if (!client)
1038 return NULL;
1039
1040 client->adapter = adap;
1041
1042 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +02001043
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +02001044 if (info->archdata)
1045 client->dev.archdata = *info->archdata;
1046
Marc Pignatee354252008-08-28 08:33:22 +02001047 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +02001048 client->addr = info->addr;
1049 client->irq = info->irq;
1050
David Brownell9c1600e2007-05-01 23:26:31 +02001051 strlcpy(client->name, info->type, sizeof(client->name));
1052
Jean Delvare3a89db52010-06-03 11:33:52 +02001053 /* Check for address validity */
1054 status = i2c_check_client_addr_validity(client);
1055 if (status) {
1056 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
1057 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
1058 goto out_err_silent;
1059 }
1060
Jean Delvaref8a227e2009-06-19 16:58:18 +02001061 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001062 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001063 if (status)
1064 goto out_err;
1065
1066 client->dev.parent = &client->adapter->dev;
1067 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +02001068 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -07001069 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001070 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001071
Jarkko Nikula70762ab2013-11-14 14:03:52 +02001072 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +02001073 status = device_register(&client->dev);
1074 if (status)
1075 goto out_err;
1076
Jean Delvaref8a227e2009-06-19 16:58:18 +02001077 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
1078 client->name, dev_name(&client->dev));
1079
David Brownell9c1600e2007-05-01 23:26:31 +02001080 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +02001081
1082out_err:
1083 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
1084 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +02001085out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +02001086 kfree(client);
1087 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +02001088}
1089EXPORT_SYMBOL_GPL(i2c_new_device);
1090
1091
1092/**
1093 * i2c_unregister_device - reverse effect of i2c_new_device()
1094 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +02001095 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +02001096 */
1097void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +02001098{
David Brownella1d9e6e2007-05-01 23:26:30 +02001099 device_unregister(&client->dev);
1100}
David Brownell9c1600e2007-05-01 23:26:31 +02001101EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001102
1103
Jean Delvare60b129d2008-05-11 20:37:06 +02001104static const struct i2c_device_id dummy_id[] = {
1105 { "dummy", 0 },
1106 { },
1107};
1108
Jean Delvared2653e92008-04-29 23:11:39 +02001109static int dummy_probe(struct i2c_client *client,
1110 const struct i2c_device_id *id)
1111{
1112 return 0;
1113}
1114
1115static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001116{
1117 return 0;
1118}
1119
1120static struct i2c_driver dummy_driver = {
1121 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001122 .probe = dummy_probe,
1123 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001124 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001125};
1126
1127/**
1128 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1129 * @adapter: the adapter managing the device
1130 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001131 * Context: can sleep
1132 *
1133 * This returns an I2C client bound to the "dummy" driver, intended for use
1134 * with devices that consume multiple addresses. Examples of such chips
1135 * include various EEPROMS (like 24c04 and 24c08 models).
1136 *
1137 * These dummy devices have two main uses. First, most I2C and SMBus calls
1138 * except i2c_transfer() need a client handle; the dummy will be that handle.
1139 * And second, this prevents the specified address from being bound to a
1140 * different driver.
1141 *
1142 * This returns the new i2c client, which should be saved for later use with
1143 * i2c_unregister_device(); or NULL to indicate an error.
1144 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001145struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001146{
1147 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001148 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001149 };
1150
David Brownelle9f13732008-01-27 18:14:52 +01001151 return i2c_new_device(adapter, &info);
1152}
1153EXPORT_SYMBOL_GPL(i2c_new_dummy);
1154
David Brownellf37dd802007-02-13 22:09:00 +01001155/* ------------------------------------------------------------------------- */
1156
David Brownell16ffadf2007-05-01 23:26:28 +02001157/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1158
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001159static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
David Brownellef2c83212007-05-01 23:26:28 +02001161 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 complete(&adap->dev_released);
1163}
1164
Jean Delvare99cd8e22009-06-19 16:58:20 +02001165/*
Jean Delvare390946b2012-09-10 10:14:02 +02001166 * This function is only needed for mutex_lock_nested, so it is never
1167 * called unless locking correctness checking is enabled. Thus we
1168 * make it inline to avoid a compiler warning. That's what gcc ends up
1169 * doing anyway.
1170 */
1171static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
1172{
1173 unsigned int depth = 0;
1174
1175 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1176 depth++;
1177
1178 return depth;
1179}
1180
1181/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001182 * Let users instantiate I2C devices through sysfs. This can be used when
1183 * platform initialization code doesn't contain the proper data for
1184 * whatever reason. Also useful for drivers that do device detection and
1185 * detection fails, either because the device uses an unexpected address,
1186 * or this is a compatible device with different ID register values.
1187 *
1188 * Parameter checking may look overzealous, but we really don't want
1189 * the user to provide incorrect parameters.
1190 */
1191static ssize_t
1192i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
1193 const char *buf, size_t count)
1194{
1195 struct i2c_adapter *adap = to_i2c_adapter(dev);
1196 struct i2c_board_info info;
1197 struct i2c_client *client;
1198 char *blank, end;
1199 int res;
1200
Jean Delvare99cd8e22009-06-19 16:58:20 +02001201 memset(&info, 0, sizeof(struct i2c_board_info));
1202
1203 blank = strchr(buf, ' ');
1204 if (!blank) {
1205 dev_err(dev, "%s: Missing parameters\n", "new_device");
1206 return -EINVAL;
1207 }
1208 if (blank - buf > I2C_NAME_SIZE - 1) {
1209 dev_err(dev, "%s: Invalid device name\n", "new_device");
1210 return -EINVAL;
1211 }
1212 memcpy(info.type, buf, blank - buf);
1213
1214 /* Parse remaining parameters, reject extra parameters */
1215 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1216 if (res < 1) {
1217 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1218 return -EINVAL;
1219 }
1220 if (res > 1 && end != '\n') {
1221 dev_err(dev, "%s: Extra parameters\n", "new_device");
1222 return -EINVAL;
1223 }
1224
Jean Delvare99cd8e22009-06-19 16:58:20 +02001225 client = i2c_new_device(adap, &info);
1226 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001227 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001228
1229 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001230 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001231 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001232 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001233 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1234 info.type, info.addr);
1235
1236 return count;
1237}
1238
1239/*
1240 * And of course let the users delete the devices they instantiated, if
1241 * they got it wrong. This interface can only be used to delete devices
1242 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1243 * don't delete devices to which some kernel code still has references.
1244 *
1245 * Parameter checking may look overzealous, but we really don't want
1246 * the user to delete the wrong device.
1247 */
1248static ssize_t
1249i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1250 const char *buf, size_t count)
1251{
1252 struct i2c_adapter *adap = to_i2c_adapter(dev);
1253 struct i2c_client *client, *next;
1254 unsigned short addr;
1255 char end;
1256 int res;
1257
1258 /* Parse parameters, reject extra parameters */
1259 res = sscanf(buf, "%hi%c", &addr, &end);
1260 if (res < 1) {
1261 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1262 return -EINVAL;
1263 }
1264 if (res > 1 && end != '\n') {
1265 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1266 return -EINVAL;
1267 }
1268
1269 /* Make sure the device was added through sysfs */
1270 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001271 mutex_lock_nested(&adap->userspace_clients_lock,
1272 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001273 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1274 detected) {
1275 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001276 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1277 "delete_device", client->name, client->addr);
1278
1279 list_del(&client->detected);
1280 i2c_unregister_device(client);
1281 res = count;
1282 break;
1283 }
1284 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001285 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001286
1287 if (res < 0)
1288 dev_err(dev, "%s: Can't find device in list\n",
1289 "delete_device");
1290 return res;
1291}
1292
Jean Delvare4f8cf822009-09-18 22:45:46 +02001293static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001294static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1295 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001296
1297static struct attribute *i2c_adapter_attrs[] = {
1298 &dev_attr_name.attr,
1299 &dev_attr_new_device.attr,
1300 &dev_attr_delete_device.attr,
1301 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001302};
1303
Jean Delvare4f8cf822009-09-18 22:45:46 +02001304static struct attribute_group i2c_adapter_attr_group = {
1305 .attrs = i2c_adapter_attrs,
1306};
1307
1308static const struct attribute_group *i2c_adapter_attr_groups[] = {
1309 &i2c_adapter_attr_group,
1310 NULL
1311};
1312
Michael Lawnick08263742010-08-11 18:21:02 +02001313struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +02001314 .groups = i2c_adapter_attr_groups,
1315 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001316};
Michael Lawnick08263742010-08-11 18:21:02 +02001317EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Stephen Warren643dd092012-04-17 12:43:33 -06001319/**
1320 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1321 * @dev: device, probably from some driver model iterator
1322 *
1323 * When traversing the driver model tree, perhaps using driver model
1324 * iterators like @device_for_each_child(), you can't assume very much
1325 * about the nodes you find. Use this function to avoid oopses caused
1326 * by wrongly treating some non-I2C device as an i2c_adapter.
1327 */
1328struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1329{
1330 return (dev->type == &i2c_adapter_type)
1331 ? to_i2c_adapter(dev)
1332 : NULL;
1333}
1334EXPORT_SYMBOL(i2c_verify_adapter);
1335
Jean Delvare2bb50952009-09-18 22:45:46 +02001336#ifdef CONFIG_I2C_COMPAT
1337static struct class_compat *i2c_adapter_compat_class;
1338#endif
1339
David Brownell9c1600e2007-05-01 23:26:31 +02001340static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1341{
1342 struct i2c_devinfo *devinfo;
1343
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001344 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001345 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1346 if (devinfo->busnum == adapter->nr
1347 && !i2c_new_device(adapter,
1348 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001349 dev_err(&adapter->dev,
1350 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001351 devinfo->board_info.addr);
1352 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001353 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001354}
1355
Wolfram Sang687b81d2013-07-11 12:56:15 +01001356/* OF support code */
1357
1358#if IS_ENABLED(CONFIG_OF)
1359static void of_i2c_register_devices(struct i2c_adapter *adap)
1360{
1361 void *result;
1362 struct device_node *node;
1363
1364 /* Only register child devices if the adapter has a node pointer set */
1365 if (!adap->dev.of_node)
1366 return;
1367
1368 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1369
1370 for_each_available_child_of_node(adap->dev.of_node, node) {
1371 struct i2c_board_info info = {};
1372 struct dev_archdata dev_ad = {};
1373 const __be32 *addr;
1374 int len;
1375
1376 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1377
1378 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1379 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1380 node->full_name);
1381 continue;
1382 }
1383
1384 addr = of_get_property(node, "reg", &len);
1385 if (!addr || (len < sizeof(int))) {
1386 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1387 node->full_name);
1388 continue;
1389 }
1390
1391 info.addr = be32_to_cpup(addr);
1392 if (info.addr > (1 << 10) - 1) {
1393 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1394 info.addr, node->full_name);
1395 continue;
1396 }
1397
1398 info.irq = irq_of_parse_and_map(node, 0);
1399 info.of_node = of_node_get(node);
1400 info.archdata = &dev_ad;
1401
1402 if (of_get_property(node, "wakeup-source", NULL))
1403 info.flags |= I2C_CLIENT_WAKE;
1404
1405 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1406
1407 result = i2c_new_device(adap, &info);
1408 if (result == NULL) {
1409 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1410 node->full_name);
1411 of_node_put(node);
1412 irq_dispose_mapping(info.irq);
1413 continue;
1414 }
1415 }
1416}
1417
1418static int of_dev_node_match(struct device *dev, void *data)
1419{
1420 return dev->of_node == data;
1421}
1422
1423/* must call put_device() when done with returned i2c_client device */
1424struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1425{
1426 struct device *dev;
1427
1428 dev = bus_find_device(&i2c_bus_type, NULL, node,
1429 of_dev_node_match);
1430 if (!dev)
1431 return NULL;
1432
1433 return i2c_verify_client(dev);
1434}
1435EXPORT_SYMBOL(of_find_i2c_device_by_node);
1436
1437/* must call put_device() when done with returned i2c_adapter device */
1438struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1439{
1440 struct device *dev;
1441
1442 dev = bus_find_device(&i2c_bus_type, NULL, node,
1443 of_dev_node_match);
1444 if (!dev)
1445 return NULL;
1446
1447 return i2c_verify_adapter(dev);
1448}
1449EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1450#else
1451static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1452#endif /* CONFIG_OF */
1453
Jean Delvare69b00892009-12-06 17:06:27 +01001454static int i2c_do_add_adapter(struct i2c_driver *driver,
1455 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001456{
Jean Delvare4735c982008-07-14 22:38:36 +02001457 /* Detect supported devices on that bus, and instantiate them */
1458 i2c_detect(adap, driver);
1459
1460 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001461 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001462 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1463 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001464 dev_warn(&adap->dev, "Please use another way to instantiate "
1465 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001466 /* We ignore the return code; if it fails, too bad */
1467 driver->attach_adapter(adap);
1468 }
1469 return 0;
1470}
1471
Jean Delvare69b00892009-12-06 17:06:27 +01001472static int __process_new_adapter(struct device_driver *d, void *data)
1473{
1474 return i2c_do_add_adapter(to_i2c_driver(d), data);
1475}
1476
David Brownell6e13e642007-05-01 23:26:31 +02001477static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Jean Delvared6703282010-08-11 18:20:59 +02001479 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
David Brownell1d0b19c2008-10-14 17:30:05 +02001481 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001482 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1483 res = -EAGAIN;
1484 goto out_list;
1485 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001486
Jean Delvare2236baa2010-11-15 22:40:38 +01001487 /* Sanity checks */
1488 if (unlikely(adap->name[0] == '\0')) {
1489 pr_err("i2c-core: Attempt to register an adapter with "
1490 "no name!\n");
1491 return -EINVAL;
1492 }
1493 if (unlikely(!adap->algo)) {
1494 pr_err("i2c-core: Attempt to register adapter '%s' with "
1495 "no algo!\n", adap->name);
1496 return -EINVAL;
1497 }
1498
Mika Kuoppala194684e2009-12-06 17:06:22 +01001499 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001500 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001501 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jean Delvare8fcfef62009-03-28 21:34:43 +01001503 /* Set default timeout to 1 second if not already set */
1504 if (adap->timeout == 0)
1505 adap->timeout = HZ;
1506
Kay Sievers27d9c182009-01-07 14:29:16 +01001507 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001508 adap->dev.bus = &i2c_bus_type;
1509 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001510 res = device_register(&adap->dev);
1511 if (res)
1512 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001514 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1515
Jean Delvare2bb50952009-09-18 22:45:46 +02001516#ifdef CONFIG_I2C_COMPAT
1517 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1518 adap->dev.parent);
1519 if (res)
1520 dev_warn(&adap->dev,
1521 "Failed to create compatibility class link\n");
1522#endif
1523
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301524 /* bus recovery specific initialization */
1525 if (adap->bus_recovery_info) {
1526 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1527
1528 if (!bri->recover_bus) {
1529 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1530 adap->bus_recovery_info = NULL;
1531 goto exit_recovery;
1532 }
1533
1534 /* Generic GPIO recovery */
1535 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1536 if (!gpio_is_valid(bri->scl_gpio)) {
1537 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1538 adap->bus_recovery_info = NULL;
1539 goto exit_recovery;
1540 }
1541
1542 if (gpio_is_valid(bri->sda_gpio))
1543 bri->get_sda = get_sda_gpio_value;
1544 else
1545 bri->get_sda = NULL;
1546
1547 bri->get_scl = get_scl_gpio_value;
1548 bri->set_scl = set_scl_gpio_value;
1549 } else if (!bri->set_scl || !bri->get_scl) {
1550 /* Generic SCL recovery */
1551 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1552 adap->bus_recovery_info = NULL;
1553 }
1554 }
1555
1556exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001557 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001558 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001559 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001560 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001561
David Brownell6e13e642007-05-01 23:26:31 +02001562 if (adap->nr < __i2c_first_dynamic_bus_num)
1563 i2c_scan_static_board_info(adap);
1564
Jean Delvare4735c982008-07-14 22:38:36 +02001565 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001566 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001567 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001568 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001569
1570 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001571
Jean Delvareb119c6c2006-08-15 18:26:30 +02001572out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001573 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001574 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001575 mutex_unlock(&core_lock);
1576 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
David Brownell6e13e642007-05-01 23:26:31 +02001579/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001580 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1581 * @adap: the adapter to register (with adap->nr initialized)
1582 * Context: can sleep
1583 *
1584 * See i2c_add_numbered_adapter() for details.
1585 */
1586static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1587{
1588 int id;
1589
1590 mutex_lock(&core_lock);
1591 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1592 GFP_KERNEL);
1593 mutex_unlock(&core_lock);
1594 if (id < 0)
1595 return id == -ENOSPC ? -EBUSY : id;
1596
1597 return i2c_register_adapter(adap);
1598}
1599
1600/**
David Brownell6e13e642007-05-01 23:26:31 +02001601 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1602 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001603 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001604 *
1605 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001606 * doesn't matter or when its bus number is specified by an dt alias.
1607 * Examples of bases when the bus number doesn't matter: I2C adapters
1608 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001609 *
1610 * When this returns zero, a new bus number was allocated and stored
1611 * in adap->nr, and the specified adapter became available for clients.
1612 * Otherwise, a negative errno value is returned.
1613 */
1614int i2c_add_adapter(struct i2c_adapter *adapter)
1615{
Doug Andersonee5c2742013-03-01 08:57:31 -08001616 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001617 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001618
Doug Andersonee5c2742013-03-01 08:57:31 -08001619 if (dev->of_node) {
1620 id = of_alias_get_id(dev->of_node, "i2c");
1621 if (id >= 0) {
1622 adapter->nr = id;
1623 return __i2c_add_numbered_adapter(adapter);
1624 }
1625 }
1626
Jean Delvarecaada322008-01-27 18:14:49 +01001627 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001628 id = idr_alloc(&i2c_adapter_idr, adapter,
1629 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001630 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001631 if (id < 0)
1632 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001633
1634 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001635
David Brownell6e13e642007-05-01 23:26:31 +02001636 return i2c_register_adapter(adapter);
1637}
1638EXPORT_SYMBOL(i2c_add_adapter);
1639
1640/**
1641 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1642 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001643 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001644 *
1645 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001646 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1647 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001648 * is used to properly configure I2C devices.
1649 *
Grant Likely488bf312011-07-25 17:49:43 +02001650 * If the requested bus number is set to -1, then this function will behave
1651 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1652 *
David Brownell6e13e642007-05-01 23:26:31 +02001653 * If no devices have pre-been declared for this bus, then be sure to
1654 * register the adapter before any dynamically allocated ones. Otherwise
1655 * the required bus ID may not be available.
1656 *
1657 * When this returns zero, the specified adapter became available for
1658 * clients using the bus number provided in adap->nr. Also, the table
1659 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1660 * and the appropriate driver model device nodes are created. Otherwise, a
1661 * negative errno value is returned.
1662 */
1663int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1664{
Grant Likely488bf312011-07-25 17:49:43 +02001665 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1666 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001667
Doug Andersonee5c2742013-03-01 08:57:31 -08001668 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001669}
1670EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1671
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001672static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001673 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001674{
Jean Delvare4735c982008-07-14 22:38:36 +02001675 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001676
Jean Delvareacec2112009-03-28 21:34:40 +01001677 /* Remove the devices we created ourselves as the result of hardware
1678 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001679 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1680 if (client->adapter == adapter) {
1681 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1682 client->name, client->addr);
1683 list_del(&client->detected);
1684 i2c_unregister_device(client);
1685 }
1686 }
Jean Delvare026526f2008-01-27 18:14:49 +01001687}
1688
Jean Delvaree549c2b2009-06-19 16:58:19 +02001689static int __unregister_client(struct device *dev, void *dummy)
1690{
1691 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001692 if (client && strcmp(client->name, "dummy"))
1693 i2c_unregister_device(client);
1694 return 0;
1695}
1696
1697static int __unregister_dummy(struct device *dev, void *dummy)
1698{
1699 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001700 if (client)
1701 i2c_unregister_device(client);
1702 return 0;
1703}
1704
Jean Delvare69b00892009-12-06 17:06:27 +01001705static int __process_removed_adapter(struct device_driver *d, void *data)
1706{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001707 i2c_do_del_adapter(to_i2c_driver(d), data);
1708 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001709}
1710
David Brownelld64f73b2007-07-12 14:12:28 +02001711/**
1712 * i2c_del_adapter - unregister I2C adapter
1713 * @adap: the adapter being unregistered
1714 * Context: can sleep
1715 *
1716 * This unregisters an I2C adapter which was previously registered
1717 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1718 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001719void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001721 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001722 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001725 mutex_lock(&core_lock);
1726 found = idr_find(&i2c_adapter_idr, adap->nr);
1727 mutex_unlock(&core_lock);
1728 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001729 pr_debug("i2c-core: attempting to delete unregistered "
1730 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001731 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Lan Tianyu5d98e612014-05-20 20:59:23 +08001734 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001735 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001736 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001737 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001738 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001739 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001741 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001742 mutex_lock_nested(&adap->userspace_clients_lock,
1743 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001744 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1745 detected) {
1746 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1747 client->addr);
1748 list_del(&client->detected);
1749 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001750 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001751 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001752
Jean Delvaree549c2b2009-06-19 16:58:19 +02001753 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001754 * check the returned value. This is a two-pass process, because
1755 * we can't remove the dummy devices during the first pass: they
1756 * could have been instantiated by real devices wishing to clean
1757 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001758 device_for_each_child(&adap->dev, NULL, __unregister_client);
1759 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Jean Delvare2bb50952009-09-18 22:45:46 +02001761#ifdef CONFIG_I2C_COMPAT
1762 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1763 adap->dev.parent);
1764#endif
1765
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001766 /* device name is gone after device_unregister */
1767 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* clean up the sysfs representation */
1770 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /* wait for sysfs to drop all references */
1774 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David Brownell6e13e642007-05-01 23:26:31 +02001776 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001777 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001779 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001781 /* Clear the device structure in case this adapter is ever going to be
1782 added again */
1783 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
David Brownellc0564602007-05-01 23:26:31 +02001785EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
David Brownell7b4fbc52007-05-01 23:26:30 +02001787/* ------------------------------------------------------------------------- */
1788
Jean Delvare7ae31482011-03-20 14:50:52 +01001789int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1790{
1791 int res;
1792
1793 mutex_lock(&core_lock);
1794 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1795 mutex_unlock(&core_lock);
1796
1797 return res;
1798}
1799EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1800
Jean Delvare69b00892009-12-06 17:06:27 +01001801static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001802{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001803 if (dev->type != &i2c_adapter_type)
1804 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001805 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001806}
1807
David Brownell7b4fbc52007-05-01 23:26:30 +02001808/*
1809 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001810 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 */
1812
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001813int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001815 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
David Brownell1d0b19c2008-10-14 17:30:05 +02001817 /* Can't register until after driver model init */
1818 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1819 return -EAGAIN;
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001822 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Jean Delvare729d6dd2009-06-19 16:58:18 +02001825 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001826 * will have called probe() for all matching-but-unbound devices.
1827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 res = driver_register(&driver->driver);
1829 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001830 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001831
Mark Brownf4e8db32011-01-14 22:03:50 +01001832 /* Drivers should switch to dev_pm_ops instead. */
1833 if (driver->suspend)
1834 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1835 driver->driver.name);
1836 if (driver->resume)
1837 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1838 driver->driver.name);
1839
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001840 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Jean Delvare4735c982008-07-14 22:38:36 +02001842 INIT_LIST_HEAD(&driver->clients);
1843 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001844 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001845
Jean Delvare7eebcb72006-02-05 23:28:21 +01001846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001848EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Jean Delvare69b00892009-12-06 17:06:27 +01001850static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001851{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001852 if (dev->type == &i2c_adapter_type)
1853 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1854 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001855}
1856
David Brownella1d9e6e2007-05-01 23:26:30 +02001857/**
1858 * i2c_del_driver - unregister I2C driver
1859 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001860 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001861 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001862void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Jean Delvare7ae31482011-03-20 14:50:52 +01001864 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001867 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
David Brownellc0564602007-05-01 23:26:31 +02001869EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
David Brownell7b4fbc52007-05-01 23:26:30 +02001871/* ------------------------------------------------------------------------- */
1872
Jean Delvaree48d3312008-01-27 18:14:48 +01001873/**
1874 * i2c_use_client - increments the reference count of the i2c client structure
1875 * @client: the client being referenced
1876 *
1877 * Each live reference to a client should be refcounted. The driver model does
1878 * that automatically as part of driver binding, so that most drivers don't
1879 * need to do this explicitly: they hold a reference until they're unbound
1880 * from the device.
1881 *
1882 * A pointer to the client with the incremented reference counter is returned.
1883 */
1884struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
David Brownell6ea438e2008-07-14 22:38:24 +02001886 if (client && get_device(&client->dev))
1887 return client;
1888 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
David Brownellc0564602007-05-01 23:26:31 +02001890EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Jean Delvaree48d3312008-01-27 18:14:48 +01001892/**
1893 * i2c_release_client - release a use of the i2c client structure
1894 * @client: the client being no longer referenced
1895 *
1896 * Must be called when a user of a client is finished with it.
1897 */
1898void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
David Brownell6ea438e2008-07-14 22:38:24 +02001900 if (client)
1901 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
David Brownellc0564602007-05-01 23:26:31 +02001903EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
David Brownell9b766b82008-01-27 18:14:51 +01001905struct i2c_cmd_arg {
1906 unsigned cmd;
1907 void *arg;
1908};
1909
1910static int i2c_cmd(struct device *dev, void *_arg)
1911{
1912 struct i2c_client *client = i2c_verify_client(dev);
1913 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001914 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001915
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001916 if (!client || !client->dev.driver)
1917 return 0;
1918
1919 driver = to_i2c_driver(client->dev.driver);
1920 if (driver->command)
1921 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001922 return 0;
1923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1926{
David Brownell9b766b82008-01-27 18:14:51 +01001927 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
David Brownell9b766b82008-01-27 18:14:51 +01001929 cmd_arg.cmd = cmd;
1930 cmd_arg.arg = arg;
1931 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
David Brownellc0564602007-05-01 23:26:31 +02001933EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935static int __init i2c_init(void)
1936{
1937 int retval;
1938
1939 retval = bus_register(&i2c_bus_type);
1940 if (retval)
1941 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001942#ifdef CONFIG_I2C_COMPAT
1943 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1944 if (!i2c_adapter_compat_class) {
1945 retval = -ENOMEM;
1946 goto bus_err;
1947 }
1948#endif
David Brownelle9f13732008-01-27 18:14:52 +01001949 retval = i2c_add_driver(&dummy_driver);
1950 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001951 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001952 return 0;
1953
Jean Delvare2bb50952009-09-18 22:45:46 +02001954class_err:
1955#ifdef CONFIG_I2C_COMPAT
1956 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001957bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001958#endif
David Brownelle9f13732008-01-27 18:14:52 +01001959 bus_unregister(&i2c_bus_type);
1960 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963static void __exit i2c_exit(void)
1964{
David Brownelle9f13732008-01-27 18:14:52 +01001965 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001966#ifdef CONFIG_I2C_COMPAT
1967 class_compat_unregister(i2c_adapter_compat_class);
1968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001970 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
David Brownella10f9e72008-10-14 17:30:06 +02001973/* We must initialize early, because some subsystems register i2c drivers
1974 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1975 */
1976postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977module_exit(i2c_exit);
1978
1979/* ----------------------------------------------------
1980 * the functional interface to the i2c busses.
1981 * ----------------------------------------------------
1982 */
1983
David Brownella1cdeda2008-07-14 22:38:24 +02001984/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001985 * __i2c_transfer - unlocked flavor of i2c_transfer
1986 * @adap: Handle to I2C bus
1987 * @msgs: One or more messages to execute before STOP is issued to
1988 * terminate the operation; each message begins with a START.
1989 * @num: Number of messages to be executed.
1990 *
1991 * Returns negative errno, else the number of messages executed.
1992 *
1993 * Adapter lock must be held when calling this function. No debug logging
1994 * takes place. adap->algo->master_xfer existence isn't checked.
1995 */
1996int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1997{
1998 unsigned long orig_jiffies;
1999 int ret, try;
2000
David Howellsd9a83d62014-03-06 13:35:59 +00002001 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2002 * enabled. This is an efficient way of keeping the for-loop from
2003 * being executed when not needed.
2004 */
2005 if (static_key_false(&i2c_trace_msg)) {
2006 int i;
2007 for (i = 0; i < num; i++)
2008 if (msgs[i].flags & I2C_M_RD)
2009 trace_i2c_read(adap, &msgs[i], i);
2010 else
2011 trace_i2c_write(adap, &msgs[i], i);
2012 }
2013
Jean Delvareb37d2a32012-06-29 07:47:19 -03002014 /* Retry automatically on arbitration loss */
2015 orig_jiffies = jiffies;
2016 for (ret = 0, try = 0; try <= adap->retries; try++) {
2017 ret = adap->algo->master_xfer(adap, msgs, num);
2018 if (ret != -EAGAIN)
2019 break;
2020 if (time_after(jiffies, orig_jiffies + adap->timeout))
2021 break;
2022 }
2023
David Howellsd9a83d62014-03-06 13:35:59 +00002024 if (static_key_false(&i2c_trace_msg)) {
2025 int i;
2026 for (i = 0; i < ret; i++)
2027 if (msgs[i].flags & I2C_M_RD)
2028 trace_i2c_reply(adap, &msgs[i], i);
2029 trace_i2c_result(adap, i, ret);
2030 }
2031
Jean Delvareb37d2a32012-06-29 07:47:19 -03002032 return ret;
2033}
2034EXPORT_SYMBOL(__i2c_transfer);
2035
2036/**
David Brownella1cdeda2008-07-14 22:38:24 +02002037 * i2c_transfer - execute a single or combined I2C message
2038 * @adap: Handle to I2C bus
2039 * @msgs: One or more messages to execute before STOP is issued to
2040 * terminate the operation; each message begins with a START.
2041 * @num: Number of messages to be executed.
2042 *
2043 * Returns negative errno, else the number of messages executed.
2044 *
2045 * Note that there is no requirement that each message be sent to
2046 * the same slave address, although that is the most common model.
2047 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002048int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002050 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
David Brownella1cdeda2008-07-14 22:38:24 +02002052 /* REVISIT the fault reporting model here is weak:
2053 *
2054 * - When we get an error after receiving N bytes from a slave,
2055 * there is no way to report "N".
2056 *
2057 * - When we get a NAK after transmitting N bytes to a slave,
2058 * there is no way to report "N" ... or to let the master
2059 * continue executing the rest of this combined message, if
2060 * that's the appropriate response.
2061 *
2062 * - When for example "num" is two and we successfully complete
2063 * the first message but get an error part way through the
2064 * second, it's unclear whether that should be reported as
2065 * one (discarding status on the second message) or errno
2066 * (discarding status on the first one).
2067 */
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (adap->algo->master_xfer) {
2070#ifdef DEBUG
2071 for (ret = 0; ret < num; ret++) {
2072 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02002073 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
2074 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
2075 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077#endif
2078
Mike Rapoportcea443a2008-01-27 18:14:50 +01002079 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002080 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002081 if (!ret)
2082 /* I2C activity is ongoing. */
2083 return -EAGAIN;
2084 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02002085 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01002086 }
2087
Jean Delvareb37d2a32012-06-29 07:47:19 -03002088 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02002089 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 return ret;
2092 } else {
2093 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02002094 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096}
David Brownellc0564602007-05-01 23:26:31 +02002097EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
David Brownella1cdeda2008-07-14 22:38:24 +02002099/**
2100 * i2c_master_send - issue a single I2C message in master transmit mode
2101 * @client: Handle to slave device
2102 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002103 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002104 *
2105 * Returns negative errno, or else the number of bytes written.
2106 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002107int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02002110 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct i2c_msg msg;
2112
Jean Delvare815f55f2005-05-07 22:58:46 +02002113 msg.addr = client->addr;
2114 msg.flags = client->flags & I2C_M_TEN;
2115 msg.len = count;
2116 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002117
Jean Delvare815f55f2005-05-07 22:58:46 +02002118 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002120 /*
2121 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2122 * transmitted, else error code.
2123 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002124 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
David Brownellc0564602007-05-01 23:26:31 +02002126EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David Brownella1cdeda2008-07-14 22:38:24 +02002128/**
2129 * i2c_master_recv - issue a single I2C message in master receive mode
2130 * @client: Handle to slave device
2131 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002132 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002133 *
2134 * Returns negative errno, or else the number of bytes read.
2135 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002136int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Farid Hammane7225acf2010-05-21 18:40:58 +02002138 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct i2c_msg msg;
2140 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Jean Delvare815f55f2005-05-07 22:58:46 +02002142 msg.addr = client->addr;
2143 msg.flags = client->flags & I2C_M_TEN;
2144 msg.flags |= I2C_M_RD;
2145 msg.len = count;
2146 msg.buf = buf;
2147
2148 ret = i2c_transfer(adap, &msg, 1);
2149
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002150 /*
2151 * If everything went ok (i.e. 1 msg received), return #bytes received,
2152 * else error code.
2153 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002154 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
David Brownellc0564602007-05-01 23:26:31 +02002156EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/* ----------------------------------------------------
2159 * the i2c address scanning function
2160 * Will not work for 10-bit addresses!
2161 * ----------------------------------------------------
2162 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002163
Jean Delvare63e4e802010-06-03 11:33:51 +02002164/*
2165 * Legacy default probe function, mostly relevant for SMBus. The default
2166 * probe method is a quick write, but it is known to corrupt the 24RF08
2167 * EEPROMs due to a state machine bug, and could also irreversibly
2168 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2169 * we use a short byte read instead. Also, some bus drivers don't implement
2170 * quick write, so we fallback to a byte read in that case too.
2171 * On x86, there is another special case for FSC hardware monitoring chips,
2172 * which want regular byte reads (address 0x73.) Fortunately, these are the
2173 * only known chips using this I2C address on PC hardware.
2174 * Returns 1 if probe succeeded, 0 if not.
2175 */
2176static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2177{
2178 int err;
2179 union i2c_smbus_data dummy;
2180
2181#ifdef CONFIG_X86
2182 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2183 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2184 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2185 I2C_SMBUS_BYTE_DATA, &dummy);
2186 else
2187#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002188 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2189 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002190 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2191 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002192 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2193 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2194 I2C_SMBUS_BYTE, &dummy);
2195 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002196 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2197 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002198 err = -EOPNOTSUPP;
2199 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002200
2201 return err >= 0;
2202}
2203
Jean Delvareccfbbd02009-12-06 17:06:25 +01002204static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002205 struct i2c_driver *driver)
2206{
2207 struct i2c_board_info info;
2208 struct i2c_adapter *adapter = temp_client->adapter;
2209 int addr = temp_client->addr;
2210 int err;
2211
2212 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02002213 err = i2c_check_addr_validity(addr);
2214 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002215 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2216 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002217 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002218 }
2219
2220 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002221 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002222 return 0;
2223
Jean Delvareccfbbd02009-12-06 17:06:25 +01002224 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002225 if (!i2c_default_probe(adapter, addr))
2226 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002227
2228 /* Finally call the custom detection function */
2229 memset(&info, 0, sizeof(struct i2c_board_info));
2230 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002231 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002232 if (err) {
2233 /* -ENODEV is returned if the detection fails. We catch it
2234 here as this isn't an error. */
2235 return err == -ENODEV ? 0 : err;
2236 }
2237
2238 /* Consistency check */
2239 if (info.type[0] == '\0') {
2240 dev_err(&adapter->dev, "%s detection function provided "
2241 "no name for 0x%x\n", driver->driver.name,
2242 addr);
2243 } else {
2244 struct i2c_client *client;
2245
2246 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002247 if (adapter->class & I2C_CLASS_DEPRECATED)
2248 dev_warn(&adapter->dev,
2249 "This adapter will soon drop class based instantiation of devices. "
2250 "Please make sure client 0x%02x gets instantiated by other means. "
2251 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2252 info.addr);
2253
Jean Delvare4735c982008-07-14 22:38:36 +02002254 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2255 info.type, info.addr);
2256 client = i2c_new_device(adapter, &info);
2257 if (client)
2258 list_add_tail(&client->detected, &driver->clients);
2259 else
2260 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2261 info.type, info.addr);
2262 }
2263 return 0;
2264}
2265
2266static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2267{
Jean Delvarec3813d62009-12-14 21:17:25 +01002268 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002269 struct i2c_client *temp_client;
2270 int i, err = 0;
2271 int adap_id = i2c_adapter_id(adapter);
2272
Jean Delvarec3813d62009-12-14 21:17:25 +01002273 address_list = driver->address_list;
2274 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002275 return 0;
2276
Wolfram Sang45552272014-07-10 13:46:21 +02002277 /* Warn that the adapter lost class based instantiation */
2278 if (adapter->class == I2C_CLASS_DEPRECATED) {
2279 dev_dbg(&adapter->dev,
2280 "This adapter dropped support for I2C classes and "
2281 "won't auto-detect %s devices anymore. If you need it, check "
2282 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2283 driver->driver.name);
2284 return 0;
2285 }
2286
Jean Delvare51b54ba2010-10-24 18:16:58 +02002287 /* Stop here if the classes do not match */
2288 if (!(adapter->class & driver->class))
2289 return 0;
2290
Jean Delvare4735c982008-07-14 22:38:36 +02002291 /* Set up a temporary client to help detect callback */
2292 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2293 if (!temp_client)
2294 return -ENOMEM;
2295 temp_client->adapter = adapter;
2296
Jean Delvarec3813d62009-12-14 21:17:25 +01002297 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002298 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002299 "addr 0x%02x\n", adap_id, address_list[i]);
2300 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002301 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002302 if (unlikely(err))
2303 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002304 }
2305
Jean Delvare4735c982008-07-14 22:38:36 +02002306 kfree(temp_client);
2307 return err;
2308}
2309
Jean Delvared44f19d2010-08-11 18:20:57 +02002310int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2311{
2312 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2313 I2C_SMBUS_QUICK, NULL) >= 0;
2314}
2315EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2316
Jean Delvare12b5053a2007-05-01 23:26:31 +02002317struct i2c_client *
2318i2c_new_probed_device(struct i2c_adapter *adap,
2319 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002320 unsigned short const *addr_list,
2321 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002322{
2323 int i;
2324
Jean Delvare8031d792010-08-11 18:21:00 +02002325 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002326 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002327
Jean Delvare12b5053a2007-05-01 23:26:31 +02002328 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2329 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002330 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002331 dev_warn(&adap->dev, "Invalid 7-bit address "
2332 "0x%02x\n", addr_list[i]);
2333 continue;
2334 }
2335
2336 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002337 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002338 dev_dbg(&adap->dev, "Address 0x%02x already in "
2339 "use, not probing\n", addr_list[i]);
2340 continue;
2341 }
2342
Jean Delvare63e4e802010-06-03 11:33:51 +02002343 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002344 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002345 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002346 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002347
2348 if (addr_list[i] == I2C_CLIENT_END) {
2349 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2350 return NULL;
2351 }
2352
2353 info->addr = addr_list[i];
2354 return i2c_new_device(adap, info);
2355}
2356EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2357
Jean Delvared735b342011-03-20 14:50:52 +01002358struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002361
Jean Delvarecaada322008-01-27 18:14:49 +01002362 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002363 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002364 if (adapter && !try_module_get(adapter->owner))
2365 adapter = NULL;
2366
Jean Delvarecaada322008-01-27 18:14:49 +01002367 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002368 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
David Brownellc0564602007-05-01 23:26:31 +02002370EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372void i2c_put_adapter(struct i2c_adapter *adap)
2373{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002374 if (adap)
2375 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
David Brownellc0564602007-05-01 23:26:31 +02002377EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379/* The SMBus parts */
2380
David Brownell438d6c22006-12-10 21:21:31 +01002381#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002382static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002385
Farid Hammane7225acf2010-05-21 18:40:58 +02002386 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002387 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 data = data ^ POLY;
2389 data = data << 1;
2390 }
2391 return (u8)(data >> 8);
2392}
2393
Jean Delvare421ef472005-10-26 21:28:55 +02002394/* Incremental CRC8 over count bytes in the array pointed to by p */
2395static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 int i;
2398
Farid Hammane7225acf2010-05-21 18:40:58 +02002399 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002400 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return crc;
2402}
2403
Jean Delvare421ef472005-10-26 21:28:55 +02002404/* Assume a 7-bit address, which is reasonable for SMBus */
2405static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Jean Delvare421ef472005-10-26 21:28:55 +02002407 /* The address will be sent first */
2408 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2409 pec = i2c_smbus_pec(pec, &addr, 1);
2410
2411 /* The data buffer follows */
2412 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Jean Delvare421ef472005-10-26 21:28:55 +02002415/* Used for write only transactions */
2416static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Jean Delvare421ef472005-10-26 21:28:55 +02002418 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2419 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Jean Delvare421ef472005-10-26 21:28:55 +02002422/* Return <0 on CRC error
2423 If there was a write before this read (most cases) we need to take the
2424 partial CRC from the write part into account.
2425 Note that this function does modify the message (we need to decrease the
2426 message length to hide the CRC byte from the caller). */
2427static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Jean Delvare421ef472005-10-26 21:28:55 +02002429 u8 rpec = msg->buf[--msg->len];
2430 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (rpec != cpec) {
2433 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2434 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002435 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
David Brownell438d6c22006-12-10 21:21:31 +01002437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
David Brownella1cdeda2008-07-14 22:38:24 +02002440/**
2441 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2442 * @client: Handle to slave device
2443 *
2444 * This executes the SMBus "receive byte" protocol, returning negative errno
2445 * else the byte received from the device.
2446 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002447s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
2449 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002450 int status;
2451
2452 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2453 I2C_SMBUS_READ, 0,
2454 I2C_SMBUS_BYTE, &data);
2455 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
David Brownellc0564602007-05-01 23:26:31 +02002457EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
David Brownella1cdeda2008-07-14 22:38:24 +02002459/**
2460 * i2c_smbus_write_byte - SMBus "send byte" protocol
2461 * @client: Handle to slave device
2462 * @value: Byte to be sent
2463 *
2464 * This executes the SMBus "send byte" protocol, returning negative errno
2465 * else zero on success.
2466 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002467s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Farid Hammane7225acf2010-05-21 18:40:58 +02002469 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002470 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
David Brownellc0564602007-05-01 23:26:31 +02002472EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
David Brownella1cdeda2008-07-14 22:38:24 +02002474/**
2475 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2476 * @client: Handle to slave device
2477 * @command: Byte interpreted by slave
2478 *
2479 * This executes the SMBus "read byte" protocol, returning negative errno
2480 * else a data byte received from the device.
2481 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002482s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002485 int status;
2486
2487 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2488 I2C_SMBUS_READ, command,
2489 I2C_SMBUS_BYTE_DATA, &data);
2490 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
David Brownellc0564602007-05-01 23:26:31 +02002492EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
David Brownella1cdeda2008-07-14 22:38:24 +02002494/**
2495 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2496 * @client: Handle to slave device
2497 * @command: Byte interpreted by slave
2498 * @value: Byte being written
2499 *
2500 * This executes the SMBus "write byte" protocol, returning negative errno
2501 * else zero on success.
2502 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002503s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2504 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 union i2c_smbus_data data;
2507 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002508 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2509 I2C_SMBUS_WRITE, command,
2510 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
David Brownellc0564602007-05-01 23:26:31 +02002512EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
David Brownella1cdeda2008-07-14 22:38:24 +02002514/**
2515 * i2c_smbus_read_word_data - SMBus "read word" protocol
2516 * @client: Handle to slave device
2517 * @command: Byte interpreted by slave
2518 *
2519 * This executes the SMBus "read word" protocol, returning negative errno
2520 * else a 16-bit unsigned "word" received from the device.
2521 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002522s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002525 int status;
2526
2527 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2528 I2C_SMBUS_READ, command,
2529 I2C_SMBUS_WORD_DATA, &data);
2530 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
David Brownellc0564602007-05-01 23:26:31 +02002532EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
David Brownella1cdeda2008-07-14 22:38:24 +02002534/**
2535 * i2c_smbus_write_word_data - SMBus "write word" protocol
2536 * @client: Handle to slave device
2537 * @command: Byte interpreted by slave
2538 * @value: 16-bit "word" being written
2539 *
2540 * This executes the SMBus "write word" protocol, returning negative errno
2541 * else zero on success.
2542 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002543s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2544 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 union i2c_smbus_data data;
2547 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002548 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2549 I2C_SMBUS_WRITE, command,
2550 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
David Brownellc0564602007-05-01 23:26:31 +02002552EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
David Brownella64ec072007-10-13 23:56:31 +02002554/**
David Brownella1cdeda2008-07-14 22:38:24 +02002555 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002556 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002557 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002558 * @values: Byte array into which data will be read; big enough to hold
2559 * the data returned by the slave. SMBus allows at most 32 bytes.
2560 *
David Brownella1cdeda2008-07-14 22:38:24 +02002561 * This executes the SMBus "block read" protocol, returning negative errno
2562 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002563 *
2564 * Note that using this function requires that the client's adapter support
2565 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2566 * support this; its emulation through I2C messaging relies on a specific
2567 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2568 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002569s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002570 u8 *values)
2571{
2572 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002573 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002574
David Brownell24a5bb72008-07-14 22:38:23 +02002575 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2576 I2C_SMBUS_READ, command,
2577 I2C_SMBUS_BLOCK_DATA, &data);
2578 if (status)
2579 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002580
2581 memcpy(values, &data.block[1], data.block[0]);
2582 return data.block[0];
2583}
2584EXPORT_SYMBOL(i2c_smbus_read_block_data);
2585
David Brownella1cdeda2008-07-14 22:38:24 +02002586/**
2587 * i2c_smbus_write_block_data - SMBus "block write" protocol
2588 * @client: Handle to slave device
2589 * @command: Byte interpreted by slave
2590 * @length: Size of data block; SMBus allows at most 32 bytes
2591 * @values: Byte array which will be written.
2592 *
2593 * This executes the SMBus "block write" protocol, returning negative errno
2594 * else zero on success.
2595 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002596s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002597 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (length > I2C_SMBUS_BLOCK_MAX)
2602 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002604 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002605 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2606 I2C_SMBUS_WRITE, command,
2607 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
David Brownellc0564602007-05-01 23:26:31 +02002609EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002612s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002613 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002616 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002617
Jean Delvare4b2643d2007-07-12 14:12:29 +02002618 if (length > I2C_SMBUS_BLOCK_MAX)
2619 length = I2C_SMBUS_BLOCK_MAX;
2620 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002621 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2622 I2C_SMBUS_READ, command,
2623 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2624 if (status < 0)
2625 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002626
2627 memcpy(values, &data.block[1], data.block[0]);
2628 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
David Brownellc0564602007-05-01 23:26:31 +02002630EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Jean Delvare0cc43a12011-01-10 22:11:23 +01002632s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002633 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002634{
2635 union i2c_smbus_data data;
2636
2637 if (length > I2C_SMBUS_BLOCK_MAX)
2638 length = I2C_SMBUS_BLOCK_MAX;
2639 data.block[0] = length;
2640 memcpy(data.block + 1, values, length);
2641 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2642 I2C_SMBUS_WRITE, command,
2643 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2644}
David Brownellc0564602007-05-01 23:26:31 +02002645EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002646
David Brownell438d6c22006-12-10 21:21:31 +01002647/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002649static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2650 unsigned short flags,
2651 char read_write, u8 command, int size,
2652 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 /* So we need to generate a series of msgs. In the case of writing, we
2655 need to use only one message; when reading, we need two. We initialize
2656 most things with sane defaults, to keep the code below somewhat
2657 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002658 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2659 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002660 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002662 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002663 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002664 struct i2c_msg msg[2] = {
2665 {
2666 .addr = addr,
2667 .flags = flags,
2668 .len = 1,
2669 .buf = msgbuf0,
2670 }, {
2671 .addr = addr,
2672 .flags = flags | I2C_M_RD,
2673 .len = 0,
2674 .buf = msgbuf1,
2675 },
2676 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002679 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 case I2C_SMBUS_QUICK:
2681 msg[0].len = 0;
2682 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002683 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2684 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 num = 1;
2686 break;
2687 case I2C_SMBUS_BYTE:
2688 if (read_write == I2C_SMBUS_READ) {
2689 /* Special case: only a read! */
2690 msg[0].flags = I2C_M_RD | flags;
2691 num = 1;
2692 }
2693 break;
2694 case I2C_SMBUS_BYTE_DATA:
2695 if (read_write == I2C_SMBUS_READ)
2696 msg[1].len = 1;
2697 else {
2698 msg[0].len = 2;
2699 msgbuf0[1] = data->byte;
2700 }
2701 break;
2702 case I2C_SMBUS_WORD_DATA:
2703 if (read_write == I2C_SMBUS_READ)
2704 msg[1].len = 2;
2705 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002706 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002708 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 break;
2711 case I2C_SMBUS_PROC_CALL:
2712 num = 2; /* Special case */
2713 read_write = I2C_SMBUS_READ;
2714 msg[0].len = 3;
2715 msg[1].len = 2;
2716 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002717 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 break;
2719 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002721 msg[1].flags |= I2C_M_RECV_LEN;
2722 msg[1].len = 1; /* block length will be added by
2723 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 } else {
2725 msg[0].len = data->block[0] + 2;
2726 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002727 dev_err(&adapter->dev,
2728 "Invalid block write size %d\n",
2729 data->block[0]);
2730 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002732 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 msgbuf0[i] = data->block[i-1];
2734 }
2735 break;
2736 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002737 num = 2; /* Another special case */
2738 read_write = I2C_SMBUS_READ;
2739 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002740 dev_err(&adapter->dev,
2741 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002742 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002743 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002744 }
2745 msg[0].len = data->block[0] + 2;
2746 for (i = 1; i < msg[0].len; i++)
2747 msgbuf0[i] = data->block[i-1];
2748 msg[1].flags |= I2C_M_RECV_LEN;
2749 msg[1].len = 1; /* block length will be added by
2750 the underlying bus driver */
2751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 case I2C_SMBUS_I2C_BLOCK_DATA:
2753 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002754 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 } else {
2756 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002757 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002758 dev_err(&adapter->dev,
2759 "Invalid block write size %d\n",
2760 data->block[0]);
2761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 for (i = 1; i <= data->block[0]; i++)
2764 msgbuf0[i] = data->block[i];
2765 }
2766 break;
2767 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002768 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2769 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
Jean Delvare421ef472005-10-26 21:28:55 +02002772 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2773 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2774 if (i) {
2775 /* Compute PEC if first message is a write */
2776 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002777 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002778 i2c_smbus_add_pec(&msg[0]);
2779 else /* Write followed by read */
2780 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2781 }
2782 /* Ask for PEC if last message is a read */
2783 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002784 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002785 }
2786
David Brownell24a5bb72008-07-14 22:38:23 +02002787 status = i2c_transfer(adapter, msg, num);
2788 if (status < 0)
2789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Jean Delvare421ef472005-10-26 21:28:55 +02002791 /* Check PEC if last message is a read */
2792 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002793 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2794 if (status < 0)
2795 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002796 }
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002799 switch (size) {
2800 case I2C_SMBUS_BYTE:
2801 data->byte = msgbuf0[0];
2802 break;
2803 case I2C_SMBUS_BYTE_DATA:
2804 data->byte = msgbuf1[0];
2805 break;
2806 case I2C_SMBUS_WORD_DATA:
2807 case I2C_SMBUS_PROC_CALL:
2808 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2809 break;
2810 case I2C_SMBUS_I2C_BLOCK_DATA:
2811 for (i = 0; i < data->block[0]; i++)
2812 data->block[i+1] = msgbuf1[i];
2813 break;
2814 case I2C_SMBUS_BLOCK_DATA:
2815 case I2C_SMBUS_BLOCK_PROC_CALL:
2816 for (i = 0; i < msgbuf1[0] + 1; i++)
2817 data->block[i] = msgbuf1[i];
2818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 return 0;
2821}
2822
David Brownella1cdeda2008-07-14 22:38:24 +02002823/**
2824 * i2c_smbus_xfer - execute SMBus protocol operations
2825 * @adapter: Handle to I2C bus
2826 * @addr: Address of SMBus slave on that bus
2827 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2828 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2829 * @command: Byte interpreted by slave, for protocols which use such bytes
2830 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2831 * @data: Data to be read or written
2832 *
2833 * This executes an SMBus protocol operation, and returns a negative
2834 * errno code else zero on success.
2835 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002836s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002837 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002838 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002840 unsigned long orig_jiffies;
2841 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
David Howells8a325992014-03-06 13:36:06 +00002844 /* If enabled, the following two tracepoints are conditional on
2845 * read_write and protocol.
2846 */
2847 trace_smbus_write(adapter, addr, flags, read_write,
2848 command, protocol, data);
2849 trace_smbus_read(adapter, addr, flags, read_write,
2850 command, protocol);
2851
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002852 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002855 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002856
2857 /* Retry automatically on arbitration loss */
2858 orig_jiffies = jiffies;
2859 for (res = 0, try = 0; try <= adapter->retries; try++) {
2860 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2861 read_write, command,
2862 protocol, data);
2863 if (res != -EAGAIN)
2864 break;
2865 if (time_after(jiffies,
2866 orig_jiffies + adapter->timeout))
2867 break;
2868 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002869 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002871 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002872 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002873 /*
2874 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2875 * implement native support for the SMBus operation.
2876 */
2877 }
2878
David Howells8a325992014-03-06 13:36:06 +00002879 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2880 command, protocol, data);
2881
2882trace:
2883 /* If enabled, the reply tracepoint is conditional on read_write. */
2884 trace_smbus_reply(adapter, addr, flags, read_write,
2885 command, protocol, data);
2886 trace_smbus_result(adapter, addr, flags, read_write,
2887 command, protocol, res);
2888
2889 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2894MODULE_DESCRIPTION("I2C-Bus main module");
2895MODULE_LICENSE("GPL");