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