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 */ 28 static 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 */ 57 static 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 */ 73 static 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 118 int 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 struct device *parent = indio_dev->dev.parent; 123 unsigned long irq_trig; 124 int err; 125 126 sdata->trig = devm_iio_trigger_alloc(parent, "%s-trigger", 127 indio_dev->name); 128 if (sdata->trig == NULL) { 129 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n"); 130 return -ENOMEM; 131 } 132 133 iio_trigger_set_drvdata(sdata->trig, indio_dev); 134 sdata->trig->ops = trigger_ops; 135 136 irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq)); 137 /* 138 * If the IRQ is triggered on falling edge, we need to mark the 139 * interrupt as active low, if the hardware supports this. 140 */ 141 switch(irq_trig) { 142 case IRQF_TRIGGER_FALLING: 143 case IRQF_TRIGGER_LOW: 144 if (!sdata->sensor_settings->drdy_irq.addr_ihl) { 145 dev_err(&indio_dev->dev, 146 "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n"); 147 if (irq_trig == IRQF_TRIGGER_FALLING) 148 irq_trig = IRQF_TRIGGER_RISING; 149 if (irq_trig == IRQF_TRIGGER_LOW) 150 irq_trig = IRQF_TRIGGER_HIGH; 151 } else { 152 /* Set up INT active low i.e. falling edge */ 153 err = st_sensors_write_data_with_mask(indio_dev, 154 sdata->sensor_settings->drdy_irq.addr_ihl, 155 sdata->sensor_settings->drdy_irq.mask_ihl, 1); 156 if (err < 0) 157 return err; 158 dev_info(&indio_dev->dev, 159 "interrupts on the falling edge or active low level\n"); 160 } 161 break; 162 case IRQF_TRIGGER_RISING: 163 dev_info(&indio_dev->dev, 164 "interrupts on the rising edge\n"); 165 break; 166 case IRQF_TRIGGER_HIGH: 167 dev_info(&indio_dev->dev, 168 "interrupts active high level\n"); 169 break; 170 default: 171 /* This is the most preferred mode, if possible */ 172 dev_err(&indio_dev->dev, 173 "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig); 174 irq_trig = IRQF_TRIGGER_RISING; 175 } 176 177 /* Tell the interrupt handler that we're dealing with edges */ 178 if (irq_trig == IRQF_TRIGGER_FALLING || 179 irq_trig == IRQF_TRIGGER_RISING) { 180 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) { 181 dev_err(&indio_dev->dev, 182 "edge IRQ not supported w/o stat register.\n"); 183 return -EOPNOTSUPP; 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 = devm_request_threaded_irq(parent, 209 sdata->irq, 210 st_sensors_irq_handler, 211 st_sensors_irq_thread, 212 irq_trig, 213 sdata->trig->name, 214 sdata->trig); 215 if (err) { 216 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n"); 217 return err; 218 } 219 220 err = devm_iio_trigger_register(parent, sdata->trig); 221 if (err < 0) { 222 dev_err(&indio_dev->dev, "failed to register iio trigger.\n"); 223 return err; 224 } 225 indio_dev->trig = iio_trigger_get(sdata->trig); 226 227 return 0; 228 } 229 EXPORT_SYMBOL(st_sensors_allocate_trigger); 230 231 int st_sensors_validate_device(struct iio_trigger *trig, 232 struct iio_dev *indio_dev) 233 { 234 struct iio_dev *indio = iio_trigger_get_drvdata(trig); 235 236 if (indio != indio_dev) 237 return -EINVAL; 238 239 return 0; 240 } 241 EXPORT_SYMBOL(st_sensors_validate_device); 242 243 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 244 MODULE_DESCRIPTION("STMicroelectronics ST-sensors trigger"); 245 MODULE_LICENSE("GPL v2"); 246