2 * 3-axis accelerometer driver for MXC4005XC Memsic sensor
4 * Copyright (c) 2014, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/iio/iio.h>
19 #include <linux/acpi.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/trigger_consumer.h>
27 #define MXC4005_DRV_NAME "mxc4005"
28 #define MXC4005_IRQ_NAME "mxc4005_event"
29 #define MXC4005_REGMAP_NAME "mxc4005_regmap"
31 #define MXC4005_REG_XOUT_UPPER 0x03
32 #define MXC4005_REG_XOUT_LOWER 0x04
33 #define MXC4005_REG_YOUT_UPPER 0x05
34 #define MXC4005_REG_YOUT_LOWER 0x06
35 #define MXC4005_REG_ZOUT_UPPER 0x07
36 #define MXC4005_REG_ZOUT_LOWER 0x08
38 #define MXC4005_REG_INT_MASK1 0x0B
39 #define MXC4005_REG_INT_MASK1_BIT_DRDYE 0x01
41 #define MXC4005_REG_INT_CLR1 0x01
42 #define MXC4005_REG_INT_CLR1_BIT_DRDYC 0x01
44 #define MXC4005_REG_CONTROL 0x0D
45 #define MXC4005_REG_CONTROL_MASK_FSR GENMASK(6, 5)
46 #define MXC4005_CONTROL_FSR_SHIFT 5
48 #define MXC4005_REG_DEVICE_ID 0x0E
65 struct regmap
*regmap
;
66 struct iio_trigger
*dready_trig
;
72 * MXC4005 can operate in the following ranges:
73 * +/- 2G, 4G, 8G (the default +/-2G)
75 * (2 + 2) * 9.81 / (2^12 - 1) = 0.009582
76 * (4 + 4) * 9.81 / (2^12 - 1) = 0.019164
77 * (8 + 8) * 9.81 / (2^12 - 1) = 0.038329
82 } mxc4005_scale_table
[] = {
83 {MXC4005_RANGE_2G
, 9582},
84 {MXC4005_RANGE_4G
, 19164},
85 {MXC4005_RANGE_8G
, 38329},
89 static IIO_CONST_ATTR(in_accel_scale_available
, "0.009582 0.019164 0.038329");
91 static struct attribute
*mxc4005_attributes
[] = {
92 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
96 static const struct attribute_group mxc4005_attrs_group
= {
97 .attrs
= mxc4005_attributes
,
100 static bool mxc4005_is_readable_reg(struct device
*dev
, unsigned int reg
)
103 case MXC4005_REG_XOUT_UPPER
:
104 case MXC4005_REG_XOUT_LOWER
:
105 case MXC4005_REG_YOUT_UPPER
:
106 case MXC4005_REG_YOUT_LOWER
:
107 case MXC4005_REG_ZOUT_UPPER
:
108 case MXC4005_REG_ZOUT_LOWER
:
109 case MXC4005_REG_DEVICE_ID
:
110 case MXC4005_REG_CONTROL
:
117 static bool mxc4005_is_writeable_reg(struct device
*dev
, unsigned int reg
)
120 case MXC4005_REG_INT_CLR1
:
121 case MXC4005_REG_INT_MASK1
:
122 case MXC4005_REG_CONTROL
:
129 static const struct regmap_config mxc4005_regmap_config
= {
130 .name
= MXC4005_REGMAP_NAME
,
135 .max_register
= MXC4005_REG_DEVICE_ID
,
137 .readable_reg
= mxc4005_is_readable_reg
,
138 .writeable_reg
= mxc4005_is_writeable_reg
,
141 static int mxc4005_read_xyz(struct mxc4005_data
*data
)
145 ret
= regmap_bulk_read(data
->regmap
, MXC4005_REG_XOUT_UPPER
,
146 (u8
*) data
->buffer
, sizeof(data
->buffer
));
148 dev_err(data
->dev
, "failed to read axes\n");
155 static int mxc4005_read_axis(struct mxc4005_data
*data
,
161 ret
= regmap_bulk_read(data
->regmap
, addr
, (u8
*) ®
, sizeof(reg
));
163 dev_err(data
->dev
, "failed to read reg %02x\n", addr
);
167 return be16_to_cpu(reg
);
170 static int mxc4005_read_scale(struct mxc4005_data
*data
)
176 ret
= regmap_read(data
->regmap
, MXC4005_REG_CONTROL
, ®
);
178 dev_err(data
->dev
, "failed to read reg_control\n");
182 i
= reg
>> MXC4005_CONTROL_FSR_SHIFT
;
184 if (i
< 0 || i
>= ARRAY_SIZE(mxc4005_scale_table
))
187 return mxc4005_scale_table
[i
].scale
;
190 static int mxc4005_set_scale(struct mxc4005_data
*data
, int val
)
196 for (i
= 0; i
< ARRAY_SIZE(mxc4005_scale_table
); i
++) {
197 if (mxc4005_scale_table
[i
].scale
== val
) {
198 reg
= i
<< MXC4005_CONTROL_FSR_SHIFT
;
199 ret
= regmap_update_bits(data
->regmap
,
201 MXC4005_REG_CONTROL_MASK_FSR
,
205 "failed to write reg_control\n");
213 static int mxc4005_read_raw(struct iio_dev
*indio_dev
,
214 struct iio_chan_spec
const *chan
,
215 int *val
, int *val2
, long mask
)
217 struct mxc4005_data
*data
= iio_priv(indio_dev
);
221 case IIO_CHAN_INFO_RAW
:
222 switch (chan
->type
) {
224 if (iio_buffer_enabled(indio_dev
))
227 ret
= mxc4005_read_axis(data
, chan
->address
);
230 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
231 chan
->scan_type
.realbits
- 1);
236 case IIO_CHAN_INFO_SCALE
:
237 ret
= mxc4005_read_scale(data
);
243 return IIO_VAL_INT_PLUS_MICRO
;
249 static int mxc4005_write_raw(struct iio_dev
*indio_dev
,
250 struct iio_chan_spec
const *chan
,
251 int val
, int val2
, long mask
)
253 struct mxc4005_data
*data
= iio_priv(indio_dev
);
256 case IIO_CHAN_INFO_SCALE
:
260 return mxc4005_set_scale(data
, val2
);
266 static const struct iio_info mxc4005_info
= {
267 .driver_module
= THIS_MODULE
,
268 .read_raw
= mxc4005_read_raw
,
269 .write_raw
= mxc4005_write_raw
,
270 .attrs
= &mxc4005_attrs_group
,
273 static const unsigned long mxc4005_scan_masks
[] = {
274 BIT(AXIS_X
) | BIT(AXIS_Y
) | BIT(AXIS_Z
),
278 #define MXC4005_CHANNEL(_axis, _addr) { \
281 .channel2 = IIO_MOD_##_axis, \
283 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
284 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
285 .scan_index = AXIS_##_axis, \
291 .endianness = IIO_BE, \
295 static const struct iio_chan_spec mxc4005_channels
[] = {
296 MXC4005_CHANNEL(X
, MXC4005_REG_XOUT_UPPER
),
297 MXC4005_CHANNEL(Y
, MXC4005_REG_YOUT_UPPER
),
298 MXC4005_CHANNEL(Z
, MXC4005_REG_ZOUT_UPPER
),
299 IIO_CHAN_SOFT_TIMESTAMP(3),
302 static irqreturn_t
mxc4005_trigger_handler(int irq
, void *private)
304 struct iio_poll_func
*pf
= private;
305 struct iio_dev
*indio_dev
= pf
->indio_dev
;
306 struct mxc4005_data
*data
= iio_priv(indio_dev
);
309 ret
= mxc4005_read_xyz(data
);
313 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
317 iio_trigger_notify_done(indio_dev
->trig
);
322 static int mxc4005_clr_intr(struct mxc4005_data
*data
)
326 /* clear interrupt */
327 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_CLR1
,
328 MXC4005_REG_INT_CLR1_BIT_DRDYC
);
330 dev_err(data
->dev
, "failed to write to reg_int_clr1\n");
337 static int mxc4005_set_trigger_state(struct iio_trigger
*trig
,
340 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
341 struct mxc4005_data
*data
= iio_priv(indio_dev
);
344 mutex_lock(&data
->mutex
);
346 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
,
347 MXC4005_REG_INT_MASK1_BIT_DRDYE
);
349 ret
= regmap_write(data
->regmap
, MXC4005_REG_INT_MASK1
,
350 ~MXC4005_REG_INT_MASK1_BIT_DRDYE
);
354 mutex_unlock(&data
->mutex
);
355 dev_err(data
->dev
, "failed to update reg_int_mask1");
359 data
->trigger_enabled
= state
;
360 mutex_unlock(&data
->mutex
);
365 static int mxc4005_trigger_try_reen(struct iio_trigger
*trig
)
367 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
368 struct mxc4005_data
*data
= iio_priv(indio_dev
);
370 if (!data
->dready_trig
)
373 return mxc4005_clr_intr(data
);
376 static const struct iio_trigger_ops mxc4005_trigger_ops
= {
377 .set_trigger_state
= mxc4005_set_trigger_state
,
378 .try_reenable
= mxc4005_trigger_try_reen
,
379 .owner
= THIS_MODULE
,
382 static int mxc4005_chip_init(struct mxc4005_data
*data
)
387 ret
= regmap_read(data
->regmap
, MXC4005_REG_DEVICE_ID
, ®
);
389 dev_err(data
->dev
, "failed to read chip id\n");
393 dev_dbg(data
->dev
, "MXC4005 chip id %02x\n", reg
);
398 static int mxc4005_probe(struct i2c_client
*client
,
399 const struct i2c_device_id
*id
)
401 struct mxc4005_data
*data
;
402 struct iio_dev
*indio_dev
;
403 struct regmap
*regmap
;
406 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
410 regmap
= devm_regmap_init_i2c(client
, &mxc4005_regmap_config
);
411 if (IS_ERR(regmap
)) {
412 dev_err(&client
->dev
, "failed to initialize regmap\n");
413 return PTR_ERR(regmap
);
416 data
= iio_priv(indio_dev
);
417 i2c_set_clientdata(client
, indio_dev
);
418 data
->dev
= &client
->dev
;
419 data
->regmap
= regmap
;
421 ret
= mxc4005_chip_init(data
);
423 dev_err(&client
->dev
, "failed to initialize chip\n");
427 mutex_init(&data
->mutex
);
429 indio_dev
->dev
.parent
= &client
->dev
;
430 indio_dev
->channels
= mxc4005_channels
;
431 indio_dev
->num_channels
= ARRAY_SIZE(mxc4005_channels
);
432 indio_dev
->available_scan_masks
= mxc4005_scan_masks
;
433 indio_dev
->name
= MXC4005_DRV_NAME
;
434 indio_dev
->modes
= INDIO_DIRECT_MODE
;
435 indio_dev
->info
= &mxc4005_info
;
437 ret
= iio_triggered_buffer_setup(indio_dev
,
438 iio_pollfunc_store_time
,
439 mxc4005_trigger_handler
,
442 dev_err(&client
->dev
,
443 "failed to setup iio triggered buffer\n");
447 if (client
->irq
> 0) {
448 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
452 if (!data
->dready_trig
)
455 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
456 iio_trigger_generic_data_rdy_poll
,
458 IRQF_TRIGGER_FALLING
|
463 dev_err(&client
->dev
,
464 "failed to init threaded irq\n");
465 goto err_buffer_cleanup
;
468 data
->dready_trig
->dev
.parent
= &client
->dev
;
469 data
->dready_trig
->ops
= &mxc4005_trigger_ops
;
470 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
471 indio_dev
->trig
= data
->dready_trig
;
472 iio_trigger_get(indio_dev
->trig
);
473 ret
= iio_trigger_register(data
->dready_trig
);
475 dev_err(&client
->dev
,
476 "failed to register trigger\n");
477 goto err_trigger_unregister
;
481 ret
= iio_device_register(indio_dev
);
483 dev_err(&client
->dev
,
484 "unable to register iio device %d\n", ret
);
485 goto err_buffer_cleanup
;
490 err_trigger_unregister
:
491 iio_trigger_unregister(data
->dready_trig
);
493 iio_triggered_buffer_cleanup(indio_dev
);
498 static int mxc4005_remove(struct i2c_client
*client
)
500 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
501 struct mxc4005_data
*data
= iio_priv(indio_dev
);
503 iio_device_unregister(indio_dev
);
505 iio_triggered_buffer_cleanup(indio_dev
);
506 if (data
->dready_trig
)
507 iio_trigger_unregister(data
->dready_trig
);
512 static const struct acpi_device_id mxc4005_acpi_match
[] = {
516 MODULE_DEVICE_TABLE(acpi
, mxc4005_acpi_match
);
518 static const struct i2c_device_id mxc4005_id
[] = {
522 MODULE_DEVICE_TABLE(i2c
, mxc4005_id
);
524 static struct i2c_driver mxc4005_driver
= {
526 .name
= MXC4005_DRV_NAME
,
527 .acpi_match_table
= ACPI_PTR(mxc4005_acpi_match
),
529 .probe
= mxc4005_probe
,
530 .remove
= mxc4005_remove
,
531 .id_table
= mxc4005_id
,
534 module_i2c_driver(mxc4005_driver
);
536 MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
537 MODULE_LICENSE("GPL v2");
538 MODULE_DESCRIPTION("MXC4005 3-axis accelerometer driver");