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