blob: 572bc6f02ca82987a9f3945bc073e4ab835effe9 [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{
Peter Meerwald398fd222014-12-06 06:46:00 +000027 iio_trigger_poll(private);
Jonathan Cameron930bae82009-08-18 18:06:31 +010028 return IRQ_HANDLED;
29}
30
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010031static const struct iio_trigger_ops iio_interrupt_trigger_ops = {
Jonathan Camerond29f73d2011-08-12 17:08:38 +010032 .owner = THIS_MODULE,
33};
34
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010035static int iio_interrupt_trigger_probe(struct platform_device *pdev)
Jonathan Cameron930bae82009-08-18 18:06:31 +010036{
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010037 struct iio_interrupt_trigger_info *trig_info;
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010038 struct iio_trigger *trig;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020039 unsigned long irqflags;
40 struct resource *irq_res;
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010041 int irq, ret = 0;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020042
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010043 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Michael Hennericha05d7ce2010-04-26 10:36:36 +020044
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010045 if (irq_res == NULL)
46 return -ENODEV;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020047
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010048 irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020049
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010050 irq = irq_res->start;
Michael Hennericha05d7ce2010-04-26 10:36:36 +020051
Jonathan Cameron78f304d2013-06-06 19:27:00 +010052 trig = iio_trigger_alloc("irqtrig%d", irq);
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010053 if (!trig) {
54 ret = -ENOMEM;
55 goto error_ret;
56 }
Michael Hennericha05d7ce2010-04-26 10:36:36 +020057
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010058 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
59 if (!trig_info) {
60 ret = -ENOMEM;
61 goto error_put_trigger;
62 }
63 iio_trigger_set_drvdata(trig, trig_info);
64 trig_info->irq = irq;
65 trig->ops = &iio_interrupt_trigger_ops;
66 ret = request_irq(irq, iio_interrupt_trigger_poll,
67 irqflags, trig->name, trig);
68 if (ret) {
69 dev_err(&pdev->dev,
70 "request IRQ-%d failed", irq);
71 goto error_free_trig_info;
72 }
Michael Hennericha05d7ce2010-04-26 10:36:36 +020073
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010074 ret = iio_trigger_register(trig);
75 if (ret)
76 goto error_release_irq;
77 platform_set_drvdata(pdev, trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +010078
Jonathan Cameron930bae82009-08-18 18:06:31 +010079 return 0;
80
81/* First clean up the partly allocated trigger */
82error_release_irq:
83 free_irq(irq, trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +010084error_free_trig_info:
85 kfree(trig_info);
86error_put_trigger:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +020087 iio_trigger_put(trig);
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010088error_ret:
Jonathan Cameron930bae82009-08-18 18:06:31 +010089 return ret;
90}
91
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010092static int iio_interrupt_trigger_remove(struct platform_device *pdev)
Jonathan Cameron930bae82009-08-18 18:06:31 +010093{
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010094 struct iio_trigger *trig;
Jonathan Camerond4fd73b2013-06-02 20:00:00 +010095 struct iio_interrupt_trigger_info *trig_info;
Jonathan Cameron930bae82009-08-18 18:06:31 +010096
Jonathan Cameron3c1e02d2013-06-02 20:00:00 +010097 trig = platform_get_drvdata(pdev);
98 trig_info = iio_trigger_get_drvdata(trig);
99 iio_trigger_unregister(trig);
100 free_irq(trig_info->irq, trig);
101 kfree(trig_info);
102 iio_trigger_put(trig);
Jonathan Cameron930bae82009-08-18 18:06:31 +0100103
104 return 0;
105}
106
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100107static struct platform_driver iio_interrupt_trigger_driver = {
108 .probe = iio_interrupt_trigger_probe,
109 .remove = iio_interrupt_trigger_remove,
Jonathan Cameron930bae82009-08-18 18:06:31 +0100110 .driver = {
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100111 .name = "iio_interrupt_trigger",
Jonathan Cameron930bae82009-08-18 18:06:31 +0100112 },
113};
114
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100115module_platform_driver(iio_interrupt_trigger_driver);
Jonathan Cameron930bae82009-08-18 18:06:31 +0100116
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100117MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Camerond4fd73b2013-06-02 20:00:00 +0100118MODULE_DESCRIPTION("Interrupt trigger for the iio subsystem");
Jonathan Cameron930bae82009-08-18 18:06:31 +0100119MODULE_LICENSE("GPL v2");