linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: Jean Delvare <khali@linux-fr.org>
Cc: "Rafael J. Wysocki" <rjw@sisk.pl>,
	ben-linux@fluff.org, w.sang@pengutronix.de,
	linux-kernel@vger.kernel.org, lenb@kernel.org,
	rafael.j.wysocki@intel.com, broonie@opensource.wolfsonmicro.com,
	grant.likely@secretlab.ca, linus.walleij@linaro.org,
	bhelgaas@google.com, mathias.nyman@linux.intel.com,
	linux-acpi@vger.kernel.org
Subject: [PATCH v2 3/3 UPDATED] i2c / ACPI: add ACPI enumeration support
Date: Fri, 16 Nov 2012 19:28:28 +0200	[thread overview]
Message-ID: <20121116172828.GY17774@intel.com> (raw)
In-Reply-To: <20121116174753.67d71043@endymion.delvare>

On Fri, Nov 16, 2012 at 05:47:53PM +0100, Jean Delvare wrote:
> Hi Mika,
> 
> On Fri, 16 Nov 2012 17:23:32 +0200, Mika Westerberg wrote:
> > Here's the updated version where we handle 10-bit addresses properly
> > (hopefully).
> > (...)
> > +static int acpi_i2c_find_device(struct device *dev, acpi_handle *handle)
> > +{
> > (...)
> > +	memset(&i2c_find, 0, sizeof(i2c_find));
> > +	i2c_find.addr = client->addr;
> > +	if (client->flags & I2C_CLIENT_TEN)
> > +		i2c_find.access_mode = ACPI_I2C_10BIT_MODE;
> 
> This works because you are lucky and ACPI_I2C_7BIT_MODE has value 0,
> but for correctness I'd rather add:
> 
> 	else
> 		i2c_find.access_mode = ACPI_I2C_7BIT_MODE;

Right, sorry about that. The patch below should address this as well.

From: Mika Westerberg <mika.westerberg@linux.intel.com>
Date: Mon, 10 Sep 2012 12:12:32 +0300
Subject: [PATCH] i2c / ACPI: add ACPI enumeration support

ACPI 5 introduced I2cSerialBus resource that makes it possible to enumerate
and configure the I2C slave devices behind the I2C controller. This patch
adds helper functions to support I2C slave enumeration.

An ACPI enabled I2C controller driver only needs to call acpi_i2c_register_devices()
in order to get its slave devices enumerated, created and bound to the
corresponding ACPI handle.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
 drivers/acpi/Kconfig     |    6 ++
 drivers/acpi/Makefile    |    1 +
 drivers/acpi/acpi_i2c.c  |  216 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/i2c/i2c-core.c   |   10 +++
 include/linux/acpi_i2c.h |   27 ++++++
 5 files changed, 260 insertions(+)
 create mode 100644 drivers/acpi/acpi_i2c.c
 create mode 100644 include/linux/acpi_i2c.h

diff --git a/drivers/acpi/Kconfig b/drivers/acpi/Kconfig
index 119d58d..0300bf6 100644
--- a/drivers/acpi/Kconfig
+++ b/drivers/acpi/Kconfig
@@ -181,6 +181,12 @@ config ACPI_DOCK
 	  This driver supports ACPI-controlled docking stations and removable
 	  drive bays such as the IBM Ultrabay and the Dell Module Bay.
 
+config ACPI_I2C
+	def_tristate I2C
+	depends on I2C
+	help
+	  ACPI I2C enumeration support.
+
 config ACPI_PROCESSOR
 	tristate "Processor"
 	select THERMAL
diff --git a/drivers/acpi/Makefile b/drivers/acpi/Makefile
index 7289828..2a4502b 100644
--- a/drivers/acpi/Makefile
+++ b/drivers/acpi/Makefile
@@ -70,6 +70,7 @@ obj-$(CONFIG_ACPI_HED)		+= hed.o
 obj-$(CONFIG_ACPI_EC_DEBUGFS)	+= ec_sys.o
 obj-$(CONFIG_ACPI_CUSTOM_METHOD)+= custom_method.o
 obj-$(CONFIG_ACPI_BGRT)		+= bgrt.o
+obj-$(CONFIG_ACPI_I2C)		+= acpi_i2c.o
 
 # processor has its own "processor." module_param namespace
 processor-y			:= processor_driver.o processor_throttling.o
diff --git a/drivers/acpi/acpi_i2c.c b/drivers/acpi/acpi_i2c.c
new file mode 100644
index 0000000..5783d93
--- /dev/null
+++ b/drivers/acpi/acpi_i2c.c
@@ -0,0 +1,216 @@
+/*
+ * ACPI I2C enumeration support
+ *
+ * Copyright (C) 2012, Intel Corporation
+ * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/acpi.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/export.h>
+#include <linux/i2c.h>
+#include <linux/ioport.h>
+
+ACPI_MODULE_NAME("i2c");
+
+static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
+{
+	struct acpi_resource_i2c_serialbus *sb;
+	struct i2c_board_info *info = data;
+
+	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
+		return 0;
+
+	sb = &ares->data.i2c_serial_bus;
+	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
+		return 0;
+
+	info->addr = sb->slave_address;
+	if (sb->access_mode == ACPI_I2C_10BIT_MODE)
+		info->flags |= I2C_CLIENT_TEN;
+
+	return 1;
+}
+
+static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
+				       void *data, void **return_value)
+{
+	struct i2c_adapter *adapter = data;
+	struct resource_list_entry *rentry;
+	struct list_head resource_list;
+	struct i2c_board_info info;
+	struct acpi_device *adev;
+	int ret;
+
+	if (acpi_bus_get_device(handle, &adev))
+		return AE_OK;
+	if (acpi_bus_get_status(adev) || !adev->status.present)
+		return AE_OK;
+
+	memset(&info, 0, sizeof(info));
+
+	INIT_LIST_HEAD(&resource_list);
+	ret = acpi_dev_get_resources(adev, &resource_list,
+				     acpi_i2c_add_resource, &info);
+	if (ret < 0)
+		return AE_OK;
+
+	list_for_each_entry(rentry, &resource_list, node) {
+		struct resource *r = &rentry->res;
+
+		if (resource_type(r) == IORESOURCE_IRQ) {
+			info.irq = r->start;
+			break;
+		}
+	}
+
+	acpi_dev_free_resource_list(&resource_list);
+
+	if (!info.addr)
+		return AE_OK;
+
+	strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
+	if (!i2c_new_device(adapter, &info)) {
+		dev_err(&adapter->dev,
+			"failed to add I2C device %s from ACPI\n",
+			dev_name(&adev->dev));
+	}
+
+	return AE_OK;
+}
+
+/**
+ * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
+ * @adapter: pointer to adapter
+ *
+ * Enumerate all I2C slave devices behind this adapter by walking the ACPI
+ * namespace. When a device is found it will be added to the Linux device
+ * model and bound to the corresponding ACPI handle.
+ */
+void acpi_i2c_register_devices(struct i2c_adapter *adapter)
+{
+	acpi_handle handle;
+	acpi_status status;
+
+	handle = adapter->dev.acpi_handle;
+	if (!handle)
+		return;
+
+	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
+				     acpi_i2c_add_device, NULL,
+				     adapter, NULL);
+	if (ACPI_FAILURE(status))
+		dev_warn(&adapter->dev, "failed to enumerate I2C slaves\n");
+}
+EXPORT_SYMBOL_GPL(acpi_i2c_register_devices);
+
+struct acpi_i2c_find {
+	acpi_handle handle;
+	u16 addr;
+	u8 access_mode;
+	bool found;
+};
+
+static int acpi_i2c_find_child_address(struct acpi_resource *ares, void *data)
+{
+	struct acpi_resource_i2c_serialbus *sb;
+	struct acpi_i2c_find *i2c_find = data;
+
+	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
+		return 1;
+
+	sb = &ares->data.i2c_serial_bus;
+	if (sb->type != ACPI_RESOURCE_SERIAL_TYPE_I2C)
+		return 1;
+
+	if (sb->slave_address == i2c_find->addr &&
+	    sb->access_mode == i2c_find->access_mode)
+		i2c_find->found = true;
+
+	return 1;
+}
+
+static acpi_status acpi_i2c_find_child(acpi_handle handle, u32 level,
+				       void *data, void **return_value)
+{
+	struct acpi_i2c_find *i2c_find = data;
+	struct list_head resource_list;
+	struct acpi_device *adev;
+	int ret;
+
+	if (acpi_bus_get_device(handle, &adev))
+		return AE_OK;
+	if (acpi_bus_get_status(adev) || !adev->status.present)
+		return AE_OK;
+
+	INIT_LIST_HEAD(&resource_list);
+	ret = acpi_dev_get_resources(adev, &resource_list,
+				     acpi_i2c_find_child_address, i2c_find);
+	if (ret < 0)
+		return AE_OK;
+
+	acpi_dev_free_resource_list(&resource_list);
+
+	if (i2c_find->found) {
+		i2c_find->handle = handle;
+		return AE_CTRL_TERMINATE;
+	}
+	return AE_OK;
+}
+
+static int acpi_i2c_find_device(struct device *dev, acpi_handle *handle)
+{
+	struct acpi_i2c_find i2c_find;
+	struct i2c_adapter *adapter;
+	struct i2c_client *client;
+	acpi_handle parent;
+	acpi_status status;
+
+	client = i2c_verify_client(dev);
+	if (!client)
+		return -ENODEV;
+
+	adapter = client->adapter;
+	if (!adapter)
+		return -ENODEV;
+
+	parent = adapter->dev.acpi_handle;
+	if (!parent)
+		return -ENODEV;
+
+	memset(&i2c_find, 0, sizeof(i2c_find));
+	i2c_find.addr = client->addr;
+	i2c_find.access_mode = client->flags & I2C_CLIENT_TEN ?
+		ACPI_I2C_10BIT_MODE : ACPI_I2C_7BIT_MODE;
+
+	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1,
+				     acpi_i2c_find_child, NULL,
+				     &i2c_find, NULL);
+	if (ACPI_FAILURE(status) || !i2c_find.handle)
+		return -ENODEV;
+
+	*handle = i2c_find.handle;
+	return 0;
+}
+
+static struct acpi_bus_type acpi_i2c_bus = {
+	.bus = &i2c_bus_type,
+	.find_device = acpi_i2c_find_device,
+};
+
+void acpi_i2c_bus_register(void)
+{
+	register_acpi_bus_type(&acpi_i2c_bus);
+}
+EXPORT_SYMBOL_GPL(acpi_i2c_bus_register);
+
+void acpi_i2c_bus_unregister(void)
+{
+	unregister_acpi_bus_type(&acpi_i2c_bus);
+}
+EXPORT_SYMBOL_GPL(acpi_i2c_bus_unregister);
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index a7edf98..7385de2f 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -39,6 +39,8 @@
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
 #include <linux/pm_runtime.h>
+#include <linux/acpi.h>
+#include <linux/acpi_i2c.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -78,6 +80,10 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv)
 	if (of_driver_match_device(dev, drv))
 		return 1;
 
+	/* Then ACPI style match */
+	if (acpi_driver_match_device(dev, drv))
+		return 1;
+
 	driver = to_i2c_driver(drv);
 	/* match on an id table if there is one */
 	if (driver->id_table)
@@ -1298,6 +1304,8 @@ static int __init i2c_init(void)
 	retval = i2c_add_driver(&dummy_driver);
 	if (retval)
 		goto class_err;
+
+	acpi_i2c_bus_register();
 	return 0;
 
 class_err:
@@ -1311,6 +1319,8 @@ bus_err:
 
 static void __exit i2c_exit(void)
 {
+	acpi_i2c_bus_unregister();
+
 	i2c_del_driver(&dummy_driver);
 #ifdef CONFIG_I2C_COMPAT
 	class_compat_unregister(i2c_adapter_compat_class);
diff --git a/include/linux/acpi_i2c.h b/include/linux/acpi_i2c.h
new file mode 100644
index 0000000..cf2233f
--- /dev/null
+++ b/include/linux/acpi_i2c.h
@@ -0,0 +1,27 @@
+/*
+ * ACPI I2C enumeration support
+ *
+ * Copyright (C) 2012, Intel Corporation
+ * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef LINUX_ACPI_I2C_H
+#define LINUX_ACPI_I2C_H
+
+struct i2c_adapter;
+
+#if IS_ENABLED(CONFIG_ACPI_I2C)
+extern void acpi_i2c_register_devices(struct i2c_adapter *adap);
+extern void acpi_i2c_bus_register(void);
+extern void acpi_i2c_bus_unregister(void);
+#else
+static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
+static inline void acpi_i2c_bus_register(void) {}
+static inline void acpi_i2c_bus_unregister(void) {}
+#endif /* IS_ENABLED(CONFIG_ACPI_I2C) */
+
+#endif /* LINUX_ACPI_I2C_H */
-- 
1.7.10.4


  reply	other threads:[~2012-11-16 17:25 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-15 11:03 [PATCH v2 0/3] ACPI 5 support for GPIO, SPI and I2C Mika Westerberg
2012-11-15 11:03 ` [PATCH v2 1/3] gpio / ACPI: add ACPI support Mika Westerberg
2012-11-16  1:34   ` Rafael J. Wysocki
2012-11-16  6:54     ` Mika Westerberg
2012-11-16  8:05       ` Mika Westerberg
2012-11-16  8:12         ` Mika Westerberg
2012-11-16 10:02           ` Rafael J. Wysocki
2012-11-16 12:49             ` Mika Westerberg
2012-11-16 10:03   ` Rafael J. Wysocki
2012-11-15 11:03 ` [PATCH v2 2/3] spi / ACPI: add ACPI enumeration support Mika Westerberg
2012-11-16 10:06   ` Rafael J. Wysocki
2012-11-17 10:11   ` Rafael J. Wysocki
2012-11-17 10:18     ` Mika Westerberg
2012-11-15 11:03 ` [PATCH v2 3/3] i2c " Mika Westerberg
2012-11-16 10:09   ` Rafael J. Wysocki
2012-11-16 13:03     ` Jean Delvare
2012-11-16 13:21       ` Rafael J. Wysocki
2012-11-16 13:42         ` Jean Delvare
2012-11-16 14:17           ` Mika Westerberg
2012-11-16 15:23             ` Mika Westerberg
2012-11-16 16:47               ` Jean Delvare
2012-11-16 17:28                 ` Mika Westerberg [this message]
2012-11-16 18:12                   ` [PATCH v2 3/3 UPDATED] " Jean Delvare
2012-11-17  6:46                   ` Bjorn Helgaas
2012-11-17  8:03                     ` Mika Westerberg
2012-11-17  9:55                       ` Mika Westerberg
2012-11-19 22:49                         ` Bjorn Helgaas
2012-11-19 23:15                           ` Rafael J. Wysocki
2012-11-19 23:28                           ` Rafael J. Wysocki
2012-11-20  7:07                           ` Mika Westerberg
2012-11-17 11:24                       ` Rafael J. Wysocki
2012-11-18 15:55                         ` Mika Westerberg
2012-11-18 21:10                           ` [PATCH 0/2] ACPI: Simplify "glueing" to physical nodes (was: Re: [PATCH v2 3/3 UPDATED] i2c / ACPI: add ACPI enumeration support) Rafael J. Wysocki
2012-11-18 21:12                             ` [PATCH 1/2] ACPI: Allow ACPI handles of devices to be initialized in advance Rafael J. Wysocki
2012-11-19  9:42                               ` Mika Westerberg
2012-11-19 12:33                                 ` [Update][PATCH " Rafael J. Wysocki
2012-11-18 21:13                             ` [PATCH 2/2] ACPI / platform: Initialize ACPI handles of platform devices " Rafael J. Wysocki
2012-11-19 16:23                               ` Greg Kroah-Hartman
2012-11-19 17:32                                 ` Rafael J. Wysocki
2012-11-19 17:45                                   ` Rafael J. Wysocki
2012-11-19 20:44                                     ` Rafael J. Wysocki
2012-11-19 21:05                                       ` Mika Westerberg
2012-11-19 21:56                                         ` Rafael J. Wysocki
2012-11-19 22:32                                       ` Greg Kroah-Hartman
2012-11-19 22:44                                         ` Rafael J. Wysocki
2012-11-19 18:25                                   ` Rafael J. Wysocki
2012-11-19 22:31                                   ` Greg Kroah-Hartman
2012-11-19 22:45                                     ` Rafael J. Wysocki
2012-11-20  0:55                             ` [Update][PATCH 0/3] ACPI: Simplify "glueing" to physical nodes Rafael J. Wysocki
2012-11-20  0:57                               ` [Update][PATCH 1/3] ACPI: Allow ACPI handles of devices to be initialized in advance Rafael J. Wysocki
2012-11-20  0:59                               ` [Update][PATCH 2/3] ACPI / driver core: Introduce struct acpi_dev_node and related macros Rafael J. Wysocki
2012-11-20  9:10                                 ` Mika Westerberg
2012-11-20  9:34                                   ` [Update 2][PATCH " Rafael J. Wysocki
2012-11-20 12:57                                     ` Mika Westerberg
2012-11-20 18:08                                     ` Greg Kroah-Hartman
2012-11-20  1:01                               ` [Update][PATCH 3/3] ACPI / platform: Initialize ACPI handles of platform devices in advance Rafael J. Wysocki
2012-11-20 18:08                                 ` Greg Kroah-Hartman
2012-11-20  9:11                               ` [Update][PATCH 0/3] ACPI: Simplify "glueing" to physical nodes Mika Westerberg
2012-11-20  9:31                                 ` Rafael J. Wysocki
2012-11-20 18:09                               ` Greg Kroah-Hartman
2012-11-20 21:40                                 ` Rafael J. Wysocki
2012-11-16 20:02             ` [PATCH v2 3/3] i2c / ACPI: add ACPI enumeration support Rafael J. Wysocki
2012-11-16 20:09               ` Mika Westerberg
2012-11-16 20:18                 ` Rafael J. Wysocki
2012-11-20 10:29 ` [PATCH v3 0/3] ACPI 5 support for GPIO, SPI and I2C Mika Westerberg
2012-11-20 10:29   ` [PATCH v3 1/3] gpio / ACPI: add ACPI support Mika Westerberg
2012-11-20 12:46     ` Rafael J. Wysocki
2012-11-20 10:29   ` [PATCH v3 2/3] spi / ACPI: add ACPI enumeration support Mika Westerberg
2012-11-20 13:05     ` Rafael J. Wysocki
2012-11-20 13:15       ` Mika Westerberg
2012-11-20 13:24         ` Rafael J. Wysocki
2012-11-20 10:29   ` [PATCH v3 3/3] i2c " Mika Westerberg
2012-11-20 13:06     ` Rafael J. Wysocki
2012-11-20 13:57       ` Mika Westerberg
2012-11-20 18:13   ` [PATCH v4 0/3] ACPI 5 support for GPIO, SPI and I2C Mika Westerberg
2012-11-20 18:13     ` [PATCH v4 1/3] gpio / ACPI: add ACPI support Mika Westerberg
2012-11-30 11:20       ` Grant Likely
2012-11-30 11:27         ` Rafael J. Wysocki
2012-11-20 18:13     ` [PATCH v4 2/3] spi / ACPI: add ACPI enumeration support Mika Westerberg
2012-11-30 11:24       ` Grant Likely
2012-11-30 11:36         ` Rafael J. Wysocki
2012-11-20 18:13     ` [PATCH v4 3/3] i2c " Mika Westerberg
2012-11-21 21:31     ` [PATCH v4 0/3] ACPI 5 support for GPIO, SPI and I2C Rafael J. Wysocki
2012-11-21 21:54       ` Jean Delvare
2012-11-23 11:39         ` Rafael J. Wysocki
2012-11-22  1:36       ` Mark Brown
2012-11-22  9:43       ` Linus Walleij
2012-11-22 10:03         ` Rafael J. Wysocki
2012-11-28 12:36           ` Mika Westerberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121116172828.GY17774@intel.com \
    --to=mika.westerberg@linux.intel.com \
    --cc=ben-linux@fluff.org \
    --cc=bhelgaas@google.com \
    --cc=broonie@opensource.wolfsonmicro.com \
    --cc=grant.likely@secretlab.ca \
    --cc=khali@linux-fr.org \
    --cc=lenb@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rjw@sisk.pl \
    --cc=w.sang@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).