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