devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <bgolaszewski-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
To: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	Hartmut Knaack <knaack.h-Mmb7MZpHnFY@public.gmane.org>,
	Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>,
	Peter Meerwald-Stadler
	<pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org>,
	Rob Herring <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	Mark Rutland <mark.rutland-5wv7dgnIgG8@public.gmane.org>
Cc: linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Kevin Hilman <khilman-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>,
	Patrick Titiano
	<ptitiano-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>,
	Neil Armstrong
	<narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>,
	Bartosz Golaszewski
	<bgolaszewski-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
Subject: [PATCH] iio: misc: add a generic regulator driver
Date: Tue, 29 Nov 2016 16:22:49 +0100	[thread overview]
Message-ID: <1480432969-20913-1-git-send-email-bgolaszewski@baylibre.com> (raw)

Some iio devices are powered externally by a regulator which, for
example, can be used to power-cycle an adc.

This patch proposes to add a simple driver representing a regulator
to the iio framework which exports attributes allowing to manipulate
the underlying hardware.

The reason for connecting the regulator and the iio frameworks is that
once libiio learns to toggle iio attributes we'll be able to
power-cycle devices remotely.

Initially the driver only supports enable/disable operations, but it
should be straightforward to extend it with other regulator operations
in the future.

Tested with a baylibre-acme board for beaglebone black.

Signed-off-by: Bartosz Golaszewski <bgolaszewski-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
---
 .../devicetree/bindings/iio/misc/iio-regulator.txt |  18 +++
 drivers/iio/Kconfig                                |   1 +
 drivers/iio/Makefile                               |   1 +
 drivers/iio/misc/Kconfig                           |  17 +++
 drivers/iio/misc/Makefile                          |   6 +
 drivers/iio/misc/iio-regulator.c                   | 121 +++++++++++++++++++++
 6 files changed, 164 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/misc/iio-regulator.txt
 create mode 100644 drivers/iio/misc/Kconfig
 create mode 100644 drivers/iio/misc/Makefile
 create mode 100644 drivers/iio/misc/iio-regulator.c

diff --git a/Documentation/devicetree/bindings/iio/misc/iio-regulator.txt b/Documentation/devicetree/bindings/iio/misc/iio-regulator.txt
new file mode 100644
index 0000000..147458f
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/misc/iio-regulator.txt
@@ -0,0 +1,18 @@
+Industrial IO regulator device driver
+-------------------------------------
+
+This document describes the bindings for the iio-regulator - a dummy device
+driver representing a physical regulator within the iio framework.
+
+Required properties:
+
+- compatible: must be "iio-regulator"
+- vcc-supply: phandle of the regulator this device represents
+
+Example
+-------
+
+iio_regulator {
+	compatible = "iio-regulator";
+	vcc-supply = <&vcc0>;
+};
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 6743b18..2e896e0 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -80,6 +80,7 @@ source "drivers/iio/gyro/Kconfig"
 source "drivers/iio/health/Kconfig"
 source "drivers/iio/humidity/Kconfig"
 source "drivers/iio/imu/Kconfig"
+source "drivers/iio/misc/Kconfig"
 source "drivers/iio/light/Kconfig"
 source "drivers/iio/magnetometer/Kconfig"
 source "drivers/iio/orientation/Kconfig"
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 87e4c43..4008d5a 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -25,6 +25,7 @@ obj-y += frequency/
 obj-y += health/
 obj-y += humidity/
 obj-y += imu/
+obj-y += misc/
 obj-y += light/
 obj-y += magnetometer/
 obj-y += orientation/
diff --git a/drivers/iio/misc/Kconfig b/drivers/iio/misc/Kconfig
new file mode 100644
index 0000000..b43a1ed
--- /dev/null
+++ b/drivers/iio/misc/Kconfig
@@ -0,0 +1,17 @@
+#
+# Miscellaneous iio drivers
+#
+# When adding new entries keep the list in alphabetical order
+
+menu "Miscellaneous iio drivers"
+
+config IIO_REGULATOR
+	tristate "IIO regulator driver"
+	depends on REGULATOR
+	help
+	  Say yes here to build support for regulators powering iio devices.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called iio-regulator.
+
+endmenu
diff --git a/drivers/iio/misc/Makefile b/drivers/iio/misc/Makefile
new file mode 100644
index 0000000..da8f56a
--- /dev/null
+++ b/drivers/iio/misc/Makefile
@@ -0,0 +1,6 @@
+#
+# Makefile for IIO misc drivers
+#
+
+# When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_IIO_REGULATOR) += iio-regulator.o
diff --git a/drivers/iio/misc/iio-regulator.c b/drivers/iio/misc/iio-regulator.c
new file mode 100644
index 0000000..0d61553
--- /dev/null
+++ b/drivers/iio/misc/iio-regulator.c
@@ -0,0 +1,121 @@
+/*
+ * Generic regulator driver for industrial IO.
+ *
+ * Copyright (C) 2016 BayLibre SAS
+ *
+ * Author:
+ *   Bartosz Golaszewski <bgolaszewski-rdvid1DuHRBpdZSXdFL2CA@public.gmane.org>
+ *
+ * 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/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regulator/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+struct iio_regulator_context {
+	struct regulator *regulator;
+};
+
+static ssize_t iio_regulator_enable_show(struct device *dev,
+					 struct device_attribute *attr,
+					 char *buf)
+{
+	struct iio_regulator_context *ctx = iio_priv(dev_to_iio_dev(dev));
+
+	return sprintf(buf, "%d\n", regulator_is_enabled(ctx->regulator));
+}
+
+static ssize_t iio_regulator_enable_store(struct device *dev,
+					  struct device_attribute *attr,
+					  const char *buf, size_t len)
+{
+	struct iio_regulator_context *ctx = iio_priv(dev_to_iio_dev(dev));
+	int ret, enabled;
+	bool val;
+
+	ret = strtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	enabled = regulator_is_enabled(ctx->regulator);
+	if ((val && enabled) || (!val && !enabled))
+		return -EPERM;
+
+	ret = val ? regulator_enable(ctx->regulator) :
+		    regulator_disable(ctx->regulator);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static IIO_DEVICE_ATTR(in_enable, 0644,
+		       iio_regulator_enable_show,
+		       iio_regulator_enable_store, 0);
+
+static struct attribute *iio_regulator_attributes[] = {
+	&iio_dev_attr_in_enable.dev_attr.attr,
+	NULL,
+};
+
+static const struct attribute_group iio_regulator_attribute_group = {
+	.attrs = iio_regulator_attributes,
+};
+
+static const struct iio_info iio_regulator_info = {
+	.driver_module = THIS_MODULE,
+	.attrs = &iio_regulator_attribute_group,
+};
+
+static int iio_regulator_probe(struct platform_device *pdev)
+{
+	struct iio_regulator_context *ctx;
+	struct iio_dev *iio_dev;
+	struct device *dev;
+
+	dev = &pdev->dev;
+
+	iio_dev = devm_iio_device_alloc(dev, sizeof(*ctx));
+	if (!iio_dev)
+		return -ENOMEM;
+
+	ctx = iio_priv(iio_dev);
+
+	ctx->regulator = devm_regulator_get(dev, "vcc");
+	if (IS_ERR(ctx->regulator)) {
+		dev_err(dev, "unable to get vcc regulator: %ld\n",
+			PTR_ERR(ctx->regulator));
+		return PTR_ERR(ctx->regulator);
+	}
+
+	iio_dev->dev.parent = dev;
+	iio_dev->dev.of_node = dev->of_node;
+	iio_dev->name = dev->driver->name;
+	iio_dev->info = &iio_regulator_info;
+
+	return devm_iio_device_register(dev, iio_dev);
+}
+
+static const struct of_device_id iio_regulator_of_match[] = {
+	{ .compatible = "iio-regulator", },
+	{ },
+};
+
+static struct platform_driver iio_regulator_platform_driver = {
+	.probe = iio_regulator_probe,
+	.driver = {
+		.name = "iio-regulator",
+		.of_match_table = iio_regulator_of_match,
+	},
+};
+module_platform_driver(iio_regulator_platform_driver);
+
+MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>");
+MODULE_DESCRIPTION("Regulator driver for iio");
+MODULE_LICENSE("GPL v2");
-- 
2.9.3

             reply	other threads:[~2016-11-29 15:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-29 15:22 Bartosz Golaszewski [this message]
     [not found] ` <1480432969-20913-1-git-send-email-bgolaszewski-rdvid1DuHRBWk0Htik3J/w@public.gmane.org>
2016-11-29 15:30   ` [PATCH] iio: misc: add a generic regulator driver Lars-Peter Clausen
     [not found]     ` <44cce3d5-f65e-1a35-20a4-5eb9fda42312-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
2016-11-29 15:35       ` Bartosz Golaszewski
     [not found]         ` <CAMpxmJWDWk-mXUHHGEKm-VVwzqEGLDQjc274Xq+Qwc2eN=YbsQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-11-30 10:10           ` Lars-Peter Clausen
     [not found]             ` <d4c7f1ca-49d4-7abe-bfc9-e1728f62a9fb-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
2016-12-01 12:07               ` Bartosz Golaszewski
2016-12-03  9:11               ` Jonathan Cameron
     [not found]                 ` <bf3dde7e-cee8-c54c-db93-447ffa063116-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2016-12-06 11:12                   ` Bartosz Golaszewski
     [not found]                     ` <CAMpxmJVJ+y9tRbRBsVg+i0EJO_6EFuvrypwC=ETnBWbMnxRFyg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-12-10 18:17                       ` Jonathan Cameron
2016-12-11 22:23                         ` Bartosz Golaszewski
2016-12-12 17:15                     ` Lars-Peter Clausen
     [not found]                       ` <c670d597-46b6-235f-545f-7136a3abff7f-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
2016-12-13 14:28                         ` Bartosz Golaszewski
     [not found]                           ` <CAMpxmJVpj57s-m_FS58J+QWvu-MnVmaZP-6ECnYHeRVSCfd-Fw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-12-28 13:00                             ` Linus Walleij
2016-12-23 10:00                       ` Geert Uytterhoeven
     [not found]                         ` <CAMuHMdX3u4cb2Yq2+VV_eDUaw2EqiOjN2TorW+F5-39Mm=mXmQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-12-23 11:35                           ` Lars-Peter Clausen
     [not found]                             ` <9609b56b-194c-9899-1142-ff2ee285c6bd-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
2016-12-23 12:56                               ` Geert Uytterhoeven
     [not found]                                 ` <CAMuHMdXnzDUYcr8qiUTwE2peBdjwuVWPRUkms2J8AHFaQ=SHHQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-12-24 10:43                                   ` Jonathan Cameron
2017-01-05 12:00                                   ` Mark Brown
2017-01-09 10:49                                     ` Linus Walleij
2016-12-28 13:08                               ` Linus Walleij

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=1480432969-20913-1-git-send-email-bgolaszewski@baylibre.com \
    --to=bgolaszewski-rdvid1duhrbwk0htik3j/w@public.gmane.org \
    --cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=khilman-rdvid1DuHRBWk0Htik3J/w@public.gmane.org \
    --cc=knaack.h-Mmb7MZpHnFY@public.gmane.org \
    --cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
    --cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=mark.rutland-5wv7dgnIgG8@public.gmane.org \
    --cc=narmstrong-rdvid1DuHRBWk0Htik3J/w@public.gmane.org \
    --cc=pmeerw-jW+XmwGofnusTnJN9+BGXg@public.gmane.org \
    --cc=ptitiano-rdvid1DuHRBWk0Htik3J/w@public.gmane.org \
    --cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    /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).