linux/drivers/iio/common/st_sensors/st_sensors_trigger.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * STMicroelectronics sensors trigger library driver
   4 *
   5 * Copyright 2012-2013 STMicroelectronics Inc.
   6 *
   7 * Denis Ciocca <denis.ciocca@st.com>
   8 */
   9
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/iio/iio.h>
  13#include <linux/iio/trigger.h>
  14#include <linux/interrupt.h>
  15#include <linux/regmap.h>
  16#include <linux/iio/common/st_sensors.h>
  17#include "st_sensors_core.h"
  18
  19/**
  20 * st_sensors_new_samples_available() - check if more samples came in
  21 * @indio_dev: IIO device reference.
  22 * @sdata: Sensor data.
  23 *
  24 * returns:
  25 * false - no new samples available or read error
  26 * true - new samples available
  27 */
  28static bool st_sensors_new_samples_available(struct iio_dev *indio_dev,
  29                                             struct st_sensor_data *sdata)
  30{
  31        int ret, status;
  32
  33        /* How would I know if I can't check it? */
  34        if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr)
  35                return true;
  36
  37        /* No scan mask, no interrupt */
  38        if (!indio_dev->active_scan_mask)
  39                return false;
  40
  41        ret = regmap_read(sdata->regmap,
  42                          sdata->sensor_settings->drdy_irq.stat_drdy.addr,
  43                          &status);
  44        if (ret < 0) {
  45                dev_err(sdata->dev, "error checking samples available\n");
  46                return false;
  47        }
  48
  49        return !!(status & sdata->sensor_settings->drdy_irq.stat_drdy.mask);
  50}
  51
  52/**
  53 * st_sensors_irq_handler() - top half of the IRQ-based triggers
  54 * @irq: irq number
  55 * @p: private handler data
  56 */
  57static irqreturn_t st_sensors_irq_handler(int irq, void *p)
  58{
  59        struct iio_trigger *trig = p;
  60        struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  61        struct st_sensor_data *sdata = iio_priv(indio_dev);
  62
  63        /* Get the time stamp as close in time as possible */
  64        sdata->hw_timestamp = iio_get_time_ns(indio_dev);
  65        return IRQ_WAKE_THREAD;
  66}
  67
  68/**
  69 * st_sensors_irq_thread() - bottom half of the IRQ-based triggers
  70 * @irq: irq number
  71 * @p: private handler data
  72 */
  73static irqreturn_t st_sensors_irq_thread(int irq, void *p)
  74{
  75        struct iio_trigger *trig = p;
  76        struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  77        struct st_sensor_data *sdata = iio_priv(indio_dev);
  78
  79        /*
  80         * If this trigger is backed by a hardware interrupt and we have a
  81         * status register, check if this IRQ came from us. Notice that
  82         * we will process also if st_sensors_new_samples_available()
  83         * returns negative: if we can't check status, then poll
  84         * unconditionally.
  85         */
  86        if (sdata->hw_irq_trigger &&
  87            st_sensors_new_samples_available(indio_dev, sdata)) {
  88                iio_trigger_poll_chained(p);
  89        } else {
  90                dev_dbg(sdata->dev, "spurious IRQ\n");
  91                return IRQ_NONE;
  92        }
  93
  94        /*
  95         * If we have proper level IRQs the handler will be re-entered if
  96         * the line is still active, so return here and come back in through
  97         * the top half if need be.
  98         */
  99        if (!sdata->edge_irq)
 100                return IRQ_HANDLED;
 101
 102        /*
 103         * If we are using edge IRQs, new samples arrived while processing
 104         * the IRQ and those may be missed unless we pick them here, so poll
 105         * again. If the sensor delivery frequency is very high, this thread
 106         * turns into a polled loop handler.
 107         */
 108        while (sdata->hw_irq_trigger &&
 109               st_sensors_new_samples_available(indio_dev, sdata)) {
 110                dev_dbg(sdata->dev, "more samples came in during polling\n");
 111                sdata->hw_timestamp = iio_get_time_ns(indio_dev);
 112                iio_trigger_poll_chained(p);
 113        }
 114
 115        return IRQ_HANDLED;
 116}
 117
 118int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
 119                                const struct iio_trigger_ops *trigger_ops)
 120{
 121        struct st_sensor_data *sdata = iio_priv(indio_dev);
 122        unsigned long irq_trig;
 123        int err;
 124
 125        sdata->trig = iio_trigger_alloc(sdata->dev, "%s-trigger",
 126                                        indio_dev->name);
 127        if (sdata->trig == NULL) {
 128                dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n");
 129                return -ENOMEM;
 130        }
 131
 132        iio_trigger_set_drvdata(sdata->trig, indio_dev);
 133        sdata->trig->ops = trigger_ops;
 134
 135        irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq));
 136        /*
 137         * If the IRQ is triggered on falling edge, we need to mark the
 138         * interrupt as active low, if the hardware supports this.
 139         */
 140        switch(irq_trig) {
 141        case IRQF_TRIGGER_FALLING:
 142        case IRQF_TRIGGER_LOW:
 143                if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
 144                        dev_err(&indio_dev->dev,
 145                                "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n");
 146                        if (irq_trig == IRQF_TRIGGER_FALLING)
 147                                irq_trig = IRQF_TRIGGER_RISING;
 148                        if (irq_trig == IRQF_TRIGGER_LOW)
 149                                irq_trig = IRQF_TRIGGER_HIGH;
 150                } else {
 151                        /* Set up INT active low i.e. falling edge */
 152                        err = st_sensors_write_data_with_mask(indio_dev,
 153                                sdata->sensor_settings->drdy_irq.addr_ihl,
 154                                sdata->sensor_settings->drdy_irq.mask_ihl, 1);
 155                        if (err < 0)
 156                                goto iio_trigger_free;
 157                        dev_info(&indio_dev->dev,
 158                                 "interrupts on the falling edge or active low level\n");
 159                }
 160                break;
 161        case IRQF_TRIGGER_RISING:
 162                dev_info(&indio_dev->dev,
 163                         "interrupts on the rising edge\n");
 164                break;
 165        case IRQF_TRIGGER_HIGH:
 166                dev_info(&indio_dev->dev,
 167                         "interrupts active high level\n");
 168                break;
 169        default:
 170                /* This is the most preferred mode, if possible */
 171                dev_err(&indio_dev->dev,
 172                        "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig);
 173                irq_trig = IRQF_TRIGGER_RISING;
 174        }
 175
 176        /* Tell the interrupt handler that we're dealing with edges */
 177        if (irq_trig == IRQF_TRIGGER_FALLING ||
 178            irq_trig == IRQF_TRIGGER_RISING) {
 179                if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) {
 180                        dev_err(&indio_dev->dev,
 181                                "edge IRQ not supported w/o stat register.\n");
 182                        err = -EOPNOTSUPP;
 183                        goto iio_trigger_free;
 184                }
 185                sdata->edge_irq = true;
 186        } else {
 187                /*
 188                 * If we're not using edges (i.e. level interrupts) we
 189                 * just mask off the IRQ, handle one interrupt, then
 190                 * if the line is still low, we return to the
 191                 * interrupt handler top half again and start over.
 192                 */
 193                irq_trig |= IRQF_ONESHOT;
 194        }
 195
 196        /*
 197         * If the interrupt pin is Open Drain, by definition this
 198         * means that the interrupt line may be shared with other
 199         * peripherals. But to do this we also need to have a status
 200         * register and mask to figure out if this sensor was firing
 201         * the IRQ or not, so we can tell the interrupt handle that
 202         * it was "our" interrupt.
 203         */
 204        if (sdata->int_pin_open_drain &&
 205            sdata->sensor_settings->drdy_irq.stat_drdy.addr)
 206                irq_trig |= IRQF_SHARED;
 207
 208        err = request_threaded_irq(sdata->irq,
 209                                   st_sensors_irq_handler,
 210                                   st_sensors_irq_thread,
 211                                   irq_trig,
 212                                   sdata->trig->name,
 213                                   sdata->trig);
 214        if (err) {
 215                dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n");
 216                goto iio_trigger_free;
 217        }
 218
 219        err = iio_trigger_register(sdata->trig);
 220        if (err < 0) {
 221                dev_err(&indio_dev->dev, "failed to register iio trigger.\n");
 222                goto iio_trigger_register_error;
 223        }
 224        indio_dev->trig = iio_trigger_get(sdata->trig);
 225
 226        return 0;
 227
 228iio_trigger_register_error:
 229        free_irq(sdata->irq, sdata->trig);
 230iio_trigger_free:
 231        iio_trigger_free(sdata->trig);
 232        return err;
 233}
 234EXPORT_SYMBOL(st_sensors_allocate_trigger);
 235
 236void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
 237{
 238        struct st_sensor_data *sdata = iio_priv(indio_dev);
 239
 240        iio_trigger_unregister(sdata->trig);
 241        free_irq(sdata->irq, sdata->trig);
 242        iio_trigger_free(sdata->trig);
 243}
 244EXPORT_SYMBOL(st_sensors_deallocate_trigger);
 245
 246int st_sensors_validate_device(struct iio_trigger *trig,
 247                               struct iio_dev *indio_dev)
 248{
 249        struct iio_dev *indio = iio_trigger_get_drvdata(trig);
 250
 251        if (indio != indio_dev)
 252                return -EINVAL;
 253
 254        return 0;
 255}
 256EXPORT_SYMBOL(st_sensors_validate_device);
 257
 258MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
 259MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger");
 260MODULE_LICENSE("GPL v2");
 261