2019-06-04 16:11:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2020-07-16 21:59:11 +08:00
|
|
|
/*
|
2011-10-14 23:34:14 +08:00
|
|
|
* Copyright (c) 2011 Jonathan Cameron
|
|
|
|
*
|
|
|
|
* Event handling elements of industrial I/O reference driver.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
|
2012-04-25 22:54:58 +08:00
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/iio/sysfs.h>
|
|
|
|
#include <linux/iio/events.h>
|
2011-10-14 23:34:14 +08:00
|
|
|
#include "iio_simple_dummy.h"
|
|
|
|
|
|
|
|
/* Evgen 'fakes' interrupt events for this example */
|
|
|
|
#include "iio_dummy_evgen.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_read_event_config() - is event enabled?
|
|
|
|
* @indio_dev: the device instance data
|
2013-10-07 22:11:00 +08:00
|
|
|
* @chan: channel for the event whose state is being queried
|
|
|
|
* @type: type of the event whose state is being queried
|
|
|
|
* @dir: direction of the vent whose state is being queried
|
2011-10-14 23:34:14 +08:00
|
|
|
*
|
|
|
|
* This function would normally query the relevant registers or a cache to
|
|
|
|
* discover if the event generation is enabled on the device.
|
|
|
|
*/
|
|
|
|
int iio_simple_dummy_read_event_config(struct iio_dev *indio_dev,
|
2013-10-07 22:11:00 +08:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir)
|
2011-10-14 23:34:14 +08:00
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
return st->event_en;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_write_event_config() - set whether event is enabled
|
|
|
|
* @indio_dev: the device instance data
|
2013-10-07 22:11:00 +08:00
|
|
|
* @chan: channel for the event whose state is being set
|
|
|
|
* @type: type of the event whose state is being set
|
|
|
|
* @dir: direction of the vent whose state is being set
|
2011-10-14 23:34:14 +08:00
|
|
|
* @state: whether to enable or disable the device.
|
|
|
|
*
|
|
|
|
* This function would normally set the relevant registers on the devices
|
|
|
|
* so that it generates the specified event. Here it just sets up a cached
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
|
2013-10-07 22:11:00 +08:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
2011-10-14 23:34:14 +08:00
|
|
|
int state)
|
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deliberately over the top code splitting to illustrate
|
|
|
|
* how this is done when multiple events exist.
|
|
|
|
*/
|
2013-10-07 22:11:00 +08:00
|
|
|
switch (chan->type) {
|
2011-10-14 23:34:14 +08:00
|
|
|
case IIO_VOLTAGE:
|
2013-10-07 22:11:00 +08:00
|
|
|
switch (type) {
|
2011-10-14 23:34:14 +08:00
|
|
|
case IIO_EV_TYPE_THRESH:
|
2013-10-07 22:11:00 +08:00
|
|
|
if (dir == IIO_EV_DIR_RISING)
|
2011-10-14 23:34:14 +08:00
|
|
|
st->event_en = state;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
2017-09-29 21:24:05 +08:00
|
|
|
break;
|
2014-11-10 20:45:34 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IIO_ACTIVITY:
|
|
|
|
switch (type) {
|
|
|
|
case IIO_EV_TYPE_THRESH:
|
|
|
|
st->event_en = state;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-04-09 03:24:43 +08:00
|
|
|
break;
|
2014-11-10 20:45:34 +08:00
|
|
|
case IIO_STEPS:
|
|
|
|
switch (type) {
|
2015-01-12 03:10:12 +08:00
|
|
|
case IIO_EV_TYPE_CHANGE:
|
2014-11-10 20:45:34 +08:00
|
|
|
st->event_en = state;
|
2011-10-14 23:34:14 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-04-09 03:24:43 +08:00
|
|
|
break;
|
2011-10-14 23:34:14 +08:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_read_event_value() - get value associated with event
|
|
|
|
* @indio_dev: device instance specific data
|
2013-10-07 22:11:00 +08:00
|
|
|
* @chan: channel for the event whose value is being read
|
|
|
|
* @type: type of the event whose value is being read
|
|
|
|
* @dir: direction of the vent whose value is being read
|
|
|
|
* @info: info type of the event whose value is being read
|
2011-10-14 23:34:14 +08:00
|
|
|
* @val: value for the event code.
|
2020-07-16 21:59:11 +08:00
|
|
|
* @val2: unused
|
2011-10-14 23:34:14 +08:00
|
|
|
*
|
|
|
|
* Many devices provide a large set of events of which only a subset may
|
|
|
|
* be enabled at a time, with value registers whose meaning changes depending
|
|
|
|
* on the event enabled. This often means that the driver must cache the values
|
|
|
|
* associated with each possible events so that the right value is in place when
|
|
|
|
* the enabled event is changed.
|
|
|
|
*/
|
|
|
|
int iio_simple_dummy_read_event_value(struct iio_dev *indio_dev,
|
2013-10-07 22:11:00 +08:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
2015-08-05 20:15:12 +08:00
|
|
|
enum iio_event_info info,
|
2013-10-07 22:11:00 +08:00
|
|
|
int *val, int *val2)
|
2011-10-14 23:34:14 +08:00
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
*val = st->event_val;
|
|
|
|
|
2013-10-07 22:11:00 +08:00
|
|
|
return IIO_VAL_INT;
|
2011-10-14 23:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_write_event_value() - set value associate with event
|
|
|
|
* @indio_dev: device instance specific data
|
2013-10-07 22:11:00 +08:00
|
|
|
* @chan: channel for the event whose value is being set
|
|
|
|
* @type: type of the event whose value is being set
|
|
|
|
* @dir: direction of the vent whose value is being set
|
|
|
|
* @info: info type of the event whose value is being set
|
2011-10-14 23:34:14 +08:00
|
|
|
* @val: the value to be set.
|
2020-07-16 21:59:11 +08:00
|
|
|
* @val2: unused
|
2011-10-14 23:34:14 +08:00
|
|
|
*/
|
|
|
|
int iio_simple_dummy_write_event_value(struct iio_dev *indio_dev,
|
2013-10-07 22:11:00 +08:00
|
|
|
const struct iio_chan_spec *chan,
|
|
|
|
enum iio_event_type type,
|
|
|
|
enum iio_event_direction dir,
|
2015-08-05 20:15:12 +08:00
|
|
|
enum iio_event_info info,
|
2013-10-07 22:11:00 +08:00
|
|
|
int val, int val2)
|
2011-10-14 23:34:14 +08:00
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
st->event_val = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:59:30 +08:00
|
|
|
static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = private;
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
2016-03-10 02:05:49 +08:00
|
|
|
st->event_timestamp = iio_get_time_ns(indio_dev);
|
2015-10-28 02:40:56 +08:00
|
|
|
return IRQ_WAKE_THREAD;
|
2015-09-11 21:59:30 +08:00
|
|
|
}
|
|
|
|
|
2011-10-14 23:34:14 +08:00
|
|
|
/**
|
|
|
|
* iio_simple_dummy_event_handler() - identify and pass on event
|
|
|
|
* @irq: irq of event line
|
|
|
|
* @private: pointer to device instance state.
|
|
|
|
*
|
|
|
|
* This handler is responsible for querying the device to find out what
|
2012-05-09 02:18:17 +08:00
|
|
|
* event occurred and for then pushing that event towards userspace.
|
2011-10-14 23:34:14 +08:00
|
|
|
* Here only one event occurs so we push that directly on with locally
|
|
|
|
* grabbed timestamp.
|
|
|
|
*/
|
|
|
|
static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
|
|
|
|
{
|
|
|
|
struct iio_dev *indio_dev = private;
|
2014-11-10 20:45:29 +08:00
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
dev_dbg(&indio_dev->dev, "id %x event %x\n",
|
|
|
|
st->regs->reg_id, st->regs->reg_data);
|
|
|
|
|
|
|
|
switch (st->regs->reg_data) {
|
|
|
|
case 0:
|
|
|
|
iio_push_event(indio_dev,
|
|
|
|
IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0,
|
|
|
|
IIO_EV_DIR_RISING,
|
|
|
|
IIO_EV_TYPE_THRESH, 0, 0, 0),
|
2015-09-11 21:59:30 +08:00
|
|
|
st->event_timestamp);
|
2014-11-10 20:45:29 +08:00
|
|
|
break;
|
2014-11-10 20:45:34 +08:00
|
|
|
case 1:
|
|
|
|
if (st->activity_running > st->event_val)
|
|
|
|
iio_push_event(indio_dev,
|
|
|
|
IIO_EVENT_CODE(IIO_ACTIVITY, 0,
|
|
|
|
IIO_MOD_RUNNING,
|
|
|
|
IIO_EV_DIR_RISING,
|
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
0, 0, 0),
|
2015-09-11 21:59:30 +08:00
|
|
|
st->event_timestamp);
|
2014-11-10 20:45:34 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (st->activity_walking < st->event_val)
|
|
|
|
iio_push_event(indio_dev,
|
|
|
|
IIO_EVENT_CODE(IIO_ACTIVITY, 0,
|
|
|
|
IIO_MOD_WALKING,
|
|
|
|
IIO_EV_DIR_FALLING,
|
|
|
|
IIO_EV_TYPE_THRESH,
|
|
|
|
0, 0, 0),
|
2015-09-11 21:59:30 +08:00
|
|
|
st->event_timestamp);
|
2014-11-10 20:45:34 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
iio_push_event(indio_dev,
|
|
|
|
IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
|
|
|
|
IIO_EV_DIR_NONE,
|
2015-01-12 03:10:12 +08:00
|
|
|
IIO_EV_TYPE_CHANGE, 0, 0, 0),
|
2015-09-11 21:59:30 +08:00
|
|
|
st->event_timestamp);
|
2014-11-10 20:45:34 +08:00
|
|
|
break;
|
2014-11-10 20:45:29 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-09-19 05:55:06 +08:00
|
|
|
|
2011-10-14 23:34:14 +08:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_events_register() - setup interrupt handling for events
|
|
|
|
* @indio_dev: device instance data
|
|
|
|
*
|
|
|
|
* This function requests the threaded interrupt to handle the events.
|
|
|
|
* Normally the irq is a hardware interrupt and the number comes
|
|
|
|
* from board configuration files. Here we get it from a companion
|
|
|
|
* module that fakes the interrupt for us. Note that module in
|
|
|
|
* no way forms part of this example. Just assume that events magically
|
|
|
|
* appear via the provided interrupt.
|
|
|
|
*/
|
|
|
|
int iio_simple_dummy_events_register(struct iio_dev *indio_dev)
|
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Fire up event source - normally not present */
|
|
|
|
st->event_irq = iio_dummy_evgen_get_irq();
|
|
|
|
if (st->event_irq < 0) {
|
|
|
|
ret = st->event_irq;
|
|
|
|
goto error_ret;
|
|
|
|
}
|
2014-11-10 20:45:29 +08:00
|
|
|
st->regs = iio_dummy_evgen_get_regs(st->event_irq);
|
|
|
|
|
2011-10-14 23:34:14 +08:00
|
|
|
ret = request_threaded_irq(st->event_irq,
|
2015-09-11 21:59:30 +08:00
|
|
|
&iio_simple_dummy_get_timestamp,
|
2011-10-14 23:34:14 +08:00
|
|
|
&iio_simple_dummy_event_handler,
|
|
|
|
IRQF_ONESHOT,
|
|
|
|
"iio_simple_event",
|
|
|
|
indio_dev);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error_free_evgen;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_free_evgen:
|
|
|
|
iio_dummy_evgen_release_irq(st->event_irq);
|
|
|
|
error_ret:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove
|
|
|
|
* @indio_dev: device instance data
|
|
|
|
*/
|
2015-05-30 16:20:16 +08:00
|
|
|
void iio_simple_dummy_events_unregister(struct iio_dev *indio_dev)
|
2011-10-14 23:34:14 +08:00
|
|
|
{
|
|
|
|
struct iio_dummy_state *st = iio_priv(indio_dev);
|
|
|
|
|
|
|
|
free_irq(st->event_irq, indio_dev);
|
|
|
|
/* Not part of normal driver */
|
|
|
|
iio_dummy_evgen_release_irq(st->event_irq);
|
|
|
|
}
|