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