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