Skip to content

Commit eb35279

Browse files
mranostayjic23
authored andcommitted
iio: proximity: as3935: noise detection + threshold changes
Most applications are too noisy to allow the default noise and watchdog settings, and thus need to be configurable via DT properties. Also default settings to POR defaults on a reset, and register distuber interrupts as noise since it prevents proper usage. Cc: [email protected] Signed-off-by: Matt Ranostay <[email protected]> Acked-by: Rob Herring <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent b2e3120 commit eb35279

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Documentation/ABI/testing/sysfs-bus-iio-proximity-as3935

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ Description:
1414
Show or set the gain boost of the amp, from 0-31 range.
1515
18 = indoors (default)
1616
14 = outdoors
17+
18+
What /sys/bus/iio/devices/iio:deviceX/noise_level_tripped
19+
Date: May 2017
20+
KernelVersion: 4.13
21+
Contact: Matt Ranostay <[email protected]>
22+
Description:
23+
When 1 the noise level is over the trip level and not reporting
24+
valid data

Documentation/devicetree/bindings/iio/proximity/as3935.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Optional properties:
1616
- ams,tuning-capacitor-pf: Calibration tuning capacitor stepping
1717
value 0 - 120pF. This will require using the calibration data from
1818
the manufacturer.
19+
- ams,nflwdth: Set the noise and watchdog threshold register on
20+
startup. This will need to set according to the noise from the
21+
MCU board, and possibly the local environment. Refer to the
22+
datasheet for the threshold settings.
1923

2024
Example:
2125

@@ -27,4 +31,5 @@ as3935@0 {
2731
interrupt-parent = <&gpio1>;
2832
interrupts = <16 1>;
2933
ams,tuning-capacitor-pf = <80>;
34+
ams,nflwdth = <0x44>;
3035
};

drivers/iio/proximity/as3935.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@
3939
#define AS3935_AFE_GAIN_MAX 0x1F
4040
#define AS3935_AFE_PWR_BIT BIT(0)
4141

42+
#define AS3935_NFLWDTH 0x01
43+
#define AS3935_NFLWDTH_MASK 0x7f
44+
4245
#define AS3935_INT 0x03
4346
#define AS3935_INT_MASK 0x0f
47+
#define AS3935_DISTURB_INT BIT(2)
4448
#define AS3935_EVENT_INT BIT(3)
4549
#define AS3935_NOISE_INT BIT(0)
4650

4751
#define AS3935_DATA 0x07
4852
#define AS3935_DATA_MASK 0x3F
4953

5054
#define AS3935_TUNE_CAP 0x08
55+
#define AS3935_DEFAULTS 0x3C
5156
#define AS3935_CALIBRATE 0x3D
5257

5358
#define AS3935_READ_DATA BIT(14)
@@ -62,7 +67,9 @@ struct as3935_state {
6267
struct mutex lock;
6368
struct delayed_work work;
6469

70+
unsigned long noise_tripped;
6571
u32 tune_cap;
72+
u32 nflwdth_reg;
6673
u8 buffer[16]; /* 8-bit data + 56-bit padding + 64-bit timestamp */
6774
u8 buf[2] ____cacheline_aligned;
6875
};
@@ -145,12 +152,29 @@ static ssize_t as3935_sensor_sensitivity_store(struct device *dev,
145152
return len;
146153
}
147154

155+
static ssize_t as3935_noise_level_tripped_show(struct device *dev,
156+
struct device_attribute *attr,
157+
char *buf)
158+
{
159+
struct as3935_state *st = iio_priv(dev_to_iio_dev(dev));
160+
int ret;
161+
162+
mutex_lock(&st->lock);
163+
ret = sprintf(buf, "%d\n", !time_after(jiffies, st->noise_tripped + HZ));
164+
mutex_unlock(&st->lock);
165+
166+
return ret;
167+
}
168+
148169
static IIO_DEVICE_ATTR(sensor_sensitivity, S_IRUGO | S_IWUSR,
149170
as3935_sensor_sensitivity_show, as3935_sensor_sensitivity_store, 0);
150171

172+
static IIO_DEVICE_ATTR(noise_level_tripped, S_IRUGO,
173+
as3935_noise_level_tripped_show, NULL, 0);
151174

152175
static struct attribute *as3935_attributes[] = {
153176
&iio_dev_attr_sensor_sensitivity.dev_attr.attr,
177+
&iio_dev_attr_noise_level_tripped.dev_attr.attr,
154178
NULL,
155179
};
156180

@@ -246,7 +270,11 @@ static void as3935_event_work(struct work_struct *work)
246270
case AS3935_EVENT_INT:
247271
iio_trigger_poll_chained(st->trig);
248272
break;
273+
case AS3935_DISTURB_INT:
249274
case AS3935_NOISE_INT:
275+
mutex_lock(&st->lock);
276+
st->noise_tripped = jiffies;
277+
mutex_unlock(&st->lock);
250278
dev_warn(&st->spi->dev, "noise level is too high\n");
251279
break;
252280
}
@@ -269,15 +297,14 @@ static irqreturn_t as3935_interrupt_handler(int irq, void *private)
269297

270298
static void calibrate_as3935(struct as3935_state *st)
271299
{
272-
/* mask disturber interrupt bit */
273-
as3935_write(st, AS3935_INT, BIT(5));
274-
300+
as3935_write(st, AS3935_DEFAULTS, 0x96);
275301
as3935_write(st, AS3935_CALIBRATE, 0x96);
276302
as3935_write(st, AS3935_TUNE_CAP,
277303
BIT(5) | (st->tune_cap / TUNE_CAP_DIV));
278304

279305
mdelay(2);
280306
as3935_write(st, AS3935_TUNE_CAP, (st->tune_cap / TUNE_CAP_DIV));
307+
as3935_write(st, AS3935_NFLWDTH, st->nflwdth_reg);
281308
}
282309

283310
#ifdef CONFIG_PM_SLEEP
@@ -370,6 +397,15 @@ static int as3935_probe(struct spi_device *spi)
370397
return -EINVAL;
371398
}
372399

400+
ret = of_property_read_u32(np,
401+
"ams,nflwdth", &st->nflwdth_reg);
402+
if (!ret && st->nflwdth_reg > AS3935_NFLWDTH_MASK) {
403+
dev_err(&spi->dev,
404+
"invalid nflwdth setting of %d\n",
405+
st->nflwdth_reg);
406+
return -EINVAL;
407+
}
408+
373409
indio_dev->dev.parent = &spi->dev;
374410
indio_dev->name = spi_get_device_id(spi)->name;
375411
indio_dev->channels = as3935_channels;
@@ -384,6 +420,7 @@ static int as3935_probe(struct spi_device *spi)
384420
return -ENOMEM;
385421

386422
st->trig = trig;
423+
st->noise_tripped = jiffies - HZ;
387424
trig->dev.parent = indio_dev->dev.parent;
388425
iio_trigger_set_drvdata(trig, indio_dev);
389426
trig->ops = &iio_interrupt_trigger_ops;

0 commit comments

Comments
 (0)