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