2 * Sensortek STK8312 3-Axis Accelerometer
4 * Copyright (c) 2015, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for STK8312; 7-bit I2C address: 0x3D.
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/trigger_consumer.h>
26 #define STK8312_REG_XOUT 0x00
27 #define STK8312_REG_YOUT 0x01
28 #define STK8312_REG_ZOUT 0x02
29 #define STK8312_REG_INTSU 0x06
30 #define STK8312_REG_MODE 0x07
31 #define STK8312_REG_SR 0x08
32 #define STK8312_REG_STH 0x13
33 #define STK8312_REG_RESET 0x20
34 #define STK8312_REG_AFECTRL 0x24
35 #define STK8312_REG_OTPADDR 0x3D
36 #define STK8312_REG_OTPDATA 0x3E
37 #define STK8312_REG_OTPCTRL 0x3F
39 #define STK8312_MODE_ACTIVE BIT(0)
40 #define STK8312_MODE_STANDBY 0x00
41 #define STK8312_MODE_INT_AH_PP 0xC0 /* active-high, push-pull */
42 #define STK8312_DREADY_BIT BIT(4)
43 #define STK8312_RNG_6G 1
44 #define STK8312_RNG_SHIFT 6
45 #define STK8312_RNG_MASK GENMASK(7, 6)
46 #define STK8312_SR_MASK GENMASK(2, 0)
47 #define STK8312_SR_400HZ_IDX 0
48 #define STK8312_ALL_CHANNEL_MASK GENMASK(2, 0)
49 #define STK8312_ALL_CHANNEL_SIZE 3
51 #define STK8312_DRIVER_NAME "stk8312"
52 #define STK8312_IRQ_NAME "stk8312_event"
55 * The accelerometer has two measurement ranges:
57 * -6g - +6g (8-bit, signed)
58 * -16g - +16g (8-bit, signed)
60 * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616
61 * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311
63 #define STK8312_SCALE_AVAIL "0.4616 1.2311"
65 static const int stk8312_scale_table
[][2] = {
66 {0, 461600}, {1, 231100}
72 } stk8312_samp_freq_table
[] = {
73 {400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0},
74 {12, 500000}, {6, 250000}, {3, 125000}
77 #define STK8312_ACCEL_CHANNEL(index, reg, axis) { \
81 .channel2 = IIO_MOD_##axis, \
82 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
83 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
84 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
85 .scan_index = index, \
90 .endianness = IIO_CPU, \
94 static const struct iio_chan_spec stk8312_channels
[] = {
95 STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT
, X
),
96 STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT
, Y
),
97 STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT
, Z
),
98 IIO_CHAN_SOFT_TIMESTAMP(3),
101 struct stk8312_data
{
102 struct i2c_client
*client
;
107 struct iio_trigger
*dready_trig
;
108 bool dready_trigger_on
;
109 s8 buffer
[16]; /* 3x8-bit channels + 5x8 padding + 64-bit timestamp */
112 static IIO_CONST_ATTR(in_accel_scale_available
, STK8312_SCALE_AVAIL
);
114 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
116 static struct attribute
*stk8312_attributes
[] = {
117 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
118 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
122 static const struct attribute_group stk8312_attribute_group
= {
123 .attrs
= stk8312_attributes
126 static int stk8312_otp_init(struct stk8312_data
*data
)
130 struct i2c_client
*client
= data
->client
;
132 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_OTPADDR
, 0x70);
135 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_OTPCTRL
, 0x02);
140 usleep_range(1000, 5000);
141 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_OTPCTRL
);
145 } while (!(ret
& BIT(7)) && count
> 0);
152 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_OTPDATA
);
158 ret
= i2c_smbus_write_byte_data(data
->client
, STK8312_REG_AFECTRL
, ret
);
166 dev_err(&client
->dev
, "failed to initialize sensor\n");
170 static int stk8312_set_mode(struct stk8312_data
*data
, u8 mode
)
173 struct i2c_client
*client
= data
->client
;
175 if (mode
== data
->mode
)
178 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_MODE
, mode
);
180 dev_err(&client
->dev
, "failed to change sensor mode\n");
185 if (mode
& STK8312_MODE_ACTIVE
) {
186 /* Need to run OTP sequence before entering active mode */
187 usleep_range(1000, 5000);
188 ret
= stk8312_otp_init(data
);
194 static int stk8312_set_interrupts(struct stk8312_data
*data
, u8 int_mask
)
198 struct i2c_client
*client
= data
->client
;
201 /* We need to go in standby mode to modify registers */
202 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
206 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_INTSU
, int_mask
);
208 dev_err(&client
->dev
, "failed to set interrupts\n");
209 stk8312_set_mode(data
, mode
);
213 return stk8312_set_mode(data
, mode
);
216 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
219 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
220 struct stk8312_data
*data
= iio_priv(indio_dev
);
224 ret
= stk8312_set_interrupts(data
, STK8312_DREADY_BIT
);
226 ret
= stk8312_set_interrupts(data
, 0x00);
229 dev_err(&data
->client
->dev
, "failed to set trigger state\n");
233 data
->dready_trigger_on
= state
;
238 static const struct iio_trigger_ops stk8312_trigger_ops
= {
239 .set_trigger_state
= stk8312_data_rdy_trigger_set_state
,
242 static int stk8312_set_sample_rate(struct stk8312_data
*data
, u8 rate
)
247 struct i2c_client
*client
= data
->client
;
249 if (rate
== data
->sample_rate_idx
)
253 /* We need to go in standby mode to modify registers */
254 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
258 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_SR
);
262 masked_reg
= (ret
& (~STK8312_SR_MASK
)) | rate
;
264 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_SR
, masked_reg
);
268 data
->sample_rate_idx
= rate
;
270 return stk8312_set_mode(data
, mode
);
273 dev_err(&client
->dev
, "failed to set sampling rate\n");
274 stk8312_set_mode(data
, mode
);
279 static int stk8312_set_range(struct stk8312_data
*data
, u8 range
)
284 struct i2c_client
*client
= data
->client
;
286 if (range
!= 1 && range
!= 2)
288 else if (range
== data
->range
)
292 /* We need to go in standby mode to modify registers */
293 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
297 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_STH
);
301 masked_reg
= ret
& (~STK8312_RNG_MASK
);
302 masked_reg
|= range
<< STK8312_RNG_SHIFT
;
304 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_STH
, masked_reg
);
310 return stk8312_set_mode(data
, mode
);
313 dev_err(&client
->dev
, "failed to change sensor range\n");
314 stk8312_set_mode(data
, mode
);
319 static int stk8312_read_accel(struct stk8312_data
*data
, u8 address
)
322 struct i2c_client
*client
= data
->client
;
327 ret
= i2c_smbus_read_byte_data(client
, address
);
329 dev_err(&client
->dev
, "register read failed\n");
334 static int stk8312_read_raw(struct iio_dev
*indio_dev
,
335 struct iio_chan_spec
const *chan
,
336 int *val
, int *val2
, long mask
)
338 struct stk8312_data
*data
= iio_priv(indio_dev
);
342 case IIO_CHAN_INFO_RAW
:
343 if (iio_buffer_enabled(indio_dev
))
345 mutex_lock(&data
->lock
);
346 ret
= stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
348 mutex_unlock(&data
->lock
);
351 ret
= stk8312_read_accel(data
, chan
->address
);
353 stk8312_set_mode(data
,
354 data
->mode
& (~STK8312_MODE_ACTIVE
));
355 mutex_unlock(&data
->lock
);
358 *val
= sign_extend32(ret
, 7);
359 ret
= stk8312_set_mode(data
,
360 data
->mode
& (~STK8312_MODE_ACTIVE
));
361 mutex_unlock(&data
->lock
);
365 case IIO_CHAN_INFO_SCALE
:
366 *val
= stk8312_scale_table
[data
->range
- 1][0];
367 *val2
= stk8312_scale_table
[data
->range
- 1][1];
368 return IIO_VAL_INT_PLUS_MICRO
;
369 case IIO_CHAN_INFO_SAMP_FREQ
:
370 *val
= stk8312_samp_freq_table
[data
->sample_rate_idx
].val
;
371 *val2
= stk8312_samp_freq_table
[data
->sample_rate_idx
].val2
;
372 return IIO_VAL_INT_PLUS_MICRO
;
378 static int stk8312_write_raw(struct iio_dev
*indio_dev
,
379 struct iio_chan_spec
const *chan
,
380 int val
, int val2
, long mask
)
385 struct stk8312_data
*data
= iio_priv(indio_dev
);
388 case IIO_CHAN_INFO_SCALE
:
389 for (i
= 0; i
< ARRAY_SIZE(stk8312_scale_table
); i
++)
390 if (val
== stk8312_scale_table
[i
][0] &&
391 val2
== stk8312_scale_table
[i
][1]) {
398 mutex_lock(&data
->lock
);
399 ret
= stk8312_set_range(data
, index
);
400 mutex_unlock(&data
->lock
);
403 case IIO_CHAN_INFO_SAMP_FREQ
:
404 for (i
= 0; i
< ARRAY_SIZE(stk8312_samp_freq_table
); i
++)
405 if (val
== stk8312_samp_freq_table
[i
].val
&&
406 val2
== stk8312_samp_freq_table
[i
].val2
) {
412 mutex_lock(&data
->lock
);
413 ret
= stk8312_set_sample_rate(data
, index
);
414 mutex_unlock(&data
->lock
);
422 static const struct iio_info stk8312_info
= {
423 .read_raw
= stk8312_read_raw
,
424 .write_raw
= stk8312_write_raw
,
425 .attrs
= &stk8312_attribute_group
,
428 static irqreturn_t
stk8312_trigger_handler(int irq
, void *p
)
430 struct iio_poll_func
*pf
= p
;
431 struct iio_dev
*indio_dev
= pf
->indio_dev
;
432 struct stk8312_data
*data
= iio_priv(indio_dev
);
435 mutex_lock(&data
->lock
);
437 * Do a bulk read if all channels are requested,
438 * from 0x00 (XOUT) to 0x02 (ZOUT)
440 if (*(indio_dev
->active_scan_mask
) == STK8312_ALL_CHANNEL_MASK
) {
441 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
443 STK8312_ALL_CHANNEL_SIZE
,
445 if (ret
< STK8312_ALL_CHANNEL_SIZE
) {
446 dev_err(&data
->client
->dev
, "register read failed\n");
447 mutex_unlock(&data
->lock
);
451 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
452 indio_dev
->masklength
) {
453 ret
= stk8312_read_accel(data
, bit
);
455 mutex_unlock(&data
->lock
);
458 data
->buffer
[i
++] = ret
;
461 mutex_unlock(&data
->lock
);
463 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
466 iio_trigger_notify_done(indio_dev
->trig
);
471 static irqreturn_t
stk8312_data_rdy_trig_poll(int irq
, void *private)
473 struct iio_dev
*indio_dev
= private;
474 struct stk8312_data
*data
= iio_priv(indio_dev
);
476 if (data
->dready_trigger_on
)
477 iio_trigger_poll(data
->dready_trig
);
482 static int stk8312_buffer_preenable(struct iio_dev
*indio_dev
)
484 struct stk8312_data
*data
= iio_priv(indio_dev
);
486 return stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
489 static int stk8312_buffer_postdisable(struct iio_dev
*indio_dev
)
491 struct stk8312_data
*data
= iio_priv(indio_dev
);
493 return stk8312_set_mode(data
, data
->mode
& (~STK8312_MODE_ACTIVE
));
496 static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops
= {
497 .preenable
= stk8312_buffer_preenable
,
498 .postenable
= iio_triggered_buffer_postenable
,
499 .predisable
= iio_triggered_buffer_predisable
,
500 .postdisable
= stk8312_buffer_postdisable
,
503 static int stk8312_probe(struct i2c_client
*client
,
504 const struct i2c_device_id
*id
)
507 struct iio_dev
*indio_dev
;
508 struct stk8312_data
*data
;
510 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
512 dev_err(&client
->dev
, "iio allocation failed!\n");
516 data
= iio_priv(indio_dev
);
517 data
->client
= client
;
518 i2c_set_clientdata(client
, indio_dev
);
519 mutex_init(&data
->lock
);
521 indio_dev
->dev
.parent
= &client
->dev
;
522 indio_dev
->info
= &stk8312_info
;
523 indio_dev
->name
= STK8312_DRIVER_NAME
;
524 indio_dev
->modes
= INDIO_DIRECT_MODE
;
525 indio_dev
->channels
= stk8312_channels
;
526 indio_dev
->num_channels
= ARRAY_SIZE(stk8312_channels
);
528 /* A software reset is recommended at power-on */
529 ret
= i2c_smbus_write_byte_data(data
->client
, STK8312_REG_RESET
, 0x00);
531 dev_err(&client
->dev
, "failed to reset sensor\n");
534 data
->sample_rate_idx
= STK8312_SR_400HZ_IDX
;
535 ret
= stk8312_set_range(data
, STK8312_RNG_6G
);
539 ret
= stk8312_set_mode(data
,
540 STK8312_MODE_INT_AH_PP
| STK8312_MODE_ACTIVE
);
544 if (client
->irq
> 0) {
545 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
546 stk8312_data_rdy_trig_poll
,
548 IRQF_TRIGGER_RISING
|
553 dev_err(&client
->dev
, "request irq %d failed\n",
558 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
562 if (!data
->dready_trig
) {
567 data
->dready_trig
->dev
.parent
= &client
->dev
;
568 data
->dready_trig
->ops
= &stk8312_trigger_ops
;
569 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
570 ret
= iio_trigger_register(data
->dready_trig
);
572 dev_err(&client
->dev
, "iio trigger register failed\n");
577 ret
= iio_triggered_buffer_setup(indio_dev
,
578 iio_pollfunc_store_time
,
579 stk8312_trigger_handler
,
580 &stk8312_buffer_setup_ops
);
582 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
583 goto err_trigger_unregister
;
586 ret
= iio_device_register(indio_dev
);
588 dev_err(&client
->dev
, "device_register failed\n");
589 goto err_buffer_cleanup
;
595 iio_triggered_buffer_cleanup(indio_dev
);
596 err_trigger_unregister
:
597 if (data
->dready_trig
)
598 iio_trigger_unregister(data
->dready_trig
);
600 stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
604 static int stk8312_remove(struct i2c_client
*client
)
606 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
607 struct stk8312_data
*data
= iio_priv(indio_dev
);
609 iio_device_unregister(indio_dev
);
610 iio_triggered_buffer_cleanup(indio_dev
);
612 if (data
->dready_trig
)
613 iio_trigger_unregister(data
->dready_trig
);
615 return stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
618 #ifdef CONFIG_PM_SLEEP
619 static int stk8312_suspend(struct device
*dev
)
621 struct stk8312_data
*data
;
623 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
625 return stk8312_set_mode(data
, data
->mode
& (~STK8312_MODE_ACTIVE
));
628 static int stk8312_resume(struct device
*dev
)
630 struct stk8312_data
*data
;
632 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
634 return stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
637 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops
, stk8312_suspend
, stk8312_resume
);
639 #define STK8312_PM_OPS (&stk8312_pm_ops)
641 #define STK8312_PM_OPS NULL
644 static const struct i2c_device_id stk8312_i2c_id
[] = {
648 MODULE_DEVICE_TABLE(i2c
, stk8312_i2c_id
);
650 static const struct acpi_device_id stk8312_acpi_id
[] = {
655 MODULE_DEVICE_TABLE(acpi
, stk8312_acpi_id
);
657 static struct i2c_driver stk8312_driver
= {
659 .name
= STK8312_DRIVER_NAME
,
660 .pm
= STK8312_PM_OPS
,
661 .acpi_match_table
= ACPI_PTR(stk8312_acpi_id
),
663 .probe
= stk8312_probe
,
664 .remove
= stk8312_remove
,
665 .id_table
= stk8312_i2c_id
,
668 module_i2c_driver(stk8312_driver
);
670 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
671 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
672 MODULE_LICENSE("GPL v2");