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