blob: 02577ec54c6b1ed650afa196a49dea3373569787 [file] [log] [blame]
Jonathan Cameron930bae82009-08-18 18:06:31 +01001/*
Jonathan Camerond4fd73b2013-06-02 20:00:00 +01002 * Industrial I/O - generic interrupt based trigger support
Jonathan Cameron930bae82009-08-18 18:06:31 +01003 *
Jonathan Camerond4fd73b2013-06-02 20:00:00 +01004 * Copyright (c) 2008-2013 Jonathan Cameron
Jonathan Cameron930bae82009-08-18 18:06:31 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
Jonathan Cameron930bae82009-08-18 18:06:31 +01009 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Jonathan Cameron930bae82009-08-18 18:06:31 +010016
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/iio.h>
18#include <linux/iio/trigger.h>
Jonathan Cameron930bae82009-08-18 18:06:31 +010019
Jonathan Cameron930bae82009-08-18 18:06:31 +010020
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010021struct iio_interrupt_trigger_info {
Michael Hennericha05d7ce2010-04-26 10:36:36 +020022 unsigned int irq;
Jonathan Cameron930bae82009-08-18 18:06:31 +010023};
Jonathan Cameron930bae82009-08-18 18:06:31 +010024
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010025static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private)
Jonathan Cameron930bae82009-08-18 18:06:31 +010026{
Jonathan Cameron7b2c33b2010-07-11 16:39:11 +010027 /* Timestamp not currently provided */
28 iio_trigger_poll(private, 0);
Jonathan Cameron930bae82009-08-18 18:06:31 +010029 return IRQ_HANDLED;
30}
31
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010032static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
Jonathan Camerond29f73d2011-08-12 17:08:38 +010033 .owner = THIS_MODULE,
34};
35
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010036static int iio_interrupt_trigger_probe(struct platform_device *pdev)
Jonathan Cameron930bae82009-08-18 18:06:31 +010037{
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010038 struct iio_interrupt_trigger_info *trig_info;
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010039 struct iio_trigger *trig;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020040 unsigned long irqflags;
41 struct resource *irq_res;
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010042 int irq, ret = 0;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020043
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010044 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Michael Hennericha05d7ce2010-04-26 10:36:36 +020045
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010046 if (irq_res == NULL)
47 return -ENODEV;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020048
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010049 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020050
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010051 irq = irq_res->start;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020052
Jonathan Cameron78f304d2013-06-06 19:27:00 +010053 trig = iio_trigger_alloc("irqtrig%d", irq);
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010054 if (!trig) {
55 ret = -ENOMEM;
56 goto error_ret;
57 }
Michael Hennericha05d7ce2010-04-26 10:36:36 +020058
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010059 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
60 if (!trig_info) {
61 ret = -ENOMEM;
62 goto error_put_trigger;
63 }
64 iio_trigger_set_drvdata(trig, trig_info);
65 trig_info->irq = irq;
66 trig->ops = &iio_interrupt_trigger_ops;
67 ret = request_irq(irq, iio_interrupt_trigger_poll,
68 irqflags, trig->name, trig);
69 if (ret) {
70 dev_err(&pdev->dev,
71 "request IRQ-%d failed", irq);
72 goto error_free_trig_info;
73 }
Michael Hennericha05d7ce2010-04-26 10:36:36 +020074
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010075 ret = iio_trigger_register(trig);
76 if (ret)
77 goto error_release_irq;
78 platform_set_drvdata(pdev, trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +010079
Jonathan Cameron930bae82009-08-18 18:06:31 +010080 return 0;
81
82/* First clean up the partly allocated trigger */
83error_release_irq:
84 free_irq(irq, trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +010085error_free_trig_info:
86 kfree(trig_info);
87error_put_trigger:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +020088 iio_trigger_put(trig);
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010089error_ret:
Jonathan Cameron930bae82009-08-18 18:06:31 +010090 return ret;
91}
92
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010093static int iio_interrupt_trigger_remove(struct platform_device *pdev)
Jonathan Cameron930bae82009-08-18 18:06:31 +010094{
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010095 struct iio_trigger *trig;
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010096 struct iio_interrupt_trigger_info *trig_info;
Jonathan Cameron930bae82009-08-18 18:06:31 +010097
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010098 trig = platform_get_drvdata(pdev);
99 trig_info = iio_trigger_get_drvdata(trig);
100 iio_trigger_unregister(trig);
101 free_irq(trig_info->irq, trig);
102 kfree(trig_info);
103 iio_trigger_put(trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +0100104
105 return 0;
106}
107
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100108static struct platform_driver iio_interrupt_trigger_driver = {
109 .probe = iio_interrupt_trigger_probe,
110 .remove = iio_interrupt_trigger_remove,
Jonathan Cameron930bae82009-08-18 18:06:31 +0100111 .driver = {
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100112 .name = "iio_interrupt_trigger",
Jonathan Cameron930bae82009-08-18 18:06:31 +0100113 .owner = THIS_MODULE,
114 },
115};
116
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100117module_platform_driver(iio_interrupt_trigger_driver);
Jonathan Cameron930bae82009-08-18 18:06:31 +0100118
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100119MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100120MODULE_DESCRIPTION("Interrupt trigger for the iio subsystem");
Jonathan Cameron930bae82009-08-18 18:06:31 +0100121MODULE_LICENSE("GPL v2");