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