1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADXL355 3-Axis Digital Accelerometer IIO core driver
5 * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
7 * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf
10 #include <linux/bits.h>
11 #include <linux/bitfield.h>
12 #include <linux/iio/buffer.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/trigger.h>
15 #include <linux/iio/triggered_buffer.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/limits.h>
18 #include <linux/math64.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <linux/units.h>
25 #include <linux/unaligned.h>
29 /* ADXL355 Register Definitions */
30 #define ADXL355_DEVID_AD_REG 0x00
31 #define ADXL355_DEVID_MST_REG 0x01
32 #define ADXL355_PARTID_REG 0x02
33 #define ADXL355_STATUS_REG 0x04
34 #define ADXL355_FIFO_ENTRIES_REG 0x05
35 #define ADXL355_TEMP2_REG 0x06
36 #define ADXL355_XDATA3_REG 0x08
37 #define ADXL355_YDATA3_REG 0x0B
38 #define ADXL355_ZDATA3_REG 0x0E
39 #define ADXL355_FIFO_DATA_REG 0x11
40 #define ADXL355_OFFSET_X_H_REG 0x1E
41 #define ADXL355_OFFSET_Y_H_REG 0x20
42 #define ADXL355_OFFSET_Z_H_REG 0x22
43 #define ADXL355_ACT_EN_REG 0x24
44 #define ADXL355_ACT_THRESH_H_REG 0x25
45 #define ADXL355_ACT_THRESH_L_REG 0x26
46 #define ADXL355_ACT_COUNT_REG 0x27
47 #define ADXL355_FILTER_REG 0x28
48 #define ADXL355_FILTER_ODR_MSK GENMASK(3, 0)
49 #define ADXL355_FILTER_HPF_MSK GENMASK(6, 4)
50 #define ADXL355_FIFO_SAMPLES_REG 0x29
51 #define ADXL355_INT_MAP_REG 0x2A
52 #define ADXL355_SYNC_REG 0x2B
53 #define ADXL355_RANGE_REG 0x2C
54 #define ADXL355_POWER_CTL_REG 0x2D
55 #define ADXL355_POWER_CTL_MODE_MSK GENMASK(1, 0)
56 #define ADXL355_POWER_CTL_DRDY_MSK BIT(2)
57 #define ADXL355_SELF_TEST_REG 0x2E
58 #define ADXL355_RESET_REG 0x2F
60 #define ADXL355_DEVID_AD_VAL 0xAD
61 #define ADXL355_DEVID_MST_VAL 0x1D
62 #define ADXL355_PARTID_VAL 0xED
63 #define ADXL359_PARTID_VAL 0xE9
64 #define ADXL355_RESET_CODE 0x52
66 static const struct regmap_range adxl355_read_reg_range
[] = {
67 regmap_reg_range(ADXL355_DEVID_AD_REG
, ADXL355_FIFO_DATA_REG
),
68 regmap_reg_range(ADXL355_OFFSET_X_H_REG
, ADXL355_SELF_TEST_REG
),
71 const struct regmap_access_table adxl355_readable_regs_tbl
= {
72 .yes_ranges
= adxl355_read_reg_range
,
73 .n_yes_ranges
= ARRAY_SIZE(adxl355_read_reg_range
),
75 EXPORT_SYMBOL_NS_GPL(adxl355_readable_regs_tbl
, "IIO_ADXL355");
77 static const struct regmap_range adxl355_write_reg_range
[] = {
78 regmap_reg_range(ADXL355_OFFSET_X_H_REG
, ADXL355_RESET_REG
),
81 const struct regmap_access_table adxl355_writeable_regs_tbl
= {
82 .yes_ranges
= adxl355_write_reg_range
,
83 .n_yes_ranges
= ARRAY_SIZE(adxl355_write_reg_range
),
85 EXPORT_SYMBOL_NS_GPL(adxl355_writeable_regs_tbl
, "IIO_ADXL355");
87 const struct adxl355_chip_info adxl35x_chip_info
[] = {
90 .part_id
= ADXL355_PARTID_VAL
,
92 * At +/- 2g with 20-bit resolution, scale is given in datasheet
93 * as 3.9ug/LSB = 0.0000039 * 9.80665 = 0.00003824593 m/s^2.
100 * The datasheet defines an intercept of 1885 LSB at 25 degC
101 * and a slope of -9.05 LSB/C. The following formula can be used
102 * to find the temperature:
103 * Temp = ((RAW - 1885)/(-9.05)) + 25 but this doesn't follow
104 * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE.
105 * Hence using some rearranging we get the scale as -110.497238
106 * and offset as -2111.25.
115 .part_id
= ADXL359_PARTID_VAL
,
117 * At +/- 10g with 20-bit resolution, scale is given in datasheet
118 * as 19.5ug/LSB = 0.0000195 * 9.80665 = 0.0.00019122967 m/s^2.
125 * The datasheet defines an intercept of 1852 LSB at 25 degC
126 * and a slope of -9.05 LSB/C. The following formula can be used
127 * to find the temperature:
128 * Temp = ((RAW - 1852)/(-9.05)) + 25 but this doesn't follow
129 * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE.
130 * Hence using some rearranging we get the scale as -110.497238
131 * and offset as -2079.25.
139 EXPORT_SYMBOL_NS_GPL(adxl35x_chip_info
, "IIO_ADXL355");
141 enum adxl355_op_mode
{
156 ADXL355_ODR_15_625HZ
,
161 enum adxl355_hpf_3db
{
171 static const int adxl355_odr_table
[][2] = {
185 static const int adxl355_hpf_3db_multipliers
[] = {
196 chan_x
, chan_y
, chan_z
,
199 struct adxl355_chan_info
{
204 static const struct adxl355_chan_info adxl355_chans
[] = {
206 .data_reg
= ADXL355_XDATA3_REG
,
207 .offset_reg
= ADXL355_OFFSET_X_H_REG
210 .data_reg
= ADXL355_YDATA3_REG
,
211 .offset_reg
= ADXL355_OFFSET_Y_H_REG
214 .data_reg
= ADXL355_ZDATA3_REG
,
215 .offset_reg
= ADXL355_OFFSET_Z_H_REG
219 struct adxl355_data
{
220 const struct adxl355_chip_info
*chip_info
;
221 struct regmap
*regmap
;
223 struct mutex lock
; /* lock to protect op_mode */
224 enum adxl355_op_mode op_mode
;
225 enum adxl355_odr odr
;
226 enum adxl355_hpf_3db hpf_3db
;
228 int adxl355_hpf_3db_table
[7][2];
229 struct iio_trigger
*dready_trig
;
236 } __aligned(IIO_DMA_MINALIGN
);
239 static int adxl355_set_op_mode(struct adxl355_data
*data
,
240 enum adxl355_op_mode op_mode
)
244 if (data
->op_mode
== op_mode
)
247 ret
= regmap_update_bits(data
->regmap
, ADXL355_POWER_CTL_REG
,
248 ADXL355_POWER_CTL_MODE_MSK
, op_mode
);
252 data
->op_mode
= op_mode
;
257 static int adxl355_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
260 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
261 struct adxl355_data
*data
= iio_priv(indio_dev
);
264 mutex_lock(&data
->lock
);
265 ret
= regmap_update_bits(data
->regmap
, ADXL355_POWER_CTL_REG
,
266 ADXL355_POWER_CTL_DRDY_MSK
,
267 FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK
,
269 mutex_unlock(&data
->lock
);
274 static void adxl355_fill_3db_frequency_table(struct adxl355_data
*data
)
281 odr
= mul_u64_u32_shr(adxl355_odr_table
[data
->odr
][0], MEGA
, 0) +
282 adxl355_odr_table
[data
->odr
][1];
284 for (i
= 0; i
< ARRAY_SIZE(adxl355_hpf_3db_multipliers
); i
++) {
285 multiplier
= adxl355_hpf_3db_multipliers
[i
];
286 div
= div64_u64_rem(mul_u64_u32_shr(odr
, multiplier
, 0),
289 data
->adxl355_hpf_3db_table
[i
][0] = div
;
290 data
->adxl355_hpf_3db_table
[i
][1] = div_u64(rem
, MEGA
* 100);
294 static int adxl355_setup(struct adxl355_data
*data
)
299 ret
= regmap_read(data
->regmap
, ADXL355_DEVID_AD_REG
, ®val
);
303 if (regval
!= ADXL355_DEVID_AD_VAL
) {
304 dev_err(data
->dev
, "Invalid ADI ID 0x%02x\n", regval
);
308 ret
= regmap_read(data
->regmap
, ADXL355_DEVID_MST_REG
, ®val
);
312 if (regval
!= ADXL355_DEVID_MST_VAL
) {
313 dev_err(data
->dev
, "Invalid MEMS ID 0x%02x\n", regval
);
317 ret
= regmap_read(data
->regmap
, ADXL355_PARTID_REG
, ®val
);
321 if (regval
!= ADXL355_PARTID_VAL
)
322 dev_warn(data
->dev
, "Invalid DEV ID 0x%02x\n", regval
);
325 * Perform a software reset to make sure the device is in a consistent
326 * state after start-up.
328 ret
= regmap_write(data
->regmap
, ADXL355_RESET_REG
, ADXL355_RESET_CODE
);
332 ret
= regmap_update_bits(data
->regmap
, ADXL355_POWER_CTL_REG
,
333 ADXL355_POWER_CTL_DRDY_MSK
,
334 FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK
, 1));
338 adxl355_fill_3db_frequency_table(data
);
340 return adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
343 static int adxl355_get_temp_data(struct adxl355_data
*data
, u8 addr
)
345 return regmap_bulk_read(data
->regmap
, addr
, data
->transf_buf
, 2);
348 static int adxl355_read_axis(struct adxl355_data
*data
, u8 addr
)
352 ret
= regmap_bulk_read(data
->regmap
, addr
, data
->transf_buf
,
353 ARRAY_SIZE(data
->transf_buf
));
357 return get_unaligned_be24(data
->transf_buf
);
360 static int adxl355_find_match(const int (*freq_tbl
)[2], const int n
,
361 const int val
, const int val2
)
365 for (i
= 0; i
< n
; i
++) {
366 if (freq_tbl
[i
][0] == val
&& freq_tbl
[i
][1] == val2
)
373 static int adxl355_set_odr(struct adxl355_data
*data
,
374 enum adxl355_odr odr
)
378 mutex_lock(&data
->lock
);
380 if (data
->odr
== odr
) {
381 mutex_unlock(&data
->lock
);
385 ret
= adxl355_set_op_mode(data
, ADXL355_STANDBY
);
389 ret
= regmap_update_bits(data
->regmap
, ADXL355_FILTER_REG
,
390 ADXL355_FILTER_ODR_MSK
,
391 FIELD_PREP(ADXL355_FILTER_ODR_MSK
, odr
));
396 adxl355_fill_3db_frequency_table(data
);
398 ret
= adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
402 mutex_unlock(&data
->lock
);
406 adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
408 mutex_unlock(&data
->lock
);
412 static int adxl355_set_hpf_3db(struct adxl355_data
*data
,
413 enum adxl355_hpf_3db hpf
)
417 mutex_lock(&data
->lock
);
419 if (data
->hpf_3db
== hpf
) {
420 mutex_unlock(&data
->lock
);
424 ret
= adxl355_set_op_mode(data
, ADXL355_STANDBY
);
428 ret
= regmap_update_bits(data
->regmap
, ADXL355_FILTER_REG
,
429 ADXL355_FILTER_HPF_MSK
,
430 FIELD_PREP(ADXL355_FILTER_HPF_MSK
, hpf
));
436 ret
= adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
440 mutex_unlock(&data
->lock
);
444 adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
446 mutex_unlock(&data
->lock
);
450 static int adxl355_set_calibbias(struct adxl355_data
*data
,
451 enum adxl355_chans chan
, int calibbias
)
455 mutex_lock(&data
->lock
);
457 ret
= adxl355_set_op_mode(data
, ADXL355_STANDBY
);
461 put_unaligned_be16(calibbias
, data
->transf_buf
);
462 ret
= regmap_bulk_write(data
->regmap
,
463 adxl355_chans
[chan
].offset_reg
,
464 data
->transf_buf
, 2);
468 data
->calibbias
[chan
] = calibbias
;
470 ret
= adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
474 mutex_unlock(&data
->lock
);
478 adxl355_set_op_mode(data
, ADXL355_MEASUREMENT
);
480 mutex_unlock(&data
->lock
);
484 static int adxl355_read_raw(struct iio_dev
*indio_dev
,
485 struct iio_chan_spec
const *chan
,
486 int *val
, int *val2
, long mask
)
488 struct adxl355_data
*data
= iio_priv(indio_dev
);
492 case IIO_CHAN_INFO_RAW
:
493 switch (chan
->type
) {
495 ret
= adxl355_get_temp_data(data
, chan
->address
);
498 *val
= get_unaligned_be16(data
->transf_buf
);
502 ret
= adxl355_read_axis(data
, adxl355_chans
[
503 chan
->address
].data_reg
);
506 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
507 chan
->scan_type
.realbits
- 1);
513 case IIO_CHAN_INFO_SCALE
:
514 switch (chan
->type
) {
517 * Temperature scale is -110.497238.
518 * See the detailed explanation in adxl35x_chip_info
523 return IIO_VAL_INT_PLUS_MICRO
;
525 *val
= data
->chip_info
->accel_scale
.integer
;
526 *val2
= data
->chip_info
->accel_scale
.decimal
;
527 return IIO_VAL_INT_PLUS_NANO
;
531 case IIO_CHAN_INFO_OFFSET
:
532 *val
= data
->chip_info
->temp_offset
.integer
;
533 *val2
= data
->chip_info
->temp_offset
.decimal
;
534 return IIO_VAL_INT_PLUS_MICRO
;
535 case IIO_CHAN_INFO_CALIBBIAS
:
536 *val
= sign_extend32(data
->calibbias
[chan
->address
], 15);
538 case IIO_CHAN_INFO_SAMP_FREQ
:
539 *val
= adxl355_odr_table
[data
->odr
][0];
540 *val2
= adxl355_odr_table
[data
->odr
][1];
541 return IIO_VAL_INT_PLUS_MICRO
;
542 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
543 *val
= data
->adxl355_hpf_3db_table
[data
->hpf_3db
][0];
544 *val2
= data
->adxl355_hpf_3db_table
[data
->hpf_3db
][1];
545 return IIO_VAL_INT_PLUS_MICRO
;
551 static int adxl355_write_raw(struct iio_dev
*indio_dev
,
552 struct iio_chan_spec
const *chan
,
553 int val
, int val2
, long mask
)
555 struct adxl355_data
*data
= iio_priv(indio_dev
);
556 int odr_idx
, hpf_idx
, calibbias
;
559 case IIO_CHAN_INFO_SAMP_FREQ
:
560 odr_idx
= adxl355_find_match(adxl355_odr_table
,
561 ARRAY_SIZE(adxl355_odr_table
),
566 return adxl355_set_odr(data
, odr_idx
);
567 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
568 hpf_idx
= adxl355_find_match(data
->adxl355_hpf_3db_table
,
569 ARRAY_SIZE(data
->adxl355_hpf_3db_table
),
574 return adxl355_set_hpf_3db(data
, hpf_idx
);
575 case IIO_CHAN_INFO_CALIBBIAS
:
576 calibbias
= clamp_t(int, val
, S16_MIN
, S16_MAX
);
578 return adxl355_set_calibbias(data
, chan
->address
, calibbias
);
584 static int adxl355_read_avail(struct iio_dev
*indio_dev
,
585 struct iio_chan_spec
const *chan
,
586 const int **vals
, int *type
, int *length
,
589 struct adxl355_data
*data
= iio_priv(indio_dev
);
592 case IIO_CHAN_INFO_SAMP_FREQ
:
593 *vals
= (const int *)adxl355_odr_table
;
594 *type
= IIO_VAL_INT_PLUS_MICRO
;
595 /* Values are stored in a 2D matrix */
596 *length
= ARRAY_SIZE(adxl355_odr_table
) * 2;
598 return IIO_AVAIL_LIST
;
599 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
600 *vals
= (const int *)data
->adxl355_hpf_3db_table
;
601 *type
= IIO_VAL_INT_PLUS_MICRO
;
602 /* Values are stored in a 2D matrix */
603 *length
= ARRAY_SIZE(data
->adxl355_hpf_3db_table
) * 2;
605 return IIO_AVAIL_LIST
;
611 static const unsigned long adxl355_avail_scan_masks
[] = {
616 static const struct iio_info adxl355_info
= {
617 .read_raw
= adxl355_read_raw
,
618 .write_raw
= adxl355_write_raw
,
619 .read_avail
= &adxl355_read_avail
,
622 static const struct iio_trigger_ops adxl355_trigger_ops
= {
623 .set_trigger_state
= &adxl355_data_rdy_trigger_set_state
,
624 .validate_device
= &iio_trigger_validate_own_device
,
627 static irqreturn_t
adxl355_trigger_handler(int irq
, void *p
)
629 struct iio_poll_func
*pf
= p
;
630 struct iio_dev
*indio_dev
= pf
->indio_dev
;
631 struct adxl355_data
*data
= iio_priv(indio_dev
);
634 mutex_lock(&data
->lock
);
637 * data->buffer is used both for triggered buffer support
638 * and read/write_raw(), hence, it has to be zeroed here before usage.
640 data
->buffer
.buf
[0] = 0;
643 * The acceleration data is 24 bits and big endian. It has to be saved
644 * in 32 bits, hence, it is saved in the 2nd byte of the 4 byte buffer.
645 * The buf array is 14 bytes as it includes 3x4=12 bytes for
646 * acceleration data of x, y, and z axis. It also includes 2 bytes for
649 ret
= regmap_bulk_read(data
->regmap
, ADXL355_XDATA3_REG
,
650 &data
->buffer
.buf
[1], 3);
652 goto out_unlock_notify
;
654 ret
= regmap_bulk_read(data
->regmap
, ADXL355_YDATA3_REG
,
655 &data
->buffer
.buf
[5], 3);
657 goto out_unlock_notify
;
659 ret
= regmap_bulk_read(data
->regmap
, ADXL355_ZDATA3_REG
,
660 &data
->buffer
.buf
[9], 3);
662 goto out_unlock_notify
;
664 ret
= regmap_bulk_read(data
->regmap
, ADXL355_TEMP2_REG
,
665 &data
->buffer
.buf
[12], 2);
667 goto out_unlock_notify
;
669 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->buffer
,
673 mutex_unlock(&data
->lock
);
674 iio_trigger_notify_done(indio_dev
->trig
);
679 #define ADXL355_ACCEL_CHANNEL(index, reg, axis) { \
683 .channel2 = IIO_MOD_##axis, \
684 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
685 BIT(IIO_CHAN_INFO_CALIBBIAS), \
686 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
687 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
688 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
689 .info_mask_shared_by_type_available = \
690 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
691 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
692 .scan_index = index, \
698 .endianness = IIO_BE, \
702 static const struct iio_chan_spec adxl355_channels
[] = {
703 ADXL355_ACCEL_CHANNEL(0, chan_x
, X
),
704 ADXL355_ACCEL_CHANNEL(1, chan_y
, Y
),
705 ADXL355_ACCEL_CHANNEL(2, chan_z
, Z
),
708 .address
= ADXL355_TEMP2_REG
,
709 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
710 BIT(IIO_CHAN_INFO_SCALE
) |
711 BIT(IIO_CHAN_INFO_OFFSET
),
717 .endianness
= IIO_BE
,
720 IIO_CHAN_SOFT_TIMESTAMP(4),
723 static int adxl355_probe_trigger(struct iio_dev
*indio_dev
, int irq
)
725 struct adxl355_data
*data
= iio_priv(indio_dev
);
728 data
->dready_trig
= devm_iio_trigger_alloc(data
->dev
, "%s-dev%d",
730 iio_device_id(indio_dev
));
731 if (!data
->dready_trig
)
734 data
->dready_trig
->ops
= &adxl355_trigger_ops
;
735 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
737 ret
= devm_request_irq(data
->dev
, irq
,
738 &iio_trigger_generic_data_rdy_poll
,
739 IRQF_ONESHOT
, "adxl355_irq", data
->dready_trig
);
741 return dev_err_probe(data
->dev
, ret
, "request irq %d failed\n",
744 ret
= devm_iio_trigger_register(data
->dev
, data
->dready_trig
);
746 dev_err(data
->dev
, "iio trigger register failed\n");
750 indio_dev
->trig
= iio_trigger_get(data
->dready_trig
);
755 int adxl355_core_probe(struct device
*dev
, struct regmap
*regmap
,
756 const struct adxl355_chip_info
*chip_info
)
758 struct adxl355_data
*data
;
759 struct iio_dev
*indio_dev
;
763 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
767 data
= iio_priv(indio_dev
);
768 data
->regmap
= regmap
;
770 data
->op_mode
= ADXL355_STANDBY
;
771 data
->chip_info
= chip_info
;
772 mutex_init(&data
->lock
);
774 indio_dev
->name
= chip_info
->name
;
775 indio_dev
->info
= &adxl355_info
;
776 indio_dev
->modes
= INDIO_DIRECT_MODE
;
777 indio_dev
->channels
= adxl355_channels
;
778 indio_dev
->num_channels
= ARRAY_SIZE(adxl355_channels
);
779 indio_dev
->available_scan_masks
= adxl355_avail_scan_masks
;
781 ret
= adxl355_setup(data
);
783 dev_err(dev
, "ADXL355 setup failed\n");
787 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
788 &iio_pollfunc_store_time
,
789 &adxl355_trigger_handler
, NULL
);
791 dev_err(dev
, "iio triggered buffer setup failed\n");
795 irq
= fwnode_irq_get_byname(dev_fwnode(dev
), "DRDY");
797 ret
= adxl355_probe_trigger(indio_dev
, irq
);
802 return devm_iio_device_register(dev
, indio_dev
);
804 EXPORT_SYMBOL_NS_GPL(adxl355_core_probe
, "IIO_ADXL355");
806 MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
807 MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer core driver");
808 MODULE_LICENSE("GPL v2");