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