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