Skip to content

Commit

Permalink
gpio: Timestamp events in hardirq handler
Browse files Browse the repository at this point in the history
Add a hardirq handler to the GPIO userspace event loop, making
sure to pick up the timestamp there, as close as possible in time
relative to the actual event causing the interrupt.

Tested with a simple pushbutton GPIO on ux500 and seems to work
fine.

Cc: Bartosz Golaszewski <brgl@bgdev.pl>
Reviewed-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Reported-by: Felipe Balbi <felipe.balbi@linux.intel.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
  • Loading branch information
Linus Walleij committed Jan 23, 2018
1 parent 24bd3ef commit d58f2bf
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ static int linehandle_create(struct gpio_device *gdev, void __user *ip)
* @events: KFIFO for the GPIO events
* @read_lock: mutex lock to protect reads from colliding with adding
* new events to the FIFO
* @timestamp: cache for the timestamp storing it between hardirq
* and IRQ thread, used to bring the timestamp close to the actual
* event
*/
struct lineevent_state {
struct gpio_device *gdev;
Expand All @@ -610,6 +613,7 @@ struct lineevent_state {
wait_queue_head_t wait;
DECLARE_KFIFO(events, struct gpioevent_data, 16);
struct mutex read_lock;
u64 timestamp;
};

#define GPIOEVENT_REQUEST_VALID_FLAGS \
Expand Down Expand Up @@ -747,7 +751,7 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p)
/* Do not leak kernel stack to userspace */
memset(&ge, 0, sizeof(ge));

ge.timestamp = ktime_get_real_ns();
ge.timestamp = le->timestamp;
level = gpiod_get_value_cansleep(le->desc);

if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
Expand Down Expand Up @@ -775,6 +779,19 @@ static irqreturn_t lineevent_irq_thread(int irq, void *p)
return IRQ_HANDLED;
}

static irqreturn_t lineevent_irq_handler(int irq, void *p)
{
struct lineevent_state *le = p;

/*
* Just store the timestamp in hardirq context so we get it as
* close in time as possible to the actual event.
*/
le->timestamp = ktime_get_real_ns();

return IRQ_WAKE_THREAD;
}

static int lineevent_create(struct gpio_device *gdev, void __user *ip)
{
struct gpioevent_request eventreq;
Expand Down Expand Up @@ -867,7 +884,7 @@ static int lineevent_create(struct gpio_device *gdev, void __user *ip)

/* Request a thread to read the events */
ret = request_threaded_irq(le->irq,
NULL,
lineevent_irq_handler,
lineevent_irq_thread,
irqflags,
le->label,
Expand Down

0 comments on commit d58f2bf

Please sign in to comment.