Skip to content

Commit 0427a10

Browse files
Linus Walleijjic23
authored andcommitted
iio: accel: kxsd9: Add triggered buffer handling
As is custom with all modern sensors, add a clever burst mode that will just stream out values from the sensor and provide it to userspace to do the proper offsetting and scaling. This is the result when tested with an HRTimer trigger: $ generic_buffer -a -c 10 -n kxsd9 -t foo /sys/bus/iio/devices/iio:device1 foo 0.371318 0.718680 9.869872 1795.000000 97545896129 -0.586922 0.179670 9.378775 2398.000000 97555864721 -0.299450 0.179670 10.348992 2672.000000 97565874055 0.371318 0.335384 11.103606 2816.000000 97575883240 0.179670 0.574944 10.540640 2847.000000 97585862351 0.335384 0.754614 9.953718 2840.000000 97595872425 0.179670 0.754614 10.732288 2879.000000 97605882351 0.000000 0.754614 10.348992 2872.000000 97615891832 -0.730658 0.574944 9.570422 2831.000000 97625871536 0.000000 1.137910 10.732288 2872.000000 97635881610 Columns shown are x, y, z acceleration, so a positive acceleration of ~9.81 (shaky due to bad calibration) along the z axis. The fourth column is the AUX IN which is floating on this system, it seems to float up to the 2.85V VDD voltage. To be able to cleanup the triggered buffer, we need to add .remove() callbacks to the I2C and SPI subdrivers and call back into an exported .remove() callback in the core. Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Tested-by: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 84e2f6f commit 0427a10

2 files changed

Lines changed: 78 additions & 6 deletions

File tree

‎drivers/iio/accel/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ config IIO_ST_ACCEL_SPI_3AXIS
119119

120120
config KXSD9
121121
tristate "Kionix KXSD9 Accelerometer Driver"
122+
select IIO_BUFFER
123+
select IIO_TRIGGERED_BUFFER
122124
help
123125
Say yes here to build support for the Kionix KXSD9 accelerometer.
124126
It can be accessed using an (optional) SPI or I2C interface.

‎drivers/iio/accel/kxsd9.c‎

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* I have a suitable wire made up.
1313
*
1414
* TODO: Support the motion detector
15-
* Uses register address incrementing so could have a
16-
* heavily optimized ring buffer access function.
1715
*/
1816

1917
#include <linux/device.h>
@@ -24,6 +22,9 @@
2422
#include <linux/regmap.h>
2523
#include <linux/iio/iio.h>
2624
#include <linux/iio/sysfs.h>
25+
#include <linux/iio/buffer.h>
26+
#include <linux/iio/triggered_buffer.h>
27+
#include <linux/iio/trigger_consumer.h>
2728

2829
#include "kxsd9.h"
2930

@@ -41,9 +42,11 @@
4142

4243
/**
4344
* struct kxsd9_state - device related storage
45+
* @dev: pointer to the parent device
4446
* @map: regmap to the device
4547
*/
4648
struct kxsd9_state {
49+
struct device *dev;
4750
struct regmap *map;
4851
};
4952

@@ -155,7 +158,35 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,
155158
error_ret:
156159
return ret;
157160
};
158-
#define KXSD9_ACCEL_CHAN(axis) \
161+
162+
static irqreturn_t kxsd9_trigger_handler(int irq, void *p)
163+
{
164+
const struct iio_poll_func *pf = p;
165+
struct iio_dev *indio_dev = pf->indio_dev;
166+
struct kxsd9_state *st = iio_priv(indio_dev);
167+
int ret;
168+
/* 4 * 16bit values AND timestamp */
169+
__be16 hw_values[8];
170+
171+
ret = regmap_bulk_read(st->map,
172+
KXSD9_REG_X,
173+
&hw_values,
174+
8);
175+
if (ret) {
176+
dev_err(st->dev,
177+
"error reading data\n");
178+
return ret;
179+
}
180+
181+
iio_push_to_buffers_with_timestamp(indio_dev,
182+
hw_values,
183+
iio_get_time_ns(indio_dev));
184+
iio_trigger_notify_done(indio_dev->trig);
185+
186+
return IRQ_HANDLED;
187+
}
188+
189+
#define KXSD9_ACCEL_CHAN(axis, index) \
159190
{ \
160191
.type = IIO_ACCEL, \
161192
.modified = 1, \
@@ -164,16 +195,35 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,
164195
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
165196
BIT(IIO_CHAN_INFO_OFFSET), \
166197
.address = KXSD9_REG_##axis, \
198+
.scan_index = index, \
199+
.scan_type = { \
200+
.sign = 'u', \
201+
.realbits = 12, \
202+
.storagebits = 16, \
203+
.shift = 4, \
204+
.endianness = IIO_BE, \
205+
}, \
167206
}
168207

169208
static const struct iio_chan_spec kxsd9_channels[] = {
170-
KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z),
209+
KXSD9_ACCEL_CHAN(X, 0),
210+
KXSD9_ACCEL_CHAN(Y, 1),
211+
KXSD9_ACCEL_CHAN(Z, 2),
171212
{
172213
.type = IIO_VOLTAGE,
173214
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
174215
.indexed = 1,
175216
.address = KXSD9_REG_AUX,
176-
}
217+
.scan_index = 3,
218+
.scan_type = {
219+
.sign = 'u',
220+
.realbits = 12,
221+
.storagebits = 16,
222+
.shift = 4,
223+
.endianness = IIO_BE,
224+
},
225+
},
226+
IIO_CHAN_SOFT_TIMESTAMP(4),
177227
};
178228

179229
static const struct attribute_group kxsd9_attribute_group = {
@@ -197,6 +247,9 @@ static const struct iio_info kxsd9_info = {
197247
.driver_module = THIS_MODULE,
198248
};
199249

250+
/* Four channels apart from timestamp, scan mask = 0x0f */
251+
static const unsigned long kxsd9_scan_masks[] = { 0xf, 0 };
252+
200253
int kxsd9_common_probe(struct device *parent,
201254
struct regmap *map,
202255
const char *name)
@@ -210,6 +263,7 @@ int kxsd9_common_probe(struct device *parent,
210263
return -ENOMEM;
211264

212265
st = iio_priv(indio_dev);
266+
st->dev = parent;
213267
st->map = map;
214268

215269
indio_dev->channels = kxsd9_channels;
@@ -218,23 +272,39 @@ int kxsd9_common_probe(struct device *parent,
218272
indio_dev->dev.parent = parent;
219273
indio_dev->info = &kxsd9_info;
220274
indio_dev->modes = INDIO_DIRECT_MODE;
275+
indio_dev->available_scan_masks = kxsd9_scan_masks;
221276

222277
kxsd9_power_up(st);
223278

279+
ret = iio_triggered_buffer_setup(indio_dev,
280+
iio_pollfunc_store_time,
281+
kxsd9_trigger_handler,
282+
NULL);
283+
if (ret) {
284+
dev_err(parent, "triggered buffer setup failed\n");
285+
return ret;
286+
}
287+
224288
ret = iio_device_register(indio_dev);
225289
if (ret)
226-
return ret;
290+
goto err_cleanup_buffer;
227291

228292
dev_set_drvdata(parent, indio_dev);
229293

230294
return 0;
295+
296+
err_cleanup_buffer:
297+
iio_triggered_buffer_cleanup(indio_dev);
298+
299+
return ret;
231300
}
232301
EXPORT_SYMBOL(kxsd9_common_probe);
233302

234303
int kxsd9_common_remove(struct device *parent)
235304
{
236305
struct iio_dev *indio_dev = dev_get_drvdata(parent);
237306

307+
iio_triggered_buffer_cleanup(indio_dev);
238308
iio_device_unregister(indio_dev);
239309

240310
return 0;

0 commit comments

Comments
 (0)