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