1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO accel core driver for Freescale MMA7455L 3-axis 10-bit accelerometer
4 * Copyright 2015 Joachim Eastwood <manabian@gmail.com>
6 * UNSUPPORTED hardware features:
7 * - 8-bit mode with different scales
8 * - INT1/INT2 interrupts
13 #include <linux/delay.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/module.h>
21 #include <linux/regmap.h>
22 #include <linux/types.h>
26 #define MMA7455_REG_XOUTL 0x00
27 #define MMA7455_REG_XOUTH 0x01
28 #define MMA7455_REG_YOUTL 0x02
29 #define MMA7455_REG_YOUTH 0x03
30 #define MMA7455_REG_ZOUTL 0x04
31 #define MMA7455_REG_ZOUTH 0x05
32 #define MMA7455_REG_STATUS 0x09
33 #define MMA7455_STATUS_DRDY BIT(0)
34 #define MMA7455_REG_WHOAMI 0x0f
35 #define MMA7455_WHOAMI_ID 0x55
36 #define MMA7455_REG_MCTL 0x16
37 #define MMA7455_MCTL_MODE_STANDBY 0x00
38 #define MMA7455_MCTL_MODE_MEASURE 0x01
39 #define MMA7455_REG_CTL1 0x18
40 #define MMA7455_CTL1_DFBW_MASK BIT(7)
41 #define MMA7455_CTL1_DFBW_125HZ BIT(7)
42 #define MMA7455_CTL1_DFBW_62_5HZ 0
43 #define MMA7455_REG_TW 0x1e
46 * When MMA7455 is used in 10-bit it has a fullscale of -8g
47 * corresponding to raw value -512. The userspace interface
48 * uses m/s^2 and we declare micro units.
49 * So scale factor is given by:
50 * g * 8 * 1e6 / 512 = 153228.90625, with g = 9.80665
52 #define MMA7455_10BIT_SCALE 153229
55 struct regmap
*regmap
;
57 * Used to reorganize data. Will ensure correct alignment of
58 * the timestamp if present
66 static int mma7455_drdy(struct mma7455_data
*mma7455
)
68 struct device
*dev
= regmap_get_device(mma7455
->regmap
);
74 ret
= regmap_read(mma7455
->regmap
, MMA7455_REG_STATUS
, ®
);
78 if (reg
& MMA7455_STATUS_DRDY
)
84 dev_warn(dev
, "data not ready\n");
89 static irqreturn_t
mma7455_trigger_handler(int irq
, void *p
)
91 struct iio_poll_func
*pf
= p
;
92 struct iio_dev
*indio_dev
= pf
->indio_dev
;
93 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
96 ret
= mma7455_drdy(mma7455
);
100 ret
= regmap_bulk_read(mma7455
->regmap
, MMA7455_REG_XOUTL
,
101 mma7455
->scan
.channels
,
102 sizeof(mma7455
->scan
.channels
));
106 iio_push_to_buffers_with_timestamp(indio_dev
, &mma7455
->scan
,
107 iio_get_time_ns(indio_dev
));
110 iio_trigger_notify_done(indio_dev
->trig
);
115 static int mma7455_read_raw(struct iio_dev
*indio_dev
,
116 struct iio_chan_spec
const *chan
,
117 int *val
, int *val2
, long mask
)
119 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
125 case IIO_CHAN_INFO_RAW
:
126 if (iio_buffer_enabled(indio_dev
))
129 ret
= mma7455_drdy(mma7455
);
133 ret
= regmap_bulk_read(mma7455
->regmap
, chan
->address
, &data
,
138 *val
= sign_extend32(le16_to_cpu(data
),
139 chan
->scan_type
.realbits
- 1);
143 case IIO_CHAN_INFO_SCALE
:
145 *val2
= MMA7455_10BIT_SCALE
;
147 return IIO_VAL_INT_PLUS_MICRO
;
149 case IIO_CHAN_INFO_SAMP_FREQ
:
150 ret
= regmap_read(mma7455
->regmap
, MMA7455_REG_CTL1
, ®
);
154 if (reg
& MMA7455_CTL1_DFBW_MASK
)
165 static int mma7455_write_raw(struct iio_dev
*indio_dev
,
166 struct iio_chan_spec
const *chan
,
167 int val
, int val2
, long mask
)
169 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
173 case IIO_CHAN_INFO_SAMP_FREQ
:
174 if (val
== 250 && val2
== 0)
175 i
= MMA7455_CTL1_DFBW_125HZ
;
176 else if (val
== 125 && val2
== 0)
177 i
= MMA7455_CTL1_DFBW_62_5HZ
;
181 return regmap_update_bits(mma7455
->regmap
, MMA7455_REG_CTL1
,
182 MMA7455_CTL1_DFBW_MASK
, i
);
184 case IIO_CHAN_INFO_SCALE
:
185 /* In 10-bit mode there is only one scale available */
186 if (val
== 0 && val2
== MMA7455_10BIT_SCALE
)
194 static IIO_CONST_ATTR(sampling_frequency_available
, "125 250");
196 static struct attribute
*mma7455_attributes
[] = {
197 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
201 static const struct attribute_group mma7455_group
= {
202 .attrs
= mma7455_attributes
,
205 static const struct iio_info mma7455_info
= {
206 .attrs
= &mma7455_group
,
207 .read_raw
= mma7455_read_raw
,
208 .write_raw
= mma7455_write_raw
,
211 #define MMA7455_CHANNEL(axis, idx) { \
214 .address = MMA7455_REG_##axis##OUTL,\
215 .channel2 = IIO_MOD_##axis, \
216 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
217 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
218 BIT(IIO_CHAN_INFO_SCALE), \
224 .endianness = IIO_LE, \
228 static const struct iio_chan_spec mma7455_channels
[] = {
229 MMA7455_CHANNEL(X
, 0),
230 MMA7455_CHANNEL(Y
, 1),
231 MMA7455_CHANNEL(Z
, 2),
232 IIO_CHAN_SOFT_TIMESTAMP(3),
235 static const unsigned long mma7455_scan_masks
[] = {0x7, 0};
237 const struct regmap_config mma7455_core_regmap
= {
240 .max_register
= MMA7455_REG_TW
,
242 EXPORT_SYMBOL_NS_GPL(mma7455_core_regmap
, "IIO_MMA7455");
244 int mma7455_core_probe(struct device
*dev
, struct regmap
*regmap
,
247 struct mma7455_data
*mma7455
;
248 struct iio_dev
*indio_dev
;
252 ret
= regmap_read(regmap
, MMA7455_REG_WHOAMI
, ®
);
254 dev_err(dev
, "unable to read reg\n");
258 if (reg
!= MMA7455_WHOAMI_ID
) {
259 dev_err(dev
, "device id mismatch\n");
263 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*mma7455
));
267 dev_set_drvdata(dev
, indio_dev
);
268 mma7455
= iio_priv(indio_dev
);
269 mma7455
->regmap
= regmap
;
271 indio_dev
->info
= &mma7455_info
;
272 indio_dev
->name
= name
;
273 indio_dev
->modes
= INDIO_DIRECT_MODE
;
274 indio_dev
->channels
= mma7455_channels
;
275 indio_dev
->num_channels
= ARRAY_SIZE(mma7455_channels
);
276 indio_dev
->available_scan_masks
= mma7455_scan_masks
;
278 regmap_write(mma7455
->regmap
, MMA7455_REG_MCTL
,
279 MMA7455_MCTL_MODE_MEASURE
);
281 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
282 mma7455_trigger_handler
, NULL
);
284 dev_err(dev
, "unable to setup triggered buffer\n");
288 ret
= iio_device_register(indio_dev
);
290 dev_err(dev
, "unable to register device\n");
291 iio_triggered_buffer_cleanup(indio_dev
);
297 EXPORT_SYMBOL_NS_GPL(mma7455_core_probe
, "IIO_MMA7455");
299 void mma7455_core_remove(struct device
*dev
)
301 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
302 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
304 iio_device_unregister(indio_dev
);
305 iio_triggered_buffer_cleanup(indio_dev
);
307 regmap_write(mma7455
->regmap
, MMA7455_REG_MCTL
,
308 MMA7455_MCTL_MODE_STANDBY
);
310 EXPORT_SYMBOL_NS_GPL(mma7455_core_remove
, "IIO_MMA7455");
312 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
313 MODULE_DESCRIPTION("Freescale MMA7455L core accelerometer driver");
314 MODULE_LICENSE("GPL v2");