spi: sprd: adi: Add a reset reason for watchdog mode
[linux/fpc-iii.git] / drivers / i2c / i2c-core-acpi.c
blob4dbbc9a35f6561fb918dbd663b5bd2e4cd64eee2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux I2C core ACPI support code
5 * Copyright (C) 2014 Intel Corp, Author: Lan Tianyu <tianyu.lan@intel.com>
6 */
8 #include <linux/acpi.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include "i2c-core.h"
18 struct i2c_acpi_handler_data {
19 struct acpi_connection_info info;
20 struct i2c_adapter *adapter;
23 struct gsb_buffer {
24 u8 status;
25 u8 len;
26 union {
27 u16 wdata;
28 u8 bdata;
29 u8 data[0];
31 } __packed;
33 struct i2c_acpi_lookup {
34 struct i2c_board_info *info;
35 acpi_handle adapter_handle;
36 acpi_handle device_handle;
37 acpi_handle search_handle;
38 int n;
39 int index;
40 u32 speed;
41 u32 min_speed;
44 /**
45 * i2c_acpi_get_i2c_resource - Gets I2cSerialBus resource if type matches
46 * @ares: ACPI resource
47 * @i2c: Pointer to I2cSerialBus resource will be returned here
49 * Checks if the given ACPI resource is of type I2cSerialBus.
50 * In this case, returns a pointer to it to the caller.
52 * Returns true if resource type is of I2cSerialBus, otherwise false.
54 bool i2c_acpi_get_i2c_resource(struct acpi_resource *ares,
55 struct acpi_resource_i2c_serialbus **i2c)
57 struct acpi_resource_i2c_serialbus *sb;
59 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
60 return false;
62 sb = &ares->data.i2c_serial_bus;
63 if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
64 return false;
66 *i2c = sb;
67 return true;
69 EXPORT_SYMBOL_GPL(i2c_acpi_get_i2c_resource);
71 static int i2c_acpi_fill_info(struct acpi_resource *ares, void *data)
73 struct i2c_acpi_lookup *lookup = data;
74 struct i2c_board_info *info = lookup->info;
75 struct acpi_resource_i2c_serialbus *sb;
76 acpi_status status;
78 if (info->addr || !i2c_acpi_get_i2c_resource(ares, &sb))
79 return 1;
81 if (lookup->index != -1 && lookup->n++ != lookup->index)
82 return 1;
84 status = acpi_get_handle(lookup->device_handle,
85 sb->resource_source.string_ptr,
86 &lookup->adapter_handle);
87 if (ACPI_FAILURE(status))
88 return 1;
90 info->addr = sb->slave_address;
91 lookup->speed = sb->connection_speed;
92 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
93 info->flags |= I2C_CLIENT_TEN;
95 return 1;
98 static const struct acpi_device_id i2c_acpi_ignored_device_ids[] = {
100 * ACPI video acpi_devices, which are handled by the acpi-video driver
101 * sometimes contain a SERIAL_TYPE_I2C ACPI resource, ignore these.
103 { ACPI_VIDEO_HID, 0 },
107 static int i2c_acpi_do_lookup(struct acpi_device *adev,
108 struct i2c_acpi_lookup *lookup)
110 struct i2c_board_info *info = lookup->info;
111 struct list_head resource_list;
112 int ret;
114 if (acpi_bus_get_status(adev) || !adev->status.present)
115 return -EINVAL;
117 if (acpi_match_device_ids(adev, i2c_acpi_ignored_device_ids) == 0)
118 return -ENODEV;
120 memset(info, 0, sizeof(*info));
121 lookup->device_handle = acpi_device_handle(adev);
123 /* Look up for I2cSerialBus resource */
124 INIT_LIST_HEAD(&resource_list);
125 ret = acpi_dev_get_resources(adev, &resource_list,
126 i2c_acpi_fill_info, lookup);
127 acpi_dev_free_resource_list(&resource_list);
129 if (ret < 0 || !info->addr)
130 return -EINVAL;
132 return 0;
135 static int i2c_acpi_add_resource(struct acpi_resource *ares, void *data)
137 int *irq = data;
138 struct resource r;
140 if (*irq <= 0 && acpi_dev_resource_interrupt(ares, 0, &r))
141 *irq = i2c_dev_irq_from_resources(&r, 1);
143 return 1; /* No need to add resource to the list */
147 * i2c_acpi_get_irq - get device IRQ number from ACPI
148 * @client: Pointer to the I2C client device
150 * Find the IRQ number used by a specific client device.
152 * Return: The IRQ number or an error code.
154 int i2c_acpi_get_irq(struct i2c_client *client)
156 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
157 struct list_head resource_list;
158 int irq = -ENOENT;
159 int ret;
161 INIT_LIST_HEAD(&resource_list);
163 ret = acpi_dev_get_resources(adev, &resource_list,
164 i2c_acpi_add_resource, &irq);
165 if (ret < 0)
166 return ret;
168 acpi_dev_free_resource_list(&resource_list);
170 if (irq == -ENOENT)
171 irq = acpi_dev_gpio_irq_get(adev, 0);
173 return irq;
176 static int i2c_acpi_get_info(struct acpi_device *adev,
177 struct i2c_board_info *info,
178 struct i2c_adapter *adapter,
179 acpi_handle *adapter_handle)
181 struct i2c_acpi_lookup lookup;
182 int ret;
184 memset(&lookup, 0, sizeof(lookup));
185 lookup.info = info;
186 lookup.index = -1;
188 if (acpi_device_enumerated(adev))
189 return -EINVAL;
191 ret = i2c_acpi_do_lookup(adev, &lookup);
192 if (ret)
193 return ret;
195 if (adapter) {
196 /* The adapter must match the one in I2cSerialBus() connector */
197 if (ACPI_HANDLE(&adapter->dev) != lookup.adapter_handle)
198 return -ENODEV;
199 } else {
200 struct acpi_device *adapter_adev;
202 /* The adapter must be present */
203 if (acpi_bus_get_device(lookup.adapter_handle, &adapter_adev))
204 return -ENODEV;
205 if (acpi_bus_get_status(adapter_adev) ||
206 !adapter_adev->status.present)
207 return -ENODEV;
210 info->fwnode = acpi_fwnode_handle(adev);
211 if (adapter_handle)
212 *adapter_handle = lookup.adapter_handle;
214 acpi_set_modalias(adev, dev_name(&adev->dev), info->type,
215 sizeof(info->type));
217 return 0;
220 static void i2c_acpi_register_device(struct i2c_adapter *adapter,
221 struct acpi_device *adev,
222 struct i2c_board_info *info)
224 adev->power.flags.ignore_parent = true;
225 acpi_device_set_enumerated(adev);
227 if (!i2c_new_device(adapter, info)) {
228 adev->power.flags.ignore_parent = false;
229 dev_err(&adapter->dev,
230 "failed to add I2C device %s from ACPI\n",
231 dev_name(&adev->dev));
235 static acpi_status i2c_acpi_add_device(acpi_handle handle, u32 level,
236 void *data, void **return_value)
238 struct i2c_adapter *adapter = data;
239 struct acpi_device *adev;
240 struct i2c_board_info info;
242 if (acpi_bus_get_device(handle, &adev))
243 return AE_OK;
245 if (i2c_acpi_get_info(adev, &info, adapter, NULL))
246 return AE_OK;
248 i2c_acpi_register_device(adapter, adev, &info);
250 return AE_OK;
253 #define I2C_ACPI_MAX_SCAN_DEPTH 32
256 * i2c_acpi_register_devices - enumerate I2C slave devices behind adapter
257 * @adap: pointer to adapter
259 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
260 * namespace. When a device is found it will be added to the Linux device
261 * model and bound to the corresponding ACPI handle.
263 void i2c_acpi_register_devices(struct i2c_adapter *adap)
265 acpi_status status;
267 if (!has_acpi_companion(&adap->dev))
268 return;
270 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
271 I2C_ACPI_MAX_SCAN_DEPTH,
272 i2c_acpi_add_device, NULL,
273 adap, NULL);
274 if (ACPI_FAILURE(status))
275 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
278 const struct acpi_device_id *
279 i2c_acpi_match_device(const struct acpi_device_id *matches,
280 struct i2c_client *client)
282 if (!(client && matches))
283 return NULL;
285 return acpi_match_device(matches, &client->dev);
288 static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level,
289 void *data, void **return_value)
291 struct i2c_acpi_lookup *lookup = data;
292 struct acpi_device *adev;
294 if (acpi_bus_get_device(handle, &adev))
295 return AE_OK;
297 if (i2c_acpi_do_lookup(adev, lookup))
298 return AE_OK;
300 if (lookup->search_handle != lookup->adapter_handle)
301 return AE_OK;
303 if (lookup->speed <= lookup->min_speed)
304 lookup->min_speed = lookup->speed;
306 return AE_OK;
310 * i2c_acpi_find_bus_speed - find I2C bus speed from ACPI
311 * @dev: The device owning the bus
313 * Find the I2C bus speed by walking the ACPI namespace for all I2C slaves
314 * devices connected to this bus and use the speed of slowest device.
316 * Returns the speed in Hz or zero
318 u32 i2c_acpi_find_bus_speed(struct device *dev)
320 struct i2c_acpi_lookup lookup;
321 struct i2c_board_info dummy;
322 acpi_status status;
324 if (!has_acpi_companion(dev))
325 return 0;
327 memset(&lookup, 0, sizeof(lookup));
328 lookup.search_handle = ACPI_HANDLE(dev);
329 lookup.min_speed = UINT_MAX;
330 lookup.info = &dummy;
331 lookup.index = -1;
333 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
334 I2C_ACPI_MAX_SCAN_DEPTH,
335 i2c_acpi_lookup_speed, NULL,
336 &lookup, NULL);
338 if (ACPI_FAILURE(status)) {
339 dev_warn(dev, "unable to find I2C bus speed from ACPI\n");
340 return 0;
343 return lookup.min_speed != UINT_MAX ? lookup.min_speed : 0;
345 EXPORT_SYMBOL_GPL(i2c_acpi_find_bus_speed);
347 static int i2c_acpi_find_match_adapter(struct device *dev, const void *data)
349 struct i2c_adapter *adapter = i2c_verify_adapter(dev);
351 if (!adapter)
352 return 0;
354 return ACPI_HANDLE(dev) == (acpi_handle)data;
357 static int i2c_acpi_find_match_device(struct device *dev, const void *data)
359 return ACPI_COMPANION(dev) == data;
362 struct i2c_adapter *i2c_acpi_find_adapter_by_handle(acpi_handle handle)
364 struct device *dev;
366 dev = bus_find_device(&i2c_bus_type, NULL, handle,
367 i2c_acpi_find_match_adapter);
368 return dev ? i2c_verify_adapter(dev) : NULL;
370 EXPORT_SYMBOL_GPL(i2c_acpi_find_adapter_by_handle);
372 static struct i2c_client *i2c_acpi_find_client_by_adev(struct acpi_device *adev)
374 struct device *dev;
376 dev = bus_find_device(&i2c_bus_type, NULL, adev,
377 i2c_acpi_find_match_device);
378 return dev ? i2c_verify_client(dev) : NULL;
381 static int i2c_acpi_notify(struct notifier_block *nb, unsigned long value,
382 void *arg)
384 struct acpi_device *adev = arg;
385 struct i2c_board_info info;
386 acpi_handle adapter_handle;
387 struct i2c_adapter *adapter;
388 struct i2c_client *client;
390 switch (value) {
391 case ACPI_RECONFIG_DEVICE_ADD:
392 if (i2c_acpi_get_info(adev, &info, NULL, &adapter_handle))
393 break;
395 adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
396 if (!adapter)
397 break;
399 i2c_acpi_register_device(adapter, adev, &info);
400 break;
401 case ACPI_RECONFIG_DEVICE_REMOVE:
402 if (!acpi_device_enumerated(adev))
403 break;
405 client = i2c_acpi_find_client_by_adev(adev);
406 if (!client)
407 break;
409 i2c_unregister_device(client);
410 put_device(&client->dev);
411 break;
414 return NOTIFY_OK;
417 struct notifier_block i2c_acpi_notifier = {
418 .notifier_call = i2c_acpi_notify,
422 * i2c_acpi_new_device - Create i2c-client for the Nth I2cSerialBus resource
423 * @dev: Device owning the ACPI resources to get the client from
424 * @index: Index of ACPI resource to get
425 * @info: describes the I2C device; note this is modified (addr gets set)
426 * Context: can sleep
428 * By default the i2c subsys creates an i2c-client for the first I2cSerialBus
429 * resource of an acpi_device, but some acpi_devices have multiple I2cSerialBus
430 * resources, in that case this function can be used to create an i2c-client
431 * for other I2cSerialBus resources in the Current Resource Settings table.
433 * Also see i2c_new_device, which this function calls to create the i2c-client.
435 * Returns a pointer to the new i2c-client, or error pointer in case of failure.
436 * Specifically, -EPROBE_DEFER is returned if the adapter is not found.
438 struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
439 struct i2c_board_info *info)
441 struct i2c_acpi_lookup lookup;
442 struct i2c_adapter *adapter;
443 struct i2c_client *client;
444 struct acpi_device *adev;
445 LIST_HEAD(resource_list);
446 int ret;
448 adev = ACPI_COMPANION(dev);
449 if (!adev)
450 return ERR_PTR(-EINVAL);
452 memset(&lookup, 0, sizeof(lookup));
453 lookup.info = info;
454 lookup.device_handle = acpi_device_handle(adev);
455 lookup.index = index;
457 ret = acpi_dev_get_resources(adev, &resource_list,
458 i2c_acpi_fill_info, &lookup);
459 if (ret < 0)
460 return ERR_PTR(ret);
462 acpi_dev_free_resource_list(&resource_list);
464 if (!info->addr)
465 return ERR_PTR(-EADDRNOTAVAIL);
467 adapter = i2c_acpi_find_adapter_by_handle(lookup.adapter_handle);
468 if (!adapter)
469 return ERR_PTR(-EPROBE_DEFER);
471 client = i2c_new_device(adapter, info);
472 if (!client)
473 return ERR_PTR(-ENODEV);
475 return client;
477 EXPORT_SYMBOL_GPL(i2c_acpi_new_device);
479 #ifdef CONFIG_ACPI_I2C_OPREGION
480 static int acpi_gsb_i2c_read_bytes(struct i2c_client *client,
481 u8 cmd, u8 *data, u8 data_len)
484 struct i2c_msg msgs[2];
485 int ret;
486 u8 *buffer;
488 buffer = kzalloc(data_len, GFP_KERNEL);
489 if (!buffer)
490 return AE_NO_MEMORY;
492 msgs[0].addr = client->addr;
493 msgs[0].flags = client->flags;
494 msgs[0].len = 1;
495 msgs[0].buf = &cmd;
497 msgs[1].addr = client->addr;
498 msgs[1].flags = client->flags | I2C_M_RD;
499 msgs[1].len = data_len;
500 msgs[1].buf = buffer;
502 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
503 if (ret < 0) {
504 /* Getting a NACK is unfortunately normal with some DSTDs */
505 if (ret == -EREMOTEIO)
506 dev_dbg(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
507 data_len, client->addr, cmd, ret);
508 else
509 dev_err(&client->adapter->dev, "i2c read %d bytes from client@%#x starting at reg %#x failed, error: %d\n",
510 data_len, client->addr, cmd, ret);
511 /* 2 transfers must have completed successfully */
512 } else if (ret == 2) {
513 memcpy(data, buffer, data_len);
514 ret = 0;
515 } else {
516 ret = -EIO;
519 kfree(buffer);
520 return ret;
523 static int acpi_gsb_i2c_write_bytes(struct i2c_client *client,
524 u8 cmd, u8 *data, u8 data_len)
527 struct i2c_msg msgs[1];
528 u8 *buffer;
529 int ret = AE_OK;
531 buffer = kzalloc(data_len + 1, GFP_KERNEL);
532 if (!buffer)
533 return AE_NO_MEMORY;
535 buffer[0] = cmd;
536 memcpy(buffer + 1, data, data_len);
538 msgs[0].addr = client->addr;
539 msgs[0].flags = client->flags;
540 msgs[0].len = data_len + 1;
541 msgs[0].buf = buffer;
543 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
545 kfree(buffer);
547 if (ret < 0) {
548 dev_err(&client->adapter->dev, "i2c write failed: %d\n", ret);
549 return ret;
552 /* 1 transfer must have completed successfully */
553 return (ret == 1) ? 0 : -EIO;
556 static acpi_status
557 i2c_acpi_space_handler(u32 function, acpi_physical_address command,
558 u32 bits, u64 *value64,
559 void *handler_context, void *region_context)
561 struct gsb_buffer *gsb = (struct gsb_buffer *)value64;
562 struct i2c_acpi_handler_data *data = handler_context;
563 struct acpi_connection_info *info = &data->info;
564 struct acpi_resource_i2c_serialbus *sb;
565 struct i2c_adapter *adapter = data->adapter;
566 struct i2c_client *client;
567 struct acpi_resource *ares;
568 u32 accessor_type = function >> 16;
569 u8 action = function & ACPI_IO_MASK;
570 acpi_status ret;
571 int status;
573 ret = acpi_buffer_to_resource(info->connection, info->length, &ares);
574 if (ACPI_FAILURE(ret))
575 return ret;
577 client = kzalloc(sizeof(*client), GFP_KERNEL);
578 if (!client) {
579 ret = AE_NO_MEMORY;
580 goto err;
583 if (!value64 || !i2c_acpi_get_i2c_resource(ares, &sb)) {
584 ret = AE_BAD_PARAMETER;
585 goto err;
588 client->adapter = adapter;
589 client->addr = sb->slave_address;
591 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
592 client->flags |= I2C_CLIENT_TEN;
594 switch (accessor_type) {
595 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV:
596 if (action == ACPI_READ) {
597 status = i2c_smbus_read_byte(client);
598 if (status >= 0) {
599 gsb->bdata = status;
600 status = 0;
602 } else {
603 status = i2c_smbus_write_byte(client, gsb->bdata);
605 break;
607 case ACPI_GSB_ACCESS_ATTRIB_BYTE:
608 if (action == ACPI_READ) {
609 status = i2c_smbus_read_byte_data(client, command);
610 if (status >= 0) {
611 gsb->bdata = status;
612 status = 0;
614 } else {
615 status = i2c_smbus_write_byte_data(client, command,
616 gsb->bdata);
618 break;
620 case ACPI_GSB_ACCESS_ATTRIB_WORD:
621 if (action == ACPI_READ) {
622 status = i2c_smbus_read_word_data(client, command);
623 if (status >= 0) {
624 gsb->wdata = status;
625 status = 0;
627 } else {
628 status = i2c_smbus_write_word_data(client, command,
629 gsb->wdata);
631 break;
633 case ACPI_GSB_ACCESS_ATTRIB_BLOCK:
634 if (action == ACPI_READ) {
635 status = i2c_smbus_read_block_data(client, command,
636 gsb->data);
637 if (status >= 0) {
638 gsb->len = status;
639 status = 0;
641 } else {
642 status = i2c_smbus_write_block_data(client, command,
643 gsb->len, gsb->data);
645 break;
647 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE:
648 if (action == ACPI_READ) {
649 status = acpi_gsb_i2c_read_bytes(client, command,
650 gsb->data, info->access_length);
651 } else {
652 status = acpi_gsb_i2c_write_bytes(client, command,
653 gsb->data, info->access_length);
655 break;
657 default:
658 dev_warn(&adapter->dev, "protocol 0x%02x not supported for client 0x%02x\n",
659 accessor_type, client->addr);
660 ret = AE_BAD_PARAMETER;
661 goto err;
664 gsb->status = status;
666 err:
667 kfree(client);
668 ACPI_FREE(ares);
669 return ret;
673 int i2c_acpi_install_space_handler(struct i2c_adapter *adapter)
675 acpi_handle handle;
676 struct i2c_acpi_handler_data *data;
677 acpi_status status;
679 if (!adapter->dev.parent)
680 return -ENODEV;
682 handle = ACPI_HANDLE(adapter->dev.parent);
684 if (!handle)
685 return -ENODEV;
687 data = kzalloc(sizeof(struct i2c_acpi_handler_data),
688 GFP_KERNEL);
689 if (!data)
690 return -ENOMEM;
692 data->adapter = adapter;
693 status = acpi_bus_attach_private_data(handle, (void *)data);
694 if (ACPI_FAILURE(status)) {
695 kfree(data);
696 return -ENOMEM;
699 status = acpi_install_address_space_handler(handle,
700 ACPI_ADR_SPACE_GSBUS,
701 &i2c_acpi_space_handler,
702 NULL,
703 data);
704 if (ACPI_FAILURE(status)) {
705 dev_err(&adapter->dev, "Error installing i2c space handler\n");
706 acpi_bus_detach_private_data(handle);
707 kfree(data);
708 return -ENOMEM;
711 acpi_walk_dep_device_list(handle);
712 return 0;
715 void i2c_acpi_remove_space_handler(struct i2c_adapter *adapter)
717 acpi_handle handle;
718 struct i2c_acpi_handler_data *data;
719 acpi_status status;
721 if (!adapter->dev.parent)
722 return;
724 handle = ACPI_HANDLE(adapter->dev.parent);
726 if (!handle)
727 return;
729 acpi_remove_address_space_handler(handle,
730 ACPI_ADR_SPACE_GSBUS,
731 &i2c_acpi_space_handler);
733 status = acpi_bus_get_private_data(handle, (void **)&data);
734 if (ACPI_SUCCESS(status))
735 kfree(data);
737 acpi_bus_detach_private_data(handle);
739 #endif /* CONFIG_ACPI_I2C_OPREGION */