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/types.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/iio/trigger_consumer.h>
21 #define MXC4005_DRV_NAME "mxc4005"
22 #define MXC4005_IRQ_NAME "mxc4005_event"
23 #define MXC4005_REGMAP_NAME "mxc4005_regmap"
25 #define MXC4005_REG_XOUT_UPPER 0x03
26 #define MXC4005_REG_XOUT_LOWER 0x04
27 #define MXC4005_REG_YOUT_UPPER 0x05
28 #define MXC4005_REG_YOUT_LOWER 0x06
29 #define MXC4005_REG_ZOUT_UPPER 0x07
30 #define MXC4005_REG_ZOUT_LOWER 0x08
32 #define MXC4005_REG_INT_MASK0 0x0A
34 #define MXC4005_REG_INT_MASK1 0x0B
35 #define MXC4005_REG_INT_MASK1_BIT_DRDYE 0x01
37 #define MXC4005_REG_INT_CLR0 0x00
39 #define MXC4005_REG_INT_CLR1 0x01
40 #define MXC4005_REG_INT_CLR1_BIT_DRDYC 0x01
41 #define MXC4005_REG_INT_CLR1_SW_RST 0x10
43 #define MXC4005_REG_CONTROL 0x0D
44 #define MXC4005_REG_CONTROL_MASK_FSR GENMASK(6, 5)
45 #define MXC4005_CONTROL_FSR_SHIFT 5
47 #define MXC4005_REG_DEVICE_ID 0x0E
49 /* Datasheet does not specify a reset time, this is a conservative guess */
50 #define MXC4005_RESET_TIME_US 2000
67 struct regmap
*regmap
;
68 struct iio_trigger
*dready_trig
;
69 struct iio_mount_matrix orientation
;
70 /* Ensure timestamp is naturally aligned */
73 aligned_s64 timestamp
;
77 unsigned int int_mask1
;
81 * MXC4005 can operate in the following ranges:
82 * +/- 2G, 4G, 8G (the default +/-2G)
84 * (2 + 2) * 9.81 / (2^12 - 1) = 0.009582
85 * (4 + 4) * 9.81 / (2^12 - 1) = 0.019164
86 * (8 + 8) * 9.81 / (2^12 - 1) = 0.038329
91 } mxc4005_scale_table
[] = {
92 {MXC4005_RANGE_2G
, 9582},
93 {MXC4005_RANGE_4G
, 19164},
94 {MXC4005_RANGE_8G
, 38329},
98 static IIO_CONST_ATTR(in_accel_scale_available
, "0.009582 0.019164 0.038329");
100 static struct attribute
*mxc4005_attributes
[] = {
101 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
105 static const struct attribute_group mxc4005_attrs_group
= {
106 .attrs
= mxc4005_attributes
,
109 static bool mxc4005_is_readable_reg(struct device
*dev
, unsigned int reg
)
112 case MXC4005_REG_XOUT_UPPER
:
113 case MXC4005_REG_XOUT_LOWER
:
114 case MXC4005_REG_YOUT_UPPER
:
115 case MXC4005_REG_YOUT_LOWER
:
116 case MXC4005_REG_ZOUT_UPPER
:
117 case MXC4005_REG_ZOUT_LOWER
:
118 case MXC4005_REG_DEVICE_ID
:
119 case MXC4005_REG_CONTROL
:
126 static bool mxc4005_is_writeable_reg(struct device
*dev
, unsigned int reg
)
129 case MXC4005_REG_INT_CLR0
:
130 case MXC4005_REG_INT_CLR1
:
131 case MXC4005_REG_INT_MASK0
:
132 case MXC4005_REG_INT_MASK1
:
133 case MXC4005_REG_CONTROL
:
140 static const struct regmap_config mxc4005_regmap_config
= {
141 .name
= MXC4005_REGMAP_NAME
,
146 .max_register
= MXC4005_REG_DEVICE_ID
,
148 .readable_reg
= mxc4005_is_readable_reg
,
149 .writeable_reg
= mxc4005_is_writeable_reg
,
152 static int mxc4005_read_xyz(struct mxc4005_data
*data
)
156 ret
= regmap_bulk_read(data
->regmap
, MXC4005_REG_XOUT_UPPER
,
157 data
->scan
.chans
, sizeof(data
->scan
.chans
));
159 dev_err(data
->dev
, "failed to read axes\n");
166 static int mxc4005_read_axis(struct mxc4005_data
*data
,
172 ret
= regmap_bulk_read(data
->regmap
, addr
, ®
, sizeof(reg
));
174 dev_err(data
->dev
, "failed to read reg %02x\n", addr
);
178 return be16_to_cpu(reg
);
181 static int mxc4005_read_scale(struct mxc4005_data
*data
)
187 ret
= regmap_read(data
->regmap
, MXC4005_REG_CONTROL
, ®
);
189 dev_err(data
->dev
, "failed to read reg_control\n");
193 i
= reg
>> MXC4005_CONTROL_FSR_SHIFT
;
195 if (i
< 0 || i
>= ARRAY_SIZE(mxc4005_scale_table
))
198 return mxc4005_scale_table
[i
].scale
;
201 static int mxc4005_set_scale(struct mxc4005_data
*data
, int val
)
207 for (i
= 0; i
< ARRAY_SIZE(mxc4005_scale_table
); i
++) {
208 if (mxc4005_scale_table
[i
].scale
== val
) {
209 reg
= i
<< MXC4005_CONTROL_FSR_SHIFT
;
210 ret
= regmap_update_bits(data
->regmap
,
212 MXC4005_REG_CONTROL_MASK_FSR
,
216 "failed to write reg_control\n");
224 static int mxc4005_read_raw(struct iio_dev
*indio_dev
,
225 struct iio_chan_spec
const *chan
,
226 int *val
, int *val2
, long mask
)
228 struct mxc4005_data
*data
= iio_priv(indio_dev
);
232 case IIO_CHAN_INFO_RAW
:
233 switch (chan
->type
) {
235 if (iio_buffer_enabled(indio_dev
))
238 ret
= mxc4005_read_axis(data
, chan
->address
);
241 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
242 chan
->scan_type
.realbits
- 1);
247 case IIO_CHAN_INFO_SCALE
:
248 ret
= mxc4005_read_scale(data
);
254 return IIO_VAL_INT_PLUS_MICRO
;
260 static int mxc4005_write_raw(struct iio_dev
*indio_dev
,
261 struct iio_chan_spec
const *chan
,
262 int val
, int val2
, long mask
)
264 struct mxc4005_data
*data
= iio_priv(indio_dev
);
267 case IIO_CHAN_INFO_SCALE
:
271 return mxc4005_set_scale(data
, val2
);
277 static const struct iio_mount_matrix
*
278 mxc4005_get_mount_matrix(const struct iio_dev
*indio_dev
,
279 const struct iio_chan_spec
*chan
)
281 struct mxc4005_data
*data
= iio_priv(indio_dev
);
283 return &data
->orientation
;
286 static const struct iio_chan_spec_ext_info mxc4005_ext_info
[] = {
287 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, mxc4005_get_mount_matrix
),
291 static const struct iio_info mxc4005_info
= {
292 .read_raw
= mxc4005_read_raw
,
293 .write_raw
= mxc4005_write_raw
,
294 .attrs
= &mxc4005_attrs_group
,
297 static const unsigned long mxc4005_scan_masks
[] = {
298 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
302 #define MXC4005_CHANNEL(_axis, _addr) { \
305 .channel2 = IIO_MOD_##_axis, \
307 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
308 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
309 .scan_index = AXIS_##_axis, \
315 .endianness = IIO_BE, \
317 .ext_info = mxc4005_ext_info, \
320 static const struct iio_chan_spec mxc4005_channels
[] = {
321 MXC4005_CHANNEL(X
, MXC4005_REG_XOUT_UPPER
),
322 MXC4005_CHANNEL(Y
, MXC4005_REG_YOUT_UPPER
),
323 MXC4005_CHANNEL(Z
, MXC4005_REG_ZOUT_UPPER
),
324 IIO_CHAN_SOFT_TIMESTAMP(3),
327 static irqreturn_t
mxc4005_trigger_handler(int irq
, void *private)
329 struct iio_poll_func
*pf
= private;
330 struct iio_dev
*indio_dev
= pf
->indio_dev
;
331 struct mxc4005_data
*data
= iio_priv(indio_dev
);
334 ret
= mxc4005_read_xyz(data
);
338 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
342 iio_trigger_notify_done(indio_dev
->trig
);
347 static void mxc4005_clr_intr(struct mxc4005_data
*data
)
351 /* clear interrupt */
352 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
353 MXC4005_REG_INT_CLR1_BIT_DRDYC
);
355 dev_err(data
->dev
, "failed to write to reg_int_clr1\n");
358 static int mxc4005_set_trigger_state(struct iio_trigger
*trig
,
361 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
362 struct mxc4005_data
*data
= iio_priv(indio_dev
);
366 mutex_lock(&data
->mutex
);
368 val
= state
? MXC4005_REG_INT_MASK1_BIT_DRDYE
: 0;
369 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, val
);
371 mutex_unlock(&data
->mutex
);
372 dev_err(data
->dev
, "failed to update reg_int_mask1");
376 data
->int_mask1
= val
;
377 data
->trigger_enabled
= state
;
378 mutex_unlock(&data
->mutex
);
383 static void mxc4005_trigger_reen(struct iio_trigger
*trig
)
385 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
386 struct mxc4005_data
*data
= iio_priv(indio_dev
);
388 if (!data
->dready_trig
)
391 mxc4005_clr_intr(data
);
394 static const struct iio_trigger_ops mxc4005_trigger_ops
= {
395 .set_trigger_state
= mxc4005_set_trigger_state
,
396 .reenable
= mxc4005_trigger_reen
,
399 static int mxc4005_chip_init(struct mxc4005_data
*data
)
404 ret
= regmap_read(data
->regmap
, MXC4005_REG_DEVICE_ID
, ®
);
406 dev_err(data
->dev
, "failed to read chip id\n");
410 dev_dbg(data
->dev
, "MXC4005 chip id %02x\n", reg
);
412 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
413 MXC4005_REG_INT_CLR1_SW_RST
);
415 return dev_err_probe(data
->dev
, ret
, "resetting chip\n");
417 fsleep(MXC4005_RESET_TIME_US
);
419 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK0
, 0);
421 return dev_err_probe(data
->dev
, ret
, "writing INT_MASK0\n");
423 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, 0);
425 return dev_err_probe(data
->dev
, ret
, "writing INT_MASK1\n");
430 static int mxc4005_probe(struct i2c_client
*client
)
432 struct mxc4005_data
*data
;
433 struct iio_dev
*indio_dev
;
434 struct regmap
*regmap
;
437 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
441 regmap
= devm_regmap_init_i2c(client
, &mxc4005_regmap_config
);
442 if (IS_ERR(regmap
)) {
443 dev_err(&client
->dev
, "failed to initialize regmap\n");
444 return PTR_ERR(regmap
);
447 data
= iio_priv(indio_dev
);
448 i2c_set_clientdata(client
, indio_dev
);
449 data
->dev
= &client
->dev
;
450 data
->regmap
= regmap
;
452 ret
= mxc4005_chip_init(data
);
454 dev_err(&client
->dev
, "failed to initialize chip\n");
458 mutex_init(&data
->mutex
);
460 if (!iio_read_acpi_mount_matrix(&client
->dev
, &data
->orientation
, "ROTM")) {
461 ret
= iio_read_mount_matrix(&client
->dev
, &data
->orientation
);
466 indio_dev
->channels
= mxc4005_channels
;
467 indio_dev
->num_channels
= ARRAY_SIZE(mxc4005_channels
);
468 indio_dev
->available_scan_masks
= mxc4005_scan_masks
;
469 indio_dev
->name
= MXC4005_DRV_NAME
;
470 indio_dev
->modes
= INDIO_DIRECT_MODE
;
471 indio_dev
->info
= &mxc4005_info
;
473 ret
= devm_iio_triggered_buffer_setup(&client
->dev
, indio_dev
,
474 iio_pollfunc_store_time
,
475 mxc4005_trigger_handler
,
478 dev_err(&client
->dev
,
479 "failed to setup iio triggered buffer\n");
483 if (client
->irq
> 0) {
484 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
487 iio_device_id(indio_dev
));
488 if (!data
->dready_trig
)
491 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
492 iio_trigger_generic_data_rdy_poll
,
494 IRQF_TRIGGER_FALLING
|
499 dev_err(&client
->dev
,
500 "failed to init threaded irq\n");
504 data
->dready_trig
->ops
= &mxc4005_trigger_ops
;
505 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
506 ret
= devm_iio_trigger_register(&client
->dev
,
509 dev_err(&client
->dev
,
510 "failed to register trigger\n");
514 indio_dev
->trig
= iio_trigger_get(data
->dready_trig
);
517 return devm_iio_device_register(&client
->dev
, indio_dev
);
520 static int mxc4005_suspend(struct device
*dev
)
522 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
523 struct mxc4005_data
*data
= iio_priv(indio_dev
);
526 /* Save control to restore it on resume */
527 ret
= regmap_read(data
->regmap
, MXC4005_REG_CONTROL
, &data
->control
);
529 dev_err(data
->dev
, "failed to read reg_control\n");
534 static int mxc4005_resume(struct device
*dev
)
536 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
537 struct mxc4005_data
*data
= iio_priv(indio_dev
);
540 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
541 MXC4005_REG_INT_CLR1_SW_RST
);
543 dev_err(data
->dev
, "failed to reset chip: %d\n", ret
);
547 fsleep(MXC4005_RESET_TIME_US
);
549 ret
= regmap_write(data
->regmap
, MXC4005_REG_CONTROL
, data
->control
);
551 dev_err(data
->dev
, "failed to restore control register\n");
555 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK0
, 0);
557 dev_err(data
->dev
, "failed to restore interrupt 0 mask\n");
561 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
, data
->int_mask1
);
563 dev_err(data
->dev
, "failed to restore interrupt 1 mask\n");
570 static DEFINE_SIMPLE_DEV_PM_OPS(mxc4005_pm_ops
, mxc4005_suspend
, mxc4005_resume
);
572 static const struct acpi_device_id mxc4005_acpi_match
[] = {
578 MODULE_DEVICE_TABLE(acpi
, mxc4005_acpi_match
);
580 static const struct of_device_id mxc4005_of_match
[] = {
581 { .compatible
= "memsic,mxc4005", },
582 { .compatible
= "memsic,mxc6655", },
585 MODULE_DEVICE_TABLE(of
, mxc4005_of_match
);
587 static const struct i2c_device_id mxc4005_id
[] = {
592 MODULE_DEVICE_TABLE(i2c
, mxc4005_id
);
594 static struct i2c_driver mxc4005_driver
= {
596 .name
= MXC4005_DRV_NAME
,
597 .acpi_match_table
= mxc4005_acpi_match
,
598 .of_match_table
= mxc4005_of_match
,
599 .pm
= pm_sleep_ptr(&mxc4005_pm_ops
),
601 .probe
= mxc4005_probe
,
602 .id_table
= mxc4005_id
,
605 module_i2c_driver(mxc4005_driver
);
607 MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
608 MODULE_LICENSE("GPL v2");
609 MODULE_DESCRIPTION("MXC4005 3-axis accelerometer driver");