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