1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Sensortek STK8BA50 3-Axis Accelerometer
4 *
5 * Copyright (c) 2015, Intel Corporation.
6 *
7 * STK8BA50 7-bit I2C address: 0x18.
8 */
9
10 #include <linux/array_size.h>
11 #include <linux/bitops.h>
12 #include <linux/dev_printk.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/pm.h>
19 #include <linux/sysfs.h>
20 #include <linux/types.h>
21
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/trigger_consumer.h>
28
29 #define STK8BA50_REG_XOUT 0x02
30 #define STK8BA50_REG_YOUT 0x04
31 #define STK8BA50_REG_ZOUT 0x06
32 #define STK8BA50_REG_RANGE 0x0F
33 #define STK8BA50_REG_BWSEL 0x10
34 #define STK8BA50_REG_POWMODE 0x11
35 #define STK8BA50_REG_SWRST 0x14
36 #define STK8BA50_REG_INTEN2 0x17
37 #define STK8BA50_REG_INTMAP2 0x1A
38
39 #define STK8BA50_MODE_NORMAL 0
40 #define STK8BA50_MODE_SUSPEND 1
41 #define STK8BA50_MODE_POWERBIT BIT(7)
42 #define STK8BA50_DATA_SHIFT 6
43 #define STK8BA50_RESET_CMD 0xB6
44 #define STK8BA50_SR_1792HZ_IDX 7
45 #define STK8BA50_DREADY_INT_MASK 0x10
46 #define STK8BA50_DREADY_INT_MAP 0x81
47 #define STK8BA50_ALL_CHANNEL_MASK 7
48 #define STK8BA50_ALL_CHANNEL_SIZE 6
49
50 #define STK8BA50_DRIVER_NAME "stk8ba50"
51
52 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069"
53
54 /*
55 * The accelerometer has four measurement ranges:
56 * +/-2g; +/-4g; +/-8g; +/-16g
57 *
58 * Acceleration values are 10-bit, 2's complement.
59 * Scales are calculated as following:
60 *
61 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384
62 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767
63 * etc.
64 *
65 * Scales are stored in this format:
66 * { <register value>, <scale value> }
67 *
68 * Locally, the range is stored as a table index.
69 */
70 static const struct {
71 u8 reg_val;
72 u32 scale_val;
73 } stk8ba50_scale_table[] = {
74 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900}
75 };
76
77 /* Sample rates are stored as { <register value>, <Hz value> } */
78 static const struct {
79 u8 reg_val;
80 u16 samp_freq;
81 } stk8ba50_samp_freq_table[] = {
82 {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112},
83 {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792}
84 };
85
86 /* Used to map scan mask bits to their corresponding channel register. */
87 static const int stk8ba50_channel_table[] = {
88 STK8BA50_REG_XOUT,
89 STK8BA50_REG_YOUT,
90 STK8BA50_REG_ZOUT
91 };
92
93 struct stk8ba50_data {
94 struct i2c_client *client;
95 struct mutex lock;
96 int range;
97 u8 sample_rate_idx;
98 struct iio_trigger *dready_trig;
99 bool dready_trigger_on;
100 /* Ensure timestamp is naturally aligned */
101 struct {
102 s16 chans[3];
103 aligned_s64 timetamp;
104 } scan;
105 };
106
107 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \
108 .type = IIO_ACCEL, \
109 .address = reg, \
110 .modified = 1, \
111 .channel2 = IIO_MOD_##axis, \
112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
113 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
114 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
115 .scan_index = index, \
116 .scan_type = { \
117 .sign = 's', \
118 .realbits = 10, \
119 .storagebits = 16, \
120 .shift = STK8BA50_DATA_SHIFT, \
121 .endianness = IIO_CPU, \
122 }, \
123 }
124
125 static const struct iio_chan_spec stk8ba50_channels[] = {
126 STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X),
127 STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y),
128 STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z),
129 IIO_CHAN_SOFT_TIMESTAMP(3),
130 };
131
132 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL);
133
134 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792");
135
136 static struct attribute *stk8ba50_attributes[] = {
137 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
138 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
139 NULL,
140 };
141
142 static const struct attribute_group stk8ba50_attribute_group = {
143 .attrs = stk8ba50_attributes
144 };
145
stk8ba50_read_accel(struct stk8ba50_data * data,u8 reg)146 static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg)
147 {
148 int ret;
149 struct i2c_client *client = data->client;
150
151 ret = i2c_smbus_read_word_data(client, reg);
152 if (ret < 0) {
153 dev_err(&client->dev, "register read failed\n");
154 return ret;
155 }
156
157 return ret;
158 }
159
stk8ba50_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)160 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig,
161 bool state)
162 {
163 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
164 struct stk8ba50_data *data = iio_priv(indio_dev);
165 int ret;
166
167 if (state)
168 ret = i2c_smbus_write_byte_data(data->client,
169 STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
170 else
171 ret = i2c_smbus_write_byte_data(data->client,
172 STK8BA50_REG_INTEN2, 0x00);
173
174 if (ret < 0)
175 dev_err(&data->client->dev, "failed to set trigger state\n");
176 else
177 data->dready_trigger_on = state;
178
179 return ret;
180 }
181
182 static const struct iio_trigger_ops stk8ba50_trigger_ops = {
183 .set_trigger_state = stk8ba50_data_rdy_trigger_set_state,
184 };
185
stk8ba50_set_power(struct stk8ba50_data * data,bool mode)186 static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode)
187 {
188 int ret;
189 u8 masked_reg;
190 struct i2c_client *client = data->client;
191
192 ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE);
193 if (ret < 0)
194 goto exit_err;
195
196 if (mode)
197 masked_reg = ret | STK8BA50_MODE_POWERBIT;
198 else
199 masked_reg = ret & (~STK8BA50_MODE_POWERBIT);
200
201 ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE,
202 masked_reg);
203 if (ret < 0)
204 goto exit_err;
205
206 return ret;
207
208 exit_err:
209 dev_err(&client->dev, "failed to change sensor mode\n");
210 return ret;
211 }
212
stk8ba50_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
216 {
217 struct stk8ba50_data *data = iio_priv(indio_dev);
218 int ret;
219
220 switch (mask) {
221 case IIO_CHAN_INFO_RAW:
222 if (iio_buffer_enabled(indio_dev))
223 return -EBUSY;
224 mutex_lock(&data->lock);
225 ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
226 if (ret < 0) {
227 mutex_unlock(&data->lock);
228 return -EINVAL;
229 }
230 ret = stk8ba50_read_accel(data, chan->address);
231 if (ret < 0) {
232 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
233 mutex_unlock(&data->lock);
234 return -EINVAL;
235 }
236 *val = sign_extend32(ret >> chan->scan_type.shift,
237 chan->scan_type.realbits - 1);
238 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
239 mutex_unlock(&data->lock);
240 return IIO_VAL_INT;
241 case IIO_CHAN_INFO_SCALE:
242 *val = 0;
243 *val2 = stk8ba50_scale_table[data->range].scale_val;
244 return IIO_VAL_INT_PLUS_MICRO;
245 case IIO_CHAN_INFO_SAMP_FREQ:
246 *val = stk8ba50_samp_freq_table
247 [data->sample_rate_idx].samp_freq;
248 *val2 = 0;
249 return IIO_VAL_INT;
250 }
251
252 return -EINVAL;
253 }
254
stk8ba50_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)255 static int stk8ba50_write_raw(struct iio_dev *indio_dev,
256 struct iio_chan_spec const *chan,
257 int val, int val2, long mask)
258 {
259 int ret;
260 int i;
261 int index = -1;
262 struct stk8ba50_data *data = iio_priv(indio_dev);
263
264 switch (mask) {
265 case IIO_CHAN_INFO_SCALE:
266 if (val != 0)
267 return -EINVAL;
268
269 for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++)
270 if (val2 == stk8ba50_scale_table[i].scale_val) {
271 index = i;
272 break;
273 }
274 if (index < 0)
275 return -EINVAL;
276
277 ret = i2c_smbus_write_byte_data(data->client,
278 STK8BA50_REG_RANGE,
279 stk8ba50_scale_table[index].reg_val);
280 if (ret < 0)
281 dev_err(&data->client->dev,
282 "failed to set measurement range\n");
283 else
284 data->range = index;
285
286 return ret;
287 case IIO_CHAN_INFO_SAMP_FREQ:
288 for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++)
289 if (val == stk8ba50_samp_freq_table[i].samp_freq) {
290 index = i;
291 break;
292 }
293 if (index < 0)
294 return -EINVAL;
295
296 ret = i2c_smbus_write_byte_data(data->client,
297 STK8BA50_REG_BWSEL,
298 stk8ba50_samp_freq_table[index].reg_val);
299 if (ret < 0)
300 dev_err(&data->client->dev,
301 "failed to set sampling rate\n");
302 else
303 data->sample_rate_idx = index;
304
305 return ret;
306 }
307
308 return -EINVAL;
309 }
310
311 static const struct iio_info stk8ba50_info = {
312 .read_raw = stk8ba50_read_raw,
313 .write_raw = stk8ba50_write_raw,
314 .attrs = &stk8ba50_attribute_group,
315 };
316
stk8ba50_trigger_handler(int irq,void * p)317 static irqreturn_t stk8ba50_trigger_handler(int irq, void *p)
318 {
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);
322 int bit, ret, i = 0;
323
324 mutex_lock(&data->lock);
325 /*
326 * Do a bulk read if all channels are requested,
327 * from 0x02 (XOUT1) to 0x07 (ZOUT2)
328 */
329 if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) {
330 ret = i2c_smbus_read_i2c_block_data(data->client,
331 STK8BA50_REG_XOUT,
332 STK8BA50_ALL_CHANNEL_SIZE,
333 (u8 *)data->scan.chans);
334 if (ret < STK8BA50_ALL_CHANNEL_SIZE) {
335 dev_err(&data->client->dev, "register read failed\n");
336 goto err;
337 }
338 } else {
339 iio_for_each_active_channel(indio_dev, bit) {
340 ret = stk8ba50_read_accel(data,
341 stk8ba50_channel_table[bit]);
342 if (ret < 0)
343 goto err;
344
345 data->scan.chans[i++] = ret;
346 }
347 }
348 iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
349 pf->timestamp);
350 err:
351 mutex_unlock(&data->lock);
352 iio_trigger_notify_done(indio_dev->trig);
353
354 return IRQ_HANDLED;
355 }
356
stk8ba50_data_rdy_trig_poll(int irq,void * private)357 static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private)
358 {
359 struct iio_dev *indio_dev = private;
360 struct stk8ba50_data *data = iio_priv(indio_dev);
361
362 if (data->dready_trigger_on)
363 iio_trigger_poll(data->dready_trig);
364
365 return IRQ_HANDLED;
366 }
367
stk8ba50_buffer_preenable(struct iio_dev * indio_dev)368 static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev)
369 {
370 struct stk8ba50_data *data = iio_priv(indio_dev);
371
372 return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
373 }
374
stk8ba50_buffer_postdisable(struct iio_dev * indio_dev)375 static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev)
376 {
377 struct stk8ba50_data *data = iio_priv(indio_dev);
378
379 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
380 }
381
382 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = {
383 .preenable = stk8ba50_buffer_preenable,
384 .postdisable = stk8ba50_buffer_postdisable,
385 };
386
stk8ba50_probe(struct i2c_client * client)387 static int stk8ba50_probe(struct i2c_client *client)
388 {
389 int ret;
390 struct iio_dev *indio_dev;
391 struct stk8ba50_data *data;
392
393 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
394 if (!indio_dev)
395 return -ENOMEM;
396
397 data = iio_priv(indio_dev);
398 data->client = client;
399 i2c_set_clientdata(client, indio_dev);
400 mutex_init(&data->lock);
401
402 indio_dev->info = &stk8ba50_info;
403 indio_dev->name = STK8BA50_DRIVER_NAME;
404 indio_dev->modes = INDIO_DIRECT_MODE;
405 indio_dev->channels = stk8ba50_channels;
406 indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels);
407
408 /* Reset all registers on startup */
409 ret = i2c_smbus_write_byte_data(client,
410 STK8BA50_REG_SWRST, STK8BA50_RESET_CMD);
411 if (ret < 0) {
412 dev_err(&client->dev, "failed to reset sensor\n");
413 goto err_power_off;
414 }
415
416 /* The default range is +/-2g */
417 data->range = 0;
418
419 /* The default sampling rate is 1792 Hz (maximum) */
420 data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX;
421
422 /* Set up interrupts */
423 ret = i2c_smbus_write_byte_data(client,
424 STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK);
425 if (ret < 0) {
426 dev_err(&client->dev, "failed to set up interrupts\n");
427 goto err_power_off;
428 }
429 ret = i2c_smbus_write_byte_data(client,
430 STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP);
431 if (ret < 0) {
432 dev_err(&client->dev, "failed to set up interrupts\n");
433 goto err_power_off;
434 }
435
436 if (client->irq > 0) {
437 ret = devm_request_irq(&client->dev, client->irq,
438 stk8ba50_data_rdy_trig_poll,
439 IRQF_TRIGGER_RISING | IRQF_NO_THREAD,
440 "stk8ba50_event", indio_dev);
441 if (ret)
442 goto err_power_off;
443
444 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
445 "%s-dev%d",
446 indio_dev->name,
447 iio_device_id(indio_dev));
448 if (!data->dready_trig) {
449 ret = -ENOMEM;
450 goto err_power_off;
451 }
452
453 data->dready_trig->ops = &stk8ba50_trigger_ops;
454 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
455 ret = iio_trigger_register(data->dready_trig);
456 if (ret) {
457 dev_err(&client->dev, "iio trigger register failed\n");
458 goto err_power_off;
459 }
460 }
461
462 ret = iio_triggered_buffer_setup(indio_dev,
463 iio_pollfunc_store_time,
464 stk8ba50_trigger_handler,
465 &stk8ba50_buffer_setup_ops);
466 if (ret < 0) {
467 dev_err(&client->dev, "iio triggered buffer setup failed\n");
468 goto err_trigger_unregister;
469 }
470
471 ret = iio_device_register(indio_dev);
472 if (ret < 0) {
473 dev_err(&client->dev, "device_register failed\n");
474 goto err_buffer_cleanup;
475 }
476
477 return ret;
478
479 err_buffer_cleanup:
480 iio_triggered_buffer_cleanup(indio_dev);
481 err_trigger_unregister:
482 if (data->dready_trig)
483 iio_trigger_unregister(data->dready_trig);
484 err_power_off:
485 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
486 return ret;
487 }
488
stk8ba50_remove(struct i2c_client * client)489 static void stk8ba50_remove(struct i2c_client *client)
490 {
491 struct iio_dev *indio_dev = i2c_get_clientdata(client);
492 struct stk8ba50_data *data = iio_priv(indio_dev);
493
494 iio_device_unregister(indio_dev);
495 iio_triggered_buffer_cleanup(indio_dev);
496
497 if (data->dready_trig)
498 iio_trigger_unregister(data->dready_trig);
499
500 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
501 }
502
stk8ba50_suspend(struct device * dev)503 static int stk8ba50_suspend(struct device *dev)
504 {
505 struct stk8ba50_data *data;
506
507 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
508
509 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND);
510 }
511
stk8ba50_resume(struct device * dev)512 static int stk8ba50_resume(struct device *dev)
513 {
514 struct stk8ba50_data *data;
515
516 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
517
518 return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL);
519 }
520
521 static DEFINE_SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend,
522 stk8ba50_resume);
523
524 static const struct i2c_device_id stk8ba50_i2c_id[] = {
525 { .name = "stk8ba50" },
526 { }
527 };
528 MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id);
529
530 static const struct acpi_device_id stk8ba50_acpi_id[] = {
531 {"STK8BA50", 0},
532 { }
533 };
534
535 MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id);
536
537 static struct i2c_driver stk8ba50_driver = {
538 .driver = {
539 .name = "stk8ba50",
540 .pm = pm_sleep_ptr(&stk8ba50_pm_ops),
541 .acpi_match_table = stk8ba50_acpi_id,
542 },
543 .probe = stk8ba50_probe,
544 .remove = stk8ba50_remove,
545 .id_table = stk8ba50_i2c_id,
546 };
547
548 module_i2c_driver(stk8ba50_driver);
549
550 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
551 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver");
552 MODULE_LICENSE("GPL v2");
553