Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
| 2 | /* ------------------------------------------------------------------------- */ |
| 3 | /* Copyright (C) 1995-99 Simon G. Vogl |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Wolfram Sang | ca1f8da | 2014-11-04 23:46:27 +0100 | [diff] [blame] | 13 | GNU General Public License for more details. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* ------------------------------------------------------------------------- */ |
| 15 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 16 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 18 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
Jean Delvare | 7c81c60f | 2014-01-29 20:40:08 +0100 | [diff] [blame] | 19 | Jean Delvare <jdelvare@suse.de> |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 20 | Mux support by Rodolfo Giometti <giometti@enneenne.com> and |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 21 | Michael Lawnick <michael.lawnick.ext@nsn.com> |
| 22 | OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de> |
| 23 | (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and |
| 24 | (c) 2013 Wolfram Sang <wsa@the-dreams.de> |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 25 | I2C ACPI code Copyright (C) 2014 Intel Corp |
| 26 | Author: Lan Tianyu <tianyu.lan@intel.com> |
Wolfram Sang | 4b1acc4 | 2014-11-18 17:04:53 +0100 | [diff] [blame] | 27 | I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com> |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 28 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | #include <linux/kernel.h> |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 32 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/errno.h> |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 34 | #include <linux/gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/slab.h> |
| 36 | #include <linux/i2c.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/idr.h> |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 40 | #include <linux/of.h> |
Grant Likely | 959e85f | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 41 | #include <linux/of_device.h> |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 42 | #include <linux/of_irq.h> |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 43 | #include <linux/clk/clk-conf.h> |
Jean Delvare | b8d6f45 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 44 | #include <linux/completion.h> |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 45 | #include <linux/hardirq.h> |
| 46 | #include <linux/irqflags.h> |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 47 | #include <linux/rwsem.h> |
Mark Brown | 6de468a | 2010-03-02 12:23:46 +0100 | [diff] [blame] | 48 | #include <linux/pm_runtime.h> |
Ulf Hansson | f48c767 | 2014-09-29 13:58:47 +0200 | [diff] [blame] | 49 | #include <linux/pm_domain.h> |
Mika Westerberg | 907ddf8 | 2012-11-23 12:23:40 +0100 | [diff] [blame] | 50 | #include <linux/acpi.h> |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 51 | #include <linux/jump_label.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/uaccess.h> |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 53 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 55 | #include "i2c-core.h" |
| 56 | |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 57 | #define CREATE_TRACE_POINTS |
| 58 | #include <trace/events/i2c.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 60 | /* core_lock protects i2c_adapter_idr, and guarantees |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 61 | that device detection, deletion of detected devices, and attach_adapter |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 62 | calls are serialized */ |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 63 | static DEFINE_MUTEX(core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static DEFINE_IDR(i2c_adapter_idr); |
| 65 | |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 66 | static struct device_type i2c_client_type; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 67 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 68 | |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 69 | static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE; |
| 70 | |
| 71 | void i2c_transfer_trace_reg(void) |
| 72 | { |
| 73 | static_key_slow_inc(&i2c_trace_msg); |
| 74 | } |
| 75 | |
| 76 | void i2c_transfer_trace_unreg(void) |
| 77 | { |
| 78 | static_key_slow_dec(&i2c_trace_msg); |
| 79 | } |
| 80 | |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 81 | #if defined(CONFIG_ACPI) |
| 82 | struct acpi_i2c_handler_data { |
| 83 | struct acpi_connection_info info; |
| 84 | struct i2c_adapter *adapter; |
| 85 | }; |
| 86 | |
| 87 | struct gsb_buffer { |
| 88 | u8 status; |
| 89 | u8 len; |
| 90 | union { |
| 91 | u16 wdata; |
| 92 | u8 bdata; |
| 93 | u8 data[0]; |
| 94 | }; |
| 95 | } __packed; |
| 96 | |
| 97 | static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data) |
| 98 | { |
| 99 | struct i2c_board_info *info = data; |
| 100 | |
| 101 | if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) { |
| 102 | struct acpi_resource_i2c_serialbus *sb; |
| 103 | |
| 104 | sb = &ares->data.i2c_serial_bus; |
Mika Westerberg | 393cc1ce | 2014-12-29 15:48:48 +0200 | [diff] [blame] | 105 | if (!info->addr && sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) { |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 106 | info->addr = sb->slave_address; |
| 107 | if (sb->access_mode == ACPI_I2C_10BIT_MODE) |
| 108 | info->flags |= I2C_CLIENT_TEN; |
| 109 | } |
Mika Westerberg | dab472e | 2015-05-06 13:29:07 +0300 | [diff] [blame^] | 110 | } else if (!info->irq) { |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 111 | struct resource r; |
| 112 | |
| 113 | if (acpi_dev_resource_interrupt(ares, 0, &r)) |
| 114 | info->irq = r.start; |
| 115 | } |
| 116 | |
| 117 | /* Tell the ACPI core to skip this resource */ |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level, |
| 122 | void *data, void **return_value) |
| 123 | { |
| 124 | struct i2c_adapter *adapter = data; |
| 125 | struct list_head resource_list; |
| 126 | struct i2c_board_info info; |
| 127 | struct acpi_device *adev; |
| 128 | int ret; |
| 129 | |
| 130 | if (acpi_bus_get_device(handle, &adev)) |
| 131 | return AE_OK; |
| 132 | if (acpi_bus_get_status(adev) || !adev->status.present) |
| 133 | return AE_OK; |
| 134 | |
| 135 | memset(&info, 0, sizeof(info)); |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 136 | info.fwnode = acpi_fwnode_handle(adev); |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 137 | |
| 138 | INIT_LIST_HEAD(&resource_list); |
| 139 | ret = acpi_dev_get_resources(adev, &resource_list, |
| 140 | acpi_i2c_add_resource, &info); |
| 141 | acpi_dev_free_resource_list(&resource_list); |
| 142 | |
| 143 | if (ret < 0 || !info.addr) |
| 144 | return AE_OK; |
| 145 | |
| 146 | adev->power.flags.ignore_parent = true; |
| 147 | strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type)); |
| 148 | if (!i2c_new_device(adapter, &info)) { |
| 149 | adev->power.flags.ignore_parent = false; |
| 150 | dev_err(&adapter->dev, |
| 151 | "failed to add I2C device %s from ACPI\n", |
| 152 | dev_name(&adev->dev)); |
| 153 | } |
| 154 | |
| 155 | return AE_OK; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter |
| 160 | * @adap: pointer to adapter |
| 161 | * |
| 162 | * Enumerate all I2C slave devices behind this adapter by walking the ACPI |
| 163 | * namespace. When a device is found it will be added to the Linux device |
| 164 | * model and bound to the corresponding ACPI handle. |
| 165 | */ |
| 166 | static void acpi_i2c_register_devices(struct i2c_adapter *adap) |
| 167 | { |
| 168 | acpi_handle handle; |
| 169 | acpi_status status; |
| 170 | |
| 171 | if (!adap->dev.parent) |
| 172 | return; |
| 173 | |
| 174 | handle = ACPI_HANDLE(adap->dev.parent); |
| 175 | if (!handle) |
| 176 | return; |
| 177 | |
| 178 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, |
| 179 | acpi_i2c_add_device, NULL, |
| 180 | adap, NULL); |
| 181 | if (ACPI_FAILURE(status)) |
| 182 | dev_warn(&adap->dev, "failed to enumerate I2C slaves\n"); |
| 183 | } |
| 184 | |
| 185 | #else /* CONFIG_ACPI */ |
| 186 | static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) { } |
| 187 | #endif /* CONFIG_ACPI */ |
| 188 | |
| 189 | #ifdef CONFIG_ACPI_I2C_OPREGION |
| 190 | static int acpi_gsb_i2c_read_bytes(struct i2c_client *client, |
| 191 | u8 cmd, u8 *data, u8 data_len) |
| 192 | { |
| 193 | |
| 194 | struct i2c_msg msgs[2]; |
| 195 | int ret; |
| 196 | u8 *buffer; |
| 197 | |
| 198 | buffer = kzalloc(data_len, GFP_KERNEL); |
| 199 | if (!buffer) |
| 200 | return AE_NO_MEMORY; |
| 201 | |
| 202 | msgs[0].addr = client->addr; |
| 203 | msgs[0].flags = client->flags; |
| 204 | msgs[0].len = 1; |
| 205 | msgs[0].buf = &cmd; |
| 206 | |
| 207 | msgs[1].addr = client->addr; |
| 208 | msgs[1].flags = client->flags | I2C_M_RD; |
| 209 | msgs[1].len = data_len; |
| 210 | msgs[1].buf = buffer; |
| 211 | |
| 212 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 213 | if (ret < 0) |
| 214 | dev_err(&client->adapter->dev, "i2c read failed\n"); |
| 215 | else |
| 216 | memcpy(data, buffer, data_len); |
| 217 | |
| 218 | kfree(buffer); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int acpi_gsb_i2c_write_bytes(struct i2c_client *client, |
| 223 | u8 cmd, u8 *data, u8 data_len) |
| 224 | { |
| 225 | |
| 226 | struct i2c_msg msgs[1]; |
| 227 | u8 *buffer; |
| 228 | int ret = AE_OK; |
| 229 | |
| 230 | buffer = kzalloc(data_len + 1, GFP_KERNEL); |
| 231 | if (!buffer) |
| 232 | return AE_NO_MEMORY; |
| 233 | |
| 234 | buffer[0] = cmd; |
| 235 | memcpy(buffer + 1, data, data_len); |
| 236 | |
| 237 | msgs[0].addr = client->addr; |
| 238 | msgs[0].flags = client->flags; |
| 239 | msgs[0].len = data_len + 1; |
| 240 | msgs[0].buf = buffer; |
| 241 | |
| 242 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 243 | if (ret < 0) |
| 244 | dev_err(&client->adapter->dev, "i2c write failed\n"); |
| 245 | |
| 246 | kfree(buffer); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static acpi_status |
| 251 | acpi_i2c_space_handler(u32 function, acpi_physical_address command, |
| 252 | u32 bits, u64 *value64, |
| 253 | void *handler_context, void *region_context) |
| 254 | { |
| 255 | struct gsb_buffer *gsb = (struct gsb_buffer *)value64; |
| 256 | struct acpi_i2c_handler_data *data = handler_context; |
| 257 | struct acpi_connection_info *info = &data->info; |
| 258 | struct acpi_resource_i2c_serialbus *sb; |
| 259 | struct i2c_adapter *adapter = data->adapter; |
| 260 | struct i2c_client client; |
| 261 | struct acpi_resource *ares; |
| 262 | u32 accessor_type = function >> 16; |
| 263 | u8 action = function & ACPI_IO_MASK; |
Wolfram Sang | 4470c72 | 2014-11-18 15:12:43 +0100 | [diff] [blame] | 264 | acpi_status ret; |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 265 | int status; |
| 266 | |
| 267 | ret = acpi_buffer_to_resource(info->connection, info->length, &ares); |
| 268 | if (ACPI_FAILURE(ret)) |
| 269 | return ret; |
| 270 | |
| 271 | if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) { |
| 272 | ret = AE_BAD_PARAMETER; |
| 273 | goto err; |
| 274 | } |
| 275 | |
| 276 | sb = &ares->data.i2c_serial_bus; |
| 277 | if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) { |
| 278 | ret = AE_BAD_PARAMETER; |
| 279 | goto err; |
| 280 | } |
| 281 | |
| 282 | memset(&client, 0, sizeof(client)); |
| 283 | client.adapter = adapter; |
| 284 | client.addr = sb->slave_address; |
| 285 | client.flags = 0; |
| 286 | |
| 287 | if (sb->access_mode == ACPI_I2C_10BIT_MODE) |
| 288 | client.flags |= I2C_CLIENT_TEN; |
| 289 | |
| 290 | switch (accessor_type) { |
| 291 | case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV: |
| 292 | if (action == ACPI_READ) { |
| 293 | status = i2c_smbus_read_byte(&client); |
| 294 | if (status >= 0) { |
| 295 | gsb->bdata = status; |
| 296 | status = 0; |
| 297 | } |
| 298 | } else { |
| 299 | status = i2c_smbus_write_byte(&client, gsb->bdata); |
| 300 | } |
| 301 | break; |
| 302 | |
| 303 | case ACPI_GSB_ACCESS_ATTRIB_BYTE: |
| 304 | if (action == ACPI_READ) { |
| 305 | status = i2c_smbus_read_byte_data(&client, command); |
| 306 | if (status >= 0) { |
| 307 | gsb->bdata = status; |
| 308 | status = 0; |
| 309 | } |
| 310 | } else { |
| 311 | status = i2c_smbus_write_byte_data(&client, command, |
| 312 | gsb->bdata); |
| 313 | } |
| 314 | break; |
| 315 | |
| 316 | case ACPI_GSB_ACCESS_ATTRIB_WORD: |
| 317 | if (action == ACPI_READ) { |
| 318 | status = i2c_smbus_read_word_data(&client, command); |
| 319 | if (status >= 0) { |
| 320 | gsb->wdata = status; |
| 321 | status = 0; |
| 322 | } |
| 323 | } else { |
| 324 | status = i2c_smbus_write_word_data(&client, command, |
| 325 | gsb->wdata); |
| 326 | } |
| 327 | break; |
| 328 | |
| 329 | case ACPI_GSB_ACCESS_ATTRIB_BLOCK: |
| 330 | if (action == ACPI_READ) { |
| 331 | status = i2c_smbus_read_block_data(&client, command, |
| 332 | gsb->data); |
| 333 | if (status >= 0) { |
| 334 | gsb->len = status; |
| 335 | status = 0; |
| 336 | } |
| 337 | } else { |
| 338 | status = i2c_smbus_write_block_data(&client, command, |
| 339 | gsb->len, gsb->data); |
| 340 | } |
| 341 | break; |
| 342 | |
| 343 | case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE: |
| 344 | if (action == ACPI_READ) { |
| 345 | status = acpi_gsb_i2c_read_bytes(&client, command, |
| 346 | gsb->data, info->access_length); |
| 347 | if (status > 0) |
| 348 | status = 0; |
| 349 | } else { |
| 350 | status = acpi_gsb_i2c_write_bytes(&client, command, |
| 351 | gsb->data, info->access_length); |
| 352 | } |
| 353 | break; |
| 354 | |
| 355 | default: |
| 356 | pr_info("protocol(0x%02x) is not supported.\n", accessor_type); |
| 357 | ret = AE_BAD_PARAMETER; |
| 358 | goto err; |
| 359 | } |
| 360 | |
| 361 | gsb->status = status; |
| 362 | |
| 363 | err: |
| 364 | ACPI_FREE(ares); |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | static int acpi_i2c_install_space_handler(struct i2c_adapter *adapter) |
| 370 | { |
Peter Hüwe | 0aef44e | 2014-09-12 21:09:47 +0200 | [diff] [blame] | 371 | acpi_handle handle; |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 372 | struct acpi_i2c_handler_data *data; |
| 373 | acpi_status status; |
| 374 | |
Peter Hüwe | 0aef44e | 2014-09-12 21:09:47 +0200 | [diff] [blame] | 375 | if (!adapter->dev.parent) |
| 376 | return -ENODEV; |
| 377 | |
| 378 | handle = ACPI_HANDLE(adapter->dev.parent); |
| 379 | |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 380 | if (!handle) |
| 381 | return -ENODEV; |
| 382 | |
| 383 | data = kzalloc(sizeof(struct acpi_i2c_handler_data), |
| 384 | GFP_KERNEL); |
| 385 | if (!data) |
| 386 | return -ENOMEM; |
| 387 | |
| 388 | data->adapter = adapter; |
| 389 | status = acpi_bus_attach_private_data(handle, (void *)data); |
| 390 | if (ACPI_FAILURE(status)) { |
| 391 | kfree(data); |
| 392 | return -ENOMEM; |
| 393 | } |
| 394 | |
| 395 | status = acpi_install_address_space_handler(handle, |
| 396 | ACPI_ADR_SPACE_GSBUS, |
| 397 | &acpi_i2c_space_handler, |
| 398 | NULL, |
| 399 | data); |
| 400 | if (ACPI_FAILURE(status)) { |
| 401 | dev_err(&adapter->dev, "Error installing i2c space handler\n"); |
| 402 | acpi_bus_detach_private_data(handle); |
| 403 | kfree(data); |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | |
Lan Tianyu | 40e7fcb | 2014-11-23 21:22:54 +0800 | [diff] [blame] | 407 | acpi_walk_dep_device_list(handle); |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter) |
| 412 | { |
Peter Hüwe | 0aef44e | 2014-09-12 21:09:47 +0200 | [diff] [blame] | 413 | acpi_handle handle; |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 414 | struct acpi_i2c_handler_data *data; |
| 415 | acpi_status status; |
| 416 | |
Peter Hüwe | 0aef44e | 2014-09-12 21:09:47 +0200 | [diff] [blame] | 417 | if (!adapter->dev.parent) |
| 418 | return; |
| 419 | |
| 420 | handle = ACPI_HANDLE(adapter->dev.parent); |
| 421 | |
Wolfram Sang | 17f4a5c | 2014-09-22 19:41:00 +0200 | [diff] [blame] | 422 | if (!handle) |
| 423 | return; |
| 424 | |
| 425 | acpi_remove_address_space_handler(handle, |
| 426 | ACPI_ADR_SPACE_GSBUS, |
| 427 | &acpi_i2c_space_handler); |
| 428 | |
| 429 | status = acpi_bus_get_private_data(handle, (void **)&data); |
| 430 | if (ACPI_SUCCESS(status)) |
| 431 | kfree(data); |
| 432 | |
| 433 | acpi_bus_detach_private_data(handle); |
| 434 | } |
| 435 | #else /* CONFIG_ACPI_I2C_OPREGION */ |
| 436 | static inline void acpi_i2c_remove_space_handler(struct i2c_adapter *adapter) |
| 437 | { } |
| 438 | |
| 439 | static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter) |
| 440 | { return 0; } |
| 441 | #endif /* CONFIG_ACPI_I2C_OPREGION */ |
| 442 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 443 | /* ------------------------------------------------------------------------- */ |
| 444 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 445 | static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, |
| 446 | const struct i2c_client *client) |
| 447 | { |
| 448 | while (id->name[0]) { |
| 449 | if (strcmp(client->name, id->name) == 0) |
| 450 | return id; |
| 451 | id++; |
| 452 | } |
| 453 | return NULL; |
| 454 | } |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
| 457 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 458 | struct i2c_client *client = i2c_verify_client(dev); |
| 459 | struct i2c_driver *driver; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 460 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 461 | if (!client) |
| 462 | return 0; |
| 463 | |
Grant Likely | 959e85f | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 464 | /* Attempt an OF style match */ |
| 465 | if (of_driver_match_device(dev, drv)) |
| 466 | return 1; |
| 467 | |
Mika Westerberg | 907ddf8 | 2012-11-23 12:23:40 +0100 | [diff] [blame] | 468 | /* Then ACPI style match */ |
| 469 | if (acpi_driver_match_device(dev, drv)) |
| 470 | return 1; |
| 471 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 472 | driver = to_i2c_driver(drv); |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 473 | /* match on an id table if there is one */ |
| 474 | if (driver->id_table) |
| 475 | return i2c_match_id(driver->id_table, client) != NULL; |
| 476 | |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 477 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 480 | |
| 481 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 482 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 483 | { |
| 484 | struct i2c_client *client = to_i2c_client(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 485 | int rc; |
| 486 | |
| 487 | rc = acpi_device_uevent_modalias(dev, env); |
| 488 | if (rc != -ENODEV) |
| 489 | return rc; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 490 | |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 491 | if (add_uevent_var(env, "MODALIAS=%s%s", |
| 492 | I2C_MODULE_PREFIX, client->name)) |
| 493 | return -ENOMEM; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 494 | dev_dbg(dev, "uevent\n"); |
| 495 | return 0; |
| 496 | } |
| 497 | |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 498 | /* i2c bus recovery routines */ |
| 499 | static int get_scl_gpio_value(struct i2c_adapter *adap) |
| 500 | { |
| 501 | return gpio_get_value(adap->bus_recovery_info->scl_gpio); |
| 502 | } |
| 503 | |
| 504 | static void set_scl_gpio_value(struct i2c_adapter *adap, int val) |
| 505 | { |
| 506 | gpio_set_value(adap->bus_recovery_info->scl_gpio, val); |
| 507 | } |
| 508 | |
| 509 | static int get_sda_gpio_value(struct i2c_adapter *adap) |
| 510 | { |
| 511 | return gpio_get_value(adap->bus_recovery_info->sda_gpio); |
| 512 | } |
| 513 | |
| 514 | static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap) |
| 515 | { |
| 516 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; |
| 517 | struct device *dev = &adap->dev; |
| 518 | int ret = 0; |
| 519 | |
| 520 | ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN | |
| 521 | GPIOF_OUT_INIT_HIGH, "i2c-scl"); |
| 522 | if (ret) { |
| 523 | dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | if (bri->get_sda) { |
| 528 | if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) { |
| 529 | /* work without SDA polling */ |
| 530 | dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n", |
| 531 | bri->sda_gpio); |
| 532 | bri->get_sda = NULL; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap) |
| 540 | { |
| 541 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; |
| 542 | |
| 543 | if (bri->get_sda) |
| 544 | gpio_free(bri->sda_gpio); |
| 545 | |
| 546 | gpio_free(bri->scl_gpio); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * We are generating clock pulses. ndelay() determines durating of clk pulses. |
| 551 | * We will generate clock with rate 100 KHz and so duration of both clock levels |
| 552 | * is: delay in ns = (10^6 / 100) / 2 |
| 553 | */ |
| 554 | #define RECOVERY_NDELAY 5000 |
| 555 | #define RECOVERY_CLK_CNT 9 |
| 556 | |
| 557 | static int i2c_generic_recovery(struct i2c_adapter *adap) |
| 558 | { |
| 559 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; |
| 560 | int i = 0, val = 1, ret = 0; |
| 561 | |
| 562 | if (bri->prepare_recovery) |
Grygorii Strashko | 2b2190a | 2015-04-06 15:38:39 +0300 | [diff] [blame] | 563 | bri->prepare_recovery(adap); |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * By this time SCL is high, as we need to give 9 falling-rising edges |
| 567 | */ |
| 568 | while (i++ < RECOVERY_CLK_CNT * 2) { |
| 569 | if (val) { |
| 570 | /* Break if SDA is high */ |
| 571 | if (bri->get_sda && bri->get_sda(adap)) |
| 572 | break; |
| 573 | /* SCL shouldn't be low here */ |
| 574 | if (!bri->get_scl(adap)) { |
| 575 | dev_err(&adap->dev, |
| 576 | "SCL is stuck low, exit recovery\n"); |
| 577 | ret = -EBUSY; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | val = !val; |
| 583 | bri->set_scl(adap, val); |
| 584 | ndelay(RECOVERY_NDELAY); |
| 585 | } |
| 586 | |
| 587 | if (bri->unprepare_recovery) |
Grygorii Strashko | 2b2190a | 2015-04-06 15:38:39 +0300 | [diff] [blame] | 588 | bri->unprepare_recovery(adap); |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 589 | |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | int i2c_generic_scl_recovery(struct i2c_adapter *adap) |
| 594 | { |
| 595 | adap->bus_recovery_info->set_scl(adap, 1); |
| 596 | return i2c_generic_recovery(adap); |
| 597 | } |
Mark Brown | c1c21f4 | 2015-04-15 19:18:39 +0100 | [diff] [blame] | 598 | EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery); |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 599 | |
| 600 | int i2c_generic_gpio_recovery(struct i2c_adapter *adap) |
| 601 | { |
| 602 | int ret; |
| 603 | |
| 604 | ret = i2c_get_gpios_for_recovery(adap); |
| 605 | if (ret) |
| 606 | return ret; |
| 607 | |
| 608 | ret = i2c_generic_recovery(adap); |
| 609 | i2c_put_gpios_for_recovery(adap); |
| 610 | |
| 611 | return ret; |
| 612 | } |
Mark Brown | c1c21f4 | 2015-04-15 19:18:39 +0100 | [diff] [blame] | 613 | EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery); |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 614 | |
| 615 | int i2c_recover_bus(struct i2c_adapter *adap) |
| 616 | { |
| 617 | if (!adap->bus_recovery_info) |
| 618 | return -EOPNOTSUPP; |
| 619 | |
| 620 | dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); |
| 621 | return adap->bus_recovery_info->recover_bus(adap); |
| 622 | } |
Mark Brown | c1c21f4 | 2015-04-15 19:18:39 +0100 | [diff] [blame] | 623 | EXPORT_SYMBOL_GPL(i2c_recover_bus); |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | static int i2c_device_probe(struct device *dev) |
| 626 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 627 | struct i2c_client *client = i2c_verify_client(dev); |
| 628 | struct i2c_driver *driver; |
Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 629 | int status; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 630 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 631 | if (!client) |
| 632 | return 0; |
| 633 | |
Laurent Pinchart | 2fd36c552 | 2014-10-30 15:59:38 +0200 | [diff] [blame] | 634 | if (!client->irq && dev->of_node) { |
| 635 | int irq = of_irq_get(dev->of_node, 0); |
| 636 | |
Geert Uytterhoeven | 6f34be7 | 2014-11-17 12:43:00 +0100 | [diff] [blame] | 637 | if (irq == -EPROBE_DEFER) |
Laurent Pinchart | 2fd36c552 | 2014-10-30 15:59:38 +0200 | [diff] [blame] | 638 | return irq; |
Geert Uytterhoeven | 6f34be7 | 2014-11-17 12:43:00 +0100 | [diff] [blame] | 639 | if (irq < 0) |
| 640 | irq = 0; |
Laurent Pinchart | 2fd36c552 | 2014-10-30 15:59:38 +0200 | [diff] [blame] | 641 | |
| 642 | client->irq = irq; |
| 643 | } |
| 644 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 645 | driver = to_i2c_driver(dev->driver); |
Jean Delvare | e045744 | 2008-07-14 22:38:30 +0200 | [diff] [blame] | 646 | if (!driver->probe || !driver->id_table) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 647 | return -ENODEV; |
Lars-Peter Clausen | 0acc2b3 | 2013-09-29 10:51:06 +0200 | [diff] [blame] | 648 | |
Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 649 | if (!device_can_wakeup(&client->dev)) |
| 650 | device_init_wakeup(&client->dev, |
| 651 | client->flags & I2C_CLIENT_WAKE); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 652 | dev_dbg(dev, "probe\n"); |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 653 | |
Sylwester Nawrocki | 86be408 | 2014-06-18 17:29:32 +0200 | [diff] [blame] | 654 | status = of_clk_set_defaults(dev->of_node, false); |
| 655 | if (status < 0) |
| 656 | return status; |
| 657 | |
Ulf Hansson | e09b0d4 | 2014-09-19 20:27:39 +0200 | [diff] [blame] | 658 | status = dev_pm_domain_attach(&client->dev, true); |
| 659 | if (status != -EPROBE_DEFER) { |
| 660 | status = driver->probe(client, i2c_match_id(driver->id_table, |
| 661 | client)); |
| 662 | if (status) |
| 663 | dev_pm_domain_detach(&client->dev, true); |
| 664 | } |
Wolfram Sang | 72fa818 | 2014-01-21 17:48:34 +0100 | [diff] [blame] | 665 | |
Hans Verkuil | 50c3304 | 2008-03-12 14:15:00 +0100 | [diff] [blame] | 666 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static int i2c_device_remove(struct device *dev) |
| 670 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 671 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 672 | struct i2c_driver *driver; |
Wolfram Sang | 72fa818 | 2014-01-21 17:48:34 +0100 | [diff] [blame] | 673 | int status = 0; |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 674 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 675 | if (!client || !dev->driver) |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 676 | return 0; |
| 677 | |
| 678 | driver = to_i2c_driver(dev->driver); |
| 679 | if (driver->remove) { |
| 680 | dev_dbg(dev, "remove\n"); |
| 681 | status = driver->remove(client); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 682 | } |
Wolfram Sang | 72fa818 | 2014-01-21 17:48:34 +0100 | [diff] [blame] | 683 | |
Ulf Hansson | e09b0d4 | 2014-09-19 20:27:39 +0200 | [diff] [blame] | 684 | dev_pm_domain_detach(&client->dev, true); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 685 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 688 | static void i2c_device_shutdown(struct device *dev) |
| 689 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 690 | struct i2c_client *client = i2c_verify_client(dev); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 691 | struct i2c_driver *driver; |
| 692 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 693 | if (!client || !dev->driver) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 694 | return; |
| 695 | driver = to_i2c_driver(dev->driver); |
| 696 | if (driver->shutdown) |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 697 | driver->shutdown(client); |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 698 | } |
| 699 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 700 | static void i2c_client_dev_release(struct device *dev) |
| 701 | { |
| 702 | kfree(to_i2c_client(dev)); |
| 703 | } |
| 704 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 705 | static ssize_t |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 706 | show_name(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 707 | { |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 708 | return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? |
| 709 | to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 710 | } |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 711 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 712 | |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 713 | static ssize_t |
| 714 | show_modalias(struct device *dev, struct device_attribute *attr, char *buf) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 715 | { |
| 716 | struct i2c_client *client = to_i2c_client(dev); |
Zhang Rui | 8c4ff6d | 2014-01-14 16:46:37 +0800 | [diff] [blame] | 717 | int len; |
| 718 | |
| 719 | len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); |
| 720 | if (len != -ENODEV) |
| 721 | return len; |
| 722 | |
Jean Delvare | eb8a790 | 2008-05-18 20:49:41 +0200 | [diff] [blame] | 723 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 724 | } |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 725 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 726 | |
| 727 | static struct attribute *i2c_dev_attrs[] = { |
| 728 | &dev_attr_name.attr, |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 729 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 730 | &dev_attr_modalias.attr, |
| 731 | NULL |
| 732 | }; |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 733 | ATTRIBUTE_GROUPS(i2c_dev); |
sonic zhang | 54067ee | 2009-12-14 21:17:30 +0100 | [diff] [blame] | 734 | |
Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 735 | struct bus_type i2c_bus_type = { |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 736 | .name = "i2c", |
| 737 | .match = i2c_device_match, |
| 738 | .probe = i2c_device_probe, |
| 739 | .remove = i2c_device_remove, |
| 740 | .shutdown = i2c_device_shutdown, |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 741 | }; |
Jon Smirl | e9ca9eb | 2008-07-14 22:38:35 +0200 | [diff] [blame] | 742 | EXPORT_SYMBOL_GPL(i2c_bus_type); |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 743 | |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 744 | static struct device_type i2c_client_type = { |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 745 | .groups = i2c_dev_groups, |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 746 | .uevent = i2c_device_uevent, |
| 747 | .release = i2c_client_dev_release, |
| 748 | }; |
| 749 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 750 | |
| 751 | /** |
| 752 | * i2c_verify_client - return parameter as i2c_client, or NULL |
| 753 | * @dev: device, probably from some driver model iterator |
| 754 | * |
| 755 | * When traversing the driver model tree, perhaps using driver model |
| 756 | * iterators like @device_for_each_child(), you can't assume very much |
| 757 | * about the nodes you find. Use this function to avoid oopses caused |
| 758 | * by wrongly treating some non-I2C device as an i2c_client. |
| 759 | */ |
| 760 | struct i2c_client *i2c_verify_client(struct device *dev) |
| 761 | { |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 762 | return (dev->type == &i2c_client_type) |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 763 | ? to_i2c_client(dev) |
| 764 | : NULL; |
| 765 | } |
| 766 | EXPORT_SYMBOL(i2c_verify_client); |
| 767 | |
| 768 | |
Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 769 | /* This is a permissive address validity check, I2C address map constraints |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 770 | * are purposely not enforced, except for the general call address. */ |
Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 771 | static int i2c_check_client_addr_validity(const struct i2c_client *client) |
| 772 | { |
| 773 | if (client->flags & I2C_CLIENT_TEN) { |
| 774 | /* 10-bit address, all values are valid */ |
| 775 | if (client->addr > 0x3ff) |
| 776 | return -EINVAL; |
| 777 | } else { |
| 778 | /* 7-bit address, reject the general call address */ |
| 779 | if (client->addr == 0x00 || client->addr > 0x7f) |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | return 0; |
| 783 | } |
| 784 | |
Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 785 | /* And this is a strict address validity check, used when probing. If a |
| 786 | * device uses a reserved address, then it shouldn't be probed. 7-bit |
| 787 | * addressing is assumed, 10-bit address devices are rare and should be |
| 788 | * explicitly enumerated. */ |
| 789 | static int i2c_check_addr_validity(unsigned short addr) |
| 790 | { |
| 791 | /* |
| 792 | * Reserved addresses per I2C specification: |
| 793 | * 0x00 General call address / START byte |
| 794 | * 0x01 CBUS address |
| 795 | * 0x02 Reserved for different bus format |
| 796 | * 0x03 Reserved for future purposes |
| 797 | * 0x04-0x07 Hs-mode master code |
| 798 | * 0x78-0x7b 10-bit slave addressing |
| 799 | * 0x7c-0x7f Reserved for future purposes |
| 800 | */ |
| 801 | if (addr < 0x08 || addr > 0x77) |
| 802 | return -EINVAL; |
| 803 | return 0; |
| 804 | } |
| 805 | |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 806 | static int __i2c_check_addr_busy(struct device *dev, void *addrp) |
| 807 | { |
| 808 | struct i2c_client *client = i2c_verify_client(dev); |
| 809 | int addr = *(int *)addrp; |
| 810 | |
| 811 | if (client && client->addr == addr) |
| 812 | return -EBUSY; |
| 813 | return 0; |
| 814 | } |
| 815 | |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 816 | /* walk up mux tree */ |
| 817 | static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) |
| 818 | { |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 819 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 820 | int result; |
| 821 | |
| 822 | result = device_for_each_child(&adapter->dev, &addr, |
| 823 | __i2c_check_addr_busy); |
| 824 | |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 825 | if (!result && parent) |
| 826 | result = i2c_check_mux_parents(parent, addr); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 827 | |
| 828 | return result; |
| 829 | } |
| 830 | |
| 831 | /* recurse down mux tree */ |
| 832 | static int i2c_check_mux_children(struct device *dev, void *addrp) |
| 833 | { |
| 834 | int result; |
| 835 | |
| 836 | if (dev->type == &i2c_adapter_type) |
| 837 | result = device_for_each_child(dev, addrp, |
| 838 | i2c_check_mux_children); |
| 839 | else |
| 840 | result = __i2c_check_addr_busy(dev, addrp); |
| 841 | |
| 842 | return result; |
| 843 | } |
| 844 | |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 845 | static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) |
| 846 | { |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 847 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 848 | int result = 0; |
| 849 | |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 850 | if (parent) |
| 851 | result = i2c_check_mux_parents(parent, addr); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 852 | |
| 853 | if (!result) |
| 854 | result = device_for_each_child(&adapter->dev, &addr, |
| 855 | i2c_check_mux_children); |
| 856 | |
| 857 | return result; |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 858 | } |
| 859 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 860 | /** |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 861 | * i2c_lock_adapter - Get exclusive access to an I2C bus segment |
| 862 | * @adapter: Target I2C bus segment |
| 863 | */ |
| 864 | void i2c_lock_adapter(struct i2c_adapter *adapter) |
| 865 | { |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 866 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
| 867 | |
| 868 | if (parent) |
| 869 | i2c_lock_adapter(parent); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 870 | else |
| 871 | rt_mutex_lock(&adapter->bus_lock); |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 872 | } |
| 873 | EXPORT_SYMBOL_GPL(i2c_lock_adapter); |
| 874 | |
| 875 | /** |
| 876 | * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment |
| 877 | * @adapter: Target I2C bus segment |
| 878 | */ |
| 879 | static int i2c_trylock_adapter(struct i2c_adapter *adapter) |
| 880 | { |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 881 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
| 882 | |
| 883 | if (parent) |
| 884 | return i2c_trylock_adapter(parent); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 885 | else |
| 886 | return rt_mutex_trylock(&adapter->bus_lock); |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
| 890 | * i2c_unlock_adapter - Release exclusive access to an I2C bus segment |
| 891 | * @adapter: Target I2C bus segment |
| 892 | */ |
| 893 | void i2c_unlock_adapter(struct i2c_adapter *adapter) |
| 894 | { |
Jean Delvare | 97cc4d4 | 2010-10-24 18:16:57 +0200 | [diff] [blame] | 895 | struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); |
| 896 | |
| 897 | if (parent) |
| 898 | i2c_unlock_adapter(parent); |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 899 | else |
| 900 | rt_mutex_unlock(&adapter->bus_lock); |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 901 | } |
| 902 | EXPORT_SYMBOL_GPL(i2c_unlock_adapter); |
| 903 | |
Jarkko Nikula | 70762ab | 2013-11-14 14:03:52 +0200 | [diff] [blame] | 904 | static void i2c_dev_set_name(struct i2c_adapter *adap, |
| 905 | struct i2c_client *client) |
| 906 | { |
| 907 | struct acpi_device *adev = ACPI_COMPANION(&client->dev); |
| 908 | |
| 909 | if (adev) { |
| 910 | dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | /* For 10-bit clients, add an arbitrary offset to avoid collisions */ |
| 915 | dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), |
| 916 | client->addr | ((client->flags & I2C_CLIENT_TEN) |
| 917 | ? 0xa000 : 0)); |
| 918 | } |
| 919 | |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 920 | /** |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 921 | * i2c_new_device - instantiate an i2c device |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 922 | * @adap: the adapter managing the device |
| 923 | * @info: describes one I2C device; bus_num is ignored |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 924 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 925 | * |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 926 | * Create an i2c device. Binding is handled through driver model |
| 927 | * probe()/remove() methods. A driver may be bound to this device when we |
| 928 | * return from this function, or any later moment (e.g. maybe hotplugging will |
| 929 | * load the driver module). This call is not appropriate for use by mainboard |
| 930 | * initialization logic, which usually runs during an arch_initcall() long |
| 931 | * before any i2c_adapter could exist. |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 932 | * |
| 933 | * This returns the new i2c client, which may be saved for later use with |
| 934 | * i2c_unregister_device(); or NULL to indicate an error. |
| 935 | */ |
| 936 | struct i2c_client * |
| 937 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) |
| 938 | { |
| 939 | struct i2c_client *client; |
| 940 | int status; |
| 941 | |
| 942 | client = kzalloc(sizeof *client, GFP_KERNEL); |
| 943 | if (!client) |
| 944 | return NULL; |
| 945 | |
| 946 | client->adapter = adap; |
| 947 | |
| 948 | client->dev.platform_data = info->platform_data; |
David Brownell | 3bbb835 | 2007-10-13 23:56:29 +0200 | [diff] [blame] | 949 | |
Anton Vorontsov | 11f1f2a | 2008-10-22 20:21:33 +0200 | [diff] [blame] | 950 | if (info->archdata) |
| 951 | client->dev.archdata = *info->archdata; |
| 952 | |
Marc Pignat | ee35425 | 2008-08-28 08:33:22 +0200 | [diff] [blame] | 953 | client->flags = info->flags; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 954 | client->addr = info->addr; |
| 955 | client->irq = info->irq; |
| 956 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 957 | strlcpy(client->name, info->type, sizeof(client->name)); |
| 958 | |
Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 959 | /* Check for address validity */ |
| 960 | status = i2c_check_client_addr_validity(client); |
| 961 | if (status) { |
| 962 | dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", |
| 963 | client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); |
| 964 | goto out_err_silent; |
| 965 | } |
| 966 | |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 967 | /* Check for address business */ |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 968 | status = i2c_check_addr_busy(adap, client->addr); |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 969 | if (status) |
| 970 | goto out_err; |
| 971 | |
| 972 | client->dev.parent = &client->adapter->dev; |
| 973 | client->dev.bus = &i2c_bus_type; |
Jean Delvare | 51298d1 | 2009-09-18 22:45:45 +0200 | [diff] [blame] | 974 | client->dev.type = &i2c_client_type; |
Grant Likely | d12d42f | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 975 | client->dev.of_node = info->of_node; |
Rafael J. Wysocki | ce79348 | 2015-03-16 23:49:03 +0100 | [diff] [blame] | 976 | client->dev.fwnode = info->fwnode; |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 977 | |
Jarkko Nikula | 70762ab | 2013-11-14 14:03:52 +0200 | [diff] [blame] | 978 | i2c_dev_set_name(adap, client); |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 979 | status = device_register(&client->dev); |
| 980 | if (status) |
| 981 | goto out_err; |
| 982 | |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 983 | dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", |
| 984 | client->name, dev_name(&client->dev)); |
| 985 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 986 | return client; |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 987 | |
| 988 | out_err: |
| 989 | dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x " |
| 990 | "(%d)\n", client->name, client->addr, status); |
Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 991 | out_err_silent: |
Jean Delvare | f8a227e | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 992 | kfree(client); |
| 993 | return NULL; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 994 | } |
| 995 | EXPORT_SYMBOL_GPL(i2c_new_device); |
| 996 | |
| 997 | |
| 998 | /** |
| 999 | * i2c_unregister_device - reverse effect of i2c_new_device() |
| 1000 | * @client: value returned from i2c_new_device() |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1001 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1002 | */ |
| 1003 | void i2c_unregister_device(struct i2c_client *client) |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1004 | { |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1005 | device_unregister(&client->dev); |
| 1006 | } |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1007 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1008 | |
| 1009 | |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 1010 | static const struct i2c_device_id dummy_id[] = { |
| 1011 | { "dummy", 0 }, |
| 1012 | { }, |
| 1013 | }; |
| 1014 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 1015 | static int dummy_probe(struct i2c_client *client, |
| 1016 | const struct i2c_device_id *id) |
| 1017 | { |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int dummy_remove(struct i2c_client *client) |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1022 | { |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static struct i2c_driver dummy_driver = { |
| 1027 | .driver.name = "dummy", |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 1028 | .probe = dummy_probe, |
| 1029 | .remove = dummy_remove, |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 1030 | .id_table = dummy_id, |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1031 | }; |
| 1032 | |
| 1033 | /** |
| 1034 | * i2c_new_dummy - return a new i2c device bound to a dummy driver |
| 1035 | * @adapter: the adapter managing the device |
| 1036 | * @address: seven bit address to be used |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1037 | * Context: can sleep |
| 1038 | * |
| 1039 | * This returns an I2C client bound to the "dummy" driver, intended for use |
| 1040 | * with devices that consume multiple addresses. Examples of such chips |
| 1041 | * include various EEPROMS (like 24c04 and 24c08 models). |
| 1042 | * |
| 1043 | * These dummy devices have two main uses. First, most I2C and SMBus calls |
| 1044 | * except i2c_transfer() need a client handle; the dummy will be that handle. |
| 1045 | * And second, this prevents the specified address from being bound to a |
| 1046 | * different driver. |
| 1047 | * |
| 1048 | * This returns the new i2c client, which should be saved for later use with |
| 1049 | * i2c_unregister_device(); or NULL to indicate an error. |
| 1050 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1051 | struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address) |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1052 | { |
| 1053 | struct i2c_board_info info = { |
Jean Delvare | 60b129d | 2008-05-11 20:37:06 +0200 | [diff] [blame] | 1054 | I2C_BOARD_INFO("dummy", address), |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1055 | }; |
| 1056 | |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1057 | return i2c_new_device(adapter, &info); |
| 1058 | } |
| 1059 | EXPORT_SYMBOL_GPL(i2c_new_dummy); |
| 1060 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 1061 | /* ------------------------------------------------------------------------- */ |
| 1062 | |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 1063 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
| 1064 | |
Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 1065 | static void i2c_adapter_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
David Brownell | ef2c8321 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 1067 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | complete(&adap->dev_released); |
| 1069 | } |
| 1070 | |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1071 | /* |
Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 1072 | * This function is only needed for mutex_lock_nested, so it is never |
| 1073 | * called unless locking correctness checking is enabled. Thus we |
| 1074 | * make it inline to avoid a compiler warning. That's what gcc ends up |
| 1075 | * doing anyway. |
| 1076 | */ |
| 1077 | static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter) |
| 1078 | { |
| 1079 | unsigned int depth = 0; |
| 1080 | |
| 1081 | while ((adapter = i2c_parent_is_i2c_adapter(adapter))) |
| 1082 | depth++; |
| 1083 | |
| 1084 | return depth; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1088 | * Let users instantiate I2C devices through sysfs. This can be used when |
| 1089 | * platform initialization code doesn't contain the proper data for |
| 1090 | * whatever reason. Also useful for drivers that do device detection and |
| 1091 | * detection fails, either because the device uses an unexpected address, |
| 1092 | * or this is a compatible device with different ID register values. |
| 1093 | * |
| 1094 | * Parameter checking may look overzealous, but we really don't want |
| 1095 | * the user to provide incorrect parameters. |
| 1096 | */ |
| 1097 | static ssize_t |
| 1098 | i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr, |
| 1099 | const char *buf, size_t count) |
| 1100 | { |
| 1101 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
| 1102 | struct i2c_board_info info; |
| 1103 | struct i2c_client *client; |
| 1104 | char *blank, end; |
| 1105 | int res; |
| 1106 | |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1107 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 1108 | |
| 1109 | blank = strchr(buf, ' '); |
| 1110 | if (!blank) { |
| 1111 | dev_err(dev, "%s: Missing parameters\n", "new_device"); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | if (blank - buf > I2C_NAME_SIZE - 1) { |
| 1115 | dev_err(dev, "%s: Invalid device name\n", "new_device"); |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | memcpy(info.type, buf, blank - buf); |
| 1119 | |
| 1120 | /* Parse remaining parameters, reject extra parameters */ |
| 1121 | res = sscanf(++blank, "%hi%c", &info.addr, &end); |
| 1122 | if (res < 1) { |
| 1123 | dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | if (res > 1 && end != '\n') { |
| 1127 | dev_err(dev, "%s: Extra parameters\n", "new_device"); |
| 1128 | return -EINVAL; |
| 1129 | } |
| 1130 | |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1131 | client = i2c_new_device(adap, &info); |
| 1132 | if (!client) |
Jean Delvare | 3a89db5 | 2010-06-03 11:33:52 +0200 | [diff] [blame] | 1133 | return -EINVAL; |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* Keep track of the added device */ |
Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1136 | mutex_lock(&adap->userspace_clients_lock); |
Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1137 | list_add_tail(&client->detected, &adap->userspace_clients); |
Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1138 | mutex_unlock(&adap->userspace_clients_lock); |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1139 | dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", |
| 1140 | info.type, info.addr); |
| 1141 | |
| 1142 | return count; |
| 1143 | } |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 1144 | static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1145 | |
| 1146 | /* |
| 1147 | * And of course let the users delete the devices they instantiated, if |
| 1148 | * they got it wrong. This interface can only be used to delete devices |
| 1149 | * instantiated by i2c_sysfs_new_device above. This guarantees that we |
| 1150 | * don't delete devices to which some kernel code still has references. |
| 1151 | * |
| 1152 | * Parameter checking may look overzealous, but we really don't want |
| 1153 | * the user to delete the wrong device. |
| 1154 | */ |
| 1155 | static ssize_t |
| 1156 | i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr, |
| 1157 | const char *buf, size_t count) |
| 1158 | { |
| 1159 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
| 1160 | struct i2c_client *client, *next; |
| 1161 | unsigned short addr; |
| 1162 | char end; |
| 1163 | int res; |
| 1164 | |
| 1165 | /* Parse parameters, reject extra parameters */ |
| 1166 | res = sscanf(buf, "%hi%c", &addr, &end); |
| 1167 | if (res < 1) { |
| 1168 | dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | if (res > 1 && end != '\n') { |
| 1172 | dev_err(dev, "%s: Extra parameters\n", "delete_device"); |
| 1173 | return -EINVAL; |
| 1174 | } |
| 1175 | |
| 1176 | /* Make sure the device was added through sysfs */ |
| 1177 | res = -ENOENT; |
Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 1178 | mutex_lock_nested(&adap->userspace_clients_lock, |
| 1179 | i2c_adapter_depth(adap)); |
Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1180 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
| 1181 | detected) { |
| 1182 | if (client->addr == addr) { |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1183 | dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", |
| 1184 | "delete_device", client->name, client->addr); |
| 1185 | |
| 1186 | list_del(&client->detected); |
| 1187 | i2c_unregister_device(client); |
| 1188 | res = count; |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1192 | mutex_unlock(&adap->userspace_clients_lock); |
Jean Delvare | 99cd8e2 | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1193 | |
| 1194 | if (res < 0) |
| 1195 | dev_err(dev, "%s: Can't find device in list\n", |
| 1196 | "delete_device"); |
| 1197 | return res; |
| 1198 | } |
Alexander Sverdlin | e9b526f | 2013-05-17 14:56:35 +0200 | [diff] [blame] | 1199 | static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL, |
| 1200 | i2c_sysfs_delete_device); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1201 | |
| 1202 | static struct attribute *i2c_adapter_attrs[] = { |
| 1203 | &dev_attr_name.attr, |
| 1204 | &dev_attr_new_device.attr, |
| 1205 | &dev_attr_delete_device.attr, |
| 1206 | NULL |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 1207 | }; |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 1208 | ATTRIBUTE_GROUPS(i2c_adapter); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1209 | |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 1210 | struct device_type i2c_adapter_type = { |
Wolfram Sang | a5eb71b | 2015-01-19 20:12:24 +0100 | [diff] [blame] | 1211 | .groups = i2c_adapter_groups, |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1212 | .release = i2c_adapter_dev_release, |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 1213 | }; |
Michael Lawnick | 0826374 | 2010-08-11 18:21:02 +0200 | [diff] [blame] | 1214 | EXPORT_SYMBOL_GPL(i2c_adapter_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
Stephen Warren | 643dd09 | 2012-04-17 12:43:33 -0600 | [diff] [blame] | 1216 | /** |
| 1217 | * i2c_verify_adapter - return parameter as i2c_adapter or NULL |
| 1218 | * @dev: device, probably from some driver model iterator |
| 1219 | * |
| 1220 | * When traversing the driver model tree, perhaps using driver model |
| 1221 | * iterators like @device_for_each_child(), you can't assume very much |
| 1222 | * about the nodes you find. Use this function to avoid oopses caused |
| 1223 | * by wrongly treating some non-I2C device as an i2c_adapter. |
| 1224 | */ |
| 1225 | struct i2c_adapter *i2c_verify_adapter(struct device *dev) |
| 1226 | { |
| 1227 | return (dev->type == &i2c_adapter_type) |
| 1228 | ? to_i2c_adapter(dev) |
| 1229 | : NULL; |
| 1230 | } |
| 1231 | EXPORT_SYMBOL(i2c_verify_adapter); |
| 1232 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1233 | #ifdef CONFIG_I2C_COMPAT |
| 1234 | static struct class_compat *i2c_adapter_compat_class; |
| 1235 | #endif |
| 1236 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1237 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
| 1238 | { |
| 1239 | struct i2c_devinfo *devinfo; |
| 1240 | |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1241 | down_read(&__i2c_board_lock); |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1242 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
| 1243 | if (devinfo->busnum == adapter->nr |
| 1244 | && !i2c_new_device(adapter, |
| 1245 | &devinfo->board_info)) |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 1246 | dev_err(&adapter->dev, |
| 1247 | "Can't create device at 0x%02x\n", |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1248 | devinfo->board_info.addr); |
| 1249 | } |
Rodolfo Giometti | f18c41d | 2009-06-19 16:58:20 +0200 | [diff] [blame] | 1250 | up_read(&__i2c_board_lock); |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1253 | /* OF support code */ |
| 1254 | |
| 1255 | #if IS_ENABLED(CONFIG_OF) |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 1256 | static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap, |
| 1257 | struct device_node *node) |
| 1258 | { |
| 1259 | struct i2c_client *result; |
| 1260 | struct i2c_board_info info = {}; |
| 1261 | struct dev_archdata dev_ad = {}; |
| 1262 | const __be32 *addr; |
| 1263 | int len; |
| 1264 | |
| 1265 | dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name); |
| 1266 | |
| 1267 | if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) { |
| 1268 | dev_err(&adap->dev, "of_i2c: modalias failure on %s\n", |
| 1269 | node->full_name); |
| 1270 | return ERR_PTR(-EINVAL); |
| 1271 | } |
| 1272 | |
| 1273 | addr = of_get_property(node, "reg", &len); |
| 1274 | if (!addr || (len < sizeof(int))) { |
| 1275 | dev_err(&adap->dev, "of_i2c: invalid reg on %s\n", |
| 1276 | node->full_name); |
| 1277 | return ERR_PTR(-EINVAL); |
| 1278 | } |
| 1279 | |
| 1280 | info.addr = be32_to_cpup(addr); |
| 1281 | if (info.addr > (1 << 10) - 1) { |
| 1282 | dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n", |
| 1283 | info.addr, node->full_name); |
| 1284 | return ERR_PTR(-EINVAL); |
| 1285 | } |
| 1286 | |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 1287 | info.of_node = of_node_get(node); |
| 1288 | info.archdata = &dev_ad; |
| 1289 | |
| 1290 | if (of_get_property(node, "wakeup-source", NULL)) |
| 1291 | info.flags |= I2C_CLIENT_WAKE; |
| 1292 | |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 1293 | result = i2c_new_device(adap, &info); |
| 1294 | if (result == NULL) { |
| 1295 | dev_err(&adap->dev, "of_i2c: Failure registering %s\n", |
| 1296 | node->full_name); |
| 1297 | of_node_put(node); |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 1298 | return ERR_PTR(-EINVAL); |
| 1299 | } |
| 1300 | return result; |
| 1301 | } |
| 1302 | |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1303 | static void of_i2c_register_devices(struct i2c_adapter *adap) |
| 1304 | { |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1305 | struct device_node *node; |
| 1306 | |
| 1307 | /* Only register child devices if the adapter has a node pointer set */ |
| 1308 | if (!adap->dev.of_node) |
| 1309 | return; |
| 1310 | |
| 1311 | dev_dbg(&adap->dev, "of_i2c: walking child nodes\n"); |
| 1312 | |
Pantelis Antoniou | a430a345 | 2014-10-28 22:36:02 +0200 | [diff] [blame] | 1313 | for_each_available_child_of_node(adap->dev.of_node, node) |
| 1314 | of_i2c_register_device(adap, node); |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | static int of_dev_node_match(struct device *dev, void *data) |
| 1318 | { |
| 1319 | return dev->of_node == data; |
| 1320 | } |
| 1321 | |
| 1322 | /* must call put_device() when done with returned i2c_client device */ |
| 1323 | struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) |
| 1324 | { |
| 1325 | struct device *dev; |
| 1326 | |
| 1327 | dev = bus_find_device(&i2c_bus_type, NULL, node, |
| 1328 | of_dev_node_match); |
| 1329 | if (!dev) |
| 1330 | return NULL; |
| 1331 | |
| 1332 | return i2c_verify_client(dev); |
| 1333 | } |
| 1334 | EXPORT_SYMBOL(of_find_i2c_device_by_node); |
| 1335 | |
| 1336 | /* must call put_device() when done with returned i2c_adapter device */ |
| 1337 | struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) |
| 1338 | { |
| 1339 | struct device *dev; |
| 1340 | |
| 1341 | dev = bus_find_device(&i2c_bus_type, NULL, node, |
| 1342 | of_dev_node_match); |
| 1343 | if (!dev) |
| 1344 | return NULL; |
| 1345 | |
| 1346 | return i2c_verify_adapter(dev); |
| 1347 | } |
| 1348 | EXPORT_SYMBOL(of_find_i2c_adapter_by_node); |
| 1349 | #else |
| 1350 | static void of_i2c_register_devices(struct i2c_adapter *adap) { } |
| 1351 | #endif /* CONFIG_OF */ |
| 1352 | |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1353 | static int i2c_do_add_adapter(struct i2c_driver *driver, |
| 1354 | struct i2c_adapter *adap) |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1355 | { |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1356 | /* Detect supported devices on that bus, and instantiate them */ |
| 1357 | i2c_detect(adap, driver); |
| 1358 | |
| 1359 | /* Let legacy drivers scan this bus for matching devices */ |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1360 | if (driver->attach_adapter) { |
Jean Delvare | a920ff4 | 2011-04-17 10:20:19 +0200 | [diff] [blame] | 1361 | dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n", |
| 1362 | driver->driver.name); |
Jean Delvare | fe6fc25 | 2011-03-20 14:50:53 +0100 | [diff] [blame] | 1363 | dev_warn(&adap->dev, "Please use another way to instantiate " |
| 1364 | "your i2c_client\n"); |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1365 | /* We ignore the return code; if it fails, too bad */ |
| 1366 | driver->attach_adapter(adap); |
| 1367 | } |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1371 | static int __process_new_adapter(struct device_driver *d, void *data) |
| 1372 | { |
| 1373 | return i2c_do_add_adapter(to_i2c_driver(d), data); |
| 1374 | } |
| 1375 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1376 | static int i2c_register_adapter(struct i2c_adapter *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | { |
Jean Delvare | d670328 | 2010-08-11 18:20:59 +0200 | [diff] [blame] | 1378 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1380 | /* Can't register until after driver model init */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1381 | if (unlikely(WARN_ON(!i2c_bus_type.p))) { |
| 1382 | res = -EAGAIN; |
| 1383 | goto out_list; |
| 1384 | } |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1385 | |
Jean Delvare | 2236baa7 | 2010-11-15 22:40:38 +0100 | [diff] [blame] | 1386 | /* Sanity checks */ |
| 1387 | if (unlikely(adap->name[0] == '\0')) { |
| 1388 | pr_err("i2c-core: Attempt to register an adapter with " |
| 1389 | "no name!\n"); |
| 1390 | return -EINVAL; |
| 1391 | } |
| 1392 | if (unlikely(!adap->algo)) { |
| 1393 | pr_err("i2c-core: Attempt to register adapter '%s' with " |
| 1394 | "no algo!\n", adap->name); |
| 1395 | return -EINVAL; |
| 1396 | } |
| 1397 | |
Mika Kuoppala | 194684e | 2009-12-06 17:06:22 +0100 | [diff] [blame] | 1398 | rt_mutex_init(&adap->bus_lock); |
Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1399 | mutex_init(&adap->userspace_clients_lock); |
Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1400 | INIT_LIST_HEAD(&adap->userspace_clients); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
Jean Delvare | 8fcfef6 | 2009-03-28 21:34:43 +0100 | [diff] [blame] | 1402 | /* Set default timeout to 1 second if not already set */ |
| 1403 | if (adap->timeout == 0) |
| 1404 | adap->timeout = HZ; |
| 1405 | |
Kay Sievers | 27d9c18 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 1406 | dev_set_name(&adap->dev, "i2c-%d", adap->nr); |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1407 | adap->dev.bus = &i2c_bus_type; |
| 1408 | adap->dev.type = &i2c_adapter_type; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1409 | res = device_register(&adap->dev); |
| 1410 | if (res) |
| 1411 | goto out_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 1413 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
| 1414 | |
Charles Keepax | 6ada5c1 | 2015-04-16 13:05:19 +0100 | [diff] [blame] | 1415 | pm_runtime_no_callbacks(&adap->dev); |
| 1416 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1417 | #ifdef CONFIG_I2C_COMPAT |
| 1418 | res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, |
| 1419 | adap->dev.parent); |
| 1420 | if (res) |
| 1421 | dev_warn(&adap->dev, |
| 1422 | "Failed to create compatibility class link\n"); |
| 1423 | #endif |
| 1424 | |
Viresh Kumar | 5f9296b | 2012-02-28 18:26:31 +0530 | [diff] [blame] | 1425 | /* bus recovery specific initialization */ |
| 1426 | if (adap->bus_recovery_info) { |
| 1427 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; |
| 1428 | |
| 1429 | if (!bri->recover_bus) { |
| 1430 | dev_err(&adap->dev, "No recover_bus() found, not using recovery\n"); |
| 1431 | adap->bus_recovery_info = NULL; |
| 1432 | goto exit_recovery; |
| 1433 | } |
| 1434 | |
| 1435 | /* Generic GPIO recovery */ |
| 1436 | if (bri->recover_bus == i2c_generic_gpio_recovery) { |
| 1437 | if (!gpio_is_valid(bri->scl_gpio)) { |
| 1438 | dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n"); |
| 1439 | adap->bus_recovery_info = NULL; |
| 1440 | goto exit_recovery; |
| 1441 | } |
| 1442 | |
| 1443 | if (gpio_is_valid(bri->sda_gpio)) |
| 1444 | bri->get_sda = get_sda_gpio_value; |
| 1445 | else |
| 1446 | bri->get_sda = NULL; |
| 1447 | |
| 1448 | bri->get_scl = get_scl_gpio_value; |
| 1449 | bri->set_scl = set_scl_gpio_value; |
| 1450 | } else if (!bri->set_scl || !bri->get_scl) { |
| 1451 | /* Generic SCL recovery */ |
| 1452 | dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n"); |
| 1453 | adap->bus_recovery_info = NULL; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | exit_recovery: |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1458 | /* create pre-declared device nodes */ |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1459 | of_i2c_register_devices(adap); |
Mika Westerberg | 55e71ed | 2013-08-21 17:28:23 +0300 | [diff] [blame] | 1460 | acpi_i2c_register_devices(adap); |
Lan Tianyu | 5d98e61 | 2014-05-20 20:59:23 +0800 | [diff] [blame] | 1461 | acpi_i2c_install_space_handler(adap); |
Wolfram Sang | 687b81d | 2013-07-11 12:56:15 +0100 | [diff] [blame] | 1462 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1463 | if (adap->nr < __i2c_first_dynamic_bus_num) |
| 1464 | i2c_scan_static_board_info(adap); |
| 1465 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1466 | /* Notify drivers */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1467 | mutex_lock(&core_lock); |
Jean Delvare | d670328 | 2010-08-11 18:20:59 +0200 | [diff] [blame] | 1468 | bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1469 | mutex_unlock(&core_lock); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1470 | |
| 1471 | return 0; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1472 | |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1473 | out_list: |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1474 | mutex_lock(&core_lock); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 1475 | idr_remove(&i2c_adapter_idr, adap->nr); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1476 | mutex_unlock(&core_lock); |
| 1477 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1480 | /** |
Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1481 | * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 |
| 1482 | * @adap: the adapter to register (with adap->nr initialized) |
| 1483 | * Context: can sleep |
| 1484 | * |
| 1485 | * See i2c_add_numbered_adapter() for details. |
| 1486 | */ |
| 1487 | static int __i2c_add_numbered_adapter(struct i2c_adapter *adap) |
| 1488 | { |
| 1489 | int id; |
| 1490 | |
| 1491 | mutex_lock(&core_lock); |
| 1492 | id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, |
| 1493 | GFP_KERNEL); |
| 1494 | mutex_unlock(&core_lock); |
| 1495 | if (id < 0) |
| 1496 | return id == -ENOSPC ? -EBUSY : id; |
| 1497 | |
| 1498 | return i2c_register_adapter(adap); |
| 1499 | } |
| 1500 | |
| 1501 | /** |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1502 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number |
| 1503 | * @adapter: the adapter to add |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1504 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1505 | * |
| 1506 | * This routine is used to declare an I2C adapter when its bus number |
Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1507 | * doesn't matter or when its bus number is specified by an dt alias. |
| 1508 | * Examples of bases when the bus number doesn't matter: I2C adapters |
| 1509 | * dynamically added by USB links or PCI plugin cards. |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1510 | * |
| 1511 | * When this returns zero, a new bus number was allocated and stored |
| 1512 | * in adap->nr, and the specified adapter became available for clients. |
| 1513 | * Otherwise, a negative errno value is returned. |
| 1514 | */ |
| 1515 | int i2c_add_adapter(struct i2c_adapter *adapter) |
| 1516 | { |
Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1517 | struct device *dev = &adapter->dev; |
Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1518 | int id; |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1519 | |
Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1520 | if (dev->of_node) { |
| 1521 | id = of_alias_get_id(dev->of_node, "i2c"); |
| 1522 | if (id >= 0) { |
| 1523 | adapter->nr = id; |
| 1524 | return __i2c_add_numbered_adapter(adapter); |
| 1525 | } |
| 1526 | } |
| 1527 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1528 | mutex_lock(&core_lock); |
Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1529 | id = idr_alloc(&i2c_adapter_idr, adapter, |
| 1530 | __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1531 | mutex_unlock(&core_lock); |
Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1532 | if (id < 0) |
| 1533 | return id; |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1534 | |
| 1535 | adapter->nr = id; |
Tejun Heo | 4ae42b0 | 2013-02-27 17:04:15 -0800 | [diff] [blame] | 1536 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1537 | return i2c_register_adapter(adapter); |
| 1538 | } |
| 1539 | EXPORT_SYMBOL(i2c_add_adapter); |
| 1540 | |
| 1541 | /** |
| 1542 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number |
| 1543 | * @adap: the adapter to register (with adap->nr initialized) |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1544 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1545 | * |
| 1546 | * This routine is used to declare an I2C adapter when its bus number |
Randy Dunlap | 8c07e46 | 2008-03-23 20:28:20 +0100 | [diff] [blame] | 1547 | * matters. For example, use it for I2C adapters from system-on-chip CPUs, |
| 1548 | * or otherwise built in to the system's mainboard, and where i2c_board_info |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1549 | * is used to properly configure I2C devices. |
| 1550 | * |
Grant Likely | 488bf31 | 2011-07-25 17:49:43 +0200 | [diff] [blame] | 1551 | * If the requested bus number is set to -1, then this function will behave |
| 1552 | * identically to i2c_add_adapter, and will dynamically assign a bus number. |
| 1553 | * |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1554 | * If no devices have pre-been declared for this bus, then be sure to |
| 1555 | * register the adapter before any dynamically allocated ones. Otherwise |
| 1556 | * the required bus ID may not be available. |
| 1557 | * |
| 1558 | * When this returns zero, the specified adapter became available for |
| 1559 | * clients using the bus number provided in adap->nr. Also, the table |
| 1560 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, |
| 1561 | * and the appropriate driver model device nodes are created. Otherwise, a |
| 1562 | * negative errno value is returned. |
| 1563 | */ |
| 1564 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) |
| 1565 | { |
Grant Likely | 488bf31 | 2011-07-25 17:49:43 +0200 | [diff] [blame] | 1566 | if (adap->nr == -1) /* -1 means dynamically assign bus id */ |
| 1567 | return i2c_add_adapter(adap); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1568 | |
Doug Anderson | ee5c274 | 2013-03-01 08:57:31 -0800 | [diff] [blame] | 1569 | return __i2c_add_numbered_adapter(adap); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1570 | } |
| 1571 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
| 1572 | |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1573 | static void i2c_do_del_adapter(struct i2c_driver *driver, |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1574 | struct i2c_adapter *adapter) |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1575 | { |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1576 | struct i2c_client *client, *_n; |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1577 | |
Jean Delvare | acec211 | 2009-03-28 21:34:40 +0100 | [diff] [blame] | 1578 | /* Remove the devices we created ourselves as the result of hardware |
| 1579 | * probing (using a driver's detect method) */ |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1580 | list_for_each_entry_safe(client, _n, &driver->clients, detected) { |
| 1581 | if (client->adapter == adapter) { |
| 1582 | dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", |
| 1583 | client->name, client->addr); |
| 1584 | list_del(&client->detected); |
| 1585 | i2c_unregister_device(client); |
| 1586 | } |
| 1587 | } |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1590 | static int __unregister_client(struct device *dev, void *dummy) |
| 1591 | { |
| 1592 | struct i2c_client *client = i2c_verify_client(dev); |
Jean Delvare | 5219bf8 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 1593 | if (client && strcmp(client->name, "dummy")) |
| 1594 | i2c_unregister_device(client); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | static int __unregister_dummy(struct device *dev, void *dummy) |
| 1599 | { |
| 1600 | struct i2c_client *client = i2c_verify_client(dev); |
Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1601 | if (client) |
| 1602 | i2c_unregister_device(client); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1606 | static int __process_removed_adapter(struct device_driver *d, void *data) |
| 1607 | { |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1608 | i2c_do_del_adapter(to_i2c_driver(d), data); |
| 1609 | return 0; |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1612 | /** |
| 1613 | * i2c_del_adapter - unregister I2C adapter |
| 1614 | * @adap: the adapter being unregistered |
| 1615 | * Context: can sleep |
| 1616 | * |
| 1617 | * This unregisters an I2C adapter which was previously registered |
| 1618 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. |
| 1619 | */ |
Lars-Peter Clausen | 7154630 | 2013-03-09 08:16:47 +0000 | [diff] [blame] | 1620 | void i2c_del_adapter(struct i2c_adapter *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | { |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1622 | struct i2c_adapter *found; |
Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1623 | struct i2c_client *client, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
| 1625 | /* First make sure that this adapter was ever added */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1626 | mutex_lock(&core_lock); |
| 1627 | found = idr_find(&i2c_adapter_idr, adap->nr); |
| 1628 | mutex_unlock(&core_lock); |
| 1629 | if (found != adap) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 1630 | pr_debug("i2c-core: attempting to delete unregistered " |
| 1631 | "adapter [%s]\n", adap->name); |
Lars-Peter Clausen | 7154630 | 2013-03-09 08:16:47 +0000 | [diff] [blame] | 1632 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Lan Tianyu | 5d98e61 | 2014-05-20 20:59:23 +0800 | [diff] [blame] | 1635 | acpi_i2c_remove_space_handler(adap); |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1636 | /* Tell drivers about this removal */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1637 | mutex_lock(&core_lock); |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1638 | bus_for_each_drv(&i2c_bus_type, NULL, adap, |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1639 | __process_removed_adapter); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1640 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1642 | /* Remove devices instantiated from sysfs */ |
Jean Delvare | 390946b | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 1643 | mutex_lock_nested(&adap->userspace_clients_lock, |
| 1644 | i2c_adapter_depth(adap)); |
Jean Delvare | 6629dcf | 2010-05-04 11:09:28 +0200 | [diff] [blame] | 1645 | list_for_each_entry_safe(client, next, &adap->userspace_clients, |
| 1646 | detected) { |
| 1647 | dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, |
| 1648 | client->addr); |
| 1649 | list_del(&client->detected); |
| 1650 | i2c_unregister_device(client); |
Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1651 | } |
Jean Delvare | dafc50d | 2010-08-11 18:21:01 +0200 | [diff] [blame] | 1652 | mutex_unlock(&adap->userspace_clients_lock); |
Jean Delvare | bbd2d9c | 2009-11-26 09:22:33 +0100 | [diff] [blame] | 1653 | |
Jean Delvare | e549c2b | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1654 | /* Detach any active clients. This can't fail, thus we do not |
Jean Delvare | 5219bf8 | 2011-01-14 22:03:49 +0100 | [diff] [blame] | 1655 | * check the returned value. This is a two-pass process, because |
| 1656 | * we can't remove the dummy devices during the first pass: they |
| 1657 | * could have been instantiated by real devices wishing to clean |
| 1658 | * them up properly, so we give them a chance to do that first. */ |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1659 | device_for_each_child(&adap->dev, NULL, __unregister_client); |
| 1660 | device_for_each_child(&adap->dev, NULL, __unregister_dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1662 | #ifdef CONFIG_I2C_COMPAT |
| 1663 | class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, |
| 1664 | adap->dev.parent); |
| 1665 | #endif |
| 1666 | |
Thadeu Lima de Souza Cascardo | c556752 | 2010-01-16 20:43:13 +0100 | [diff] [blame] | 1667 | /* device name is gone after device_unregister */ |
| 1668 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
| 1669 | |
Wolfram Sang | 26680ee | 2015-01-29 22:45:09 +0100 | [diff] [blame] | 1670 | /* wait until all references to the device are gone |
| 1671 | * |
| 1672 | * FIXME: This is old code and should ideally be replaced by an |
| 1673 | * alternative which results in decoupling the lifetime of the struct |
| 1674 | * device from the i2c_adapter, like spi or netdev do. Any solution |
| 1675 | * should be throughly tested with DEBUG_KOBJECT_RELEASE enabled! |
| 1676 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | init_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | device_unregister(&adap->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | wait_for_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1681 | /* free bus id */ |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1682 | mutex_lock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | idr_remove(&i2c_adapter_idr, adap->nr); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1684 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
Jean Delvare | bd4bc3db | 2008-07-16 19:30:05 +0200 | [diff] [blame] | 1686 | /* Clear the device structure in case this adapter is ever going to be |
| 1687 | added again */ |
| 1688 | memset(&adap->dev, 0, sizeof(adap->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1690 | EXPORT_SYMBOL(i2c_del_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1692 | /* ------------------------------------------------------------------------- */ |
| 1693 | |
Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1694 | int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *)) |
| 1695 | { |
| 1696 | int res; |
| 1697 | |
| 1698 | mutex_lock(&core_lock); |
| 1699 | res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); |
| 1700 | mutex_unlock(&core_lock); |
| 1701 | |
| 1702 | return res; |
| 1703 | } |
| 1704 | EXPORT_SYMBOL_GPL(i2c_for_each_dev); |
| 1705 | |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1706 | static int __process_new_driver(struct device *dev, void *data) |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1707 | { |
Jean Delvare | 4f8cf82 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1708 | if (dev->type != &i2c_adapter_type) |
| 1709 | return 0; |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1710 | return i2c_do_add_adapter(data, to_i2c_adapter(dev)); |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1713 | /* |
| 1714 | * An i2c_driver is used with one or more i2c_client (device) nodes to access |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1715 | * i2c slave chips, on a bus instance associated with some i2c_adapter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | */ |
| 1717 | |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1718 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | { |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1720 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | |
David Brownell | 1d0b19c | 2008-10-14 17:30:05 +0200 | [diff] [blame] | 1722 | /* Can't register until after driver model init */ |
| 1723 | if (unlikely(WARN_ON(!i2c_bus_type.p))) |
| 1724 | return -EAGAIN; |
| 1725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | /* add the driver to the list of i2c drivers in the driver core */ |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1727 | driver->driver.owner = owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | driver->driver.bus = &i2c_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
Jean Delvare | 729d6dd | 2009-06-19 16:58:18 +0200 | [diff] [blame] | 1730 | /* When registration returns, the driver core |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1731 | * will have called probe() for all matching-but-unbound devices. |
| 1732 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | res = driver_register(&driver->driver); |
| 1734 | if (res) |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1735 | return res; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1736 | |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 1737 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 1739 | INIT_LIST_HEAD(&driver->clients); |
| 1740 | /* Walk the adapters that are already present */ |
Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1741 | i2c_for_each_dev(driver, __process_new_driver); |
Jean Delvare | 35fc37f | 2009-06-19 16:58:19 +0200 | [diff] [blame] | 1742 | |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 1743 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 1745 | EXPORT_SYMBOL(i2c_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
Jean Delvare | 69b0089 | 2009-12-06 17:06:27 +0100 | [diff] [blame] | 1747 | static int __process_removed_driver(struct device *dev, void *data) |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1748 | { |
Lars-Peter Clausen | 19baba4 | 2013-03-09 08:16:44 +0000 | [diff] [blame] | 1749 | if (dev->type == &i2c_adapter_type) |
| 1750 | i2c_do_del_adapter(data, to_i2c_adapter(dev)); |
| 1751 | return 0; |
Dave Young | 7f101a9 | 2008-07-14 22:38:19 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1754 | /** |
| 1755 | * i2c_del_driver - unregister I2C driver |
| 1756 | * @driver: the driver being unregistered |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 1757 | * Context: can sleep |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1758 | */ |
Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 1759 | void i2c_del_driver(struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | { |
Jean Delvare | 7ae3148 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 1761 | i2c_for_each_dev(driver, __process_removed_driver); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | driver_unregister(&driver->driver); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 1764 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1766 | EXPORT_SYMBOL(i2c_del_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 1768 | /* ------------------------------------------------------------------------- */ |
| 1769 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 1770 | /** |
| 1771 | * i2c_use_client - increments the reference count of the i2c client structure |
| 1772 | * @client: the client being referenced |
| 1773 | * |
| 1774 | * Each live reference to a client should be refcounted. The driver model does |
| 1775 | * that automatically as part of driver binding, so that most drivers don't |
| 1776 | * need to do this explicitly: they hold a reference until they're unbound |
| 1777 | * from the device. |
| 1778 | * |
| 1779 | * A pointer to the client with the incremented reference counter is returned. |
| 1780 | */ |
| 1781 | struct i2c_client *i2c_use_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | { |
David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1783 | if (client && get_device(&client->dev)) |
| 1784 | return client; |
| 1785 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1787 | EXPORT_SYMBOL(i2c_use_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 1789 | /** |
| 1790 | * i2c_release_client - release a use of the i2c client structure |
| 1791 | * @client: the client being no longer referenced |
| 1792 | * |
| 1793 | * Must be called when a user of a client is finished with it. |
| 1794 | */ |
| 1795 | void i2c_release_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | { |
David Brownell | 6ea438e | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1797 | if (client) |
| 1798 | put_device(&client->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1800 | EXPORT_SYMBOL(i2c_release_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1802 | struct i2c_cmd_arg { |
| 1803 | unsigned cmd; |
| 1804 | void *arg; |
| 1805 | }; |
| 1806 | |
| 1807 | static int i2c_cmd(struct device *dev, void *_arg) |
| 1808 | { |
| 1809 | struct i2c_client *client = i2c_verify_client(dev); |
| 1810 | struct i2c_cmd_arg *arg = _arg; |
Lars-Peter Clausen | 0acc2b3 | 2013-09-29 10:51:06 +0200 | [diff] [blame] | 1811 | struct i2c_driver *driver; |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1812 | |
Lars-Peter Clausen | 0acc2b3 | 2013-09-29 10:51:06 +0200 | [diff] [blame] | 1813 | if (!client || !client->dev.driver) |
| 1814 | return 0; |
| 1815 | |
| 1816 | driver = to_i2c_driver(client->dev.driver); |
| 1817 | if (driver->command) |
| 1818 | driver->command(client, arg->cmd, arg->arg); |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1819 | return 0; |
| 1820 | } |
| 1821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
| 1823 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1824 | struct i2c_cmd_arg cmd_arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1826 | cmd_arg.cmd = cmd; |
| 1827 | cmd_arg.arg = arg; |
| 1828 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1830 | EXPORT_SYMBOL(i2c_clients_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | |
Pantelis Antoniou | ea7513b | 2014-10-28 22:36:03 +0200 | [diff] [blame] | 1832 | #if IS_ENABLED(CONFIG_OF_DYNAMIC) |
| 1833 | static int of_i2c_notify(struct notifier_block *nb, unsigned long action, |
| 1834 | void *arg) |
| 1835 | { |
| 1836 | struct of_reconfig_data *rd = arg; |
| 1837 | struct i2c_adapter *adap; |
| 1838 | struct i2c_client *client; |
| 1839 | |
| 1840 | switch (of_reconfig_get_state_change(action, rd)) { |
| 1841 | case OF_RECONFIG_CHANGE_ADD: |
| 1842 | adap = of_find_i2c_adapter_by_node(rd->dn->parent); |
| 1843 | if (adap == NULL) |
| 1844 | return NOTIFY_OK; /* not for us */ |
| 1845 | |
| 1846 | client = of_i2c_register_device(adap, rd->dn); |
| 1847 | put_device(&adap->dev); |
| 1848 | |
| 1849 | if (IS_ERR(client)) { |
| 1850 | pr_err("%s: failed to create for '%s'\n", |
| 1851 | __func__, rd->dn->full_name); |
| 1852 | return notifier_from_errno(PTR_ERR(client)); |
| 1853 | } |
| 1854 | break; |
| 1855 | case OF_RECONFIG_CHANGE_REMOVE: |
| 1856 | /* find our device by node */ |
| 1857 | client = of_find_i2c_device_by_node(rd->dn); |
| 1858 | if (client == NULL) |
| 1859 | return NOTIFY_OK; /* no? not meant for us */ |
| 1860 | |
| 1861 | /* unregister takes one ref away */ |
| 1862 | i2c_unregister_device(client); |
| 1863 | |
| 1864 | /* and put the reference of the find */ |
| 1865 | put_device(&client->dev); |
| 1866 | break; |
| 1867 | } |
| 1868 | |
| 1869 | return NOTIFY_OK; |
| 1870 | } |
| 1871 | static struct notifier_block i2c_of_notifier = { |
| 1872 | .notifier_call = of_i2c_notify, |
| 1873 | }; |
| 1874 | #else |
| 1875 | extern struct notifier_block i2c_of_notifier; |
| 1876 | #endif /* CONFIG_OF_DYNAMIC */ |
| 1877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | static int __init i2c_init(void) |
| 1879 | { |
| 1880 | int retval; |
| 1881 | |
Wolfram Sang | 03bde7c | 2015-03-12 17:17:59 +0100 | [diff] [blame] | 1882 | retval = of_alias_get_highest_id("i2c"); |
| 1883 | |
| 1884 | down_write(&__i2c_board_lock); |
| 1885 | if (retval >= __i2c_first_dynamic_bus_num) |
| 1886 | __i2c_first_dynamic_bus_num = retval + 1; |
| 1887 | up_write(&__i2c_board_lock); |
| 1888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | retval = bus_register(&i2c_bus_type); |
| 1890 | if (retval) |
| 1891 | return retval; |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1892 | #ifdef CONFIG_I2C_COMPAT |
| 1893 | i2c_adapter_compat_class = class_compat_register("i2c-adapter"); |
| 1894 | if (!i2c_adapter_compat_class) { |
| 1895 | retval = -ENOMEM; |
| 1896 | goto bus_err; |
| 1897 | } |
| 1898 | #endif |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1899 | retval = i2c_add_driver(&dummy_driver); |
| 1900 | if (retval) |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1901 | goto class_err; |
Pantelis Antoniou | ea7513b | 2014-10-28 22:36:03 +0200 | [diff] [blame] | 1902 | |
| 1903 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) |
| 1904 | WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier)); |
| 1905 | |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1906 | return 0; |
| 1907 | |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1908 | class_err: |
| 1909 | #ifdef CONFIG_I2C_COMPAT |
| 1910 | class_compat_unregister(i2c_adapter_compat_class); |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1911 | bus_err: |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1912 | #endif |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1913 | bus_unregister(&i2c_bus_type); |
| 1914 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static void __exit i2c_exit(void) |
| 1918 | { |
Pantelis Antoniou | ea7513b | 2014-10-28 22:36:03 +0200 | [diff] [blame] | 1919 | if (IS_ENABLED(CONFIG_OF_DYNAMIC)) |
| 1920 | WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier)); |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 1921 | i2c_del_driver(&dummy_driver); |
Jean Delvare | 2bb5095 | 2009-09-18 22:45:46 +0200 | [diff] [blame] | 1922 | #ifdef CONFIG_I2C_COMPAT |
| 1923 | class_compat_unregister(i2c_adapter_compat_class); |
| 1924 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | bus_unregister(&i2c_bus_type); |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 1926 | tracepoint_synchronize_unregister(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
David Brownell | a10f9e7 | 2008-10-14 17:30:06 +0200 | [diff] [blame] | 1929 | /* We must initialize early, because some subsystems register i2c drivers |
| 1930 | * in subsys_initcall() code, but are linked (and initialized) before i2c. |
| 1931 | */ |
| 1932 | postcore_initcall(i2c_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | module_exit(i2c_exit); |
| 1934 | |
| 1935 | /* ---------------------------------------------------- |
| 1936 | * the functional interface to the i2c busses. |
| 1937 | * ---------------------------------------------------- |
| 1938 | */ |
| 1939 | |
Wolfram Sang | b7f6258 | 2015-01-05 23:45:59 +0100 | [diff] [blame] | 1940 | /* Check if val is exceeding the quirk IFF quirk is non 0 */ |
| 1941 | #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk))) |
| 1942 | |
| 1943 | static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg) |
| 1944 | { |
| 1945 | dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", |
| 1946 | err_msg, msg->addr, msg->len, |
| 1947 | msg->flags & I2C_M_RD ? "read" : "write"); |
| 1948 | return -EOPNOTSUPP; |
| 1949 | } |
| 1950 | |
| 1951 | static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 1952 | { |
| 1953 | const struct i2c_adapter_quirks *q = adap->quirks; |
| 1954 | int max_num = q->max_num_msgs, i; |
| 1955 | bool do_len_check = true; |
| 1956 | |
| 1957 | if (q->flags & I2C_AQ_COMB) { |
| 1958 | max_num = 2; |
| 1959 | |
| 1960 | /* special checks for combined messages */ |
| 1961 | if (num == 2) { |
| 1962 | if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) |
| 1963 | return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write"); |
| 1964 | |
| 1965 | if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) |
| 1966 | return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read"); |
| 1967 | |
| 1968 | if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) |
| 1969 | return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr"); |
| 1970 | |
| 1971 | if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) |
| 1972 | return i2c_quirk_error(adap, &msgs[0], "msg too long"); |
| 1973 | |
| 1974 | if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) |
| 1975 | return i2c_quirk_error(adap, &msgs[1], "msg too long"); |
| 1976 | |
| 1977 | do_len_check = false; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | if (i2c_quirk_exceeded(num, max_num)) |
| 1982 | return i2c_quirk_error(adap, &msgs[0], "too many messages"); |
| 1983 | |
| 1984 | for (i = 0; i < num; i++) { |
| 1985 | u16 len = msgs[i].len; |
| 1986 | |
| 1987 | if (msgs[i].flags & I2C_M_RD) { |
| 1988 | if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) |
| 1989 | return i2c_quirk_error(adap, &msgs[i], "msg too long"); |
| 1990 | } else { |
| 1991 | if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) |
| 1992 | return i2c_quirk_error(adap, &msgs[i], "msg too long"); |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 1999 | /** |
Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 2000 | * __i2c_transfer - unlocked flavor of i2c_transfer |
| 2001 | * @adap: Handle to I2C bus |
| 2002 | * @msgs: One or more messages to execute before STOP is issued to |
| 2003 | * terminate the operation; each message begins with a START. |
| 2004 | * @num: Number of messages to be executed. |
| 2005 | * |
| 2006 | * Returns negative errno, else the number of messages executed. |
| 2007 | * |
| 2008 | * Adapter lock must be held when calling this function. No debug logging |
| 2009 | * takes place. adap->algo->master_xfer existence isn't checked. |
| 2010 | */ |
| 2011 | int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 2012 | { |
| 2013 | unsigned long orig_jiffies; |
| 2014 | int ret, try; |
| 2015 | |
Wolfram Sang | b7f6258 | 2015-01-05 23:45:59 +0100 | [diff] [blame] | 2016 | if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) |
| 2017 | return -EOPNOTSUPP; |
| 2018 | |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 2019 | /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets |
| 2020 | * enabled. This is an efficient way of keeping the for-loop from |
| 2021 | * being executed when not needed. |
| 2022 | */ |
| 2023 | if (static_key_false(&i2c_trace_msg)) { |
| 2024 | int i; |
| 2025 | for (i = 0; i < num; i++) |
| 2026 | if (msgs[i].flags & I2C_M_RD) |
| 2027 | trace_i2c_read(adap, &msgs[i], i); |
| 2028 | else |
| 2029 | trace_i2c_write(adap, &msgs[i], i); |
| 2030 | } |
| 2031 | |
Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 2032 | /* Retry automatically on arbitration loss */ |
| 2033 | orig_jiffies = jiffies; |
| 2034 | for (ret = 0, try = 0; try <= adap->retries; try++) { |
| 2035 | ret = adap->algo->master_xfer(adap, msgs, num); |
| 2036 | if (ret != -EAGAIN) |
| 2037 | break; |
| 2038 | if (time_after(jiffies, orig_jiffies + adap->timeout)) |
| 2039 | break; |
| 2040 | } |
| 2041 | |
David Howells | d9a83d6 | 2014-03-06 13:35:59 +0000 | [diff] [blame] | 2042 | if (static_key_false(&i2c_trace_msg)) { |
| 2043 | int i; |
| 2044 | for (i = 0; i < ret; i++) |
| 2045 | if (msgs[i].flags & I2C_M_RD) |
| 2046 | trace_i2c_reply(adap, &msgs[i], i); |
| 2047 | trace_i2c_result(adap, i, ret); |
| 2048 | } |
| 2049 | |
Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 2050 | return ret; |
| 2051 | } |
| 2052 | EXPORT_SYMBOL(__i2c_transfer); |
| 2053 | |
| 2054 | /** |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2055 | * i2c_transfer - execute a single or combined I2C message |
| 2056 | * @adap: Handle to I2C bus |
| 2057 | * @msgs: One or more messages to execute before STOP is issued to |
| 2058 | * terminate the operation; each message begins with a START. |
| 2059 | * @num: Number of messages to be executed. |
| 2060 | * |
| 2061 | * Returns negative errno, else the number of messages executed. |
| 2062 | * |
| 2063 | * Note that there is no requirement that each message be sent to |
| 2064 | * the same slave address, although that is the most common model. |
| 2065 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2066 | int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | { |
Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 2068 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2070 | /* REVISIT the fault reporting model here is weak: |
| 2071 | * |
| 2072 | * - When we get an error after receiving N bytes from a slave, |
| 2073 | * there is no way to report "N". |
| 2074 | * |
| 2075 | * - When we get a NAK after transmitting N bytes to a slave, |
| 2076 | * there is no way to report "N" ... or to let the master |
| 2077 | * continue executing the rest of this combined message, if |
| 2078 | * that's the appropriate response. |
| 2079 | * |
| 2080 | * - When for example "num" is two and we successfully complete |
| 2081 | * the first message but get an error part way through the |
| 2082 | * second, it's unclear whether that should be reported as |
| 2083 | * one (discarding status on the second message) or errno |
| 2084 | * (discarding status on the first one). |
| 2085 | */ |
| 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | if (adap->algo->master_xfer) { |
| 2088 | #ifdef DEBUG |
| 2089 | for (ret = 0; ret < num; ret++) { |
| 2090 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2091 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
| 2092 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, |
| 2093 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
| 2095 | #endif |
| 2096 | |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 2097 | if (in_atomic() || irqs_disabled()) { |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2098 | ret = i2c_trylock_adapter(adap); |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 2099 | if (!ret) |
| 2100 | /* I2C activity is ongoing. */ |
| 2101 | return -EAGAIN; |
| 2102 | } else { |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2103 | i2c_lock_adapter(adap); |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
Jean Delvare | b37d2a3 | 2012-06-29 07:47:19 -0300 | [diff] [blame] | 2106 | ret = __i2c_transfer(adap, msgs, num); |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2107 | i2c_unlock_adapter(adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
| 2109 | return ret; |
| 2110 | } else { |
| 2111 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2112 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2115 | EXPORT_SYMBOL(i2c_transfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2117 | /** |
| 2118 | * i2c_master_send - issue a single I2C message in master transmit mode |
| 2119 | * @client: Handle to slave device |
| 2120 | * @buf: Data that will be written to the slave |
Zhangfei Gao | 0c43ea5 | 2010-03-02 12:23:49 +0100 | [diff] [blame] | 2121 | * @count: How many bytes to write, must be less than 64k since msg.len is u16 |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2122 | * |
| 2123 | * Returns negative errno, or else the number of bytes written. |
| 2124 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2125 | int i2c_master_send(const struct i2c_client *client, const char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
| 2127 | int ret; |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2128 | struct i2c_adapter *adap = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | struct i2c_msg msg; |
| 2130 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 2131 | msg.addr = client->addr; |
| 2132 | msg.flags = client->flags & I2C_M_TEN; |
| 2133 | msg.len = count; |
| 2134 | msg.buf = (char *)buf; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2135 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 2136 | ret = i2c_transfer(adap, &msg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | |
Wolfram Sang | 834aa6f | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 2138 | /* |
| 2139 | * If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 2140 | * transmitted, else error code. |
| 2141 | */ |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 2142 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2144 | EXPORT_SYMBOL(i2c_master_send); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2146 | /** |
| 2147 | * i2c_master_recv - issue a single I2C message in master receive mode |
| 2148 | * @client: Handle to slave device |
| 2149 | * @buf: Where to store data read from slave |
Zhangfei Gao | 0c43ea5 | 2010-03-02 12:23:49 +0100 | [diff] [blame] | 2150 | * @count: How many bytes to read, must be less than 64k since msg.len is u16 |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2151 | * |
| 2152 | * Returns negative errno, or else the number of bytes read. |
| 2153 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2154 | int i2c_master_recv(const struct i2c_client *client, char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2156 | struct i2c_adapter *adap = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | struct i2c_msg msg; |
| 2158 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 2160 | msg.addr = client->addr; |
| 2161 | msg.flags = client->flags & I2C_M_TEN; |
| 2162 | msg.flags |= I2C_M_RD; |
| 2163 | msg.len = count; |
| 2164 | msg.buf = buf; |
| 2165 | |
| 2166 | ret = i2c_transfer(adap, &msg, 1); |
| 2167 | |
Wolfram Sang | 834aa6f | 2012-03-15 18:11:05 +0100 | [diff] [blame] | 2168 | /* |
| 2169 | * If everything went ok (i.e. 1 msg received), return #bytes received, |
| 2170 | * else error code. |
| 2171 | */ |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 2172 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2174 | EXPORT_SYMBOL(i2c_master_recv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | /* ---------------------------------------------------- |
| 2177 | * the i2c address scanning function |
| 2178 | * Will not work for 10-bit addresses! |
| 2179 | * ---------------------------------------------------- |
| 2180 | */ |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 2181 | |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2182 | /* |
| 2183 | * Legacy default probe function, mostly relevant for SMBus. The default |
| 2184 | * probe method is a quick write, but it is known to corrupt the 24RF08 |
| 2185 | * EEPROMs due to a state machine bug, and could also irreversibly |
| 2186 | * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, |
| 2187 | * we use a short byte read instead. Also, some bus drivers don't implement |
| 2188 | * quick write, so we fallback to a byte read in that case too. |
| 2189 | * On x86, there is another special case for FSC hardware monitoring chips, |
| 2190 | * which want regular byte reads (address 0x73.) Fortunately, these are the |
| 2191 | * only known chips using this I2C address on PC hardware. |
| 2192 | * Returns 1 if probe succeeded, 0 if not. |
| 2193 | */ |
| 2194 | static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) |
| 2195 | { |
| 2196 | int err; |
| 2197 | union i2c_smbus_data dummy; |
| 2198 | |
| 2199 | #ifdef CONFIG_X86 |
| 2200 | if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) |
| 2201 | && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 2202 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, |
| 2203 | I2C_SMBUS_BYTE_DATA, &dummy); |
| 2204 | else |
| 2205 | #endif |
Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 2206 | if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) |
| 2207 | && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2208 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, |
| 2209 | I2C_SMBUS_QUICK, NULL); |
Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 2210 | else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) |
| 2211 | err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, |
| 2212 | I2C_SMBUS_BYTE, &dummy); |
| 2213 | else { |
Andy Lutomirski | d63a9e8 | 2013-07-17 13:27:59 -0700 | [diff] [blame] | 2214 | dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", |
| 2215 | addr); |
Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 2216 | err = -EOPNOTSUPP; |
| 2217 | } |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2218 | |
| 2219 | return err >= 0; |
| 2220 | } |
| 2221 | |
Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 2222 | static int i2c_detect_address(struct i2c_client *temp_client, |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2223 | struct i2c_driver *driver) |
| 2224 | { |
| 2225 | struct i2c_board_info info; |
| 2226 | struct i2c_adapter *adapter = temp_client->adapter; |
| 2227 | int addr = temp_client->addr; |
| 2228 | int err; |
| 2229 | |
| 2230 | /* Make sure the address is valid */ |
Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 2231 | err = i2c_check_addr_validity(addr); |
| 2232 | if (err) { |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2233 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
| 2234 | addr); |
Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 2235 | return err; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Skip if already in use */ |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 2239 | if (i2c_check_addr_busy(adapter, addr)) |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2240 | return 0; |
| 2241 | |
Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 2242 | /* Make sure there is something at this address */ |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2243 | if (!i2c_default_probe(adapter, addr)) |
| 2244 | return 0; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2245 | |
| 2246 | /* Finally call the custom detection function */ |
| 2247 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 2248 | info.addr = addr; |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 2249 | err = driver->detect(temp_client, &info); |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2250 | if (err) { |
| 2251 | /* -ENODEV is returned if the detection fails. We catch it |
| 2252 | here as this isn't an error. */ |
| 2253 | return err == -ENODEV ? 0 : err; |
| 2254 | } |
| 2255 | |
| 2256 | /* Consistency check */ |
| 2257 | if (info.type[0] == '\0') { |
| 2258 | dev_err(&adapter->dev, "%s detection function provided " |
| 2259 | "no name for 0x%x\n", driver->driver.name, |
| 2260 | addr); |
| 2261 | } else { |
| 2262 | struct i2c_client *client; |
| 2263 | |
| 2264 | /* Detection succeeded, instantiate the device */ |
Wolfram Sang | 0c17617 | 2014-02-10 11:03:56 +0100 | [diff] [blame] | 2265 | if (adapter->class & I2C_CLASS_DEPRECATED) |
| 2266 | dev_warn(&adapter->dev, |
| 2267 | "This adapter will soon drop class based instantiation of devices. " |
| 2268 | "Please make sure client 0x%02x gets instantiated by other means. " |
| 2269 | "Check 'Documentation/i2c/instantiating-devices' for details.\n", |
| 2270 | info.addr); |
| 2271 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2272 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", |
| 2273 | info.type, info.addr); |
| 2274 | client = i2c_new_device(adapter, &info); |
| 2275 | if (client) |
| 2276 | list_add_tail(&client->detected, &driver->clients); |
| 2277 | else |
| 2278 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", |
| 2279 | info.type, info.addr); |
| 2280 | } |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
| 2284 | static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) |
| 2285 | { |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 2286 | const unsigned short *address_list; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2287 | struct i2c_client *temp_client; |
| 2288 | int i, err = 0; |
| 2289 | int adap_id = i2c_adapter_id(adapter); |
| 2290 | |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 2291 | address_list = driver->address_list; |
| 2292 | if (!driver->detect || !address_list) |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2293 | return 0; |
| 2294 | |
Wolfram Sang | 4555227 | 2014-07-10 13:46:21 +0200 | [diff] [blame] | 2295 | /* Warn that the adapter lost class based instantiation */ |
| 2296 | if (adapter->class == I2C_CLASS_DEPRECATED) { |
| 2297 | dev_dbg(&adapter->dev, |
| 2298 | "This adapter dropped support for I2C classes and " |
| 2299 | "won't auto-detect %s devices anymore. If you need it, check " |
| 2300 | "'Documentation/i2c/instantiating-devices' for alternatives.\n", |
| 2301 | driver->driver.name); |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Jean Delvare | 51b54ba | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 2305 | /* Stop here if the classes do not match */ |
| 2306 | if (!(adapter->class & driver->class)) |
| 2307 | return 0; |
| 2308 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2309 | /* Set up a temporary client to help detect callback */ |
| 2310 | temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
| 2311 | if (!temp_client) |
| 2312 | return -ENOMEM; |
| 2313 | temp_client->adapter = adapter; |
| 2314 | |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 2315 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2316 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 2317 | "addr 0x%02x\n", adap_id, address_list[i]); |
| 2318 | temp_client->addr = address_list[i]; |
Jean Delvare | ccfbbd0 | 2009-12-06 17:06:25 +0100 | [diff] [blame] | 2319 | err = i2c_detect_address(temp_client, driver); |
Jean Delvare | 51b54ba | 2010-10-24 18:16:58 +0200 | [diff] [blame] | 2320 | if (unlikely(err)) |
| 2321 | break; |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2322 | } |
| 2323 | |
Jean Delvare | 4735c98 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 2324 | kfree(temp_client); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
Jean Delvare | d44f19d | 2010-08-11 18:20:57 +0200 | [diff] [blame] | 2328 | int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) |
| 2329 | { |
| 2330 | return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, |
| 2331 | I2C_SMBUS_QUICK, NULL) >= 0; |
| 2332 | } |
| 2333 | EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); |
| 2334 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2335 | struct i2c_client * |
| 2336 | i2c_new_probed_device(struct i2c_adapter *adap, |
| 2337 | struct i2c_board_info *info, |
Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 2338 | unsigned short const *addr_list, |
| 2339 | int (*probe)(struct i2c_adapter *, unsigned short addr)) |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2340 | { |
| 2341 | int i; |
| 2342 | |
Jean Delvare | 8031d79 | 2010-08-11 18:21:00 +0200 | [diff] [blame] | 2343 | if (!probe) |
Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 2344 | probe = i2c_default_probe; |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2345 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2346 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
| 2347 | /* Check address validity */ |
Jean Delvare | 656b876 | 2010-06-03 11:33:53 +0200 | [diff] [blame] | 2348 | if (i2c_check_addr_validity(addr_list[i]) < 0) { |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2349 | dev_warn(&adap->dev, "Invalid 7-bit address " |
| 2350 | "0x%02x\n", addr_list[i]); |
| 2351 | continue; |
| 2352 | } |
| 2353 | |
| 2354 | /* Check address availability */ |
Jean Delvare | 3b5f794 | 2010-06-03 11:33:55 +0200 | [diff] [blame] | 2355 | if (i2c_check_addr_busy(adap, addr_list[i])) { |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2356 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
| 2357 | "use, not probing\n", addr_list[i]); |
| 2358 | continue; |
| 2359 | } |
| 2360 | |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2361 | /* Test address responsiveness */ |
Jean Delvare | 9a94241 | 2010-08-11 18:20:56 +0200 | [diff] [blame] | 2362 | if (probe(adap, addr_list[i])) |
Jean Delvare | 63e4e80 | 2010-06-03 11:33:51 +0200 | [diff] [blame] | 2363 | break; |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2364 | } |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2365 | |
| 2366 | if (addr_list[i] == I2C_CLIENT_END) { |
| 2367 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | |
| 2371 | info->addr = addr_list[i]; |
| 2372 | return i2c_new_device(adap, info); |
| 2373 | } |
| 2374 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); |
| 2375 | |
Jean Delvare | d735b34 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 2376 | struct i2c_adapter *i2c_get_adapter(int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | struct i2c_adapter *adapter; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2379 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 2380 | mutex_lock(&core_lock); |
Jean Delvare | d735b34 | 2011-03-20 14:50:52 +0100 | [diff] [blame] | 2381 | adapter = idr_find(&i2c_adapter_idr, nr); |
Mark M. Hoffman | a0920e10 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 2382 | if (adapter && !try_module_get(adapter->owner)) |
| 2383 | adapter = NULL; |
| 2384 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 2385 | mutex_unlock(&core_lock); |
Mark M. Hoffman | a0920e10 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 2386 | return adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2388 | EXPORT_SYMBOL(i2c_get_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | |
| 2390 | void i2c_put_adapter(struct i2c_adapter *adap) |
| 2391 | { |
Sebastian Hesselbarth | c66c4cc | 2013-08-01 14:10:46 +0200 | [diff] [blame] | 2392 | if (adap) |
| 2393 | module_put(adap->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2395 | EXPORT_SYMBOL(i2c_put_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | |
| 2397 | /* The SMBus parts */ |
| 2398 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2399 | #define POLY (0x1070U << 3) |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2400 | static u8 crc8(u16 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | { |
| 2402 | int i; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2403 | |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2404 | for (i = 0; i < 8; i++) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2405 | if (data & 0x8000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | data = data ^ POLY; |
| 2407 | data = data << 1; |
| 2408 | } |
| 2409 | return (u8)(data >> 8); |
| 2410 | } |
| 2411 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2412 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
| 2413 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | { |
| 2415 | int i; |
| 2416 | |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2417 | for (i = 0; i < count; i++) |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2418 | crc = crc8((crc ^ p[i]) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | return crc; |
| 2420 | } |
| 2421 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2422 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
| 2423 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2425 | /* The address will be sent first */ |
| 2426 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
| 2427 | pec = i2c_smbus_pec(pec, &addr, 1); |
| 2428 | |
| 2429 | /* The data buffer follows */ |
| 2430 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2433 | /* Used for write only transactions */ |
| 2434 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2436 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
| 2437 | msg->len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2440 | /* Return <0 on CRC error |
| 2441 | If there was a write before this read (most cases) we need to take the |
| 2442 | partial CRC from the write part into account. |
| 2443 | Note that this function does modify the message (we need to decrease the |
| 2444 | message length to hide the CRC byte from the caller). */ |
| 2445 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2447 | u8 rpec = msg->buf[--msg->len]; |
| 2448 | cpec = i2c_smbus_msg_pec(cpec, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | if (rpec != cpec) { |
| 2451 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
| 2452 | rpec, cpec); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2453 | return -EBADMSG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | } |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2458 | /** |
| 2459 | * i2c_smbus_read_byte - SMBus "receive byte" protocol |
| 2460 | * @client: Handle to slave device |
| 2461 | * |
| 2462 | * This executes the SMBus "receive byte" protocol, returning negative errno |
| 2463 | * else the byte received from the device. |
| 2464 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2465 | s32 i2c_smbus_read_byte(const struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | { |
| 2467 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2468 | int status; |
| 2469 | |
| 2470 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2471 | I2C_SMBUS_READ, 0, |
| 2472 | I2C_SMBUS_BYTE, &data); |
| 2473 | return (status < 0) ? status : data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2475 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2477 | /** |
| 2478 | * i2c_smbus_write_byte - SMBus "send byte" protocol |
| 2479 | * @client: Handle to slave device |
| 2480 | * @value: Byte to be sent |
| 2481 | * |
| 2482 | * This executes the SMBus "send byte" protocol, returning negative errno |
| 2483 | * else zero on success. |
| 2484 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2485 | s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | { |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2487 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2488 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2490 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2492 | /** |
| 2493 | * i2c_smbus_read_byte_data - SMBus "read byte" protocol |
| 2494 | * @client: Handle to slave device |
| 2495 | * @command: Byte interpreted by slave |
| 2496 | * |
| 2497 | * This executes the SMBus "read byte" protocol, returning negative errno |
| 2498 | * else a data byte received from the device. |
| 2499 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2500 | s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | { |
| 2502 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2503 | int status; |
| 2504 | |
| 2505 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2506 | I2C_SMBUS_READ, command, |
| 2507 | I2C_SMBUS_BYTE_DATA, &data); |
| 2508 | return (status < 0) ? status : data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2510 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2512 | /** |
| 2513 | * i2c_smbus_write_byte_data - SMBus "write byte" protocol |
| 2514 | * @client: Handle to slave device |
| 2515 | * @command: Byte interpreted by slave |
| 2516 | * @value: Byte being written |
| 2517 | * |
| 2518 | * This executes the SMBus "write byte" protocol, returning negative errno |
| 2519 | * else zero on success. |
| 2520 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2521 | s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, |
| 2522 | u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | { |
| 2524 | union i2c_smbus_data data; |
| 2525 | data.byte = value; |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2526 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2527 | I2C_SMBUS_WRITE, command, |
| 2528 | I2C_SMBUS_BYTE_DATA, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2530 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2532 | /** |
| 2533 | * i2c_smbus_read_word_data - SMBus "read word" protocol |
| 2534 | * @client: Handle to slave device |
| 2535 | * @command: Byte interpreted by slave |
| 2536 | * |
| 2537 | * This executes the SMBus "read word" protocol, returning negative errno |
| 2538 | * else a 16-bit unsigned "word" received from the device. |
| 2539 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2540 | s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | { |
| 2542 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2543 | int status; |
| 2544 | |
| 2545 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2546 | I2C_SMBUS_READ, command, |
| 2547 | I2C_SMBUS_WORD_DATA, &data); |
| 2548 | return (status < 0) ? status : data.word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2550 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2552 | /** |
| 2553 | * i2c_smbus_write_word_data - SMBus "write word" protocol |
| 2554 | * @client: Handle to slave device |
| 2555 | * @command: Byte interpreted by slave |
| 2556 | * @value: 16-bit "word" being written |
| 2557 | * |
| 2558 | * This executes the SMBus "write word" protocol, returning negative errno |
| 2559 | * else zero on success. |
| 2560 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2561 | s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, |
| 2562 | u16 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | { |
| 2564 | union i2c_smbus_data data; |
| 2565 | data.word = value; |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2566 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2567 | I2C_SMBUS_WRITE, command, |
| 2568 | I2C_SMBUS_WORD_DATA, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2570 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2572 | /** |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2573 | * i2c_smbus_read_block_data - SMBus "block read" protocol |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2574 | * @client: Handle to slave device |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2575 | * @command: Byte interpreted by slave |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2576 | * @values: Byte array into which data will be read; big enough to hold |
| 2577 | * the data returned by the slave. SMBus allows at most 32 bytes. |
| 2578 | * |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2579 | * This executes the SMBus "block read" protocol, returning negative errno |
| 2580 | * else the number of data bytes in the slave's response. |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 2581 | * |
| 2582 | * Note that using this function requires that the client's adapter support |
| 2583 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers |
| 2584 | * support this; its emulation through I2C messaging relies on a specific |
| 2585 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. |
| 2586 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2587 | s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command, |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2588 | u8 *values) |
| 2589 | { |
| 2590 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2591 | int status; |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2592 | |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2593 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2594 | I2C_SMBUS_READ, command, |
| 2595 | I2C_SMBUS_BLOCK_DATA, &data); |
| 2596 | if (status) |
| 2597 | return status; |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 2598 | |
| 2599 | memcpy(values, &data.block[1], data.block[0]); |
| 2600 | return data.block[0]; |
| 2601 | } |
| 2602 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
| 2603 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2604 | /** |
| 2605 | * i2c_smbus_write_block_data - SMBus "block write" protocol |
| 2606 | * @client: Handle to slave device |
| 2607 | * @command: Byte interpreted by slave |
| 2608 | * @length: Size of data block; SMBus allows at most 32 bytes |
| 2609 | * @values: Byte array which will be written. |
| 2610 | * |
| 2611 | * This executes the SMBus "block write" protocol, returning negative errno |
| 2612 | * else zero on success. |
| 2613 | */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2614 | s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 2615 | u8 length, const u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | { |
| 2617 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 2620 | length = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | data.block[0] = length; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2622 | memcpy(&data.block[1], values, length); |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2623 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2624 | I2C_SMBUS_WRITE, command, |
| 2625 | I2C_SMBUS_BLOCK_DATA, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2627 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | |
| 2629 | /* Returns the number of read bytes */ |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2630 | s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2631 | u8 length, u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | { |
| 2633 | union i2c_smbus_data data; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2634 | int status; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2635 | |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2636 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 2637 | length = I2C_SMBUS_BLOCK_MAX; |
| 2638 | data.block[0] = length; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2639 | status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2640 | I2C_SMBUS_READ, command, |
| 2641 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 2642 | if (status < 0) |
| 2643 | return status; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 2644 | |
| 2645 | memcpy(values, &data.block[1], data.block[0]); |
| 2646 | return data.block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2648 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | |
Jean Delvare | 0cc43a1 | 2011-01-10 22:11:23 +0100 | [diff] [blame] | 2650 | s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 2651 | u8 length, const u8 *values) |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 2652 | { |
| 2653 | union i2c_smbus_data data; |
| 2654 | |
| 2655 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 2656 | length = I2C_SMBUS_BLOCK_MAX; |
| 2657 | data.block[0] = length; |
| 2658 | memcpy(data.block + 1, values, length); |
| 2659 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2660 | I2C_SMBUS_WRITE, command, |
| 2661 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 2662 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2663 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 2664 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2665 | /* Simulate a SMBus command using the i2c protocol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | No checking of parameters is done! */ |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2667 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, |
| 2668 | unsigned short flags, |
| 2669 | char read_write, u8 command, int size, |
| 2670 | union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | { |
| 2672 | /* So we need to generate a series of msgs. In the case of writing, we |
| 2673 | need to use only one message; when reading, we need two. We initialize |
| 2674 | most things with sane defaults, to keep the code below somewhat |
| 2675 | simpler. */ |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 2676 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
| 2677 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2678 | int num = read_write == I2C_SMBUS_READ ? 2 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | int i; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2680 | u8 partial_pec = 0; |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2681 | int status; |
Shubhrajyoti D | 230da09 | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 2682 | struct i2c_msg msg[2] = { |
| 2683 | { |
| 2684 | .addr = addr, |
| 2685 | .flags = flags, |
| 2686 | .len = 1, |
| 2687 | .buf = msgbuf0, |
| 2688 | }, { |
| 2689 | .addr = addr, |
| 2690 | .flags = flags | I2C_M_RD, |
| 2691 | .len = 0, |
| 2692 | .buf = msgbuf1, |
| 2693 | }, |
| 2694 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | |
| 2696 | msgbuf0[0] = command; |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2697 | switch (size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | case I2C_SMBUS_QUICK: |
| 2699 | msg[0].len = 0; |
| 2700 | /* Special case: The read/write field is used as data */ |
Roel Kluin | f29d2e0 | 2009-02-24 19:19:48 +0100 | [diff] [blame] | 2701 | msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? |
| 2702 | I2C_M_RD : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | num = 1; |
| 2704 | break; |
| 2705 | case I2C_SMBUS_BYTE: |
| 2706 | if (read_write == I2C_SMBUS_READ) { |
| 2707 | /* Special case: only a read! */ |
| 2708 | msg[0].flags = I2C_M_RD | flags; |
| 2709 | num = 1; |
| 2710 | } |
| 2711 | break; |
| 2712 | case I2C_SMBUS_BYTE_DATA: |
| 2713 | if (read_write == I2C_SMBUS_READ) |
| 2714 | msg[1].len = 1; |
| 2715 | else { |
| 2716 | msg[0].len = 2; |
| 2717 | msgbuf0[1] = data->byte; |
| 2718 | } |
| 2719 | break; |
| 2720 | case I2C_SMBUS_WORD_DATA: |
| 2721 | if (read_write == I2C_SMBUS_READ) |
| 2722 | msg[1].len = 2; |
| 2723 | else { |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2724 | msg[0].len = 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 2726 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | break; |
| 2729 | case I2C_SMBUS_PROC_CALL: |
| 2730 | num = 2; /* Special case */ |
| 2731 | read_write = I2C_SMBUS_READ; |
| 2732 | msg[0].len = 3; |
| 2733 | msg[1].len = 2; |
| 2734 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 2735 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | break; |
| 2737 | case I2C_SMBUS_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2739 | msg[1].flags |= I2C_M_RECV_LEN; |
| 2740 | msg[1].len = 1; /* block length will be added by |
| 2741 | the underlying bus driver */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | } else { |
| 2743 | msg[0].len = data->block[0] + 2; |
| 2744 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2745 | dev_err(&adapter->dev, |
| 2746 | "Invalid block write size %d\n", |
| 2747 | data->block[0]); |
| 2748 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 2750 | for (i = 1; i < msg[0].len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | msgbuf0[i] = data->block[i-1]; |
| 2752 | } |
| 2753 | break; |
| 2754 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2755 | num = 2; /* Another special case */ |
| 2756 | read_write = I2C_SMBUS_READ; |
| 2757 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2758 | dev_err(&adapter->dev, |
| 2759 | "Invalid block write size %d\n", |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2760 | data->block[0]); |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2761 | return -EINVAL; |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 2762 | } |
| 2763 | msg[0].len = data->block[0] + 2; |
| 2764 | for (i = 1; i < msg[0].len; i++) |
| 2765 | msgbuf0[i] = data->block[i-1]; |
| 2766 | msg[1].flags |= I2C_M_RECV_LEN; |
| 2767 | msg[1].len = 1; /* block length will be added by |
| 2768 | the underlying bus driver */ |
| 2769 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 2771 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2772 | msg[1].len = data->block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } else { |
| 2774 | msg[0].len = data->block[0] + 1; |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 2775 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2776 | dev_err(&adapter->dev, |
| 2777 | "Invalid block write size %d\n", |
| 2778 | data->block[0]); |
| 2779 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | } |
| 2781 | for (i = 1; i <= data->block[0]; i++) |
| 2782 | msgbuf0[i] = data->block[i]; |
| 2783 | } |
| 2784 | break; |
| 2785 | default: |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2786 | dev_err(&adapter->dev, "Unsupported transaction %d\n", size); |
| 2787 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | } |
| 2789 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2790 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
| 2791 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
| 2792 | if (i) { |
| 2793 | /* Compute PEC if first message is a write */ |
| 2794 | if (!(msg[0].flags & I2C_M_RD)) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2795 | if (num == 1) /* Write only */ |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2796 | i2c_smbus_add_pec(&msg[0]); |
| 2797 | else /* Write followed by read */ |
| 2798 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
| 2799 | } |
| 2800 | /* Ask for PEC if last message is a read */ |
| 2801 | if (msg[num-1].flags & I2C_M_RD) |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 2802 | msg[num-1].len++; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2803 | } |
| 2804 | |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2805 | status = i2c_transfer(adapter, msg, num); |
| 2806 | if (status < 0) |
| 2807 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2809 | /* Check PEC if last message is a read */ |
| 2810 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
David Brownell | 24a5bb7 | 2008-07-14 22:38:23 +0200 | [diff] [blame] | 2811 | status = i2c_smbus_check_pec(partial_pec, &msg[num-1]); |
| 2812 | if (status < 0) |
| 2813 | return status; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 2814 | } |
| 2815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | if (read_write == I2C_SMBUS_READ) |
Farid Hammane | 7225acf | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 2817 | switch (size) { |
| 2818 | case I2C_SMBUS_BYTE: |
| 2819 | data->byte = msgbuf0[0]; |
| 2820 | break; |
| 2821 | case I2C_SMBUS_BYTE_DATA: |
| 2822 | data->byte = msgbuf1[0]; |
| 2823 | break; |
| 2824 | case I2C_SMBUS_WORD_DATA: |
| 2825 | case I2C_SMBUS_PROC_CALL: |
| 2826 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
| 2827 | break; |
| 2828 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 2829 | for (i = 0; i < data->block[0]; i++) |
| 2830 | data->block[i+1] = msgbuf1[i]; |
| 2831 | break; |
| 2832 | case I2C_SMBUS_BLOCK_DATA: |
| 2833 | case I2C_SMBUS_BLOCK_PROC_CALL: |
| 2834 | for (i = 0; i < msgbuf1[0] + 1; i++) |
| 2835 | data->block[i] = msgbuf1[i]; |
| 2836 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | } |
| 2838 | return 0; |
| 2839 | } |
| 2840 | |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2841 | /** |
| 2842 | * i2c_smbus_xfer - execute SMBus protocol operations |
| 2843 | * @adapter: Handle to I2C bus |
| 2844 | * @addr: Address of SMBus slave on that bus |
| 2845 | * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) |
| 2846 | * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE |
| 2847 | * @command: Byte interpreted by slave, for protocols which use such bytes |
| 2848 | * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL |
| 2849 | * @data: Data to be read or written |
| 2850 | * |
| 2851 | * This executes an SMBus protocol operation, and returns a negative |
| 2852 | * errno code else zero on success. |
| 2853 | */ |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2854 | s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags, |
David Brownell | a1cdeda | 2008-07-14 22:38:24 +0200 | [diff] [blame] | 2855 | char read_write, u8 command, int protocol, |
Zhenwen Xu | 09b8ce0 | 2009-03-28 21:34:46 +0100 | [diff] [blame] | 2856 | union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | { |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 2858 | unsigned long orig_jiffies; |
| 2859 | int try; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | s32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
David Howells | 8a32599 | 2014-03-06 13:36:06 +0000 | [diff] [blame] | 2862 | /* If enabled, the following two tracepoints are conditional on |
| 2863 | * read_write and protocol. |
| 2864 | */ |
| 2865 | trace_smbus_write(adapter, addr, flags, read_write, |
| 2866 | command, protocol, data); |
| 2867 | trace_smbus_read(adapter, addr, flags, read_write, |
| 2868 | command, protocol); |
| 2869 | |
Laurent Pinchart | d47726c | 2012-07-24 14:13:59 +0200 | [diff] [blame] | 2870 | flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | |
| 2872 | if (adapter->algo->smbus_xfer) { |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2873 | i2c_lock_adapter(adapter); |
Clifford Wolf | 66b650f | 2009-06-15 18:01:46 +0200 | [diff] [blame] | 2874 | |
| 2875 | /* Retry automatically on arbitration loss */ |
| 2876 | orig_jiffies = jiffies; |
| 2877 | for (res = 0, try = 0; try <= adapter->retries; try++) { |
| 2878 | res = adapter->algo->smbus_xfer(adapter, addr, flags, |
| 2879 | read_write, command, |
| 2880 | protocol, data); |
| 2881 | if (res != -EAGAIN) |
| 2882 | break; |
| 2883 | if (time_after(jiffies, |
| 2884 | orig_jiffies + adapter->timeout)) |
| 2885 | break; |
| 2886 | } |
Jean Delvare | fe61e07 | 2010-08-11 18:20:58 +0200 | [diff] [blame] | 2887 | i2c_unlock_adapter(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | |
Laurent Pinchart | 72fc2c7 | 2012-07-24 14:13:59 +0200 | [diff] [blame] | 2889 | if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) |
David Howells | 8a32599 | 2014-03-06 13:36:06 +0000 | [diff] [blame] | 2890 | goto trace; |
Laurent Pinchart | 72fc2c7 | 2012-07-24 14:13:59 +0200 | [diff] [blame] | 2891 | /* |
| 2892 | * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't |
| 2893 | * implement native support for the SMBus operation. |
| 2894 | */ |
| 2895 | } |
| 2896 | |
David Howells | 8a32599 | 2014-03-06 13:36:06 +0000 | [diff] [blame] | 2897 | res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, |
| 2898 | command, protocol, data); |
| 2899 | |
| 2900 | trace: |
| 2901 | /* If enabled, the reply tracepoint is conditional on read_write. */ |
| 2902 | trace_smbus_reply(adapter, addr, flags, read_write, |
| 2903 | command, protocol, data); |
| 2904 | trace_smbus_result(adapter, addr, flags, read_write, |
| 2905 | command, protocol, res); |
| 2906 | |
| 2907 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | EXPORT_SYMBOL(i2c_smbus_xfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | |
Jean Delvare | d5fd120 | 2015-01-26 20:59:31 +0100 | [diff] [blame] | 2911 | #if IS_ENABLED(CONFIG_I2C_SLAVE) |
Wolfram Sang | 4b1acc4 | 2014-11-18 17:04:53 +0100 | [diff] [blame] | 2912 | int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb) |
| 2913 | { |
| 2914 | int ret; |
| 2915 | |
| 2916 | if (!client || !slave_cb) |
| 2917 | return -EINVAL; |
| 2918 | |
| 2919 | if (!(client->flags & I2C_CLIENT_TEN)) { |
| 2920 | /* Enforce stricter address checking */ |
| 2921 | ret = i2c_check_addr_validity(client->addr); |
| 2922 | if (ret) |
| 2923 | return ret; |
| 2924 | } |
| 2925 | |
| 2926 | if (!client->adapter->algo->reg_slave) |
| 2927 | return -EOPNOTSUPP; |
| 2928 | |
| 2929 | client->slave_cb = slave_cb; |
| 2930 | |
| 2931 | i2c_lock_adapter(client->adapter); |
| 2932 | ret = client->adapter->algo->reg_slave(client); |
| 2933 | i2c_unlock_adapter(client->adapter); |
| 2934 | |
| 2935 | if (ret) |
| 2936 | client->slave_cb = NULL; |
| 2937 | |
| 2938 | return ret; |
| 2939 | } |
| 2940 | EXPORT_SYMBOL_GPL(i2c_slave_register); |
| 2941 | |
| 2942 | int i2c_slave_unregister(struct i2c_client *client) |
| 2943 | { |
| 2944 | int ret; |
| 2945 | |
| 2946 | if (!client->adapter->algo->unreg_slave) |
| 2947 | return -EOPNOTSUPP; |
| 2948 | |
| 2949 | i2c_lock_adapter(client->adapter); |
| 2950 | ret = client->adapter->algo->unreg_slave(client); |
| 2951 | i2c_unlock_adapter(client->adapter); |
| 2952 | |
| 2953 | if (ret == 0) |
| 2954 | client->slave_cb = NULL; |
| 2955 | |
| 2956 | return ret; |
| 2957 | } |
| 2958 | EXPORT_SYMBOL_GPL(i2c_slave_unregister); |
Jean Delvare | d5fd120 | 2015-01-26 20:59:31 +0100 | [diff] [blame] | 2959 | #endif |
Wolfram Sang | 4b1acc4 | 2014-11-18 17:04:53 +0100 | [diff] [blame] | 2960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 2962 | MODULE_DESCRIPTION("I2C-Bus main module"); |
| 2963 | MODULE_LICENSE("GPL"); |