linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Darren Hart <dvhart@infradead.org>,
	platform-driver-x86@vger.kernel.org,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>,
	linux-acpi@vger.kernel.org, Jonathan Cameron <jic23@kernel.org>,
	Wolfram Sang <wsa@the-dreams.de>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	linux-i2c@vger.kernel.org, Hans de Goede <hdegoede@redhat.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	linux-kernel@vger.kernel.org
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: [PATCH v4 10/15] platform/x86: i2c-multi-instantiate: Count I2cSerialBus() resources
Date: Wed, 28 Nov 2018 13:30:19 +0200	[thread overview]
Message-ID: <20181128113024.42126-11-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20181128113024.42126-1-andriy.shevchenko@linux.intel.com>

Instead of relying on hard coded and thus expected number of I2C clients,
count the real amount provided by firmware.

This allows to support non-fixed amount of the slaves.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/platform/x86/i2c-multi-instantiate.c | 41 ++++++++++++++++++--
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/drivers/platform/x86/i2c-multi-instantiate.c b/drivers/platform/x86/i2c-multi-instantiate.c
index 724535673089..d99bbaed0ca2 100644
--- a/drivers/platform/x86/i2c-multi-instantiate.c
+++ b/drivers/platform/x86/i2c-multi-instantiate.c
@@ -12,6 +12,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/types.h>
 
 struct i2c_inst_data {
 	const char *type;
@@ -23,6 +24,31 @@ struct i2c_multi_inst_data {
 	struct i2c_client *clients[0];
 };
 
+static int i2c_multi_inst_count(struct acpi_resource *ares, void *data)
+{
+	struct acpi_resource_i2c_serialbus *sb;
+	int *count = data;
+
+	if (i2c_acpi_get_i2c_resource(ares, &sb))
+		*count = *count + 1;
+
+	return 1;
+}
+
+static int i2c_multi_inst_count_resources(struct acpi_device *adev)
+{
+	LIST_HEAD(r);
+	int count = 0;
+	int ret;
+
+	ret = acpi_dev_get_resources(adev, &r, i2c_multi_inst_count, &count);
+	if (ret < 0)
+		return ret;
+
+	acpi_dev_free_resource_list(&r);
+	return count;
+}
+
 static int i2c_multi_inst_probe(struct platform_device *pdev)
 {
 	struct i2c_multi_inst_data *multi;
@@ -44,17 +70,19 @@ static int i2c_multi_inst_probe(struct platform_device *pdev)
 	adev = ACPI_COMPANION(dev);
 
 	/* Count number of clients to instantiate */
-	for (i = 0; inst_data[i].type; i++) {}
+	ret = i2c_multi_inst_count_resources(adev);
+	if (ret < 0)
+		return ret;
 
 	multi = devm_kmalloc(dev,
-			offsetof(struct i2c_multi_inst_data, clients[i]),
+			offsetof(struct i2c_multi_inst_data, clients[ret]),
 			GFP_KERNEL);
 	if (!multi)
 		return -ENOMEM;
 
-	multi->num_clients = i;
+	multi->num_clients = ret;
 
-	for (i = 0; i < multi->num_clients; i++) {
+	for (i = 0; i < multi->num_clients && inst_data[i].type; i++) {
 		memset(&board_info, 0, sizeof(board_info));
 		strlcpy(board_info.type, inst_data[i].type, I2C_NAME_SIZE);
 		snprintf(name, sizeof(name), "%s-%s", match->id,
@@ -79,6 +107,11 @@ static int i2c_multi_inst_probe(struct platform_device *pdev)
 			goto error;
 		}
 	}
+	if (i < multi->num_clients) {
+		dev_err(dev, "Error finding driver, idx %d\n", i);
+		ret = -ENODEV;
+		goto error;
+	}
 
 	platform_set_drvdata(pdev, multi);
 	return 0;
-- 
2.19.2


  parent reply	other threads:[~2018-11-28 11:31 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-28 11:30 [PATCH v4 00/15] i2c-multi-instantiate: Adapt for INT3515 and alike Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 01/15] defconfig: enable I2C_DEBUG_CORE Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 02/15] platform/x86: intel_cht_int33fe: Remove duplicate NULL check Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 03/15] platform/x86: intel_cht_int33fe: Accept errors of i2c_acpi_new_device() Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 04/15] platform/x86: i2c-multi-instantiate: " Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 05/15] i2c: acpi: Return error pointers from i2c_acpi_new_device() Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 06/15] platform/x86: intel_cht_int33fe: Get rid of obsolete conditional Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 07/15] platform/x86: i2c-multi-instantiate: " Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 08/15] i2c: acpi: Use ACPI_FAILURE instead of !ACPI_SUCCESS Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 09/15] i2c: acpi: Introduce i2c_acpi_get_i2c_resource() helper Andy Shevchenko
2018-11-28 11:30 ` Andy Shevchenko [this message]
2018-11-28 11:30 ` [PATCH v4 11/15] platform/x86: i2c-multi-instantiate: Distinguish IRQ resource type Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 12/15] platform/x86: i2c-multi-instantiate: Introduce IOAPIC IRQ support Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 13/15] platform/x86: i2c-multi-instantiate: Allow to have same slaves Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 14/15] ACPI / scan: Create platform device for INT3515 ACPI nodes Andy Shevchenko
2018-11-28 11:30 ` [PATCH v4 15/15] iio: inv_mpu6050: Use i2c_acpi_get_i2c_resource() helper Andy Shevchenko
2018-11-28 11:34 ` [PATCH v4 00/15] i2c-multi-instantiate: Adapt for INT3515 and alike Andy Shevchenko

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=20181128113024.42126-11-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=dvhart@infradead.org \
    --cc=hdegoede@redhat.com \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=jic23@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=wsa@the-dreams.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).