| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * STMicroelectronics sensors trigger library driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2012-2013 STMicroelectronics Inc. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Denis Ciocca <denis.ciocca@st.com> |
|---|
| 7 | | - * |
|---|
| 8 | | - * Licensed under the GPL-2. |
|---|
| 9 | 8 | */ |
|---|
| 10 | 9 | |
|---|
| 11 | 10 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 14 | 13 | #include <linux/iio/iio.h> |
|---|
| 15 | 14 | #include <linux/iio/trigger.h> |
|---|
| 16 | 15 | #include <linux/interrupt.h> |
|---|
| 16 | +#include <linux/regmap.h> |
|---|
| 17 | 17 | #include <linux/iio/common/st_sensors.h> |
|---|
| 18 | 18 | #include "st_sensors_core.h" |
|---|
| 19 | 19 | |
|---|
| 20 | 20 | /** |
|---|
| 21 | 21 | * st_sensors_new_samples_available() - check if more samples came in |
|---|
| 22 | + * @indio_dev: IIO device reference. |
|---|
| 23 | + * @sdata: Sensor data. |
|---|
| 24 | + * |
|---|
| 22 | 25 | * returns: |
|---|
| 23 | | - * 0 - no new samples available |
|---|
| 24 | | - * 1 - new samples available |
|---|
| 25 | | - * negative - error or unknown |
|---|
| 26 | + * false - no new samples available or read error |
|---|
| 27 | + * true - new samples available |
|---|
| 26 | 28 | */ |
|---|
| 27 | | -static int st_sensors_new_samples_available(struct iio_dev *indio_dev, |
|---|
| 28 | | - struct st_sensor_data *sdata) |
|---|
| 29 | +static bool st_sensors_new_samples_available(struct iio_dev *indio_dev, |
|---|
| 30 | + struct st_sensor_data *sdata) |
|---|
| 29 | 31 | { |
|---|
| 30 | | - u8 status; |
|---|
| 31 | | - int ret; |
|---|
| 32 | + int ret, status; |
|---|
| 32 | 33 | |
|---|
| 33 | 34 | /* How would I know if I can't check it? */ |
|---|
| 34 | 35 | if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) |
|---|
| 35 | | - return -EINVAL; |
|---|
| 36 | + return true; |
|---|
| 36 | 37 | |
|---|
| 37 | 38 | /* No scan mask, no interrupt */ |
|---|
| 38 | 39 | if (!indio_dev->active_scan_mask) |
|---|
| 39 | | - return 0; |
|---|
| 40 | + return false; |
|---|
| 40 | 41 | |
|---|
| 41 | | - ret = sdata->tf->read_byte(&sdata->tb, sdata->dev, |
|---|
| 42 | | - sdata->sensor_settings->drdy_irq.stat_drdy.addr, |
|---|
| 43 | | - &status); |
|---|
| 42 | + ret = regmap_read(sdata->regmap, |
|---|
| 43 | + sdata->sensor_settings->drdy_irq.stat_drdy.addr, |
|---|
| 44 | + &status); |
|---|
| 44 | 45 | if (ret < 0) { |
|---|
| 45 | | - dev_err(sdata->dev, |
|---|
| 46 | | - "error checking samples available\n"); |
|---|
| 47 | | - return ret; |
|---|
| 46 | + dev_err(sdata->dev, "error checking samples available\n"); |
|---|
| 47 | + return false; |
|---|
| 48 | 48 | } |
|---|
| 49 | 49 | |
|---|
| 50 | | - if (status & sdata->sensor_settings->drdy_irq.stat_drdy.mask) |
|---|
| 51 | | - return 1; |
|---|
| 52 | | - |
|---|
| 53 | | - return 0; |
|---|
| 50 | + return !!(status & sdata->sensor_settings->drdy_irq.stat_drdy.mask); |
|---|
| 54 | 51 | } |
|---|
| 55 | 52 | |
|---|
| 56 | 53 | /** |
|---|
| .. | .. |
|---|
| 104 | 101 | return IRQ_HANDLED; |
|---|
| 105 | 102 | |
|---|
| 106 | 103 | /* |
|---|
| 107 | | - * If we are using egde IRQs, new samples arrived while processing |
|---|
| 104 | + * If we are using edge IRQs, new samples arrived while processing |
|---|
| 108 | 105 | * the IRQ and those may be missed unless we pick them here, so poll |
|---|
| 109 | 106 | * again. If the sensor delivery frequency is very high, this thread |
|---|
| 110 | 107 | * turns into a polled loop handler. |
|---|
| .. | .. |
|---|
| 122 | 119 | int st_sensors_allocate_trigger(struct iio_dev *indio_dev, |
|---|
| 123 | 120 | const struct iio_trigger_ops *trigger_ops) |
|---|
| 124 | 121 | { |
|---|
| 125 | | - int err, irq; |
|---|
| 126 | 122 | struct st_sensor_data *sdata = iio_priv(indio_dev); |
|---|
| 127 | 123 | unsigned long irq_trig; |
|---|
| 124 | + int err; |
|---|
| 128 | 125 | |
|---|
| 129 | 126 | sdata->trig = iio_trigger_alloc("%s-trigger", indio_dev->name); |
|---|
| 130 | 127 | if (sdata->trig == NULL) { |
|---|
| .. | .. |
|---|
| 136 | 133 | sdata->trig->ops = trigger_ops; |
|---|
| 137 | 134 | sdata->trig->dev.parent = sdata->dev; |
|---|
| 138 | 135 | |
|---|
| 139 | | - irq = sdata->get_irq_data_ready(indio_dev); |
|---|
| 140 | | - irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); |
|---|
| 136 | + irq_trig = irqd_get_trigger_type(irq_get_irq_data(sdata->irq)); |
|---|
| 141 | 137 | /* |
|---|
| 142 | 138 | * If the IRQ is triggered on falling edge, we need to mark the |
|---|
| 143 | 139 | * interrupt as active low, if the hardware supports this. |
|---|
| .. | .. |
|---|
| 147 | 143 | case IRQF_TRIGGER_LOW: |
|---|
| 148 | 144 | if (!sdata->sensor_settings->drdy_irq.addr_ihl) { |
|---|
| 149 | 145 | dev_err(&indio_dev->dev, |
|---|
| 150 | | - "falling/low specified for IRQ " |
|---|
| 151 | | - "but hardware only support rising/high: " |
|---|
| 152 | | - "will request rising/high\n"); |
|---|
| 146 | + "falling/low specified for IRQ but hardware supports only rising/high: will request rising/high\n"); |
|---|
| 153 | 147 | if (irq_trig == IRQF_TRIGGER_FALLING) |
|---|
| 154 | 148 | irq_trig = IRQF_TRIGGER_RISING; |
|---|
| 155 | 149 | if (irq_trig == IRQF_TRIGGER_LOW) |
|---|
| .. | .. |
|---|
| 162 | 156 | if (err < 0) |
|---|
| 163 | 157 | goto iio_trigger_free; |
|---|
| 164 | 158 | dev_info(&indio_dev->dev, |
|---|
| 165 | | - "interrupts on the falling edge or " |
|---|
| 166 | | - "active low level\n"); |
|---|
| 159 | + "interrupts on the falling edge or active low level\n"); |
|---|
| 167 | 160 | } |
|---|
| 168 | 161 | break; |
|---|
| 169 | 162 | case IRQF_TRIGGER_RISING: |
|---|
| .. | .. |
|---|
| 177 | 170 | default: |
|---|
| 178 | 171 | /* This is the most preferred mode, if possible */ |
|---|
| 179 | 172 | dev_err(&indio_dev->dev, |
|---|
| 180 | | - "unsupported IRQ trigger specified (%lx), enforce " |
|---|
| 181 | | - "rising edge\n", irq_trig); |
|---|
| 173 | + "unsupported IRQ trigger specified (%lx), enforce rising edge\n", irq_trig); |
|---|
| 182 | 174 | irq_trig = IRQF_TRIGGER_RISING; |
|---|
| 183 | 175 | } |
|---|
| 184 | 176 | |
|---|
| 185 | 177 | /* Tell the interrupt handler that we're dealing with edges */ |
|---|
| 186 | 178 | if (irq_trig == IRQF_TRIGGER_FALLING || |
|---|
| 187 | | - irq_trig == IRQF_TRIGGER_RISING) |
|---|
| 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 | + err = -EOPNOTSUPP; |
|---|
| 184 | + goto iio_trigger_free; |
|---|
| 185 | + } |
|---|
| 188 | 186 | sdata->edge_irq = true; |
|---|
| 189 | | - else |
|---|
| 187 | + } else { |
|---|
| 190 | 188 | /* |
|---|
| 191 | 189 | * If we're not using edges (i.e. level interrupts) we |
|---|
| 192 | 190 | * just mask off the IRQ, handle one interrupt, then |
|---|
| .. | .. |
|---|
| 194 | 192 | * interrupt handler top half again and start over. |
|---|
| 195 | 193 | */ |
|---|
| 196 | 194 | irq_trig |= IRQF_ONESHOT; |
|---|
| 195 | + } |
|---|
| 197 | 196 | |
|---|
| 198 | 197 | /* |
|---|
| 199 | 198 | * If the interrupt pin is Open Drain, by definition this |
|---|
| .. | .. |
|---|
| 207 | 206 | sdata->sensor_settings->drdy_irq.stat_drdy.addr) |
|---|
| 208 | 207 | irq_trig |= IRQF_SHARED; |
|---|
| 209 | 208 | |
|---|
| 210 | | - err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev), |
|---|
| 211 | | - st_sensors_irq_handler, |
|---|
| 212 | | - st_sensors_irq_thread, |
|---|
| 213 | | - irq_trig, |
|---|
| 214 | | - sdata->trig->name, |
|---|
| 215 | | - sdata->trig); |
|---|
| 209 | + err = request_threaded_irq(sdata->irq, |
|---|
| 210 | + st_sensors_irq_handler, |
|---|
| 211 | + st_sensors_irq_thread, |
|---|
| 212 | + irq_trig, |
|---|
| 213 | + sdata->trig->name, |
|---|
| 214 | + sdata->trig); |
|---|
| 216 | 215 | if (err) { |
|---|
| 217 | 216 | dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n"); |
|---|
| 218 | 217 | goto iio_trigger_free; |
|---|
| .. | .. |
|---|
| 228 | 227 | return 0; |
|---|
| 229 | 228 | |
|---|
| 230 | 229 | iio_trigger_register_error: |
|---|
| 231 | | - free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig); |
|---|
| 230 | + free_irq(sdata->irq, sdata->trig); |
|---|
| 232 | 231 | iio_trigger_free: |
|---|
| 233 | 232 | iio_trigger_free(sdata->trig); |
|---|
| 234 | 233 | return err; |
|---|
| .. | .. |
|---|
| 240 | 239 | struct st_sensor_data *sdata = iio_priv(indio_dev); |
|---|
| 241 | 240 | |
|---|
| 242 | 241 | iio_trigger_unregister(sdata->trig); |
|---|
| 243 | | - free_irq(sdata->get_irq_data_ready(indio_dev), sdata->trig); |
|---|
| 242 | + free_irq(sdata->irq, sdata->trig); |
|---|
| 244 | 243 | iio_trigger_free(sdata->trig); |
|---|
| 245 | 244 | } |
|---|
| 246 | 245 | EXPORT_SYMBOL(st_sensors_deallocate_trigger); |
|---|