Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 1 | /* |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 2 | * Industrial I/O - generic interrupt based trigger support |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 3 | * |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008-2013 Jonathan Cameron |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 5 | * |
| 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 Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 16 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 17 | #include <linux/iio/iio.h> |
| 18 | #include <linux/iio/trigger.h> |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 19 | |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 21 | struct iio_interrupt_trigger_info { |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 22 | unsigned int irq; |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 23 | }; |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 24 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 25 | static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private) |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 26 | { |
Peter Meerwald | 398fd22 | 2014-12-06 06:46:00 +0000 | [diff] [blame] | 27 | iio_trigger_poll(private); |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 28 | return IRQ_HANDLED; |
| 29 | } |
| 30 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 31 | static const struct iio_trigger_ops iio_interrupt_trigger_ops = { |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 32 | .owner = THIS_MODULE, |
| 33 | }; |
| 34 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 35 | static int iio_interrupt_trigger_probe(struct platform_device *pdev) |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 36 | { |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 37 | struct iio_interrupt_trigger_info *trig_info; |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 38 | struct iio_trigger *trig; |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 39 | unsigned long irqflags; |
| 40 | struct resource *irq_res; |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 41 | int irq, ret = 0; |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 42 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 43 | irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 44 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 45 | if (irq_res == NULL) |
| 46 | return -ENODEV; |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 47 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 48 | irqflags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED; |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 49 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 50 | irq = irq_res->start; |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 51 | |
Jonathan Cameron | 78f304d | 2013-06-06 19:27:00 +0100 | [diff] [blame] | 52 | trig = iio_trigger_alloc("irqtrig%d", irq); |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 53 | if (!trig) { |
| 54 | ret = -ENOMEM; |
| 55 | goto error_ret; |
| 56 | } |
Michael Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 57 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 58 | 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 Hennerich | a05d7ce | 2010-04-26 10:36:36 +0200 | [diff] [blame] | 73 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 74 | ret = iio_trigger_register(trig); |
| 75 | if (ret) |
| 76 | goto error_release_irq; |
| 77 | platform_set_drvdata(pdev, trig); |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 78 | |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 79 | return 0; |
| 80 | |
| 81 | /* First clean up the partly allocated trigger */ |
| 82 | error_release_irq: |
| 83 | free_irq(irq, trig); |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 84 | error_free_trig_info: |
| 85 | kfree(trig_info); |
| 86 | error_put_trigger: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 87 | iio_trigger_put(trig); |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 88 | error_ret: |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 89 | return ret; |
| 90 | } |
| 91 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 92 | static int iio_interrupt_trigger_remove(struct platform_device *pdev) |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 93 | { |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 94 | struct iio_trigger *trig; |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 95 | struct iio_interrupt_trigger_info *trig_info; |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 96 | |
Jonathan Cameron | 3c1e02d | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 97 | 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 Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 107 | static struct platform_driver iio_interrupt_trigger_driver = { |
| 108 | .probe = iio_interrupt_trigger_probe, |
| 109 | .remove = iio_interrupt_trigger_remove, |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 110 | .driver = { |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 111 | .name = "iio_interrupt_trigger", |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 112 | }, |
| 113 | }; |
| 114 | |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 115 | module_platform_driver(iio_interrupt_trigger_driver); |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 116 | |
Jonathan Cameron | 0f8c962 | 2012-09-02 21:34:59 +0100 | [diff] [blame] | 117 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | d4fd73b | 2013-06-02 20:00:00 +0100 | [diff] [blame] | 118 | MODULE_DESCRIPTION("Interrupt trigger for the iio subsystem"); |
Jonathan Cameron | 930bae8 | 2009-08-18 18:06:31 +0100 | [diff] [blame] | 119 | MODULE_LICENSE("GPL v2"); |