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