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/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/iio/trigger_consumer.h>
27 #define STK8312_REG_XOUT 0x00
28 #define STK8312_REG_YOUT 0x01
29 #define STK8312_REG_ZOUT 0x02
30 #define STK8312_REG_INTSU 0x06
31 #define STK8312_REG_MODE 0x07
32 #define STK8312_REG_SR 0x08
33 #define STK8312_REG_STH 0x13
34 #define STK8312_REG_RESET 0x20
35 #define STK8312_REG_AFECTRL 0x24
36 #define STK8312_REG_OTPADDR 0x3D
37 #define STK8312_REG_OTPDATA 0x3E
38 #define STK8312_REG_OTPCTRL 0x3F
40 #define STK8312_MODE_ACTIVE BIT(0)
41 #define STK8312_MODE_STANDBY 0x00
42 #define STK8312_MODE_INT_AH_PP 0xC0 /* active-high, push-pull */
43 #define STK8312_DREADY_BIT BIT(4)
44 #define STK8312_RNG_6G 1
45 #define STK8312_RNG_SHIFT 6
46 #define STK8312_RNG_MASK GENMASK(7, 6)
47 #define STK8312_SR_MASK GENMASK(2, 0)
48 #define STK8312_SR_400HZ_IDX 0
49 #define STK8312_ALL_CHANNEL_MASK GENMASK(2, 0)
50 #define STK8312_ALL_CHANNEL_SIZE 3
52 #define STK8312_DRIVER_NAME "stk8312"
53 #define STK8312_IRQ_NAME "stk8312_event"
56 * The accelerometer has two measurement ranges:
58 * -6g - +6g (8-bit, signed)
59 * -16g - +16g (8-bit, signed)
61 * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616
62 * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311
64 #define STK8312_SCALE_AVAIL "0.4616 1.2311"
66 static const int stk8312_scale_table
[][2] = {
67 {0, 461600}, {1, 231100}
73 } stk8312_samp_freq_table
[] = {
74 {400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0},
75 {12, 500000}, {6, 250000}, {3, 125000}
78 #define STK8312_ACCEL_CHANNEL(index, reg, axis) { \
82 .channel2 = IIO_MOD_##axis, \
83 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
84 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
85 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
86 .scan_index = index, \
91 .endianness = IIO_CPU, \
95 static const struct iio_chan_spec stk8312_channels
[] = {
96 STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT
, X
),
97 STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT
, Y
),
98 STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT
, Z
),
99 IIO_CHAN_SOFT_TIMESTAMP(3),
102 struct stk8312_data
{
103 struct i2c_client
*client
;
108 struct iio_trigger
*dready_trig
;
109 bool dready_trigger_on
;
110 s8 buffer
[16]; /* 3x8-bit channels + 5x8 padding + 64-bit timestamp */
113 static IIO_CONST_ATTR(in_accel_scale_available
, STK8312_SCALE_AVAIL
);
115 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
117 static struct attribute
*stk8312_attributes
[] = {
118 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
119 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
123 static const struct attribute_group stk8312_attribute_group
= {
124 .attrs
= stk8312_attributes
127 static int stk8312_otp_init(struct stk8312_data
*data
)
131 struct i2c_client
*client
= data
->client
;
133 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_OTPADDR
, 0x70);
136 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_OTPCTRL
, 0x02);
141 usleep_range(1000, 5000);
142 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_OTPCTRL
);
146 } while (!(ret
& BIT(7)) && count
> 0);
153 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_OTPDATA
);
159 ret
= i2c_smbus_write_byte_data(data
->client
, STK8312_REG_AFECTRL
, ret
);
167 dev_err(&client
->dev
, "failed to initialize sensor\n");
171 static int stk8312_set_mode(struct stk8312_data
*data
, u8 mode
)
174 struct i2c_client
*client
= data
->client
;
176 if (mode
== data
->mode
)
179 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_MODE
, mode
);
181 dev_err(&client
->dev
, "failed to change sensor mode\n");
186 if (mode
& STK8312_MODE_ACTIVE
) {
187 /* Need to run OTP sequence before entering active mode */
188 usleep_range(1000, 5000);
189 ret
= stk8312_otp_init(data
);
195 static int stk8312_set_interrupts(struct stk8312_data
*data
, u8 int_mask
)
199 struct i2c_client
*client
= data
->client
;
202 /* We need to go in standby mode to modify registers */
203 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
207 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_INTSU
, int_mask
);
209 dev_err(&client
->dev
, "failed to set interrupts\n");
210 stk8312_set_mode(data
, mode
);
214 return stk8312_set_mode(data
, mode
);
217 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
220 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
221 struct stk8312_data
*data
= iio_priv(indio_dev
);
225 ret
= stk8312_set_interrupts(data
, STK8312_DREADY_BIT
);
227 ret
= stk8312_set_interrupts(data
, 0x00);
230 dev_err(&data
->client
->dev
, "failed to set trigger state\n");
234 data
->dready_trigger_on
= state
;
239 static const struct iio_trigger_ops stk8312_trigger_ops
= {
240 .set_trigger_state
= stk8312_data_rdy_trigger_set_state
,
241 .owner
= THIS_MODULE
,
244 static int stk8312_set_sample_rate(struct stk8312_data
*data
, u8 rate
)
249 struct i2c_client
*client
= data
->client
;
251 if (rate
== data
->sample_rate_idx
)
255 /* We need to go in standby mode to modify registers */
256 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
260 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_SR
);
264 masked_reg
= (ret
& (~STK8312_SR_MASK
)) | rate
;
266 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_SR
, masked_reg
);
270 data
->sample_rate_idx
= rate
;
272 return stk8312_set_mode(data
, mode
);
275 dev_err(&client
->dev
, "failed to set sampling rate\n");
276 stk8312_set_mode(data
, mode
);
281 static int stk8312_set_range(struct stk8312_data
*data
, u8 range
)
286 struct i2c_client
*client
= data
->client
;
288 if (range
!= 1 && range
!= 2)
290 else if (range
== data
->range
)
294 /* We need to go in standby mode to modify registers */
295 ret
= stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
299 ret
= i2c_smbus_read_byte_data(client
, STK8312_REG_STH
);
303 masked_reg
= ret
& (~STK8312_RNG_MASK
);
304 masked_reg
|= range
<< STK8312_RNG_SHIFT
;
306 ret
= i2c_smbus_write_byte_data(client
, STK8312_REG_STH
, masked_reg
);
312 return stk8312_set_mode(data
, mode
);
315 dev_err(&client
->dev
, "failed to change sensor range\n");
316 stk8312_set_mode(data
, mode
);
321 static int stk8312_read_accel(struct stk8312_data
*data
, u8 address
)
324 struct i2c_client
*client
= data
->client
;
329 ret
= i2c_smbus_read_byte_data(client
, address
);
331 dev_err(&client
->dev
, "register read failed\n");
336 static int stk8312_read_raw(struct iio_dev
*indio_dev
,
337 struct iio_chan_spec
const *chan
,
338 int *val
, int *val2
, long mask
)
340 struct stk8312_data
*data
= iio_priv(indio_dev
);
344 case IIO_CHAN_INFO_RAW
:
345 if (iio_buffer_enabled(indio_dev
))
347 mutex_lock(&data
->lock
);
348 ret
= stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
350 mutex_unlock(&data
->lock
);
353 ret
= stk8312_read_accel(data
, chan
->address
);
355 stk8312_set_mode(data
,
356 data
->mode
& (~STK8312_MODE_ACTIVE
));
357 mutex_unlock(&data
->lock
);
360 *val
= sign_extend32(ret
, 7);
361 ret
= stk8312_set_mode(data
,
362 data
->mode
& (~STK8312_MODE_ACTIVE
));
363 mutex_unlock(&data
->lock
);
367 case IIO_CHAN_INFO_SCALE
:
368 *val
= stk8312_scale_table
[data
->range
- 1][0];
369 *val2
= stk8312_scale_table
[data
->range
- 1][1];
370 return IIO_VAL_INT_PLUS_MICRO
;
371 case IIO_CHAN_INFO_SAMP_FREQ
:
372 *val
= stk8312_samp_freq_table
[data
->sample_rate_idx
].val
;
373 *val2
= stk8312_samp_freq_table
[data
->sample_rate_idx
].val2
;
374 return IIO_VAL_INT_PLUS_MICRO
;
380 static int stk8312_write_raw(struct iio_dev
*indio_dev
,
381 struct iio_chan_spec
const *chan
,
382 int val
, int val2
, long mask
)
387 struct stk8312_data
*data
= iio_priv(indio_dev
);
390 case IIO_CHAN_INFO_SCALE
:
391 for (i
= 0; i
< ARRAY_SIZE(stk8312_scale_table
); i
++)
392 if (val
== stk8312_scale_table
[i
][0] &&
393 val2
== stk8312_scale_table
[i
][1]) {
400 mutex_lock(&data
->lock
);
401 ret
= stk8312_set_range(data
, index
);
402 mutex_unlock(&data
->lock
);
405 case IIO_CHAN_INFO_SAMP_FREQ
:
406 for (i
= 0; i
< ARRAY_SIZE(stk8312_samp_freq_table
); i
++)
407 if (val
== stk8312_samp_freq_table
[i
].val
&&
408 val2
== stk8312_samp_freq_table
[i
].val2
) {
414 mutex_lock(&data
->lock
);
415 ret
= stk8312_set_sample_rate(data
, index
);
416 mutex_unlock(&data
->lock
);
424 static const struct iio_info stk8312_info
= {
425 .driver_module
= THIS_MODULE
,
426 .read_raw
= stk8312_read_raw
,
427 .write_raw
= stk8312_write_raw
,
428 .attrs
= &stk8312_attribute_group
,
431 static irqreturn_t
stk8312_trigger_handler(int irq
, void *p
)
433 struct iio_poll_func
*pf
= p
;
434 struct iio_dev
*indio_dev
= pf
->indio_dev
;
435 struct stk8312_data
*data
= iio_priv(indio_dev
);
438 mutex_lock(&data
->lock
);
440 * Do a bulk read if all channels are requested,
441 * from 0x00 (XOUT) to 0x02 (ZOUT)
443 if (*(indio_dev
->active_scan_mask
) == STK8312_ALL_CHANNEL_MASK
) {
444 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
446 STK8312_ALL_CHANNEL_SIZE
,
448 if (ret
< STK8312_ALL_CHANNEL_SIZE
) {
449 dev_err(&data
->client
->dev
, "register read failed\n");
450 mutex_unlock(&data
->lock
);
454 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
455 indio_dev
->masklength
) {
456 ret
= stk8312_read_accel(data
, bit
);
458 mutex_unlock(&data
->lock
);
461 data
->buffer
[i
++] = ret
;
464 mutex_unlock(&data
->lock
);
466 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
469 iio_trigger_notify_done(indio_dev
->trig
);
474 static irqreturn_t
stk8312_data_rdy_trig_poll(int irq
, void *private)
476 struct iio_dev
*indio_dev
= private;
477 struct stk8312_data
*data
= iio_priv(indio_dev
);
479 if (data
->dready_trigger_on
)
480 iio_trigger_poll(data
->dready_trig
);
485 static int stk8312_buffer_preenable(struct iio_dev
*indio_dev
)
487 struct stk8312_data
*data
= iio_priv(indio_dev
);
489 return stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
492 static int stk8312_buffer_postdisable(struct iio_dev
*indio_dev
)
494 struct stk8312_data
*data
= iio_priv(indio_dev
);
496 return stk8312_set_mode(data
, data
->mode
& (~STK8312_MODE_ACTIVE
));
499 static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops
= {
500 .preenable
= stk8312_buffer_preenable
,
501 .postenable
= iio_triggered_buffer_postenable
,
502 .predisable
= iio_triggered_buffer_predisable
,
503 .postdisable
= stk8312_buffer_postdisable
,
506 static int stk8312_probe(struct i2c_client
*client
,
507 const struct i2c_device_id
*id
)
510 struct iio_dev
*indio_dev
;
511 struct stk8312_data
*data
;
513 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
515 dev_err(&client
->dev
, "iio allocation failed!\n");
519 data
= iio_priv(indio_dev
);
520 data
->client
= client
;
521 i2c_set_clientdata(client
, indio_dev
);
522 mutex_init(&data
->lock
);
524 indio_dev
->dev
.parent
= &client
->dev
;
525 indio_dev
->info
= &stk8312_info
;
526 indio_dev
->name
= STK8312_DRIVER_NAME
;
527 indio_dev
->modes
= INDIO_DIRECT_MODE
;
528 indio_dev
->channels
= stk8312_channels
;
529 indio_dev
->num_channels
= ARRAY_SIZE(stk8312_channels
);
531 /* A software reset is recommended at power-on */
532 ret
= i2c_smbus_write_byte_data(data
->client
, STK8312_REG_RESET
, 0x00);
534 dev_err(&client
->dev
, "failed to reset sensor\n");
537 data
->sample_rate_idx
= STK8312_SR_400HZ_IDX
;
538 ret
= stk8312_set_range(data
, STK8312_RNG_6G
);
542 ret
= stk8312_set_mode(data
,
543 STK8312_MODE_INT_AH_PP
| STK8312_MODE_ACTIVE
);
547 if (client
->irq
> 0) {
548 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
549 stk8312_data_rdy_trig_poll
,
551 IRQF_TRIGGER_RISING
|
556 dev_err(&client
->dev
, "request irq %d failed\n",
561 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
565 if (!data
->dready_trig
) {
570 data
->dready_trig
->dev
.parent
= &client
->dev
;
571 data
->dready_trig
->ops
= &stk8312_trigger_ops
;
572 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
573 ret
= iio_trigger_register(data
->dready_trig
);
575 dev_err(&client
->dev
, "iio trigger register failed\n");
580 ret
= iio_triggered_buffer_setup(indio_dev
,
581 iio_pollfunc_store_time
,
582 stk8312_trigger_handler
,
583 &stk8312_buffer_setup_ops
);
585 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
586 goto err_trigger_unregister
;
589 ret
= iio_device_register(indio_dev
);
591 dev_err(&client
->dev
, "device_register failed\n");
592 goto err_buffer_cleanup
;
598 iio_triggered_buffer_cleanup(indio_dev
);
599 err_trigger_unregister
:
600 if (data
->dready_trig
)
601 iio_trigger_unregister(data
->dready_trig
);
603 stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
607 static int stk8312_remove(struct i2c_client
*client
)
609 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
610 struct stk8312_data
*data
= iio_priv(indio_dev
);
612 iio_device_unregister(indio_dev
);
613 iio_triggered_buffer_cleanup(indio_dev
);
615 if (data
->dready_trig
)
616 iio_trigger_unregister(data
->dready_trig
);
618 return stk8312_set_mode(data
, STK8312_MODE_STANDBY
);
621 #ifdef CONFIG_PM_SLEEP
622 static int stk8312_suspend(struct device
*dev
)
624 struct stk8312_data
*data
;
626 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
628 return stk8312_set_mode(data
, data
->mode
& (~STK8312_MODE_ACTIVE
));
631 static int stk8312_resume(struct device
*dev
)
633 struct stk8312_data
*data
;
635 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
637 return stk8312_set_mode(data
, data
->mode
| STK8312_MODE_ACTIVE
);
640 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops
, stk8312_suspend
, stk8312_resume
);
642 #define STK8312_PM_OPS (&stk8312_pm_ops)
644 #define STK8312_PM_OPS NULL
647 static const struct i2c_device_id stk8312_i2c_id
[] = {
651 MODULE_DEVICE_TABLE(i2c
, stk8312_i2c_id
);
653 static const struct acpi_device_id stk8312_acpi_id
[] = {
658 MODULE_DEVICE_TABLE(acpi
, stk8312_acpi_id
);
660 static struct i2c_driver stk8312_driver
= {
662 .name
= STK8312_DRIVER_NAME
,
663 .pm
= STK8312_PM_OPS
,
664 .acpi_match_table
= ACPI_PTR(stk8312_acpi_id
),
666 .probe
= stk8312_probe
,
667 .remove
= stk8312_remove
,
668 .id_table
= stk8312_i2c_id
,
671 module_i2c_driver(stk8312_driver
);
673 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
674 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
675 MODULE_LICENSE("GPL v2");