2 * Sensortek STK8BA50 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 * STK8BA50 7-bit I2C address: 0x18.
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/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 STK8BA50_REG_XOUT 0x02
27 #define STK8BA50_REG_YOUT 0x04
28 #define STK8BA50_REG_ZOUT 0x06
29 #define STK8BA50_REG_RANGE 0x0F
30 #define STK8BA50_REG_BWSEL 0x10
31 #define STK8BA50_REG_POWMODE 0x11
32 #define STK8BA50_REG_SWRST 0x14
33 #define STK8BA50_REG_INTEN2 0x17
34 #define STK8BA50_REG_INTMAP2 0x1A
36 #define STK8BA50_MODE_NORMAL 0
37 #define STK8BA50_MODE_SUSPEND 1
38 #define STK8BA50_MODE_POWERBIT BIT(7)
39 #define STK8BA50_DATA_SHIFT 6
40 #define STK8BA50_RESET_CMD 0xB6
41 #define STK8BA50_SR_1792HZ_IDX 7
42 #define STK8BA50_DREADY_INT_MASK 0x10
43 #define STK8BA50_DREADY_INT_MAP 0x81
44 #define STK8BA50_ALL_CHANNEL_MASK 7
45 #define STK8BA50_ALL_CHANNEL_SIZE 6
47 #define STK8BA50_DRIVER_NAME "stk8ba50"
48 #define STK8BA50_IRQ_NAME "stk8ba50_event"
50 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
53 * The accelerometer has four measurement ranges:
54 * +/-2g; +/-4g; +/-8g; +/-16g
56 * Acceleration values are 10-bit, 2's complement.
57 * Scales are calculated as following:
59 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
60 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
63 * Scales are stored in this format:
64 * { <register value>, <scale value> }
66 * Locally, the range is stored as a table index.
71 } stk8ba50_scale_table
[] = {
72 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
75 /* Sample rates are stored as { <register value>, <Hz value> } */
79 } stk8ba50_samp_freq_table
[] = {
80 {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112},
81 {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
84 /* Used to map scan mask bits to their corresponding channel register. */
85 static const int stk8ba50_channel_table
[] = {
91 struct stk8ba50_data
{
92 struct i2c_client
*client
;
96 struct iio_trigger
*dready_trig
;
97 bool dready_trigger_on
;
99 * 3 x 16-bit channels (10-bit data, 6-bit padding) +
101 * 4 x 16 64-bit timestamp
106 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \
110 .channel2 = IIO_MOD_##axis, \
111 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
112 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
113 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
114 .scan_index = index, \
119 .shift = STK8BA50_DATA_SHIFT, \
120 .endianness = IIO_CPU, \
124 static const struct iio_chan_spec stk8ba50_channels
[] = {
125 STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT
, X
),
126 STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT
, Y
),
127 STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT
, Z
),
128 IIO_CHAN_SOFT_TIMESTAMP(3),
131 static IIO_CONST_ATTR(in_accel_scale_available
, STK8BA50_SCALE_AVAIL
);
133 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
135 static struct attribute
*stk8ba50_attributes
[] = {
136 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
137 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
141 static const struct attribute_group stk8ba50_attribute_group
= {
142 .attrs
= stk8ba50_attributes
145 static int stk8ba50_read_accel(struct stk8ba50_data
*data
, u8 reg
)
148 struct i2c_client
*client
= data
->client
;
150 ret
= i2c_smbus_read_word_data(client
, reg
);
152 dev_err(&client
->dev
, "register read failed\n");
159 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
162 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
163 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
167 ret
= i2c_smbus_write_byte_data(data
->client
,
168 STK8BA50_REG_INTEN2
, STK8BA50_DREADY_INT_MASK
);
170 ret
= i2c_smbus_write_byte_data(data
->client
,
171 STK8BA50_REG_INTEN2
, 0x00);
174 dev_err(&data
->client
->dev
, "failed to set trigger state\n");
176 data
->dready_trigger_on
= state
;
181 static const struct iio_trigger_ops stk8ba50_trigger_ops
= {
182 .set_trigger_state
= stk8ba50_data_rdy_trigger_set_state
,
183 .owner
= THIS_MODULE
,
186 static int stk8ba50_set_power(struct stk8ba50_data
*data
, bool mode
)
190 struct i2c_client
*client
= data
->client
;
192 ret
= i2c_smbus_read_byte_data(client
, STK8BA50_REG_POWMODE
);
197 masked_reg
= ret
| STK8BA50_MODE_POWERBIT
;
199 masked_reg
= ret
& (~STK8BA50_MODE_POWERBIT
);
201 ret
= i2c_smbus_write_byte_data(client
, STK8BA50_REG_POWMODE
,
209 dev_err(&client
->dev
, "failed to change sensor mode\n");
213 static int stk8ba50_read_raw(struct iio_dev
*indio_dev
,
214 struct iio_chan_spec
const *chan
,
215 int *val
, int *val2
, long mask
)
217 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
221 case IIO_CHAN_INFO_RAW
:
222 if (iio_buffer_enabled(indio_dev
))
224 mutex_lock(&data
->lock
);
225 ret
= stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
227 mutex_unlock(&data
->lock
);
230 ret
= stk8ba50_read_accel(data
, chan
->address
);
232 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
233 mutex_unlock(&data
->lock
);
236 *val
= sign_extend32(ret
>> STK8BA50_DATA_SHIFT
, 9);
237 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
238 mutex_unlock(&data
->lock
);
240 case IIO_CHAN_INFO_SCALE
:
242 *val2
= stk8ba50_scale_table
[data
->range
].scale_val
;
243 return IIO_VAL_INT_PLUS_MICRO
;
244 case IIO_CHAN_INFO_SAMP_FREQ
:
245 *val
= stk8ba50_samp_freq_table
246 [data
->sample_rate_idx
].samp_freq
;
254 static int stk8ba50_write_raw(struct iio_dev
*indio_dev
,
255 struct iio_chan_spec
const *chan
,
256 int val
, int val2
, long mask
)
261 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
264 case IIO_CHAN_INFO_SCALE
:
268 for (i
= 0; i
< ARRAY_SIZE(stk8ba50_scale_table
); i
++)
269 if (val2
== stk8ba50_scale_table
[i
].scale_val
) {
276 ret
= i2c_smbus_write_byte_data(data
->client
,
278 stk8ba50_scale_table
[index
].reg_val
);
280 dev_err(&data
->client
->dev
,
281 "failed to set measurement range\n");
286 case IIO_CHAN_INFO_SAMP_FREQ
:
287 for (i
= 0; i
< ARRAY_SIZE(stk8ba50_samp_freq_table
); i
++)
288 if (val
== stk8ba50_samp_freq_table
[i
].samp_freq
) {
295 ret
= i2c_smbus_write_byte_data(data
->client
,
297 stk8ba50_samp_freq_table
[index
].reg_val
);
299 dev_err(&data
->client
->dev
,
300 "failed to set sampling rate\n");
302 data
->sample_rate_idx
= index
;
310 static const struct iio_info stk8ba50_info
= {
311 .driver_module
= THIS_MODULE
,
312 .read_raw
= stk8ba50_read_raw
,
313 .write_raw
= stk8ba50_write_raw
,
314 .attrs
= &stk8ba50_attribute_group
,
317 static irqreturn_t
stk8ba50_trigger_handler(int irq
, void *p
)
319 struct iio_poll_func
*pf
= p
;
320 struct iio_dev
*indio_dev
= pf
->indio_dev
;
321 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
324 mutex_lock(&data
->lock
);
326 * Do a bulk read if all channels are requested,
327 * from 0x02 (XOUT1) to 0x07 (ZOUT2)
329 if (*(indio_dev
->active_scan_mask
) == STK8BA50_ALL_CHANNEL_MASK
) {
330 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
332 STK8BA50_ALL_CHANNEL_SIZE
,
334 if (ret
< STK8BA50_ALL_CHANNEL_SIZE
) {
335 dev_err(&data
->client
->dev
, "register read failed\n");
339 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
340 indio_dev
->masklength
) {
341 ret
= stk8ba50_read_accel(data
,
342 stk8ba50_channel_table
[bit
]);
346 data
->buffer
[i
++] = ret
;
349 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
352 mutex_unlock(&data
->lock
);
353 iio_trigger_notify_done(indio_dev
->trig
);
358 static irqreturn_t
stk8ba50_data_rdy_trig_poll(int irq
, void *private)
360 struct iio_dev
*indio_dev
= private;
361 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
363 if (data
->dready_trigger_on
)
364 iio_trigger_poll(data
->dready_trig
);
369 static int stk8ba50_buffer_preenable(struct iio_dev
*indio_dev
)
371 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
373 return stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
376 static int stk8ba50_buffer_postdisable(struct iio_dev
*indio_dev
)
378 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
380 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
383 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops
= {
384 .preenable
= stk8ba50_buffer_preenable
,
385 .postenable
= iio_triggered_buffer_postenable
,
386 .predisable
= iio_triggered_buffer_predisable
,
387 .postdisable
= stk8ba50_buffer_postdisable
,
390 static int stk8ba50_probe(struct i2c_client
*client
,
391 const struct i2c_device_id
*id
)
394 struct iio_dev
*indio_dev
;
395 struct stk8ba50_data
*data
;
397 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
399 dev_err(&client
->dev
, "iio allocation failed!\n");
403 data
= iio_priv(indio_dev
);
404 data
->client
= client
;
405 i2c_set_clientdata(client
, indio_dev
);
406 mutex_init(&data
->lock
);
408 indio_dev
->dev
.parent
= &client
->dev
;
409 indio_dev
->info
= &stk8ba50_info
;
410 indio_dev
->name
= STK8BA50_DRIVER_NAME
;
411 indio_dev
->modes
= INDIO_DIRECT_MODE
;
412 indio_dev
->channels
= stk8ba50_channels
;
413 indio_dev
->num_channels
= ARRAY_SIZE(stk8ba50_channels
);
415 /* Reset all registers on startup */
416 ret
= i2c_smbus_write_byte_data(client
,
417 STK8BA50_REG_SWRST
, STK8BA50_RESET_CMD
);
419 dev_err(&client
->dev
, "failed to reset sensor\n");
423 /* The default range is +/-2g */
426 /* The default sampling rate is 1792 Hz (maximum) */
427 data
->sample_rate_idx
= STK8BA50_SR_1792HZ_IDX
;
429 /* Set up interrupts */
430 ret
= i2c_smbus_write_byte_data(client
,
431 STK8BA50_REG_INTEN2
, STK8BA50_DREADY_INT_MASK
);
433 dev_err(&client
->dev
, "failed to set up interrupts\n");
436 ret
= i2c_smbus_write_byte_data(client
,
437 STK8BA50_REG_INTMAP2
, STK8BA50_DREADY_INT_MAP
);
439 dev_err(&client
->dev
, "failed to set up interrupts\n");
443 if (client
->irq
> 0) {
444 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
445 stk8ba50_data_rdy_trig_poll
,
447 IRQF_TRIGGER_RISING
|
452 dev_err(&client
->dev
, "request irq %d failed\n",
457 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
461 if (!data
->dready_trig
) {
466 data
->dready_trig
->dev
.parent
= &client
->dev
;
467 data
->dready_trig
->ops
= &stk8ba50_trigger_ops
;
468 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
469 ret
= iio_trigger_register(data
->dready_trig
);
471 dev_err(&client
->dev
, "iio trigger register failed\n");
476 ret
= iio_triggered_buffer_setup(indio_dev
,
477 iio_pollfunc_store_time
,
478 stk8ba50_trigger_handler
,
479 &stk8ba50_buffer_setup_ops
);
481 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
482 goto err_trigger_unregister
;
485 ret
= iio_device_register(indio_dev
);
487 dev_err(&client
->dev
, "device_register failed\n");
488 goto err_buffer_cleanup
;
494 iio_triggered_buffer_cleanup(indio_dev
);
495 err_trigger_unregister
:
496 if (data
->dready_trig
)
497 iio_trigger_unregister(data
->dready_trig
);
499 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
503 static int stk8ba50_remove(struct i2c_client
*client
)
505 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
506 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
508 iio_device_unregister(indio_dev
);
509 iio_triggered_buffer_cleanup(indio_dev
);
511 if (data
->dready_trig
)
512 iio_trigger_unregister(data
->dready_trig
);
514 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
517 #ifdef CONFIG_PM_SLEEP
518 static int stk8ba50_suspend(struct device
*dev
)
520 struct stk8ba50_data
*data
;
522 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
524 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
527 static int stk8ba50_resume(struct device
*dev
)
529 struct stk8ba50_data
*data
;
531 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
533 return stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
536 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops
, stk8ba50_suspend
, stk8ba50_resume
);
538 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops)
540 #define STK8BA50_PM_OPS NULL
543 static const struct i2c_device_id stk8ba50_i2c_id
[] = {
547 MODULE_DEVICE_TABLE(i2c
, stk8ba50_i2c_id
);
549 static const struct acpi_device_id stk8ba50_acpi_id
[] = {
554 MODULE_DEVICE_TABLE(acpi
, stk8ba50_acpi_id
);
556 static struct i2c_driver stk8ba50_driver
= {
559 .pm
= STK8BA50_PM_OPS
,
560 .acpi_match_table
= ACPI_PTR(stk8ba50_acpi_id
),
562 .probe
= stk8ba50_probe
,
563 .remove
= stk8ba50_remove
,
564 .id_table
= stk8ba50_i2c_id
,
567 module_i2c_driver(stk8ba50_driver
);
569 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
570 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
571 MODULE_LICENSE("GPL v2");