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