1 // SPDX-License-Identifier: GPL-2.0-only
3 * 3-axis accelerometer driver for MXC4005XC Memsic sensor
5 * Copyright (c) 2014, Intel Corporation.
8 #include <linux/delay.h>
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/iio/iio.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/regmap.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/iio/trigger_consumer.h>
20 #define MXC4005_DRV_NAME "mxc4005"
21 #define MXC4005_IRQ_NAME "mxc4005_event"
22 #define MXC4005_REGMAP_NAME "mxc4005_regmap"
24 #define MXC4005_REG_XOUT_UPPER 0x03
25 #define MXC4005_REG_XOUT_LOWER 0x04
26 #define MXC4005_REG_YOUT_UPPER 0x05
27 #define MXC4005_REG_YOUT_LOWER 0x06
28 #define MXC4005_REG_ZOUT_UPPER 0x07
29 #define MXC4005_REG_ZOUT_LOWER 0x08
31 #define MXC4005_REG_INT_MASK0 0x0A
33 #define MXC4005_REG_INT_MASK1 0x0B
34 #define MXC4005_REG_INT_MASK1_BIT_DRDYE 0x01
36 #define MXC4005_REG_INT_CLR0 0x00
38 #define MXC4005_REG_INT_CLR1 0x01
39 #define MXC4005_REG_INT_CLR1_BIT_DRDYC 0x01
40 #define MXC4005_REG_INT_CLR1_SW_RST 0x10
42 #define MXC4005_REG_CONTROL 0x0D
43 #define MXC4005_REG_CONTROL_MASK_FSR GENMASK(6, 5)
44 #define MXC4005_CONTROL_FSR_SHIFT 5
46 #define MXC4005_REG_DEVICE_ID 0x0E
48 /* Datasheet does not specify a reset time, this is a conservative guess */
49 #define MXC4005_RESET_TIME_US 2000
66 struct regmap
*regmap
;
67 struct iio_trigger
*dready_trig
;
68 struct iio_mount_matrix orientation
;
69 /* Ensure timestamp is naturally aligned */
72 s64 timestamp
__aligned(8);
76 unsigned int int_mask1
;
80 * MXC4005 can operate in the following ranges:
81 * +/- 2G, 4G, 8G (the default +/-2G)
83 * (2 + 2) * 9.81 / (2^12 - 1) = 0.009582
84 * (4 + 4) * 9.81 / (2^12 - 1) = 0.019164
85 * (8 + 8) * 9.81 / (2^12 - 1) = 0.038329
90 } mxc4005_scale_table
[] = {
91 {MXC4005_RANGE_2G
, 9582},
92 {MXC4005_RANGE_4G
, 19164},
93 {MXC4005_RANGE_8G
, 38329},
97 static IIO_CONST_ATTR(in_accel_scale_available
, "0.009582 0.019164 0.038329");
99 static struct attribute
*mxc4005_attributes
[] = {
100 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
104 static const struct attribute_group mxc4005_attrs_group
= {
105 .attrs
= mxc4005_attributes
,
108 static bool mxc4005_is_readable_reg(struct device
*dev
, unsigned int reg
)
111 case MXC4005_REG_XOUT_UPPER
:
112 case MXC4005_REG_XOUT_LOWER
:
113 case MXC4005_REG_YOUT_UPPER
:
114 case MXC4005_REG_YOUT_LOWER
:
115 case MXC4005_REG_ZOUT_UPPER
:
116 case MXC4005_REG_ZOUT_LOWER
:
117 case MXC4005_REG_DEVICE_ID
:
118 case MXC4005_REG_CONTROL
:
125 static bool mxc4005_is_writeable_reg(struct device
*dev
, unsigned int reg
)
128 case MXC4005_REG_INT_CLR0
:
129 case MXC4005_REG_INT_CLR1
:
130 case MXC4005_REG_INT_MASK0
:
131 case MXC4005_REG_INT_MASK1
:
132 case MXC4005_REG_CONTROL
:
139 static const struct regmap_config mxc4005_regmap_config
= {
140 .name
= MXC4005_REGMAP_NAME
,
145 .max_register
= MXC4005_REG_DEVICE_ID
,
147 .readable_reg
= mxc4005_is_readable_reg
,
148 .writeable_reg
= mxc4005_is_writeable_reg
,
151 static int mxc4005_read_xyz(struct mxc4005_data
*data
)
155 ret
= regmap_bulk_read(data
->regmap
, MXC4005_REG_XOUT_UPPER
,
156 data
->scan
.chans
, sizeof(data
->scan
.chans
));
158 dev_err(data
->dev
, "failed to read axes\n");
165 static int mxc4005_read_axis(struct mxc4005_data
*data
,
171 ret
= regmap_bulk_read(data
->regmap
, addr
, ®
, sizeof(reg
));
173 dev_err(data
->dev
, "failed to read reg %02x\n", addr
);
177 return be16_to_cpu(reg
);
180 static int mxc4005_read_scale(struct mxc4005_data
*data
)
186 ret
= regmap_read(data
->regmap
, MXC4005_REG_CONTROL
, ®
);
188 dev_err(data
->dev
, "failed to read reg_control\n");
192 i
= reg
>> MXC4005_CONTROL_FSR_SHIFT
;
194 if (i
< 0 || i
>= ARRAY_SIZE(mxc4005_scale_table
))
197 return mxc4005_scale_table
[i
].scale
;
200 static int mxc4005_set_scale(struct mxc4005_data
*data
, int val
)
206 for (i
= 0; i
< ARRAY_SIZE(mxc4005_scale_table
); i
++) {
207 if (mxc4005_scale_table
[i
].scale
== val
) {
208 reg
= i
<< MXC4005_CONTROL_FSR_SHIFT
;
209 ret
= regmap_update_bits(data
->regmap
,
211 MXC4005_REG_CONTROL_MASK_FSR
,
215 "failed to write reg_control\n");
223 static int mxc4005_read_raw(struct iio_dev
*indio_dev
,
224 struct iio_chan_spec
const *chan
,
225 int *val
, int *val2
, long mask
)
227 struct mxc4005_data
*data
= iio_priv(indio_dev
);
231 case IIO_CHAN_INFO_RAW
:
232 switch (chan
->type
) {
234 if (iio_buffer_enabled(indio_dev
))
237 ret
= mxc4005_read_axis(data
, chan
->address
);
240 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
241 chan
->scan_type
.realbits
- 1);
246 case IIO_CHAN_INFO_SCALE
:
247 ret
= mxc4005_read_scale(data
);
253 return IIO_VAL_INT_PLUS_MICRO
;
259 static int mxc4005_write_raw(struct iio_dev
*indio_dev
,
260 struct iio_chan_spec
const *chan
,
261 int val
, int val2
, long mask
)
263 struct mxc4005_data
*data
= iio_priv(indio_dev
);
266 case IIO_CHAN_INFO_SCALE
:
270 return mxc4005_set_scale(data
, val2
);
276 static const struct iio_mount_matrix
*
277 mxc4005_get_mount_matrix(const struct iio_dev
*indio_dev
,
278 const struct iio_chan_spec
*chan
)
280 struct mxc4005_data
*data
= iio_priv(indio_dev
);
282 return &data
->orientation
;
285 static const struct iio_chan_spec_ext_info mxc4005_ext_info
[] = {
286 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, mxc4005_get_mount_matrix
),
290 static const struct iio_info mxc4005_info
= {
291 .read_raw
= mxc4005_read_raw
,
292 .write_raw
= mxc4005_write_raw
,
293 .attrs
= &mxc4005_attrs_group
,
296 static const unsigned long mxc4005_scan_masks
[] = {
297 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
301 #define MXC4005_CHANNEL(_axis, _addr) { \
304 .channel2 = IIO_MOD_##_axis, \
306 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
307 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
308 .scan_index = AXIS_##_axis, \
314 .endianness = IIO_BE, \
316 .ext_info = mxc4005_ext_info, \
319 static const struct iio_chan_spec mxc4005_channels
[] = {
320 MXC4005_CHANNEL(X
, MXC4005_REG_XOUT_UPPER
),
321 MXC4005_CHANNEL(Y
, MXC4005_REG_YOUT_UPPER
),
322 MXC4005_CHANNEL(Z
, MXC4005_REG_ZOUT_UPPER
),
323 IIO_CHAN_SOFT_TIMESTAMP(3),
326 static irqreturn_t
mxc4005_trigger_handler(int irq
, void *private)
328 struct iio_poll_func
*pf
= private;
329 struct iio_dev
*indio_dev
= pf
->indio_dev
;
330 struct mxc4005_data
*data
= iio_priv(indio_dev
);
333 ret
= mxc4005_read_xyz(data
);
337 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
341 iio_trigger_notify_done(indio_dev
->trig
);
346 static void mxc4005_clr_intr(struct mxc4005_data
*data
)
350 /* clear interrupt */
351 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
352 MXC4005_REG_INT_CLR1_BIT_DRDYC
);
354 dev_err(data
->dev
, "failed to write to reg_int_clr1\n");
357 static int mxc4005_set_trigger_state(struct iio_trigger
*trig
,
360 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
361 struct mxc4005_data
*data
= iio_priv(indio_dev
);
365 mutex_lock(&data
->mutex
);
367 val
= state
? MXC4005_REG_INT_MASK1_BIT_DRDYE
: 0;
368 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, val
);
370 mutex_unlock(&data
->mutex
);
371 dev_err(data
->dev
, "failed to update reg_int_mask1");
375 data
->int_mask1
= val
;
376 data
->trigger_enabled
= state
;
377 mutex_unlock(&data
->mutex
);
382 static void mxc4005_trigger_reen(struct iio_trigger
*trig
)
384 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
385 struct mxc4005_data
*data
= iio_priv(indio_dev
);
387 if (!data
->dready_trig
)
390 mxc4005_clr_intr(data
);
393 static const struct iio_trigger_ops mxc4005_trigger_ops
= {
394 .set_trigger_state
= mxc4005_set_trigger_state
,
395 .reenable
= mxc4005_trigger_reen
,
398 static int mxc4005_chip_init(struct mxc4005_data
*data
)
403 ret
= regmap_read(data
->regmap
, MXC4005_REG_DEVICE_ID
, ®
);
405 dev_err(data
->dev
, "failed to read chip id\n");
409 dev_dbg(data
->dev
, "MXC4005 chip id %02x\n", reg
);
411 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
412 MXC4005_REG_INT_CLR1_SW_RST
);
414 return dev_err_probe(data
->dev
, ret
, "resetting chip\n");
416 fsleep(MXC4005_RESET_TIME_US
);
418 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK0
, 0);
420 return dev_err_probe(data
->dev
, ret
, "writing INT_MASK0\n");
422 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, 0);
424 return dev_err_probe(data
->dev
, ret
, "writing INT_MASK1\n");
429 static int mxc4005_probe(struct i2c_client
*client
)
431 struct mxc4005_data
*data
;
432 struct iio_dev
*indio_dev
;
433 struct regmap
*regmap
;
436 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
440 regmap
= devm_regmap_init_i2c(client
, &mxc4005_regmap_config
);
441 if (IS_ERR(regmap
)) {
442 dev_err(&client
->dev
, "failed to initialize regmap\n");
443 return PTR_ERR(regmap
);
446 data
= iio_priv(indio_dev
);
447 i2c_set_clientdata(client
, indio_dev
);
448 data
->dev
= &client
->dev
;
449 data
->regmap
= regmap
;
451 ret
= mxc4005_chip_init(data
);
453 dev_err(&client
->dev
, "failed to initialize chip\n");
457 mutex_init(&data
->mutex
);
459 if (!iio_read_acpi_mount_matrix(&client
->dev
, &data
->orientation
, "ROTM")) {
460 ret
= iio_read_mount_matrix(&client
->dev
, &data
->orientation
);
465 indio_dev
->channels
= mxc4005_channels
;
466 indio_dev
->num_channels
= ARRAY_SIZE(mxc4005_channels
);
467 indio_dev
->available_scan_masks
= mxc4005_scan_masks
;
468 indio_dev
->name
= MXC4005_DRV_NAME
;
469 indio_dev
->modes
= INDIO_DIRECT_MODE
;
470 indio_dev
->info
= &mxc4005_info
;
472 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
,
473 iio_pollfunc_store_time
,
474 mxc4005_trigger_handler
,
477 dev_err(&client
->dev
,
478 "failed to setup iio triggered buffer\n");
482 if (client
->irq
> 0) {
483 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
486 iio_device_id(indio_dev
));
487 if (!data
->dready_trig
)
490 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
491 iio_trigger_generic_data_rdy_poll
,
493 IRQF_TRIGGER_FALLING
|
498 dev_err(&client
->dev
,
499 "failed to init threaded irq\n");
503 data
->dready_trig
->ops
= &mxc4005_trigger_ops
;
504 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
505 ret
= devm_iio_trigger_register(&client
->dev
,
508 dev_err(&client
->dev
,
509 "failed to register trigger\n");
513 indio_dev
->trig
= iio_trigger_get(data
->dready_trig
);
516 return devm_iio_device_register(&client
->dev
, indio_dev
);
519 static int mxc4005_suspend(struct device
*dev
)
521 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
522 struct mxc4005_data
*data
= iio_priv(indio_dev
);
525 /* Save control to restore it on resume */
526 ret
= regmap_read(data
->regmap
, MXC4005_REG_CONTROL
, &data
->control
);
528 dev_err(data
->dev
, "failed to read reg_control\n");
533 static int mxc4005_resume(struct device
*dev
)
535 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
536 struct mxc4005_data
*data
= iio_priv(indio_dev
);
539 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
540 MXC4005_REG_INT_CLR1_SW_RST
);
542 dev_err(data
->dev
, "failed to reset chip: %d\n", ret
);
546 fsleep(MXC4005_RESET_TIME_US
);
548 ret
= regmap_write(data
->regmap
, MXC4005_REG_CONTROL
, data
->control
);
550 dev_err(data
->dev
, "failed to restore control register\n");
554 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK0
, 0);
556 dev_err(data
->dev
, "failed to restore interrupt 0 mask\n");
560 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, data
->int_mask1
);
562 dev_err(data
->dev
, "failed to restore interrupt 1 mask\n");
569 static DEFINE_SIMPLE_DEV_PM_OPS(mxc4005_pm_ops
, mxc4005_suspend
, mxc4005_resume
);
571 static const struct acpi_device_id mxc4005_acpi_match
[] = {
577 MODULE_DEVICE_TABLE(acpi
, mxc4005_acpi_match
);
579 static const struct of_device_id mxc4005_of_match
[] = {
580 { .compatible
= "memsic,mxc4005", },
581 { .compatible
= "memsic,mxc6655", },
584 MODULE_DEVICE_TABLE(of
, mxc4005_of_match
);
586 static const struct i2c_device_id mxc4005_id
[] = {
591 MODULE_DEVICE_TABLE(i2c
, mxc4005_id
);
593 static struct i2c_driver mxc4005_driver
= {
595 .name
= MXC4005_DRV_NAME
,
596 .acpi_match_table
= mxc4005_acpi_match
,
597 .of_match_table
= mxc4005_of_match
,
598 .pm
= pm_sleep_ptr(&mxc4005_pm_ops
),
600 .probe
= mxc4005_probe
,
601 .id_table
= mxc4005_id
,
604 module_i2c_driver(mxc4005_driver
);
606 MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
607 MODULE_LICENSE("GPL v2");
608 MODULE_DESCRIPTION("MXC4005 3-axis accelerometer driver");