1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sensortek STK8BA50 3-Axis Accelerometer
5 * Copyright (c) 2015, Intel Corporation.
7 * STK8BA50 7-bit I2C address: 0x18.
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
22 #define STK8BA50_REG_XOUT 0x02
23 #define STK8BA50_REG_YOUT 0x04
24 #define STK8BA50_REG_ZOUT 0x06
25 #define STK8BA50_REG_RANGE 0x0F
26 #define STK8BA50_REG_BWSEL 0x10
27 #define STK8BA50_REG_POWMODE 0x11
28 #define STK8BA50_REG_SWRST 0x14
29 #define STK8BA50_REG_INTEN2 0x17
30 #define STK8BA50_REG_INTMAP2 0x1A
32 #define STK8BA50_MODE_NORMAL 0
33 #define STK8BA50_MODE_SUSPEND 1
34 #define STK8BA50_MODE_POWERBIT BIT(7)
35 #define STK8BA50_DATA_SHIFT 6
36 #define STK8BA50_RESET_CMD 0xB6
37 #define STK8BA50_SR_1792HZ_IDX 7
38 #define STK8BA50_DREADY_INT_MASK 0x10
39 #define STK8BA50_DREADY_INT_MAP 0x81
40 #define STK8BA50_ALL_CHANNEL_MASK 7
41 #define STK8BA50_ALL_CHANNEL_SIZE 6
43 #define STK8BA50_DRIVER_NAME "stk8ba50"
44 #define STK8BA50_IRQ_NAME "stk8ba50_event"
46 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
49 * The accelerometer has four measurement ranges:
50 * +/-2g; +/-4g; +/-8g; +/-16g
52 * Acceleration values are 10-bit, 2's complement.
53 * Scales are calculated as following:
55 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
56 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
59 * Scales are stored in this format:
60 * { <register value>, <scale value> }
62 * Locally, the range is stored as a table index.
67 } stk8ba50_scale_table
[] = {
68 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
71 /* Sample rates are stored as { <register value>, <Hz value> } */
75 } stk8ba50_samp_freq_table
[] = {
76 {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112},
77 {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
80 /* Used to map scan mask bits to their corresponding channel register. */
81 static const int stk8ba50_channel_table
[] = {
87 struct stk8ba50_data
{
88 struct i2c_client
*client
;
92 struct iio_trigger
*dready_trig
;
93 bool dready_trigger_on
;
94 /* Ensure timestamp is naturally aligned */
97 s64 timetamp
__aligned(8);
101 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \
105 .channel2 = IIO_MOD_##axis, \
106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
107 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
108 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
109 .scan_index = index, \
114 .shift = STK8BA50_DATA_SHIFT, \
115 .endianness = IIO_CPU, \
119 static const struct iio_chan_spec stk8ba50_channels
[] = {
120 STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT
, X
),
121 STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT
, Y
),
122 STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT
, Z
),
123 IIO_CHAN_SOFT_TIMESTAMP(3),
126 static IIO_CONST_ATTR(in_accel_scale_available
, STK8BA50_SCALE_AVAIL
);
128 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
130 static struct attribute
*stk8ba50_attributes
[] = {
131 &iio_const_attr_in_accel_scale_available
.dev_attr
.attr
,
132 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
136 static const struct attribute_group stk8ba50_attribute_group
= {
137 .attrs
= stk8ba50_attributes
140 static int stk8ba50_read_accel(struct stk8ba50_data
*data
, u8 reg
)
143 struct i2c_client
*client
= data
->client
;
145 ret
= i2c_smbus_read_word_data(client
, reg
);
147 dev_err(&client
->dev
, "register read failed\n");
154 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
157 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
158 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
162 ret
= i2c_smbus_write_byte_data(data
->client
,
163 STK8BA50_REG_INTEN2
, STK8BA50_DREADY_INT_MASK
);
165 ret
= i2c_smbus_write_byte_data(data
->client
,
166 STK8BA50_REG_INTEN2
, 0x00);
169 dev_err(&data
->client
->dev
, "failed to set trigger state\n");
171 data
->dready_trigger_on
= state
;
176 static const struct iio_trigger_ops stk8ba50_trigger_ops
= {
177 .set_trigger_state
= stk8ba50_data_rdy_trigger_set_state
,
180 static int stk8ba50_set_power(struct stk8ba50_data
*data
, bool mode
)
184 struct i2c_client
*client
= data
->client
;
186 ret
= i2c_smbus_read_byte_data(client
, STK8BA50_REG_POWMODE
);
191 masked_reg
= ret
| STK8BA50_MODE_POWERBIT
;
193 masked_reg
= ret
& (~STK8BA50_MODE_POWERBIT
);
195 ret
= i2c_smbus_write_byte_data(client
, STK8BA50_REG_POWMODE
,
203 dev_err(&client
->dev
, "failed to change sensor mode\n");
207 static int stk8ba50_read_raw(struct iio_dev
*indio_dev
,
208 struct iio_chan_spec
const *chan
,
209 int *val
, int *val2
, long mask
)
211 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
215 case IIO_CHAN_INFO_RAW
:
216 if (iio_buffer_enabled(indio_dev
))
218 mutex_lock(&data
->lock
);
219 ret
= stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
221 mutex_unlock(&data
->lock
);
224 ret
= stk8ba50_read_accel(data
, chan
->address
);
226 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
227 mutex_unlock(&data
->lock
);
230 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
231 chan
->scan_type
.realbits
- 1);
232 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
233 mutex_unlock(&data
->lock
);
235 case IIO_CHAN_INFO_SCALE
:
237 *val2
= stk8ba50_scale_table
[data
->range
].scale_val
;
238 return IIO_VAL_INT_PLUS_MICRO
;
239 case IIO_CHAN_INFO_SAMP_FREQ
:
240 *val
= stk8ba50_samp_freq_table
241 [data
->sample_rate_idx
].samp_freq
;
249 static int stk8ba50_write_raw(struct iio_dev
*indio_dev
,
250 struct iio_chan_spec
const *chan
,
251 int val
, int val2
, long mask
)
256 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
259 case IIO_CHAN_INFO_SCALE
:
263 for (i
= 0; i
< ARRAY_SIZE(stk8ba50_scale_table
); i
++)
264 if (val2
== stk8ba50_scale_table
[i
].scale_val
) {
271 ret
= i2c_smbus_write_byte_data(data
->client
,
273 stk8ba50_scale_table
[index
].reg_val
);
275 dev_err(&data
->client
->dev
,
276 "failed to set measurement range\n");
281 case IIO_CHAN_INFO_SAMP_FREQ
:
282 for (i
= 0; i
< ARRAY_SIZE(stk8ba50_samp_freq_table
); i
++)
283 if (val
== stk8ba50_samp_freq_table
[i
].samp_freq
) {
290 ret
= i2c_smbus_write_byte_data(data
->client
,
292 stk8ba50_samp_freq_table
[index
].reg_val
);
294 dev_err(&data
->client
->dev
,
295 "failed to set sampling rate\n");
297 data
->sample_rate_idx
= index
;
305 static const struct iio_info stk8ba50_info
= {
306 .read_raw
= stk8ba50_read_raw
,
307 .write_raw
= stk8ba50_write_raw
,
308 .attrs
= &stk8ba50_attribute_group
,
311 static irqreturn_t
stk8ba50_trigger_handler(int irq
, void *p
)
313 struct iio_poll_func
*pf
= p
;
314 struct iio_dev
*indio_dev
= pf
->indio_dev
;
315 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
318 mutex_lock(&data
->lock
);
320 * Do a bulk read if all channels are requested,
321 * from 0x02 (XOUT1) to 0x07 (ZOUT2)
323 if (*(indio_dev
->active_scan_mask
) == STK8BA50_ALL_CHANNEL_MASK
) {
324 ret
= i2c_smbus_read_i2c_block_data(data
->client
,
326 STK8BA50_ALL_CHANNEL_SIZE
,
327 (u8
*)data
->scan
.chans
);
328 if (ret
< STK8BA50_ALL_CHANNEL_SIZE
) {
329 dev_err(&data
->client
->dev
, "register read failed\n");
333 iio_for_each_active_channel(indio_dev
, bit
) {
334 ret
= stk8ba50_read_accel(data
,
335 stk8ba50_channel_table
[bit
]);
339 data
->scan
.chans
[i
++] = ret
;
342 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
345 mutex_unlock(&data
->lock
);
346 iio_trigger_notify_done(indio_dev
->trig
);
351 static irqreturn_t
stk8ba50_data_rdy_trig_poll(int irq
, void *private)
353 struct iio_dev
*indio_dev
= private;
354 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
356 if (data
->dready_trigger_on
)
357 iio_trigger_poll(data
->dready_trig
);
362 static int stk8ba50_buffer_preenable(struct iio_dev
*indio_dev
)
364 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
366 return stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
369 static int stk8ba50_buffer_postdisable(struct iio_dev
*indio_dev
)
371 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
373 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
376 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops
= {
377 .preenable
= stk8ba50_buffer_preenable
,
378 .postdisable
= stk8ba50_buffer_postdisable
,
381 static int stk8ba50_probe(struct i2c_client
*client
)
384 struct iio_dev
*indio_dev
;
385 struct stk8ba50_data
*data
;
387 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
389 dev_err(&client
->dev
, "iio allocation failed!\n");
393 data
= iio_priv(indio_dev
);
394 data
->client
= client
;
395 i2c_set_clientdata(client
, indio_dev
);
396 mutex_init(&data
->lock
);
398 indio_dev
->info
= &stk8ba50_info
;
399 indio_dev
->name
= STK8BA50_DRIVER_NAME
;
400 indio_dev
->modes
= INDIO_DIRECT_MODE
;
401 indio_dev
->channels
= stk8ba50_channels
;
402 indio_dev
->num_channels
= ARRAY_SIZE(stk8ba50_channels
);
404 /* Reset all registers on startup */
405 ret
= i2c_smbus_write_byte_data(client
,
406 STK8BA50_REG_SWRST
, STK8BA50_RESET_CMD
);
408 dev_err(&client
->dev
, "failed to reset sensor\n");
412 /* The default range is +/-2g */
415 /* The default sampling rate is 1792 Hz (maximum) */
416 data
->sample_rate_idx
= STK8BA50_SR_1792HZ_IDX
;
418 /* Set up interrupts */
419 ret
= i2c_smbus_write_byte_data(client
,
420 STK8BA50_REG_INTEN2
, STK8BA50_DREADY_INT_MASK
);
422 dev_err(&client
->dev
, "failed to set up interrupts\n");
425 ret
= i2c_smbus_write_byte_data(client
,
426 STK8BA50_REG_INTMAP2
, STK8BA50_DREADY_INT_MAP
);
428 dev_err(&client
->dev
, "failed to set up interrupts\n");
432 if (client
->irq
> 0) {
433 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
434 stk8ba50_data_rdy_trig_poll
,
436 IRQF_TRIGGER_RISING
|
441 dev_err(&client
->dev
, "request irq %d failed\n",
446 data
->dready_trig
= devm_iio_trigger_alloc(&client
->dev
,
449 iio_device_id(indio_dev
));
450 if (!data
->dready_trig
) {
455 data
->dready_trig
->ops
= &stk8ba50_trigger_ops
;
456 iio_trigger_set_drvdata(data
->dready_trig
, indio_dev
);
457 ret
= iio_trigger_register(data
->dready_trig
);
459 dev_err(&client
->dev
, "iio trigger register failed\n");
464 ret
= iio_triggered_buffer_setup(indio_dev
,
465 iio_pollfunc_store_time
,
466 stk8ba50_trigger_handler
,
467 &stk8ba50_buffer_setup_ops
);
469 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
470 goto err_trigger_unregister
;
473 ret
= iio_device_register(indio_dev
);
475 dev_err(&client
->dev
, "device_register failed\n");
476 goto err_buffer_cleanup
;
482 iio_triggered_buffer_cleanup(indio_dev
);
483 err_trigger_unregister
:
484 if (data
->dready_trig
)
485 iio_trigger_unregister(data
->dready_trig
);
487 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
491 static void stk8ba50_remove(struct i2c_client
*client
)
493 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
494 struct stk8ba50_data
*data
= iio_priv(indio_dev
);
496 iio_device_unregister(indio_dev
);
497 iio_triggered_buffer_cleanup(indio_dev
);
499 if (data
->dready_trig
)
500 iio_trigger_unregister(data
->dready_trig
);
502 stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
505 static int stk8ba50_suspend(struct device
*dev
)
507 struct stk8ba50_data
*data
;
509 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
511 return stk8ba50_set_power(data
, STK8BA50_MODE_SUSPEND
);
514 static int stk8ba50_resume(struct device
*dev
)
516 struct stk8ba50_data
*data
;
518 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
520 return stk8ba50_set_power(data
, STK8BA50_MODE_NORMAL
);
523 static DEFINE_SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops
, stk8ba50_suspend
,
526 static const struct i2c_device_id stk8ba50_i2c_id
[] = {
530 MODULE_DEVICE_TABLE(i2c
, stk8ba50_i2c_id
);
532 static const struct acpi_device_id stk8ba50_acpi_id
[] = {
537 MODULE_DEVICE_TABLE(acpi
, stk8ba50_acpi_id
);
539 static struct i2c_driver stk8ba50_driver
= {
542 .pm
= pm_sleep_ptr(&stk8ba50_pm_ops
),
543 .acpi_match_table
= stk8ba50_acpi_id
,
545 .probe
= stk8ba50_probe
,
546 .remove
= stk8ba50_remove
,
547 .id_table
= stk8ba50_i2c_id
,
550 module_i2c_driver(stk8ba50_driver
);
552 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
553 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
554 MODULE_LICENSE("GPL v2");