summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWolfram Sang <wsa@the-dreams.de>2017-05-23 10:22:23 -0400
committerWolfram Sang <wsa@the-dreams.de>2017-05-31 15:01:04 -0400
commit53f8f7c5cf145d639ebd6d13cfdf2e3e9764add3 (patch)
tree2b69acd17f0daac9bc508f7135c50cffcb20bc9a
parent5bf4fa7daea6d5257357b613d0bb81c68e2d1af2 (diff)
i2c: break out ACPI support into separate file
Removes some ifdeffery. Also add the new file to the relevant MAINTAINERS section. Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/i2c/Makefile1
-rw-r--r--drivers/i2c/i2c-core-acpi.c653
-rw-r--r--drivers/i2c/i2c-core-base.c648
-rw-r--r--drivers/i2c/i2c-core.h15
5 files changed, 670 insertions, 648 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 053c3bdd1fe5..34b0324d53c5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6273,6 +6273,7 @@ M: Mika Westerberg <mika.westerberg@linux.intel.com>
6273L: linux-i2c@vger.kernel.org 6273L: linux-i2c@vger.kernel.org
6274L: linux-acpi@vger.kernel.org 6274L: linux-acpi@vger.kernel.org
6275S: Maintained 6275S: Maintained
6276F: drivers/i2c/i2c-core-acpi.c
6276 6277
6277I2C-TAOS-EVM DRIVER 6278I2C-TAOS-EVM DRIVER
6278M: Jean Delvare <jdelvare@suse.com> 6279M: Jean Delvare <jdelvare@suse.com>
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index 189e0e6476f0..7bb65a4369e1 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -5,6 +5,7 @@
5obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o 5obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
6obj-$(CONFIG_I2C) += i2c-core.o 6obj-$(CONFIG_I2C) += i2c-core.o
7i2c-core-objs := i2c-core-base.o i2c-core-smbus.o 7i2c-core-objs := i2c-core-base.o i2c-core-smbus.o
8i2c-core-$(CONFIG_ACPI) += i2c-core-acpi.o
8i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o 9i2c-core-$(CONFIG_I2C_SLAVE) += i2c-core-slave.o
9i2c-core-$(CONFIG_OF) += i2c-core-of.o 10i2c-core-$(CONFIG_OF) += i2c-core-of.o
10 11
diff --git a/drivers/i2c/i2c-core-acpi.c b/drivers/i2c/i2c-core-acpi.c
new file mode 100644
index 000000000000..052005579ed6
--- /dev/null
+++ b/drivers/i2c/i2c-core-acpi.c
@@ -0,0 +1,653 @@
1/*
2 * Linux I2C core ACPI support code
3 *
4 * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 */
11
12#include <linux/acpi.h>
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/i2c.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19
20#include "i2c-core.h"
21
22struct i2c_acpi_handler_data {
23 struct acpi_connection_info info;
24 struct i2c_adapter *adapter;
25};
26
27struct gsb_buffer {
28 u8 status;
29 u8 len;
30 union {
31 u16 wdata;
32 u8 bdata;
33 u8 data[0];
34 };
35} __packed;
36
37struct i2c_acpi_lookup {
38 struct i2c_board_info *info;
39 acpi_handle adapter_handle;
40 acpi_handle device_handle;
41 acpi_handle search_handle;
42 int n;
43 int index;
44 u32 speed;
45 u32 min_speed;
46};
47
48static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
49{
50 struct i2c_acpi_lookup *lookup = data;
51 struct i2c_board_info *info = lookup->info;
52 struct acpi_resource_i2c_serialbus *sb;
53 acpi_status status;
54
55 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
56 return 1;
57
58 sb = &ares->data.i2c_serial_bus;
59 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
60 return 1;
61
62 if (lookup->index != -1 && lookup->n++ != lookup->index)
63 return 1;
64
65 status = acpi_get_handle(lookup->device_handle,
66 sb->resource_source.string_ptr,
67 &lookup->adapter_handle);
68 if (!ACPI_SUCCESS(status))
69 return 1;
70
71 info->addr = sb->slave_address;
72 lookup->speed = sb->connection_speed;
73 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
74 info->flags |= I2C_CLIENT_TEN;
75
76 return 1;
77}
78
79static int i2c_acpi_do_lookup(struct acpi_device *adev,
80 struct i2c_acpi_lookup *lookup)
81{
82 struct i2c_board_info *info = lookup->info;
83 struct list_head resource_list;
84 int ret;
85
86 if (acpi_bus_get_status(adev) || !adev->status.present ||
87 acpi_device_enumerated(adev))
88 return -EINVAL;
89
90 memset(info, 0, sizeof(*info));
91 lookup->device_handle = acpi_device_handle(adev);
92
93 /* Look up for I2cSerialBus resource */
94 INIT_LIST_HEAD(&resource_list);
95 ret = acpi_dev_get_resources(adev, &resource_list,
96 i2c_acpi_fill_info, lookup);
97 acpi_dev_free_resource_list(&resource_list);
98
99 if (ret < 0 || !info->addr)
100 return -EINVAL;
101
102 return 0;
103}
104
105static int i2c_acpi_get_info(struct acpi_device *adev,
106 struct i2c_board_info *info,
107 struct i2c_adapter *adapter,
108 acpi_handle *adapter_handle)
109{
110 struct list_head resource_list;
111 struct resource_entry *entry;
112 struct i2c_acpi_lookup lookup;
113 int ret;
114
115 memset(&lookup, 0, sizeof(lookup));
116 lookup.info = info;
117 lookup.index = -1;
118
119 ret = i2c_acpi_do_lookup(adev, &lookup);
120 if (ret)
121 return ret;
122
123 if (adapter) {
124 /* The adapter must match the one in I2cSerialBus() connector */
125 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
126 return -ENODEV;
127 } else {
128 struct acpi_device *adapter_adev;
129
130 /* The adapter must be present */
131 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
132 return -ENODEV;
133 if (acpi_bus_get_status(adapter_adev) ||
134 !adapter_adev->status.present)
135 return -ENODEV;
136 }
137
138 info->fwnode = acpi_fwnode_handle(adev);
139 if (adapter_handle)
140 *adapter_handle = lookup.adapter_handle;
141
142 /* Then fill IRQ number if any */
143 INIT_LIST_HEAD(&resource_list);
144 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
145 if (ret < 0)
146 return -EINVAL;
147
148 resource_list_for_each_entry(entry, &resource_list) {
149 if (resource_type(entry->res) == IORESOURCE_IRQ) {
150 info->irq = entry->res->start;
151 break;
152 }
153 }
154
155 acpi_dev_free_resource_list(&resource_list);
156
157 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
158 sizeof(info->type));
159
160 return 0;
161}
162
163static void i2c_acpi_register_device(struct i2c_adapter *adapter,
164 struct acpi_device *adev,
165 struct i2c_board_info *info)
166{
167 adev->power.flags.ignore_parent = true;
168 acpi_device_set_enumerated(adev);
169
170 if (!i2c_new_device(adapter, info)) {
171 adev->power.flags.ignore_parent = false;
172 dev_err(&adapter->dev,
173 "failed to add I2C device %s from ACPI\n",
174 dev_name(&adev->dev));
175 }
176}
177
178static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
179 void *data, void **return_value)
180{
181 struct i2c_adapter *adapter = data;
182 struct acpi_device *adev;
183 struct i2c_board_info info;
184
185 if (acpi_bus_get_device(handle, &adev))
186 return AE_OK;
187
188 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
189 return AE_OK;
190
191 i2c_acpi_register_device(adapter, adev, &info);
192
193 return AE_OK;
194}
195
196#define I2C_ACPI_MAX_SCAN_DEPTH 32
197
198/**
199 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
200 * @adap: pointer to adapter
201 *
202 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
203 * namespace. When a device is found it will be added to the Linux device
204 * model and bound to the corresponding ACPI handle.
205 */
206void i2c_acpi_register_devices(struct i2c_adapter *adap)
207{
208 acpi_status status;
209
210 if (!has_acpi_companion(&adap->dev))
211 return;
212
213 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
214 I2C_ACPI_MAX_SCAN_DEPTH,
215 i2c_acpi_add_device, NULL,
216 adap, NULL);
217 if (ACPI_FAILURE(status))
218 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
219}
220
221static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
222 void *data, void **return_value)
223{
224 struct i2c_acpi_lookup *lookup = data;
225 struct acpi_device *adev;
226
227 if (acpi_bus_get_device(handle, &adev))
228 return AE_OK;
229
230 if (i2c_acpi_do_lookup(adev, lookup))
231 return AE_OK;
232
233 if (lookup->search_handle != lookup->adapter_handle)
234 return AE_OK;
235
236 if (lookup->speed <= lookup->min_speed)
237 lookup->min_speed = lookup->speed;
238
239 return AE_OK;
240}
241
242/**
243 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
244 * @dev: The device owning the bus
245 *
246 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
247 * devices connected to this bus and use the speed of slowest device.
248 *
249 * Returns the speed in Hz or zero
250 */
251u32 i2c_acpi_find_bus_speed(struct device *dev)
252{
253 struct i2c_acpi_lookup lookup;
254 struct i2c_board_info dummy;
255 acpi_status status;
256
257 if (!has_acpi_companion(dev))
258 return 0;
259
260 memset(&lookup, 0, sizeof(lookup));
261 lookup.search_handle = ACPI_HANDLE(dev);
262 lookup.min_speed = UINT_MAX;
263 lookup.info = &dummy;
264 lookup.index = -1;
265
266 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
267 I2C_ACPI_MAX_SCAN_DEPTH,
268 i2c_acpi_lookup_speed, NULL,
269 &lookup, NULL);
270
271 if (ACPI_FAILURE(status)) {
272 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
273 return 0;
274 }
275
276 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
277}
278EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
279
280static int i2c_acpi_match_adapter(struct device *dev, void *data)
281{
282 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
283
284 if (!adapter)
285 return 0;
286
287 return ACPI_HANDLE(dev) == (acpi_handle)data;
288}
289
290static int i2c_acpi_match_device(struct device *dev, void *data)
291{
292 return ACPI_COMPANION(dev) == data;
293}
294
295static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
296{
297 struct device *dev;
298
299 dev = bus_find_device(&i2c_bus_type, NULL, handle,
300 i2c_acpi_match_adapter);
301 return dev ? i2c_verify_adapter(dev) : NULL;
302}
303
304static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
305{
306 struct device *dev;
307
308 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
309 return dev ? i2c_verify_client(dev) : NULL;
310}
311
312static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
313 void *arg)
314{
315 struct acpi_device *adev = arg;
316 struct i2c_board_info info;
317 acpi_handle adapter_handle;
318 struct i2c_adapter *adapter;
319 struct i2c_client *client;
320
321 switch (value) {
322 case ACPI_RECONFIG_DEVICE_ADD:
323 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
324 break;
325
326 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
327 if (!adapter)
328 break;
329
330 i2c_acpi_register_device(adapter, adev, &info);
331 break;
332 case ACPI_RECONFIG_DEVICE_REMOVE:
333 if (!acpi_device_enumerated(adev))
334 break;
335
336 client = i2c_acpi_find_client_by_adev(adev);
337 if (!client)
338 break;
339
340 i2c_unregister_device(client);
341 put_device(&client->dev);
342 break;
343 }
344
345 return NOTIFY_OK;
346}
347
348struct notifier_block i2c_acpi_notifier = {
349 .notifier_call = i2c_acpi_notify,
350};
351
352/**
353 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
354 * @dev: Device owning the ACPI resources to get the client from
355 * @index: Index of ACPI resource to get
356 * @info: describes the I2C device; note this is modified (addr gets set)
357 * Context: can sleep
358 *
359 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
360 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
361 * resources, in that case this function can be used to create an i2c-client
362 * for other I2cSerialBus resources in the Current Resource Settings table.
363 *
364 * Also see i2c_new_device, which this function calls to create the i2c-client.
365 *
366 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
367 */
368struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
369 struct i2c_board_info *info)
370{
371 struct i2c_acpi_lookup lookup;
372 struct i2c_adapter *adapter;
373 struct acpi_device *adev;
374 LIST_HEAD(resource_list);
375 int ret;
376
377 adev = ACPI_COMPANION(dev);
378 if (!adev)
379 return NULL;
380
381 memset(&lookup, 0, sizeof(lookup));
382 lookup.info = info;
383 lookup.device_handle = acpi_device_handle(adev);
384 lookup.index = index;
385
386 ret = acpi_dev_get_resources(adev, &resource_list,
387 i2c_acpi_fill_info, &lookup);
388 acpi_dev_free_resource_list(&resource_list);
389
390 if (ret < 0 || !info->addr)
391 return NULL;
392
393 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
394 if (!adapter)
395 return NULL;
396
397 return i2c_new_device(adapter, info);
398}
399EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
400
401#ifdef CONFIG_ACPI_I2C_OPREGION
402static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
403 u8 cmd, u8 *data, u8 data_len)
404{
405
406 struct i2c_msg msgs[2];
407 int ret;
408 u8 *buffer;
409
410 buffer = kzalloc(data_len, GFP_KERNEL);
411 if (!buffer)
412 return AE_NO_MEMORY;
413
414 msgs[0].addr = client->addr;
415 msgs[0].flags = client->flags;
416 msgs[0].len = 1;
417 msgs[0].buf = &cmd;
418
419 msgs[1].addr = client->addr;
420 msgs[1].flags = client->flags | I2C_M_RD;
421 msgs[1].len = data_len;
422 msgs[1].buf = buffer;
423
424 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
425 if (ret < 0)
426 dev_err(&client->adapter->dev, "i2c read failed\n");
427 else
428 memcpy(data, buffer, data_len);
429
430 kfree(buffer);
431 return ret;
432}
433
434static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
435 u8 cmd, u8 *data, u8 data_len)
436{
437
438 struct i2c_msg msgs[1];
439 u8 *buffer;
440 int ret = AE_OK;
441
442 buffer = kzalloc(data_len + 1, GFP_KERNEL);
443 if (!buffer)
444 return AE_NO_MEMORY;
445
446 buffer[0] = cmd;
447 memcpy(buffer + 1, data, data_len);
448
449 msgs[0].addr = client->addr;
450 msgs[0].flags = client->flags;
451 msgs[0].len = data_len + 1;
452 msgs[0].buf = buffer;
453
454 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
455 if (ret < 0)
456 dev_err(&client->adapter->dev, "i2c write failed\n");
457
458 kfree(buffer);
459 return ret;
460}
461
462static acpi_status
463i2c_acpi_space_handler(u32 function, acpi_physical_address command,
464 u32 bits, u64 *value64,
465 void *handler_context, void *region_context)
466{
467 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
468 struct i2c_acpi_handler_data *data = handler_context;
469 struct acpi_connection_info *info = &data->info;
470 struct acpi_resource_i2c_serialbus *sb;
471 struct i2c_adapter *adapter = data->adapter;
472 struct i2c_client *client;
473 struct acpi_resource *ares;
474 u32 accessor_type = function >> 16;
475 u8 action = function & ACPI_IO_MASK;
476 acpi_status ret;
477 int status;
478
479 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
480 if (ACPI_FAILURE(ret))
481 return ret;
482
483 client = kzalloc(sizeof(*client), GFP_KERNEL);
484 if (!client) {
485 ret = AE_NO_MEMORY;
486 goto err;
487 }
488
489 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
490 ret = AE_BAD_PARAMETER;
491 goto err;
492 }
493
494 sb = &ares->data.i2c_serial_bus;
495 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
496 ret = AE_BAD_PARAMETER;
497 goto err;
498 }
499
500 client->adapter = adapter;
501 client->addr = sb->slave_address;
502
503 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
504 client->flags |= I2C_CLIENT_TEN;
505
506 switch (accessor_type) {
507 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
508 if (action == ACPI_READ) {
509 status = i2c_smbus_read_byte(client);
510 if (status >= 0) {
511 gsb->bdata = status;
512 status = 0;
513 }
514 } else {
515 status = i2c_smbus_write_byte(client, gsb->bdata);
516 }
517 break;
518
519 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
520 if (action == ACPI_READ) {
521 status = i2c_smbus_read_byte_data(client, command);
522 if (status >= 0) {
523 gsb->bdata = status;
524 status = 0;
525 }
526 } else {
527 status = i2c_smbus_write_byte_data(client, command,
528 gsb->bdata);
529 }
530 break;
531
532 case ACPI_GSB_ACCESS_ATTRIB_WORD:
533 if (action == ACPI_READ) {
534 status = i2c_smbus_read_word_data(client, command);
535 if (status >= 0) {
536 gsb->wdata = status;
537 status = 0;
538 }
539 } else {
540 status = i2c_smbus_write_word_data(client, command,
541 gsb->wdata);
542 }
543 break;
544
545 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
546 if (action == ACPI_READ) {
547 status = i2c_smbus_read_block_data(client, command,
548 gsb->data);
549 if (status >= 0) {
550 gsb->len = status;
551 status = 0;
552 }
553 } else {
554 status = i2c_smbus_write_block_data(client, command,
555 gsb->len, gsb->data);
556 }
557 break;
558
559 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
560 if (action == ACPI_READ) {
561 status = acpi_gsb_i2c_read_bytes(client, command,
562 gsb->data, info->access_length);
563 if (status > 0)
564 status = 0;
565 } else {
566 status = acpi_gsb_i2c_write_bytes(client, command,
567 gsb->data, info->access_length);
568 }
569 break;
570
571 default:
572 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
573 accessor_type, client->addr);
574 ret = AE_BAD_PARAMETER;
575 goto err;
576 }
577
578 gsb->status = status;
579
580 err:
581 kfree(client);
582 ACPI_FREE(ares);
583 return ret;
584}
585
586
587int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
588{
589 acpi_handle handle;
590 struct i2c_acpi_handler_data *data;
591 acpi_status status;
592
593 if (!adapter->dev.parent)
594 return -ENODEV;
595
596 handle = ACPI_HANDLE(adapter->dev.parent);
597
598 if (!handle)
599 return -ENODEV;
600
601 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
602 GFP_KERNEL);
603 if (!data)
604 return -ENOMEM;
605
606 data->adapter = adapter;
607 status = acpi_bus_attach_private_data(handle, (void *)data);
608 if (ACPI_FAILURE(status)) {
609 kfree(data);
610 return -ENOMEM;
611 }
612
613 status = acpi_install_address_space_handler(handle,
614 ACPI_ADR_SPACE_GSBUS,
615 &i2c_acpi_space_handler,
616 NULL,
617 data);
618 if (ACPI_FAILURE(status)) {
619 dev_err(&adapter->dev, "Error installing i2c space handler\n");
620 acpi_bus_detach_private_data(handle);
621 kfree(data);
622 return -ENOMEM;
623 }
624
625 acpi_walk_dep_device_list(handle);
626 return 0;
627}
628
629void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
630{
631 acpi_handle handle;
632 struct i2c_acpi_handler_data *data;
633 acpi_status status;
634
635 if (!adapter->dev.parent)
636 return;
637
638 handle = ACPI_HANDLE(adapter->dev.parent);
639
640 if (!handle)
641 return;
642
643 acpi_remove_address_space_handler(handle,
644 ACPI_ADR_SPACE_GSBUS,
645 &i2c_acpi_space_handler);
646
647 status = acpi_bus_get_private_data(handle, (void **)&data);
648 if (ACPI_SUCCESS(status))
649 kfree(data);
650
651 acpi_bus_detach_private_data(handle);
652}
653#endif /* CONFIG_ACPI_I2C_OPREGION */
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 461451da1065..ac7b95e4cda7 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -16,8 +16,6 @@
16/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. 16/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
17 Mux support by Rodolfo Giometti <giometti@enneenne.com> and 17 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
18 Michael Lawnick <michael.lawnick.ext@nsn.com> 18 Michael Lawnick <michael.lawnick.ext@nsn.com>
19 I2C ACPI code Copyright (C) 2014 Intel Corp
20 Author: Lan Tianyu <tianyu.lan@intel.com>
21 */ 19 */
22 20
23#define pr_fmt(fmt) "i2c-core: " fmt 21#define pr_fmt(fmt) "i2c-core: " fmt
@@ -83,652 +81,6 @@ void i2c_transfer_trace_unreg(void)
83 static_key_slow_dec(&i2c_trace_msg); 81 static_key_slow_dec(&i2c_trace_msg);
84} 82}
85 83
86#if defined(CONFIG_ACPI)
87struct i2c_acpi_handler_data {
88 struct acpi_connection_info info;
89 struct i2c_adapter *adapter;
90};
91
92struct gsb_buffer {
93 u8 status;
94 u8 len;
95 union {
96 u16 wdata;
97 u8 bdata;
98 u8 data[0];
99 };
100} __packed;
101
102struct i2c_acpi_lookup {
103 struct i2c_board_info *info;
104 acpi_handle adapter_handle;
105 acpi_handle device_handle;
106 acpi_handle search_handle;
107 int n;
108 int index;
109 u32 speed;
110 u32 min_speed;
111};
112
113static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
114{
115 struct i2c_acpi_lookup *lookup = data;
116 struct i2c_board_info *info = lookup->info;
117 struct acpi_resource_i2c_serialbus *sb;
118 acpi_status status;
119
120 if (info->addr || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
121 return 1;
122
123 sb = &ares->data.i2c_serial_bus;
124 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
125 return 1;
126
127 if (lookup->index != -1 && lookup->n++ != lookup->index)
128 return 1;
129
130 status = acpi_get_handle(lookup->device_handle,
131 sb->resource_source.string_ptr,
132 &lookup->adapter_handle);
133 if (!ACPI_SUCCESS(status))
134 return 1;
135
136 info->addr = sb->slave_address;
137 lookup->speed = sb->connection_speed;
138 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
139 info->flags |= I2C_CLIENT_TEN;
140
141 return 1;
142}
143
144static int i2c_acpi_do_lookup(struct acpi_device *adev,
145 struct i2c_acpi_lookup *lookup)
146{
147 struct i2c_board_info *info = lookup->info;
148 struct list_head resource_list;
149 int ret;
150
151 if (acpi_bus_get_status(adev) || !adev->status.present ||
152 acpi_device_enumerated(adev))
153 return -EINVAL;
154
155 memset(info, 0, sizeof(*info));
156 lookup->device_handle = acpi_device_handle(adev);
157
158 /* Look up for I2cSerialBus resource */
159 INIT_LIST_HEAD(&resource_list);
160 ret = acpi_dev_get_resources(adev, &resource_list,
161 i2c_acpi_fill_info, lookup);
162 acpi_dev_free_resource_list(&resource_list);
163
164 if (ret < 0 || !info->addr)
165 return -EINVAL;
166
167 return 0;
168}
169
170static int i2c_acpi_get_info(struct acpi_device *adev,
171 struct i2c_board_info *info,
172 struct i2c_adapter *adapter,
173 acpi_handle *adapter_handle)
174{
175 struct list_head resource_list;
176 struct resource_entry *entry;
177 struct i2c_acpi_lookup lookup;
178 int ret;
179
180 memset(&lookup, 0, sizeof(lookup));
181 lookup.info = info;
182 lookup.index = -1;
183
184 ret = i2c_acpi_do_lookup(adev, &lookup);
185 if (ret)
186 return ret;
187
188 if (adapter) {
189 /* The adapter must match the one in I2cSerialBus() connector */
190 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
191 return -ENODEV;
192 } else {
193 struct acpi_device *adapter_adev;
194
195 /* The adapter must be present */
196 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
197 return -ENODEV;
198 if (acpi_bus_get_status(adapter_adev) ||
199 !adapter_adev->status.present)
200 return -ENODEV;
201 }
202
203 info->fwnode = acpi_fwnode_handle(adev);
204 if (adapter_handle)
205 *adapter_handle = lookup.adapter_handle;
206
207 /* Then fill IRQ number if any */
208 INIT_LIST_HEAD(&resource_list);
209 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
210 if (ret < 0)
211 return -EINVAL;
212
213 resource_list_for_each_entry(entry, &resource_list) {
214 if (resource_type(entry->res) == IORESOURCE_IRQ) {
215 info->irq = entry->res->start;
216 break;
217 }
218 }
219
220 acpi_dev_free_resource_list(&resource_list);
221
222 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
223 sizeof(info->type));
224
225 return 0;
226}
227
228static void i2c_acpi_register_device(struct i2c_adapter *adapter,
229 struct acpi_device *adev,
230 struct i2c_board_info *info)
231{
232 adev->power.flags.ignore_parent = true;
233 acpi_device_set_enumerated(adev);
234
235 if (!i2c_new_device(adapter, info)) {
236 adev->power.flags.ignore_parent = false;
237 dev_err(&adapter->dev,
238 "failed to add I2C device %s from ACPI\n",
239 dev_name(&adev->dev));
240 }
241}
242
243static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
244 void *data, void **return_value)
245{
246 struct i2c_adapter *adapter = data;
247 struct acpi_device *adev;
248 struct i2c_board_info info;
249
250 if (acpi_bus_get_device(handle, &adev))
251 return AE_OK;
252
253 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
254 return AE_OK;
255
256 i2c_acpi_register_device(adapter, adev, &info);
257
258 return AE_OK;
259}
260
261#define I2C_ACPI_MAX_SCAN_DEPTH 32
262
263/**
264 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
265 * @adap: pointer to adapter
266 *
267 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
268 * namespace. When a device is found it will be added to the Linux device
269 * model and bound to the corresponding ACPI handle.
270 */
271static void i2c_acpi_register_devices(struct i2c_adapter *adap)
272{
273 acpi_status status;
274
275 if (!has_acpi_companion(&adap->dev))
276 return;
277
278 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
279 I2C_ACPI_MAX_SCAN_DEPTH,
280 i2c_acpi_add_device, NULL,
281 adap, NULL);
282 if (ACPI_FAILURE(status))
283 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
284}
285
286static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
287 void *data, void **return_value)
288{
289 struct i2c_acpi_lookup *lookup = data;
290 struct acpi_device *adev;
291
292 if (acpi_bus_get_device(handle, &adev))
293 return AE_OK;
294
295 if (i2c_acpi_do_lookup(adev, lookup))
296 return AE_OK;
297
298 if (lookup->search_handle != lookup->adapter_handle)
299 return AE_OK;
300
301 if (lookup->speed <= lookup->min_speed)
302 lookup->min_speed = lookup->speed;
303
304 return AE_OK;
305}
306
307/**
308 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
309 * @dev: The device owning the bus
310 *
311 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
312 * devices connected to this bus and use the speed of slowest device.
313 *
314 * Returns the speed in Hz or zero
315 */
316u32 i2c_acpi_find_bus_speed(struct device *dev)
317{
318 struct i2c_acpi_lookup lookup;
319 struct i2c_board_info dummy;
320 acpi_status status;
321
322 if (!has_acpi_companion(dev))
323 return 0;
324
325 memset(&lookup, 0, sizeof(lookup));
326 lookup.search_handle = ACPI_HANDLE(dev);
327 lookup.min_speed = UINT_MAX;
328 lookup.info = &dummy;
329 lookup.index = -1;
330
331 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
332 I2C_ACPI_MAX_SCAN_DEPTH,
333 i2c_acpi_lookup_speed, NULL,
334 &lookup, NULL);
335
336 if (ACPI_FAILURE(status)) {
337 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
338 return 0;
339 }
340
341 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
342}
343EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
344
345static int i2c_acpi_match_adapter(struct device *dev, void *data)
346{
347 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
348
349 if (!adapter)
350 return 0;
351
352 return ACPI_HANDLE(dev) == (acpi_handle)data;
353}
354
355static int i2c_acpi_match_device(struct device *dev, void *data)
356{
357 return ACPI_COMPANION(dev) == data;
358}
359
360static struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
361{
362 struct device *dev;
363
364 dev = bus_find_device(&i2c_bus_type, NULL, handle,
365 i2c_acpi_match_adapter);
366 return dev ? i2c_verify_adapter(dev) : NULL;
367}
368
369static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
370{
371 struct device *dev;
372
373 dev = bus_find_device(&i2c_bus_type, NULL, adev, i2c_acpi_match_device);
374 return dev ? i2c_verify_client(dev) : NULL;
375}
376
377static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
378 void *arg)
379{
380 struct acpi_device *adev = arg;
381 struct i2c_board_info info;
382 acpi_handle adapter_handle;
383 struct i2c_adapter *adapter;
384 struct i2c_client *client;
385
386 switch (value) {
387 case ACPI_RECONFIG_DEVICE_ADD:
388 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
389 break;
390
391 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
392 if (!adapter)
393 break;
394
395 i2c_acpi_register_device(adapter, adev, &info);
396 break;
397 case ACPI_RECONFIG_DEVICE_REMOVE:
398 if (!acpi_device_enumerated(adev))
399 break;
400
401 client = i2c_acpi_find_client_by_adev(adev);
402 if (!client)
403 break;
404
405 i2c_unregister_device(client);
406 put_device(&client->dev);
407 break;
408 }
409
410 return NOTIFY_OK;
411}
412
413static struct notifier_block i2c_acpi_notifier = {
414 .notifier_call = i2c_acpi_notify,
415};
416
417/**
418 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
419 * @dev: Device owning the ACPI resources to get the client from
420 * @index: Index of ACPI resource to get
421 * @info: describes the I2C device; note this is modified (addr gets set)
422 * Context: can sleep
423 *
424 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
425 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
426 * resources, in that case this function can be used to create an i2c-client
427 * for other I2cSerialBus resources in the Current Resource Settings table.
428 *
429 * Also see i2c_new_device, which this function calls to create the i2c-client.
430 *
431 * Returns a pointer to the new i2c-client, or NULL if the adapter is not found.
432 */
433struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
434 struct i2c_board_info *info)
435{
436 struct i2c_acpi_lookup lookup;
437 struct i2c_adapter *adapter;
438 struct acpi_device *adev;
439 LIST_HEAD(resource_list);
440 int ret;
441
442 adev = ACPI_COMPANION(dev);
443 if (!adev)
444 return NULL;
445
446 memset(&lookup, 0, sizeof(lookup));
447 lookup.info = info;
448 lookup.device_handle = acpi_device_handle(adev);
449 lookup.index = index;
450
451 ret = acpi_dev_get_resources(adev, &resource_list,
452 i2c_acpi_fill_info, &lookup);
453 acpi_dev_free_resource_list(&resource_list);
454
455 if (ret < 0 || !info->addr)
456 return NULL;
457
458 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
459 if (!adapter)
460 return NULL;
461
462 return i2c_new_device(adapter, info);
463}
464EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
465#else /* CONFIG_ACPI */
466static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
467extern struct notifier_block i2c_acpi_notifier;
468#endif /* CONFIG_ACPI */
469
470#ifdef CONFIG_ACPI_I2C_OPREGION
471static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
472 u8 cmd, u8 *data, u8 data_len)
473{
474
475 struct i2c_msg msgs[2];
476 int ret;
477 u8 *buffer;
478
479 buffer = kzalloc(data_len, GFP_KERNEL);
480 if (!buffer)
481 return AE_NO_MEMORY;
482
483 msgs[0].addr = client->addr;
484 msgs[0].flags = client->flags;
485 msgs[0].len = 1;
486 msgs[0].buf = &cmd;
487
488 msgs[1].addr = client->addr;
489 msgs[1].flags = client->flags | I2C_M_RD;
490 msgs[1].len = data_len;
491 msgs[1].buf = buffer;
492
493 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
494 if (ret < 0)
495 dev_err(&client->adapter->dev, "i2c read failed\n");
496 else
497 memcpy(data, buffer, data_len);
498
499 kfree(buffer);
500 return ret;
501}
502
503static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
504 u8 cmd, u8 *data, u8 data_len)
505{
506
507 struct i2c_msg msgs[1];
508 u8 *buffer;
509 int ret = AE_OK;
510
511 buffer = kzalloc(data_len + 1, GFP_KERNEL);
512 if (!buffer)
513 return AE_NO_MEMORY;
514
515 buffer[0] = cmd;
516 memcpy(buffer + 1, data, data_len);
517
518 msgs[0].addr = client->addr;
519 msgs[0].flags = client->flags;
520 msgs[0].len = data_len + 1;
521 msgs[0].buf = buffer;
522
523 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
524 if (ret < 0)
525 dev_err(&client->adapter->dev, "i2c write failed\n");
526
527 kfree(buffer);
528 return ret;
529}
530
531static acpi_status
532i2c_acpi_space_handler(u32 function, acpi_physical_address command,
533 u32 bits, u64 *value64,
534 void *handler_context, void *region_context)
535{
536 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
537 struct i2c_acpi_handler_data *data = handler_context;
538 struct acpi_connection_info *info = &data->info;
539 struct acpi_resource_i2c_serialbus *sb;
540 struct i2c_adapter *adapter = data->adapter;
541 struct i2c_client *client;
542 struct acpi_resource *ares;
543 u32 accessor_type = function >> 16;
544 u8 action = function & ACPI_IO_MASK;
545 acpi_status ret;
546 int status;
547
548 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
549 if (ACPI_FAILURE(ret))
550 return ret;
551
552 client = kzalloc(sizeof(*client), GFP_KERNEL);
553 if (!client) {
554 ret = AE_NO_MEMORY;
555 goto err;
556 }
557
558 if (!value64 || ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) {
559 ret = AE_BAD_PARAMETER;
560 goto err;
561 }
562
563 sb = &ares->data.i2c_serial_bus;
564 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
565 ret = AE_BAD_PARAMETER;
566 goto err;
567 }
568
569 client->adapter = adapter;
570 client->addr = sb->slave_address;
571
572 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
573 client->flags |= I2C_CLIENT_TEN;
574
575 switch (accessor_type) {
576 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
577 if (action == ACPI_READ) {
578 status = i2c_smbus_read_byte(client);
579 if (status >= 0) {
580 gsb->bdata = status;
581 status = 0;
582 }
583 } else {
584 status = i2c_smbus_write_byte(client, gsb->bdata);
585 }
586 break;
587
588 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
589 if (action == ACPI_READ) {
590 status = i2c_smbus_read_byte_data(client, command);
591 if (status >= 0) {
592 gsb->bdata = status;
593 status = 0;
594 }
595 } else {
596 status = i2c_smbus_write_byte_data(client, command,
597 gsb->bdata);
598 }
599 break;
600
601 case ACPI_GSB_ACCESS_ATTRIB_WORD:
602 if (action == ACPI_READ) {
603 status = i2c_smbus_read_word_data(client, command);
604 if (status >= 0) {
605 gsb->wdata = status;
606 status = 0;
607 }
608 } else {
609 status = i2c_smbus_write_word_data(client, command,
610 gsb->wdata);
611 }
612 break;
613
614 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
615 if (action == ACPI_READ) {
616 status = i2c_smbus_read_block_data(client, command,
617 gsb->data);
618 if (status >= 0) {
619 gsb->len = status;
620 status = 0;
621 }
622 } else {
623 status = i2c_smbus_write_block_data(client, command,
624 gsb->len, gsb->data);
625 }
626 break;
627
628 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
629 if (action == ACPI_READ) {
630 status = acpi_gsb_i2c_read_bytes(client, command,
631 gsb->data, info->access_length);
632 if (status > 0)
633 status = 0;
634 } else {
635 status = acpi_gsb_i2c_write_bytes(client, command,
636 gsb->data, info->access_length);
637 }
638 break;
639
640 default:
641 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
642 accessor_type, client->addr);
643 ret = AE_BAD_PARAMETER;
644 goto err;
645 }
646
647 gsb->status = status;
648
649 err:
650 kfree(client);
651 ACPI_FREE(ares);
652 return ret;
653}
654
655
656static int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
657{
658 acpi_handle handle;
659 struct i2c_acpi_handler_data *data;
660 acpi_status status;
661
662 if (!adapter->dev.parent)
663 return -ENODEV;
664
665 handle = ACPI_HANDLE(adapter->dev.parent);
666
667 if (!handle)
668 return -ENODEV;
669
670 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
671 GFP_KERNEL);
672 if (!data)
673 return -ENOMEM;
674
675 data->adapter = adapter;
676 status = acpi_bus_attach_private_data(handle, (void *)data);
677 if (ACPI_FAILURE(status)) {
678 kfree(data);
679 return -ENOMEM;
680 }
681
682 status = acpi_install_address_space_handler(handle,
683 ACPI_ADR_SPACE_GSBUS,
684 &i2c_acpi_space_handler,
685 NULL,
686 data);
687 if (ACPI_FAILURE(status)) {
688 dev_err(&adapter->dev, "Error installing i2c space handler\n");
689 acpi_bus_detach_private_data(handle);
690 kfree(data);
691 return -ENOMEM;
692 }
693
694 acpi_walk_dep_device_list(handle);
695 return 0;
696}
697
698static void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
699{
700 acpi_handle handle;
701 struct i2c_acpi_handler_data *data;
702 acpi_status status;
703
704 if (!adapter->dev.parent)
705 return;
706
707 handle = ACPI_HANDLE(adapter->dev.parent);
708
709 if (!handle)
710 return;
711
712 acpi_remove_address_space_handler(handle,
713 ACPI_ADR_SPACE_GSBUS,
714 &i2c_acpi_space_handler);
715
716 status = acpi_bus_get_private_data(handle, (void **)&data);
717 if (ACPI_SUCCESS(status))
718 kfree(data);
719
720 acpi_bus_detach_private_data(handle);
721}
722#else /* CONFIG_ACPI_I2C_OPREGION */
723static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
724{ }
725
726static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
727{ return 0; }
728#endif /* CONFIG_ACPI_I2C_OPREGION */
729
730/* ------------------------------------------------------------------------- */
731
732const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 84const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
733 const struct i2c_client *client) 85 const struct i2c_client *client)
734{ 86{
diff --git a/drivers/i2c/i2c-core.h b/drivers/i2c/i2c-core.h
index 22151c88e885..3b63f5e5b89c 100644
--- a/drivers/i2c/i2c-core.h
+++ b/drivers/i2c/i2c-core.h
@@ -30,6 +30,21 @@ extern int __i2c_first_dynamic_bus_num;
30int i2c_check_addr_validity(unsigned addr, unsigned short flags); 30int i2c_check_addr_validity(unsigned addr, unsigned short flags);
31int i2c_check_7bit_addr_validity_strict(unsigned short addr); 31int i2c_check_7bit_addr_validity_strict(unsigned short addr);
32 32
33#ifdef CONFIG_ACPI
34void i2c_acpi_register_devices(struct i2c_adapter *adap);
35#else /* CONFIG_ACPI */
36static inline void i2c_acpi_register_devices(struct i2c_adapter *adap) { }
37#endif /* CONFIG_ACPI */
38extern struct notifier_block i2c_acpi_notifier;
39
40#ifdef CONFIG_ACPI_I2C_OPREGION
41int i2c_acpi_install_space_handler(struct i2c_adapter *adapter);
42void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter);
43#else /* CONFIG_ACPI_I2C_OPREGION */
44static inline int i2c_acpi_install_space_handler(struct i2c_adapter *adapter) { return 0; }
45static inline void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter) { }
46#endif /* CONFIG_ACPI_I2C_OPREGION */
47
33#ifdef CONFIG_OF 48#ifdef CONFIG_OF
34void of_i2c_register_devices(struct i2c_adapter *adap); 49void of_i2c_register_devices(struct i2c_adapter *adap);
35#else 50#else