xref: /linux/drivers/iio/accel/sca3300.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
19cc9806eSTomas Melin // SPDX-License-Identifier: GPL-2.0-only
29cc9806eSTomas Melin /*
39cc9806eSTomas Melin  * Murata SCA3300 3-axis industrial accelerometer
49cc9806eSTomas Melin  *
59cc9806eSTomas Melin  * Copyright (c) 2021 Vaisala Oyj. All rights reserved.
69cc9806eSTomas Melin  */
79cc9806eSTomas Melin 
89cc9806eSTomas Melin #include <linux/bitops.h>
99cc9806eSTomas Melin #include <linux/crc8.h>
109cc9806eSTomas Melin #include <linux/delay.h>
119cc9806eSTomas Melin #include <linux/kernel.h>
129cc9806eSTomas Melin #include <linux/module.h>
139cc9806eSTomas Melin #include <linux/spi/spi.h>
149cc9806eSTomas Melin 
15*5f60d5f6SAl Viro #include <linux/unaligned.h>
169cc9806eSTomas Melin 
179cc9806eSTomas Melin #include <linux/iio/buffer.h>
189cc9806eSTomas Melin #include <linux/iio/iio.h>
199cc9806eSTomas Melin #include <linux/iio/sysfs.h>
209cc9806eSTomas Melin #include <linux/iio/trigger_consumer.h>
219cc9806eSTomas Melin #include <linux/iio/triggered_buffer.h>
229cc9806eSTomas Melin 
239cc9806eSTomas Melin #define SCA3300_ALIAS "sca3300"
249cc9806eSTomas Melin 
259cc9806eSTomas Melin #define SCA3300_CRC8_POLYNOMIAL 0x1d
269cc9806eSTomas Melin 
279cc9806eSTomas Melin /* Device mode register */
289cc9806eSTomas Melin #define SCA3300_REG_MODE	0xd
299cc9806eSTomas Melin #define SCA3300_MODE_SW_RESET	0x20
309cc9806eSTomas Melin 
319cc9806eSTomas Melin /* Last register in map */
329cc9806eSTomas Melin #define SCA3300_REG_SELBANK	0x1f
339cc9806eSTomas Melin 
349cc9806eSTomas Melin /* Device status and mask */
359cc9806eSTomas Melin #define SCA3300_REG_STATUS	0x6
369cc9806eSTomas Melin #define SCA3300_STATUS_MASK	GENMASK(8, 0)
379cc9806eSTomas Melin 
389cc9806eSTomas Melin /* Device ID */
399cc9806eSTomas Melin #define SCA3300_REG_WHOAMI	0x10
409cc9806eSTomas Melin #define SCA3300_WHOAMI_ID	0x51
4124fb2d3cSLI Qingwu #define SCL3300_WHOAMI_ID	0xC1
429cc9806eSTomas Melin 
439cc9806eSTomas Melin /* Device return status and mask */
449cc9806eSTomas Melin #define SCA3300_VALUE_RS_ERROR	0x3
459cc9806eSTomas Melin #define SCA3300_MASK_RS_STATUS	GENMASK(1, 0)
469cc9806eSTomas Melin 
4738d5cd1eSLI Qingwu #define SCL3300_REG_ANG_CTRL 0x0C
4838d5cd1eSLI Qingwu #define SCL3300_ANG_ENABLE   0x1F
4938d5cd1eSLI Qingwu 
509cc9806eSTomas Melin enum sca3300_scan_indexes {
519cc9806eSTomas Melin 	SCA3300_ACC_X = 0,
529cc9806eSTomas Melin 	SCA3300_ACC_Y,
539cc9806eSTomas Melin 	SCA3300_ACC_Z,
549cc9806eSTomas Melin 	SCA3300_TEMP,
5538d5cd1eSLI Qingwu 	SCA3300_INCLI_X,
5638d5cd1eSLI Qingwu 	SCA3300_INCLI_Y,
5738d5cd1eSLI Qingwu 	SCA3300_INCLI_Z,
583cfb0e1dSLI Qingwu 	SCA3300_SCAN_MAX
599cc9806eSTomas Melin };
609cc9806eSTomas Melin 
613cfb0e1dSLI Qingwu /*
623cfb0e1dSLI Qingwu  * Buffer size max case:
633cfb0e1dSLI Qingwu  * Three accel channels, two bytes per channel.
643cfb0e1dSLI Qingwu  * Temperature channel, two bytes.
653cfb0e1dSLI Qingwu  * Three incli channels, two bytes per channel.
663cfb0e1dSLI Qingwu  * Timestamp channel, eight bytes.
673cfb0e1dSLI Qingwu  */
683cfb0e1dSLI Qingwu #define SCA3300_MAX_BUFFER_SIZE (ALIGN(sizeof(s16) * SCA3300_SCAN_MAX, sizeof(s64)) + sizeof(s64))
693cfb0e1dSLI Qingwu 
709cc9806eSTomas Melin #define SCA3300_ACCEL_CHANNEL(index, reg, axis) {			\
719cc9806eSTomas Melin 	.type = IIO_ACCEL,						\
729cc9806eSTomas Melin 	.address = reg,							\
739cc9806eSTomas Melin 	.modified = 1,							\
749cc9806eSTomas Melin 	.channel2 = IIO_MOD_##axis,					\
759cc9806eSTomas Melin 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
769cc9806eSTomas Melin 	.info_mask_shared_by_type =					\
779cc9806eSTomas Melin 	BIT(IIO_CHAN_INFO_SCALE) |					\
789cc9806eSTomas Melin 	BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),		\
799cc9806eSTomas Melin 	.info_mask_shared_by_type_available =				\
809cc9806eSTomas Melin 	BIT(IIO_CHAN_INFO_SCALE) |					\
819cc9806eSTomas Melin 	BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),		\
829cc9806eSTomas Melin 	.scan_index = index,						\
839cc9806eSTomas Melin 	.scan_type = {							\
849cc9806eSTomas Melin 		.sign = 's',						\
859cc9806eSTomas Melin 		.realbits = 16,						\
869cc9806eSTomas Melin 		.storagebits = 16,					\
879cc9806eSTomas Melin 		.endianness = IIO_CPU,					\
889cc9806eSTomas Melin 	},								\
899cc9806eSTomas Melin }
909cc9806eSTomas Melin 
9138d5cd1eSLI Qingwu #define SCA3300_INCLI_CHANNEL(index, reg, axis) {			\
9238d5cd1eSLI Qingwu 	.type = IIO_INCLI,						\
9338d5cd1eSLI Qingwu 	.address = reg,							\
9438d5cd1eSLI Qingwu 	.modified = 1,							\
9538d5cd1eSLI Qingwu 	.channel2 = IIO_MOD_##axis,					\
9638d5cd1eSLI Qingwu 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
9738d5cd1eSLI Qingwu 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
9838d5cd1eSLI Qingwu 	.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
9938d5cd1eSLI Qingwu 	.scan_index = index,						\
10038d5cd1eSLI Qingwu 	.scan_type = {							\
10138d5cd1eSLI Qingwu 		.sign = 's',						\
10238d5cd1eSLI Qingwu 		.realbits = 16,						\
10338d5cd1eSLI Qingwu 		.storagebits = 16,					\
10438d5cd1eSLI Qingwu 		.endianness = IIO_CPU,					\
10538d5cd1eSLI Qingwu 	},								\
10638d5cd1eSLI Qingwu }
10738d5cd1eSLI Qingwu 
108e59dd3acSLI Qingwu #define SCA3300_TEMP_CHANNEL(index, reg) {				\
109e59dd3acSLI Qingwu 		.type = IIO_TEMP,					\
110e59dd3acSLI Qingwu 		.address = reg,						\
111e59dd3acSLI Qingwu 		.scan_index = index,					\
112e59dd3acSLI Qingwu 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
113e59dd3acSLI Qingwu 		.scan_type = {						\
114e59dd3acSLI Qingwu 			.sign = 's',					\
115e59dd3acSLI Qingwu 			.realbits = 16,					\
116e59dd3acSLI Qingwu 			.storagebits = 16,				\
117e59dd3acSLI Qingwu 			.endianness = IIO_CPU,				\
118e59dd3acSLI Qingwu 		},							\
119e59dd3acSLI Qingwu }
120e59dd3acSLI Qingwu 
1219cc9806eSTomas Melin static const struct iio_chan_spec sca3300_channels[] = {
1229cc9806eSTomas Melin 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_X, 0x1, X),
1239cc9806eSTomas Melin 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_Y, 0x2, Y),
1249cc9806eSTomas Melin 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_Z, 0x3, Z),
125e59dd3acSLI Qingwu 	SCA3300_TEMP_CHANNEL(SCA3300_TEMP, 0x05),
1269cc9806eSTomas Melin 	IIO_CHAN_SOFT_TIMESTAMP(4),
1279cc9806eSTomas Melin };
1289cc9806eSTomas Melin 
129ad985d4dSLI Qingwu static const int sca3300_lp_freq[] = {70, 10};
130ad985d4dSLI Qingwu static const int sca3300_lp_freq_map[] = {0, 0, 0, 1};
1319cc9806eSTomas Melin 
13224fb2d3cSLI Qingwu static const int scl3300_lp_freq[] = {40, 70, 10};
13324fb2d3cSLI Qingwu static const int scl3300_lp_freq_map[] = {0, 1, 2};
13424fb2d3cSLI Qingwu 
135ad985d4dSLI Qingwu static const int sca3300_accel_scale[][2] = {{0, 370}, {0, 741}, {0, 185}};
136ad985d4dSLI Qingwu static const int sca3300_accel_scale_map[] = {0, 1, 2, 2};
137ad985d4dSLI Qingwu 
13824fb2d3cSLI Qingwu static const int scl3300_accel_scale[][2] = {{0, 167}, {0, 333}, {0, 83}};
13924fb2d3cSLI Qingwu static const int scl3300_accel_scale_map[] = {0, 1, 2};
14024fb2d3cSLI Qingwu 
14138d5cd1eSLI Qingwu static const int scl3300_incli_scale[][2] = {{0, 5495}};
14238d5cd1eSLI Qingwu static const int scl3300_incli_scale_map[] = {0, 0, 0};
14338d5cd1eSLI Qingwu 
144ad985d4dSLI Qingwu static const int sca3300_avail_modes_map[] = {0, 1, 2, 3};
14524fb2d3cSLI Qingwu static const int scl3300_avail_modes_map[] = {0, 1, 3};
14624fb2d3cSLI Qingwu 
14738d5cd1eSLI Qingwu static const struct iio_chan_spec scl3300_channels[] = {
14838d5cd1eSLI Qingwu 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_X, 0x1, X),
14938d5cd1eSLI Qingwu 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_Y, 0x2, Y),
15038d5cd1eSLI Qingwu 	SCA3300_ACCEL_CHANNEL(SCA3300_ACC_Z, 0x3, Z),
15138d5cd1eSLI Qingwu 	SCA3300_TEMP_CHANNEL(SCA3300_TEMP, 0x05),
15238d5cd1eSLI Qingwu 	SCA3300_INCLI_CHANNEL(SCA3300_INCLI_X, 0x09, X),
15338d5cd1eSLI Qingwu 	SCA3300_INCLI_CHANNEL(SCA3300_INCLI_Y, 0x0A, Y),
15438d5cd1eSLI Qingwu 	SCA3300_INCLI_CHANNEL(SCA3300_INCLI_Z, 0x0B, Z),
1553cfb0e1dSLI Qingwu 	IIO_CHAN_SOFT_TIMESTAMP(7),
15638d5cd1eSLI Qingwu };
15738d5cd1eSLI Qingwu 
1589cc9806eSTomas Melin static const unsigned long sca3300_scan_masks[] = {
1599cc9806eSTomas Melin 	BIT(SCA3300_ACC_X) | BIT(SCA3300_ACC_Y) | BIT(SCA3300_ACC_Z) |
1609cc9806eSTomas Melin 	BIT(SCA3300_TEMP),
1619cc9806eSTomas Melin 	0
1629cc9806eSTomas Melin };
1639cc9806eSTomas Melin 
16438d5cd1eSLI Qingwu static const unsigned long scl3300_scan_masks[] = {
16538d5cd1eSLI Qingwu 	BIT(SCA3300_ACC_X) | BIT(SCA3300_ACC_Y) | BIT(SCA3300_ACC_Z) |
16638d5cd1eSLI Qingwu 	BIT(SCA3300_TEMP) |
16738d5cd1eSLI Qingwu 	BIT(SCA3300_INCLI_X) | BIT(SCA3300_INCLI_Y) | BIT(SCA3300_INCLI_Z),
16838d5cd1eSLI Qingwu 	0
16938d5cd1eSLI Qingwu };
17038d5cd1eSLI Qingwu 
171ad985d4dSLI Qingwu struct sca3300_chip_info {
172ad985d4dSLI Qingwu 	const char *name;
173ad985d4dSLI Qingwu 	const unsigned long *scan_masks;
174ad985d4dSLI Qingwu 	const struct iio_chan_spec *channels;
175ad985d4dSLI Qingwu 	u8 num_channels;
176ad985d4dSLI Qingwu 	u8 num_accel_scales;
177ad985d4dSLI Qingwu 	const int (*accel_scale)[2];
178ad985d4dSLI Qingwu 	const int *accel_scale_map;
17938d5cd1eSLI Qingwu 	const int (*incli_scale)[2];
18038d5cd1eSLI Qingwu 	const int *incli_scale_map;
18138d5cd1eSLI Qingwu 	u8 num_incli_scales;
182ad985d4dSLI Qingwu 	u8 num_freqs;
183ad985d4dSLI Qingwu 	const int *freq_table;
184ad985d4dSLI Qingwu 	const int *freq_map;
185ad985d4dSLI Qingwu 	const int *avail_modes_table;
186ad985d4dSLI Qingwu 	u8 num_avail_modes;
187ad985d4dSLI Qingwu 	u8 chip_id;
18838d5cd1eSLI Qingwu 	bool angle_supported;
189ad985d4dSLI Qingwu };
190ad985d4dSLI Qingwu 
1919cc9806eSTomas Melin /**
1929cc9806eSTomas Melin  * struct sca3300_data - device data
1939cc9806eSTomas Melin  * @spi: SPI device structure
1949cc9806eSTomas Melin  * @lock: Data buffer lock
195ad985d4dSLI Qingwu  * @chip: Sensor chip specific information
1963cfb0e1dSLI Qingwu  * @buffer: Triggered buffer:
1973cfb0e1dSLI Qingwu  *          -SCA3300: 4 channel 16-bit data + 64-bit timestamp
1983cfb0e1dSLI Qingwu  *          -SCL3300: 7 channel 16-bit data + 64-bit timestamp
1999cc9806eSTomas Melin  * @txbuf: Transmit buffer
2009cc9806eSTomas Melin  * @rxbuf: Receive buffer
2019cc9806eSTomas Melin  */
2029cc9806eSTomas Melin struct sca3300_data {
2039cc9806eSTomas Melin 	struct spi_device *spi;
2049cc9806eSTomas Melin 	struct mutex lock;
205ad985d4dSLI Qingwu 	const struct sca3300_chip_info *chip;
2063cfb0e1dSLI Qingwu 	u8 buffer[SCA3300_MAX_BUFFER_SIZE] __aligned(sizeof(s64));
207b1d3a806SJonathan Cameron 	u8 txbuf[4] __aligned(IIO_DMA_MINALIGN);
2089cc9806eSTomas Melin 	u8 rxbuf[4];
2099cc9806eSTomas Melin };
2109cc9806eSTomas Melin 
211ad985d4dSLI Qingwu static const struct sca3300_chip_info sca3300_chip_tbl[] = {
212ad985d4dSLI Qingwu 	{
213ad985d4dSLI Qingwu 		.name = "sca3300",
214ad985d4dSLI Qingwu 		.scan_masks = sca3300_scan_masks,
215ad985d4dSLI Qingwu 		.channels = sca3300_channels,
216ad985d4dSLI Qingwu 		.num_channels = ARRAY_SIZE(sca3300_channels),
217ad985d4dSLI Qingwu 		.num_accel_scales = ARRAY_SIZE(sca3300_accel_scale)*2,
218ad985d4dSLI Qingwu 		.accel_scale = sca3300_accel_scale,
219ad985d4dSLI Qingwu 		.accel_scale_map = sca3300_accel_scale_map,
220ad985d4dSLI Qingwu 		.num_freqs = ARRAY_SIZE(sca3300_lp_freq),
221ad985d4dSLI Qingwu 		.freq_table = sca3300_lp_freq,
222ad985d4dSLI Qingwu 		.freq_map = sca3300_lp_freq_map,
223ad985d4dSLI Qingwu 		.avail_modes_table = sca3300_avail_modes_map,
224ad985d4dSLI Qingwu 		.num_avail_modes = 4,
225ad985d4dSLI Qingwu 		.chip_id = SCA3300_WHOAMI_ID,
22638d5cd1eSLI Qingwu 		.angle_supported = false,
227ad985d4dSLI Qingwu 	},
22824fb2d3cSLI Qingwu 	{
22924fb2d3cSLI Qingwu 		.name = "scl3300",
23038d5cd1eSLI Qingwu 		.scan_masks = scl3300_scan_masks,
23138d5cd1eSLI Qingwu 		.channels = scl3300_channels,
23238d5cd1eSLI Qingwu 		.num_channels = ARRAY_SIZE(scl3300_channels),
23324fb2d3cSLI Qingwu 		.num_accel_scales = ARRAY_SIZE(scl3300_accel_scale)*2,
23424fb2d3cSLI Qingwu 		.accel_scale = scl3300_accel_scale,
23524fb2d3cSLI Qingwu 		.accel_scale_map = scl3300_accel_scale_map,
23638d5cd1eSLI Qingwu 		.incli_scale = scl3300_incli_scale,
23738d5cd1eSLI Qingwu 		.incli_scale_map = scl3300_incli_scale_map,
23838d5cd1eSLI Qingwu 		.num_incli_scales =  ARRAY_SIZE(scl3300_incli_scale)*2,
23924fb2d3cSLI Qingwu 		.num_freqs = ARRAY_SIZE(scl3300_lp_freq),
24024fb2d3cSLI Qingwu 		.freq_table = scl3300_lp_freq,
24124fb2d3cSLI Qingwu 		.freq_map = scl3300_lp_freq_map,
24224fb2d3cSLI Qingwu 		.avail_modes_table = scl3300_avail_modes_map,
24324fb2d3cSLI Qingwu 		.num_avail_modes = 3,
24424fb2d3cSLI Qingwu 		.chip_id = SCL3300_WHOAMI_ID,
24538d5cd1eSLI Qingwu 		.angle_supported = true,
24624fb2d3cSLI Qingwu 	},
247ad985d4dSLI Qingwu };
248ad985d4dSLI Qingwu 
2499cc9806eSTomas Melin DECLARE_CRC8_TABLE(sca3300_crc_table);
2509cc9806eSTomas Melin 
sca3300_transfer(struct sca3300_data * sca_data,int * val)2519cc9806eSTomas Melin static int sca3300_transfer(struct sca3300_data *sca_data, int *val)
2529cc9806eSTomas Melin {
2539cc9806eSTomas Melin 	/* Consecutive requests min. 10 us delay (Datasheet section 5.1.2) */
2549cc9806eSTomas Melin 	struct spi_delay delay = { .value = 10, .unit = SPI_DELAY_UNIT_USECS };
2559cc9806eSTomas Melin 	int32_t ret;
2569cc9806eSTomas Melin 	int rs;
2579cc9806eSTomas Melin 	u8 crc;
2589cc9806eSTomas Melin 	struct spi_transfer xfers[2] = {
2599cc9806eSTomas Melin 		{
2609cc9806eSTomas Melin 			.tx_buf = sca_data->txbuf,
2619cc9806eSTomas Melin 			.len = ARRAY_SIZE(sca_data->txbuf),
2629cc9806eSTomas Melin 			.delay = delay,
2639cc9806eSTomas Melin 			.cs_change = 1,
2649cc9806eSTomas Melin 		},
2659cc9806eSTomas Melin 		{
2669cc9806eSTomas Melin 			.rx_buf = sca_data->rxbuf,
2679cc9806eSTomas Melin 			.len = ARRAY_SIZE(sca_data->rxbuf),
2689cc9806eSTomas Melin 			.delay = delay,
2699cc9806eSTomas Melin 		}
2709cc9806eSTomas Melin 	};
2719cc9806eSTomas Melin 
2729cc9806eSTomas Melin 	/* inverted crc value as described in device data sheet */
2739cc9806eSTomas Melin 	crc = ~crc8(sca3300_crc_table, &sca_data->txbuf[0], 3, CRC8_INIT_VALUE);
2749cc9806eSTomas Melin 	sca_data->txbuf[3] = crc;
2759cc9806eSTomas Melin 
2769cc9806eSTomas Melin 	ret = spi_sync_transfer(sca_data->spi, xfers, ARRAY_SIZE(xfers));
2779cc9806eSTomas Melin 	if (ret) {
2789cc9806eSTomas Melin 		dev_err(&sca_data->spi->dev,
2799cc9806eSTomas Melin 			"transfer error, error: %d\n", ret);
2809cc9806eSTomas Melin 		return -EIO;
2819cc9806eSTomas Melin 	}
2829cc9806eSTomas Melin 
2839cc9806eSTomas Melin 	crc = ~crc8(sca3300_crc_table, &sca_data->rxbuf[0], 3, CRC8_INIT_VALUE);
2849cc9806eSTomas Melin 	if (sca_data->rxbuf[3] != crc) {
2859cc9806eSTomas Melin 		dev_err(&sca_data->spi->dev, "CRC checksum mismatch");
2869cc9806eSTomas Melin 		return -EIO;
2879cc9806eSTomas Melin 	}
2889cc9806eSTomas Melin 
2899cc9806eSTomas Melin 	/* get return status */
2909cc9806eSTomas Melin 	rs = sca_data->rxbuf[0] & SCA3300_MASK_RS_STATUS;
2919cc9806eSTomas Melin 	if (rs == SCA3300_VALUE_RS_ERROR)
2929cc9806eSTomas Melin 		ret = -EINVAL;
2939cc9806eSTomas Melin 
2949cc9806eSTomas Melin 	*val = sign_extend32(get_unaligned_be16(&sca_data->rxbuf[1]), 15);
2959cc9806eSTomas Melin 
2969cc9806eSTomas Melin 	return ret;
2979cc9806eSTomas Melin }
2989cc9806eSTomas Melin 
sca3300_error_handler(struct sca3300_data * sca_data)2999cc9806eSTomas Melin static int sca3300_error_handler(struct sca3300_data *sca_data)
3009cc9806eSTomas Melin {
3019cc9806eSTomas Melin 	int ret;
3029cc9806eSTomas Melin 	int val;
3039cc9806eSTomas Melin 
3049cc9806eSTomas Melin 	mutex_lock(&sca_data->lock);
3059cc9806eSTomas Melin 	sca_data->txbuf[0] = SCA3300_REG_STATUS << 2;
3069cc9806eSTomas Melin 	ret = sca3300_transfer(sca_data, &val);
3079cc9806eSTomas Melin 	mutex_unlock(&sca_data->lock);
3089cc9806eSTomas Melin 	/*
3099cc9806eSTomas Melin 	 * Return status error is cleared after reading status register once,
3109cc9806eSTomas Melin 	 * expect EINVAL here.
3119cc9806eSTomas Melin 	 */
3129cc9806eSTomas Melin 	if (ret != -EINVAL) {
3139cc9806eSTomas Melin 		dev_err(&sca_data->spi->dev,
3149cc9806eSTomas Melin 			"error reading device status: %d\n", ret);
3159cc9806eSTomas Melin 		return ret;
3169cc9806eSTomas Melin 	}
3179cc9806eSTomas Melin 
3189cc9806eSTomas Melin 	dev_err(&sca_data->spi->dev, "device status: 0x%lx\n",
3199cc9806eSTomas Melin 		val & SCA3300_STATUS_MASK);
3209cc9806eSTomas Melin 
3219cc9806eSTomas Melin 	return 0;
3229cc9806eSTomas Melin }
3239cc9806eSTomas Melin 
sca3300_read_reg(struct sca3300_data * sca_data,u8 reg,int * val)3249cc9806eSTomas Melin static int sca3300_read_reg(struct sca3300_data *sca_data, u8 reg, int *val)
3259cc9806eSTomas Melin {
3269cc9806eSTomas Melin 	int ret;
3279cc9806eSTomas Melin 
3289cc9806eSTomas Melin 	mutex_lock(&sca_data->lock);
3299cc9806eSTomas Melin 	sca_data->txbuf[0] = reg << 2;
3309cc9806eSTomas Melin 	ret = sca3300_transfer(sca_data, val);
3319cc9806eSTomas Melin 	mutex_unlock(&sca_data->lock);
3329cc9806eSTomas Melin 	if (ret != -EINVAL)
3339cc9806eSTomas Melin 		return ret;
3349cc9806eSTomas Melin 
3359cc9806eSTomas Melin 	return sca3300_error_handler(sca_data);
3369cc9806eSTomas Melin }
3379cc9806eSTomas Melin 
sca3300_write_reg(struct sca3300_data * sca_data,u8 reg,int val)3389cc9806eSTomas Melin static int sca3300_write_reg(struct sca3300_data *sca_data, u8 reg, int val)
3399cc9806eSTomas Melin {
3409cc9806eSTomas Melin 	int reg_val = 0;
3419cc9806eSTomas Melin 	int ret;
3429cc9806eSTomas Melin 
3439cc9806eSTomas Melin 	mutex_lock(&sca_data->lock);
3449cc9806eSTomas Melin 	/* BIT(7) for write operation */
3459cc9806eSTomas Melin 	sca_data->txbuf[0] = BIT(7) | (reg << 2);
3469cc9806eSTomas Melin 	put_unaligned_be16(val, &sca_data->txbuf[1]);
3479cc9806eSTomas Melin 	ret = sca3300_transfer(sca_data, &reg_val);
3489cc9806eSTomas Melin 	mutex_unlock(&sca_data->lock);
3499cc9806eSTomas Melin 	if (ret != -EINVAL)
3509cc9806eSTomas Melin 		return ret;
3519cc9806eSTomas Melin 
3529cc9806eSTomas Melin 	return sca3300_error_handler(sca_data);
3539cc9806eSTomas Melin }
3549cc9806eSTomas Melin 
sca3300_set_op_mode(struct sca3300_data * sca_data,int index)355ad985d4dSLI Qingwu static int sca3300_set_op_mode(struct sca3300_data *sca_data, int index)
356ad985d4dSLI Qingwu {
357ad985d4dSLI Qingwu 	if ((index < 0) || (index >= sca_data->chip->num_avail_modes))
358ad985d4dSLI Qingwu 		return -EINVAL;
359ad985d4dSLI Qingwu 
360ad985d4dSLI Qingwu 	return sca3300_write_reg(sca_data, SCA3300_REG_MODE,
361ad985d4dSLI Qingwu 				 sca_data->chip->avail_modes_table[index]);
362ad985d4dSLI Qingwu }
363ad985d4dSLI Qingwu 
sca3300_get_op_mode(struct sca3300_data * sca_data,int * index)364ad985d4dSLI Qingwu static int sca3300_get_op_mode(struct sca3300_data *sca_data, int *index)
365ad985d4dSLI Qingwu {
366ad985d4dSLI Qingwu 	int reg_val;
367ad985d4dSLI Qingwu 	int ret;
368ad985d4dSLI Qingwu 	int i;
369ad985d4dSLI Qingwu 
370ad985d4dSLI Qingwu 	ret = sca3300_read_reg(sca_data, SCA3300_REG_MODE, &reg_val);
371ad985d4dSLI Qingwu 	if (ret)
372ad985d4dSLI Qingwu 		return ret;
373ad985d4dSLI Qingwu 
374ad985d4dSLI Qingwu 	for (i = 0; i < sca_data->chip->num_avail_modes; i++) {
375ad985d4dSLI Qingwu 		if (sca_data->chip->avail_modes_table[i] == reg_val)
376ad985d4dSLI Qingwu 			break;
377ad985d4dSLI Qingwu 	}
378ad985d4dSLI Qingwu 	if (i == sca_data->chip->num_avail_modes)
379ad985d4dSLI Qingwu 		return -EINVAL;
380ad985d4dSLI Qingwu 
381ad985d4dSLI Qingwu 	*index = i;
382ad985d4dSLI Qingwu 	return 0;
383ad985d4dSLI Qingwu }
384ad985d4dSLI Qingwu 
sca3300_set_frequency(struct sca3300_data * data,int val)385ad985d4dSLI Qingwu static int sca3300_set_frequency(struct sca3300_data *data, int val)
386ad985d4dSLI Qingwu {
387ad985d4dSLI Qingwu 	const struct sca3300_chip_info *chip = data->chip;
388ad985d4dSLI Qingwu 	unsigned int index;
389ad985d4dSLI Qingwu 	int *opmode_scale;
390ad985d4dSLI Qingwu 	int *new_scale;
391ad985d4dSLI Qingwu 	unsigned int i;
392ad985d4dSLI Qingwu 
393ad985d4dSLI Qingwu 	if (sca3300_get_op_mode(data, &index))
394ad985d4dSLI Qingwu 		return -EINVAL;
395ad985d4dSLI Qingwu 
396ad985d4dSLI Qingwu 	/*
397ad985d4dSLI Qingwu 	 * Find a mode in which the requested sampling frequency is available
398ad985d4dSLI Qingwu 	 * and the scaling currently set is retained.
399ad985d4dSLI Qingwu 	 */
400ad985d4dSLI Qingwu 	opmode_scale = (int *)chip->accel_scale[chip->accel_scale_map[index]];
401ad985d4dSLI Qingwu 	for (i = 0; i < chip->num_avail_modes; i++) {
402ad985d4dSLI Qingwu 		new_scale = (int *)chip->accel_scale[chip->accel_scale_map[i]];
403ad985d4dSLI Qingwu 		if ((val == chip->freq_table[chip->freq_map[i]]) &&
404ad985d4dSLI Qingwu 		    (opmode_scale[1] == new_scale[1]) &&
405ad985d4dSLI Qingwu 		    (opmode_scale[0] == new_scale[0]))
406ad985d4dSLI Qingwu 			break;
407ad985d4dSLI Qingwu 	}
408ad985d4dSLI Qingwu 	if (i == chip->num_avail_modes)
409ad985d4dSLI Qingwu 		return -EINVAL;
410ad985d4dSLI Qingwu 
411ad985d4dSLI Qingwu 	return sca3300_set_op_mode(data, i);
412ad985d4dSLI Qingwu }
413ad985d4dSLI Qingwu 
sca3300_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)4149cc9806eSTomas Melin static int sca3300_write_raw(struct iio_dev *indio_dev,
4159cc9806eSTomas Melin 			     struct iio_chan_spec const *chan,
4169cc9806eSTomas Melin 			     int val, int val2, long mask)
4179cc9806eSTomas Melin {
4189cc9806eSTomas Melin 	struct sca3300_data *data = iio_priv(indio_dev);
419ad985d4dSLI Qingwu 	int index;
4209cc9806eSTomas Melin 	int i;
4219cc9806eSTomas Melin 
4229cc9806eSTomas Melin 	switch (mask) {
4239cc9806eSTomas Melin 	case IIO_CHAN_INFO_SCALE:
424ad985d4dSLI Qingwu 		if (chan->type != IIO_ACCEL)
4259cc9806eSTomas Melin 			return -EINVAL;
426ad985d4dSLI Qingwu 		/*
427ad985d4dSLI Qingwu 		 * Letting scale take priority over sampling frequency.
428ad985d4dSLI Qingwu 		 * That makes sense given we can only ever end up increasing
429ad985d4dSLI Qingwu 		 * the sampling frequency which is unlikely to be a problem.
430ad985d4dSLI Qingwu 		 */
431ad985d4dSLI Qingwu 		for (i = 0; i < data->chip->num_avail_modes; i++) {
432ad985d4dSLI Qingwu 			index = data->chip->accel_scale_map[i];
433ad985d4dSLI Qingwu 			if ((val  == data->chip->accel_scale[index][0]) &&
434ad985d4dSLI Qingwu 			    (val2 == data->chip->accel_scale[index][1]))
435ad985d4dSLI Qingwu 				return sca3300_set_op_mode(data, i);
4369cc9806eSTomas Melin 		}
4379cc9806eSTomas Melin 		return -EINVAL;
4389cc9806eSTomas Melin 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
439ad985d4dSLI Qingwu 		return sca3300_set_frequency(data, val);
4409cc9806eSTomas Melin 	default:
4419cc9806eSTomas Melin 		return -EINVAL;
4429cc9806eSTomas Melin 	}
4439cc9806eSTomas Melin }
4449cc9806eSTomas Melin 
sca3300_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)4459cc9806eSTomas Melin static int sca3300_read_raw(struct iio_dev *indio_dev,
4469cc9806eSTomas Melin 			    struct iio_chan_spec const *chan,
4479cc9806eSTomas Melin 			    int *val, int *val2, long mask)
4489cc9806eSTomas Melin {
4499cc9806eSTomas Melin 	struct sca3300_data *data = iio_priv(indio_dev);
450ad985d4dSLI Qingwu 	int index;
4519cc9806eSTomas Melin 	int ret;
4529cc9806eSTomas Melin 
4539cc9806eSTomas Melin 	switch (mask) {
4549cc9806eSTomas Melin 	case IIO_CHAN_INFO_RAW:
4559cc9806eSTomas Melin 		ret = sca3300_read_reg(data, chan->address, val);
4569cc9806eSTomas Melin 		if (ret)
4579cc9806eSTomas Melin 			return ret;
4589cc9806eSTomas Melin 		return IIO_VAL_INT;
4599cc9806eSTomas Melin 	case IIO_CHAN_INFO_SCALE:
460ad985d4dSLI Qingwu 		ret = sca3300_get_op_mode(data, &index);
4619cc9806eSTomas Melin 		if (ret)
4629cc9806eSTomas Melin 			return ret;
463ad985d4dSLI Qingwu 		switch (chan->type) {
46438d5cd1eSLI Qingwu 		case IIO_INCLI:
46538d5cd1eSLI Qingwu 			index = data->chip->incli_scale_map[index];
46638d5cd1eSLI Qingwu 			*val  = data->chip->incli_scale[index][0];
46738d5cd1eSLI Qingwu 			*val2 = data->chip->incli_scale[index][1];
46838d5cd1eSLI Qingwu 			return IIO_VAL_INT_PLUS_MICRO;
469ad985d4dSLI Qingwu 		case IIO_ACCEL:
470ad985d4dSLI Qingwu 			index = data->chip->accel_scale_map[index];
471ad985d4dSLI Qingwu 			*val  = data->chip->accel_scale[index][0];
472ad985d4dSLI Qingwu 			*val2 = data->chip->accel_scale[index][1];
4739cc9806eSTomas Melin 			return IIO_VAL_INT_PLUS_MICRO;
474ad985d4dSLI Qingwu 		default:
475ad985d4dSLI Qingwu 			return -EINVAL;
476ad985d4dSLI Qingwu 		}
4779cc9806eSTomas Melin 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
478ad985d4dSLI Qingwu 		ret = sca3300_get_op_mode(data, &index);
4799cc9806eSTomas Melin 		if (ret)
4809cc9806eSTomas Melin 			return ret;
481ad985d4dSLI Qingwu 		index = data->chip->freq_map[index];
482ad985d4dSLI Qingwu 		*val = data->chip->freq_table[index];
4839cc9806eSTomas Melin 		return IIO_VAL_INT;
4849cc9806eSTomas Melin 	default:
4859cc9806eSTomas Melin 		return -EINVAL;
4869cc9806eSTomas Melin 	}
4879cc9806eSTomas Melin }
4889cc9806eSTomas Melin 
sca3300_trigger_handler(int irq,void * p)4899cc9806eSTomas Melin static irqreturn_t sca3300_trigger_handler(int irq, void *p)
4909cc9806eSTomas Melin {
4919cc9806eSTomas Melin 	struct iio_poll_func *pf = p;
4929cc9806eSTomas Melin 	struct iio_dev *indio_dev = pf->indio_dev;
4939cc9806eSTomas Melin 	struct sca3300_data *data = iio_priv(indio_dev);
4949cc9806eSTomas Melin 	int bit, ret, val, i = 0;
4953cfb0e1dSLI Qingwu 	s16 *channels = (s16 *)data->buffer;
4969cc9806eSTomas Melin 
497cf7ec085SNuno Sa 	iio_for_each_active_channel(indio_dev, bit) {
4983cfb0e1dSLI Qingwu 		ret = sca3300_read_reg(data, indio_dev->channels[bit].address, &val);
4999cc9806eSTomas Melin 		if (ret) {
5009cc9806eSTomas Melin 			dev_err_ratelimited(&data->spi->dev,
5019cc9806eSTomas Melin 				"failed to read register, error: %d\n", ret);
5029cc9806eSTomas Melin 			/* handled, but bailing out due to errors */
5039cc9806eSTomas Melin 			goto out;
5049cc9806eSTomas Melin 		}
5053cfb0e1dSLI Qingwu 		channels[i++] = val;
5069cc9806eSTomas Melin 	}
5079cc9806eSTomas Melin 
5083cfb0e1dSLI Qingwu 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
5099cc9806eSTomas Melin 					   iio_get_time_ns(indio_dev));
5109cc9806eSTomas Melin out:
5119cc9806eSTomas Melin 	iio_trigger_notify_done(indio_dev->trig);
5129cc9806eSTomas Melin 
5139cc9806eSTomas Melin 	return IRQ_HANDLED;
5149cc9806eSTomas Melin }
5159cc9806eSTomas Melin 
5169cc9806eSTomas Melin /*
5179cc9806eSTomas Melin  * sca3300_init - Device init sequence. See datasheet rev 2 section
5189cc9806eSTomas Melin  * 4.2 Start-Up Sequence for details.
5199cc9806eSTomas Melin  */
sca3300_init(struct sca3300_data * sca_data,struct iio_dev * indio_dev)5209cc9806eSTomas Melin static int sca3300_init(struct sca3300_data *sca_data,
5219cc9806eSTomas Melin 			struct iio_dev *indio_dev)
5229cc9806eSTomas Melin {
5239cc9806eSTomas Melin 	int value = 0;
5249cc9806eSTomas Melin 	int ret;
525ad985d4dSLI Qingwu 	int i;
5269cc9806eSTomas Melin 
5279cc9806eSTomas Melin 	ret = sca3300_write_reg(sca_data, SCA3300_REG_MODE,
5289cc9806eSTomas Melin 				SCA3300_MODE_SW_RESET);
5299cc9806eSTomas Melin 	if (ret)
5309cc9806eSTomas Melin 		return ret;
5319cc9806eSTomas Melin 
5329cc9806eSTomas Melin 	/*
5339cc9806eSTomas Melin 	 * Wait 1ms after SW-reset command.
53424fb2d3cSLI Qingwu 	 * Wait for the settling of signal paths,
53524fb2d3cSLI Qingwu 	 * 15ms for SCA3300 and 25ms for SCL3300,
5369cc9806eSTomas Melin 	 */
53724fb2d3cSLI Qingwu 	usleep_range(26e3, 50e3);
5389cc9806eSTomas Melin 
5399cc9806eSTomas Melin 	ret = sca3300_read_reg(sca_data, SCA3300_REG_WHOAMI, &value);
5409cc9806eSTomas Melin 	if (ret)
5419cc9806eSTomas Melin 		return ret;
5429cc9806eSTomas Melin 
543ad985d4dSLI Qingwu 	for (i = 0; i < ARRAY_SIZE(sca3300_chip_tbl); i++) {
544ad985d4dSLI Qingwu 		if (sca3300_chip_tbl[i].chip_id == value)
545ad985d4dSLI Qingwu 			break;
546ad985d4dSLI Qingwu 	}
547ad985d4dSLI Qingwu 	if (i == ARRAY_SIZE(sca3300_chip_tbl)) {
548ad985d4dSLI Qingwu 		dev_err(&sca_data->spi->dev, "unknown chip id %x\n", value);
5499cc9806eSTomas Melin 		return -ENODEV;
5509cc9806eSTomas Melin 	}
551ad985d4dSLI Qingwu 
552ad985d4dSLI Qingwu 	sca_data->chip = &sca3300_chip_tbl[i];
553ad985d4dSLI Qingwu 
55438d5cd1eSLI Qingwu 	if (sca_data->chip->angle_supported) {
55538d5cd1eSLI Qingwu 		ret = sca3300_write_reg(sca_data, SCL3300_REG_ANG_CTRL,
55638d5cd1eSLI Qingwu 					SCL3300_ANG_ENABLE);
55738d5cd1eSLI Qingwu 		if (ret)
55838d5cd1eSLI Qingwu 			return ret;
55938d5cd1eSLI Qingwu 	}
56038d5cd1eSLI Qingwu 
5619cc9806eSTomas Melin 	return 0;
5629cc9806eSTomas Melin }
5639cc9806eSTomas Melin 
sca3300_debugfs_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)5649cc9806eSTomas Melin static int sca3300_debugfs_reg_access(struct iio_dev *indio_dev,
5659cc9806eSTomas Melin 				      unsigned int reg, unsigned int writeval,
5669cc9806eSTomas Melin 				      unsigned int *readval)
5679cc9806eSTomas Melin {
5689cc9806eSTomas Melin 	struct sca3300_data *data = iio_priv(indio_dev);
5699cc9806eSTomas Melin 	int value;
5709cc9806eSTomas Melin 	int ret;
5719cc9806eSTomas Melin 
5729cc9806eSTomas Melin 	if (reg > SCA3300_REG_SELBANK)
5739cc9806eSTomas Melin 		return -EINVAL;
5749cc9806eSTomas Melin 
5759cc9806eSTomas Melin 	if (!readval)
5769cc9806eSTomas Melin 		return sca3300_write_reg(data, reg, writeval);
5779cc9806eSTomas Melin 
5789cc9806eSTomas Melin 	ret = sca3300_read_reg(data, reg, &value);
5799cc9806eSTomas Melin 	if (ret)
5809cc9806eSTomas Melin 		return ret;
5819cc9806eSTomas Melin 
5829cc9806eSTomas Melin 	*readval = value;
5839cc9806eSTomas Melin 
5849cc9806eSTomas Melin 	return 0;
5859cc9806eSTomas Melin }
5869cc9806eSTomas Melin 
sca3300_read_avail(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)5879cc9806eSTomas Melin static int sca3300_read_avail(struct iio_dev *indio_dev,
5889cc9806eSTomas Melin 			      struct iio_chan_spec const *chan,
5899cc9806eSTomas Melin 			      const int **vals, int *type, int *length,
5909cc9806eSTomas Melin 			      long mask)
5919cc9806eSTomas Melin {
592ad985d4dSLI Qingwu 	struct sca3300_data *data = iio_priv(indio_dev);
5939cc9806eSTomas Melin 	switch (mask) {
5949cc9806eSTomas Melin 	case IIO_CHAN_INFO_SCALE:
595ad985d4dSLI Qingwu 		switch (chan->type) {
59638d5cd1eSLI Qingwu 		case IIO_INCLI:
59738d5cd1eSLI Qingwu 			*vals = (const int *)data->chip->incli_scale;
59838d5cd1eSLI Qingwu 			*length = data->chip->num_incli_scales;
59938d5cd1eSLI Qingwu 			*type = IIO_VAL_INT_PLUS_MICRO;
60038d5cd1eSLI Qingwu 			return IIO_AVAIL_LIST;
601ad985d4dSLI Qingwu 		case IIO_ACCEL:
602ad985d4dSLI Qingwu 			*vals = (const int *)data->chip->accel_scale;
603ad985d4dSLI Qingwu 			*length = data->chip->num_accel_scales;
6049cc9806eSTomas Melin 			*type = IIO_VAL_INT_PLUS_MICRO;
6059cc9806eSTomas Melin 			return IIO_AVAIL_LIST;
606ad985d4dSLI Qingwu 		default:
607ad985d4dSLI Qingwu 			return -EINVAL;
608ad985d4dSLI Qingwu 		}
6099cc9806eSTomas Melin 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
610ad985d4dSLI Qingwu 		*vals = (const int *)data->chip->freq_table;
611ad985d4dSLI Qingwu 		*length = data->chip->num_freqs;
6129cc9806eSTomas Melin 		*type = IIO_VAL_INT;
6139cc9806eSTomas Melin 		return IIO_AVAIL_LIST;
6149cc9806eSTomas Melin 	default:
6159cc9806eSTomas Melin 		return -EINVAL;
6169cc9806eSTomas Melin 	}
6179cc9806eSTomas Melin }
6189cc9806eSTomas Melin 
6199cc9806eSTomas Melin static const struct iio_info sca3300_info = {
6209cc9806eSTomas Melin 	.read_raw = sca3300_read_raw,
6219cc9806eSTomas Melin 	.write_raw = sca3300_write_raw,
6229cc9806eSTomas Melin 	.debugfs_reg_access = &sca3300_debugfs_reg_access,
6239cc9806eSTomas Melin 	.read_avail = sca3300_read_avail,
6249cc9806eSTomas Melin };
6259cc9806eSTomas Melin 
sca3300_probe(struct spi_device * spi)6269cc9806eSTomas Melin static int sca3300_probe(struct spi_device *spi)
6279cc9806eSTomas Melin {
6289cc9806eSTomas Melin 	struct sca3300_data *sca_data;
6299cc9806eSTomas Melin 	struct iio_dev *indio_dev;
6309cc9806eSTomas Melin 	int ret;
6319cc9806eSTomas Melin 
6329cc9806eSTomas Melin 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*sca_data));
6339cc9806eSTomas Melin 	if (!indio_dev)
6349cc9806eSTomas Melin 		return -ENOMEM;
6359cc9806eSTomas Melin 
6369cc9806eSTomas Melin 	sca_data = iio_priv(indio_dev);
6379cc9806eSTomas Melin 	mutex_init(&sca_data->lock);
6389cc9806eSTomas Melin 	sca_data->spi = spi;
6399cc9806eSTomas Melin 
6409cc9806eSTomas Melin 	crc8_populate_msb(sca3300_crc_table, SCA3300_CRC8_POLYNOMIAL);
6419cc9806eSTomas Melin 
6429cc9806eSTomas Melin 	indio_dev->info = &sca3300_info;
6439cc9806eSTomas Melin 
6449cc9806eSTomas Melin 	ret = sca3300_init(sca_data, indio_dev);
6459cc9806eSTomas Melin 	if (ret) {
6469cc9806eSTomas Melin 		dev_err(&spi->dev, "failed to init device, error: %d\n", ret);
6479cc9806eSTomas Melin 		return ret;
6489cc9806eSTomas Melin 	}
6499cc9806eSTomas Melin 
650ad985d4dSLI Qingwu 	indio_dev->name = sca_data->chip->name;
651ad985d4dSLI Qingwu 	indio_dev->modes = INDIO_DIRECT_MODE;
652ad985d4dSLI Qingwu 	indio_dev->channels = sca_data->chip->channels;
653ad985d4dSLI Qingwu 	indio_dev->num_channels = sca_data->chip->num_channels;
654ad985d4dSLI Qingwu 	indio_dev->available_scan_masks = sca_data->chip->scan_masks;
655ad985d4dSLI Qingwu 
6569cc9806eSTomas Melin 	ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
6579cc9806eSTomas Melin 					      iio_pollfunc_store_time,
6589cc9806eSTomas Melin 					      sca3300_trigger_handler, NULL);
6599cc9806eSTomas Melin 	if (ret) {
6609cc9806eSTomas Melin 		dev_err(&spi->dev,
6619cc9806eSTomas Melin 			"iio triggered buffer setup failed, error: %d\n", ret);
6629cc9806eSTomas Melin 		return ret;
6639cc9806eSTomas Melin 	}
6649cc9806eSTomas Melin 
6659cc9806eSTomas Melin 	ret = devm_iio_device_register(&spi->dev, indio_dev);
6669cc9806eSTomas Melin 	if (ret) {
6679cc9806eSTomas Melin 		dev_err(&spi->dev, "iio device register failed, error: %d\n",
6689cc9806eSTomas Melin 			ret);
6699cc9806eSTomas Melin 	}
6709cc9806eSTomas Melin 
6719cc9806eSTomas Melin 	return ret;
6729cc9806eSTomas Melin }
6739cc9806eSTomas Melin 
6749cc9806eSTomas Melin static const struct of_device_id sca3300_dt_ids[] = {
6759cc9806eSTomas Melin 	{ .compatible = "murata,sca3300"},
67624fb2d3cSLI Qingwu 	{ .compatible = "murata,scl3300"},
6779cc9806eSTomas Melin 	{}
6789cc9806eSTomas Melin };
6799cc9806eSTomas Melin MODULE_DEVICE_TABLE(of, sca3300_dt_ids);
6809cc9806eSTomas Melin 
68135dab731SWei Yongjun static const struct spi_device_id sca3300_ids[] = {
68235dab731SWei Yongjun 	{ "sca3300" },
68335dab731SWei Yongjun 	{ "scl3300" },
68435dab731SWei Yongjun 	{}
68535dab731SWei Yongjun };
68635dab731SWei Yongjun MODULE_DEVICE_TABLE(spi, sca3300_ids);
68735dab731SWei Yongjun 
6889cc9806eSTomas Melin static struct spi_driver sca3300_driver = {
6899cc9806eSTomas Melin 	.driver   = {
6909cc9806eSTomas Melin 		.name		= SCA3300_ALIAS,
6919cc9806eSTomas Melin 		.of_match_table = sca3300_dt_ids,
6929cc9806eSTomas Melin 	},
6939cc9806eSTomas Melin 	.probe	  = sca3300_probe,
69435dab731SWei Yongjun 	.id_table = sca3300_ids,
6959cc9806eSTomas Melin };
6969cc9806eSTomas Melin module_spi_driver(sca3300_driver);
6979cc9806eSTomas Melin 
6989cc9806eSTomas Melin MODULE_AUTHOR("Tomas Melin <tomas.melin@vaisala.com>");
6999cc9806eSTomas Melin MODULE_DESCRIPTION("Murata SCA3300 SPI Accelerometer");
7009cc9806eSTomas Melin MODULE_LICENSE("GPL v2");
701