Linux IIO development
 help / color / mirror / Atom feed
From: Marc Titinger <mtitinger@baylibre.com>
To: jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net
Cc: daniel.baluta@intel.com, linux-kernel@vger.kernel.org,
	linux-iio@vger.kernel.org, Marc Titinger <mtitinger@baylibre.com>
Subject: [RFC 9/9] iio: (RFC) illustrate creation/destruction of hrtimer trigger upon buffer enable
Date: Wed, 18 Nov 2015 15:38:35 +0100	[thread overview]
Message-ID: <1447857515-23935-10-git-send-email-mtitinger@baylibre.com> (raw)
In-Reply-To: <1447857515-23935-1-git-send-email-mtitinger@baylibre.com>

This also raises the question of how to programmatically set the period of
the hrtimer from the owner driver, I had to locally copy iio_hrtimer_info
Maybe this should go to linux/iio/hrtimer_trigger.h ?

Signed-off-by: Marc Titinger <mtitinger@baylibre.com>
---
 drivers/iio/adc/ina2xx-iio.c | 74 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 73 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ina2xx-iio.c b/drivers/iio/adc/ina2xx-iio.c
index e47f30d..5f61296b 100644
--- a/drivers/iio/adc/ina2xx-iio.c
+++ b/drivers/iio/adc/ina2xx-iio.c
@@ -19,6 +19,7 @@
  *
  * Configurable 7-bit I2C slave address from 0x40 to 0x4F
  */
+
 #include <linux/module.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -32,6 +33,10 @@
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>
 
+#include <linux/iio/trigger.h>
+#include <linux/iio/sw_trigger.h>
+
+
 /*
  * INA2XX registers definition
  */
@@ -96,6 +101,7 @@ struct ina2xx_config {
 
 struct ina2xx_chip_info {
 	const struct ina2xx_config *config;
+	struct iio_sw_trigger *swtrig;
 	struct mutex state_lock;
 	long rshunt;
 	int avg;
@@ -283,6 +289,11 @@ static int ina2xx_write_raw(struct iio_dev *indio_dev,
 
 	mutex_lock(&chip->state_lock);
 
+	if (iio_buffer_enabled(indio_dev)) {
+		ret = -EBUSY;
+		goto _err;
+	}
+
 	ret = regmap_read(chip->regmap, INA2XX_CONFIG, &config);
 	if (ret < 0)
 		goto _err;
@@ -316,6 +327,13 @@ _err:
 	return ret;
 }
 
+/* FIXME */
+struct iio_hrtimer_info {
+        struct iio_sw_trigger swt;
+        struct hrtimer timer;
+        unsigned long sampling_frequency;
+        ktime_t period;
+};
 
 static ssize_t ina2xx_averaging_steps_show(struct device *dev,
 					   struct device_attribute *attr,
@@ -387,6 +405,7 @@ static int ina2xx_debug_reg(struct iio_dev *indio_dev,
 	return regmap_read(chip->regmap, reg, readval);
 }
 
+
 static s64 prev_ns;
 
 static irqreturn_t ina2xx_trigger_handler(int irq, void *p)
@@ -478,6 +497,58 @@ static int ina2xx_init(struct ina2xx_chip_info *chip, unsigned int config)
 	return ina2xx_calibrate(chip);
 }
 
+
+static int ina2xx_trigger_create(struct iio_dev *indio_dev)
+{
+	struct iio_sw_trigger *swtrig;
+	struct iio_hrtimer_info *info;
+	struct ina2xx_chip_info *chip = iio_priv(indio_dev);
+
+	swtrig = iio_sw_trigger_create("hrtimer", indio_dev->name);
+	if (IS_ERR(swtrig))
+		return -EINVAL;
+
+	info = iio_trigger_get_drvdata(swtrig->trigger);
+
+	mutex_lock(&chip->state_lock);
+
+	info->sampling_frequency = chip->freq;
+	info->period = ktime_set(0, NSEC_PER_SEC / chip->freq);
+
+	chip->swtrig = swtrig;
+	indio_dev->trig = swtrig->trigger;
+
+	mutex_unlock(&chip->state_lock);
+
+	iio_trigger_get(indio_dev->trig);
+
+	return 0;
+}
+
+int ina2xx_trigger_destroy(struct iio_dev *indio_dev)
+{
+	struct ina2xx_chip_info *chip = iio_priv(indio_dev);
+
+	mutex_lock(&chip->state_lock);
+
+	iio_trigger_put(indio_dev->trig);
+	iio_sw_trigger_destroy(chip->swtrig);
+
+	indio_dev->trig = NULL;
+
+	mutex_unlock(&chip->state_lock);
+
+	return 0;
+}
+
+
+static const struct iio_buffer_setup_ops ina2xx_buffer_setup_ops = {
+	.enable_trigger = &ina2xx_trigger_create,
+	.postenable = &iio_triggered_buffer_postenable,
+	.predisable = &iio_triggered_buffer_predisable,
+	.postdisable = &ina2xx_trigger_destroy,
+};
+
 static int ina2xx_probe(struct i2c_client *client,
 			const struct i2c_device_id *id)
 {
@@ -547,7 +618,8 @@ static int ina2xx_probe(struct i2c_client *client,
 	}
 
 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
-					&ina2xx_trigger_handler, NULL);
+					&ina2xx_trigger_handler,
+					&ina2xx_buffer_setup_ops);
 	if (ret)
 		return ret;
 
-- 
1.9.1


      parent reply	other threads:[~2015-11-18 14:39 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-18 14:38 [RFC 0/9] spawn hrtimer trigger from client driver upon enabling buffer Marc Titinger
2015-11-18 14:38 ` [RFC 1/9] configfs: Allow dynamic group creation Marc Titinger
2015-11-18 14:38 ` [RFC 2/9] iio: core: Introduce IIO configfs support Marc Titinger
2015-11-18 14:38 ` [RFC 3/9] iio: core: Introduce IIO software triggers Marc Titinger
2015-11-18 14:38 ` [RFC 4/9] iio: trigger: Introduce IIO hrtimer based trigger Marc Titinger
2015-11-18 14:38 ` [RFC 5/9] iio: Documentation: Add IIO configfs documentation Marc Titinger
2015-11-18 15:38   ` Crt Mori
2015-11-18 16:06     ` Marc Titinger
2015-11-18 16:15       ` Daniel Baluta
2015-11-18 17:32         ` Jonathan Cameron
2015-11-18 14:38 ` [RFC 6/9] iio: ina2xx: add direct IO support for TI INA2xx Power Monitors Marc Titinger
2015-11-21 18:13   ` Jonathan Cameron
2015-11-23 16:15     ` Marc Titinger
2015-11-29 15:17       ` Jonathan Cameron
2015-11-18 14:38 ` [RFC 7/9] iio: ina2xx: add triggered buffer Marc Titinger
2015-11-18 14:38 ` [RFC 8/9] iio: buffer: allow for last-second trigger spawning from device driver Marc Titinger
2015-11-18 18:55   ` Jonathan Cameron
2015-11-19  9:15     ` Marc Titinger
2015-11-21 18:18       ` Jonathan Cameron
2015-11-18 14:38 ` Marc Titinger [this message]

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=1447857515-23935-10-git-send-email-mtitinger@baylibre.com \
    --to=mtitinger@baylibre.com \
    --cc=daniel.baluta@intel.com \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /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