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>
25 #define MMA7455_REG_XOUTL 0x00
26 #define MMA7455_REG_XOUTH 0x01
27 #define MMA7455_REG_YOUTL 0x02
28 #define MMA7455_REG_YOUTH 0x03
29 #define MMA7455_REG_ZOUTL 0x04
30 #define MMA7455_REG_ZOUTH 0x05
31 #define MMA7455_REG_STATUS 0x09
32 #define MMA7455_STATUS_DRDY BIT(0)
33 #define MMA7455_REG_WHOAMI 0x0f
34 #define MMA7455_WHOAMI_ID 0x55
35 #define MMA7455_REG_MCTL 0x16
36 #define MMA7455_MCTL_MODE_STANDBY 0x00
37 #define MMA7455_MCTL_MODE_MEASURE 0x01
38 #define MMA7455_REG_CTL1 0x18
39 #define MMA7455_CTL1_DFBW_MASK BIT(7)
40 #define MMA7455_CTL1_DFBW_125HZ BIT(7)
41 #define MMA7455_CTL1_DFBW_62_5HZ 0
42 #define MMA7455_REG_TW 0x1e
45 * When MMA7455 is used in 10-bit it has a fullscale of -8g
46 * corresponding to raw value -512. The userspace interface
47 * uses m/s^2 and we declare micro units.
48 * So scale factor is given by:
49 * g * 8 * 1e6 / 512 = 153228.90625, with g = 9.80665
51 #define MMA7455_10BIT_SCALE 153229
54 struct regmap
*regmap
;
57 static int mma7455_drdy(struct mma7455_data
*mma7455
)
59 struct device
*dev
= regmap_get_device(mma7455
->regmap
);
65 ret
= regmap_read(mma7455
->regmap
, MMA7455_REG_STATUS
, ®
);
69 if (reg
& MMA7455_STATUS_DRDY
)
75 dev_warn(dev
, "data not ready\n");
80 static irqreturn_t
mma7455_trigger_handler(int irq
, void *p
)
82 struct iio_poll_func
*pf
= p
;
83 struct iio_dev
*indio_dev
= pf
->indio_dev
;
84 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
85 u8 buf
[16]; /* 3 x 16-bit channels + padding + ts */
88 ret
= mma7455_drdy(mma7455
);
92 ret
= regmap_bulk_read(mma7455
->regmap
, MMA7455_REG_XOUTL
, buf
,
97 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
98 iio_get_time_ns(indio_dev
));
101 iio_trigger_notify_done(indio_dev
->trig
);
106 static int mma7455_read_raw(struct iio_dev
*indio_dev
,
107 struct iio_chan_spec
const *chan
,
108 int *val
, int *val2
, long mask
)
110 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
116 case IIO_CHAN_INFO_RAW
:
117 if (iio_buffer_enabled(indio_dev
))
120 ret
= mma7455_drdy(mma7455
);
124 ret
= regmap_bulk_read(mma7455
->regmap
, chan
->address
, &data
,
129 *val
= sign_extend32(le16_to_cpu(data
), 9);
133 case IIO_CHAN_INFO_SCALE
:
135 *val2
= MMA7455_10BIT_SCALE
;
137 return IIO_VAL_INT_PLUS_MICRO
;
139 case IIO_CHAN_INFO_SAMP_FREQ
:
140 ret
= regmap_read(mma7455
->regmap
, MMA7455_REG_CTL1
, ®
);
144 if (reg
& MMA7455_CTL1_DFBW_MASK
)
155 static int mma7455_write_raw(struct iio_dev
*indio_dev
,
156 struct iio_chan_spec
const *chan
,
157 int val
, int val2
, long mask
)
159 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
163 case IIO_CHAN_INFO_SAMP_FREQ
:
164 if (val
== 250 && val2
== 0)
165 i
= MMA7455_CTL1_DFBW_125HZ
;
166 else if (val
== 125 && val2
== 0)
167 i
= MMA7455_CTL1_DFBW_62_5HZ
;
171 return regmap_update_bits(mma7455
->regmap
, MMA7455_REG_CTL1
,
172 MMA7455_CTL1_DFBW_MASK
, i
);
174 case IIO_CHAN_INFO_SCALE
:
175 /* In 10-bit mode there is only one scale available */
176 if (val
== 0 && val2
== MMA7455_10BIT_SCALE
)
184 static IIO_CONST_ATTR(sampling_frequency_available
, "125 250");
186 static struct attribute
*mma7455_attributes
[] = {
187 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
191 static const struct attribute_group mma7455_group
= {
192 .attrs
= mma7455_attributes
,
195 static const struct iio_info mma7455_info
= {
196 .attrs
= &mma7455_group
,
197 .read_raw
= mma7455_read_raw
,
198 .write_raw
= mma7455_write_raw
,
201 #define MMA7455_CHANNEL(axis, idx) { \
204 .address = MMA7455_REG_##axis##OUTL,\
205 .channel2 = IIO_MOD_##axis, \
206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
207 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
208 BIT(IIO_CHAN_INFO_SCALE), \
214 .endianness = IIO_LE, \
218 static const struct iio_chan_spec mma7455_channels
[] = {
219 MMA7455_CHANNEL(X
, 0),
220 MMA7455_CHANNEL(Y
, 1),
221 MMA7455_CHANNEL(Z
, 2),
222 IIO_CHAN_SOFT_TIMESTAMP(3),
225 static const unsigned long mma7455_scan_masks
[] = {0x7, 0};
227 const struct regmap_config mma7455_core_regmap
= {
230 .max_register
= MMA7455_REG_TW
,
232 EXPORT_SYMBOL_GPL(mma7455_core_regmap
);
234 int mma7455_core_probe(struct device
*dev
, struct regmap
*regmap
,
237 struct mma7455_data
*mma7455
;
238 struct iio_dev
*indio_dev
;
242 ret
= regmap_read(regmap
, MMA7455_REG_WHOAMI
, ®
);
244 dev_err(dev
, "unable to read reg\n");
248 if (reg
!= MMA7455_WHOAMI_ID
) {
249 dev_err(dev
, "device id mismatch\n");
253 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*mma7455
));
257 dev_set_drvdata(dev
, indio_dev
);
258 mma7455
= iio_priv(indio_dev
);
259 mma7455
->regmap
= regmap
;
261 indio_dev
->info
= &mma7455_info
;
262 indio_dev
->name
= name
;
263 indio_dev
->dev
.parent
= dev
;
264 indio_dev
->modes
= INDIO_DIRECT_MODE
;
265 indio_dev
->channels
= mma7455_channels
;
266 indio_dev
->num_channels
= ARRAY_SIZE(mma7455_channels
);
267 indio_dev
->available_scan_masks
= mma7455_scan_masks
;
269 regmap_write(mma7455
->regmap
, MMA7455_REG_MCTL
,
270 MMA7455_MCTL_MODE_MEASURE
);
272 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
273 mma7455_trigger_handler
, NULL
);
275 dev_err(dev
, "unable to setup triggered buffer\n");
279 ret
= iio_device_register(indio_dev
);
281 dev_err(dev
, "unable to register device\n");
282 iio_triggered_buffer_cleanup(indio_dev
);
288 EXPORT_SYMBOL_GPL(mma7455_core_probe
);
290 int mma7455_core_remove(struct device
*dev
)
292 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
293 struct mma7455_data
*mma7455
= iio_priv(indio_dev
);
295 iio_device_unregister(indio_dev
);
296 iio_triggered_buffer_cleanup(indio_dev
);
298 regmap_write(mma7455
->regmap
, MMA7455_REG_MCTL
,
299 MMA7455_MCTL_MODE_STANDBY
);
303 EXPORT_SYMBOL_GPL(mma7455_core_remove
);
305 MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
306 MODULE_DESCRIPTION("Freescale MMA7455L core accelerometer driver");
307 MODULE_LICENSE("GPL v2");