This source file includes following definitions.
- stk8312_otp_init
- stk8312_set_mode
- stk8312_set_interrupts
- stk8312_data_rdy_trigger_set_state
- stk8312_set_sample_rate
- stk8312_set_range
- stk8312_read_accel
- stk8312_read_raw
- stk8312_write_raw
- stk8312_trigger_handler
- stk8312_data_rdy_trig_poll
- stk8312_buffer_preenable
- stk8312_buffer_postdisable
- stk8312_probe
- stk8312_remove
- stk8312_suspend
- stk8312_resume
1
2
3
4
5
6
7
8
9
10 #include <linux/acpi.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/delay.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22
23 #define STK8312_REG_XOUT 0x00
24 #define STK8312_REG_YOUT 0x01
25 #define STK8312_REG_ZOUT 0x02
26 #define STK8312_REG_INTSU 0x06
27 #define STK8312_REG_MODE 0x07
28 #define STK8312_REG_SR 0x08
29 #define STK8312_REG_STH 0x13
30 #define STK8312_REG_RESET 0x20
31 #define STK8312_REG_AFECTRL 0x24
32 #define STK8312_REG_OTPADDR 0x3D
33 #define STK8312_REG_OTPDATA 0x3E
34 #define STK8312_REG_OTPCTRL 0x3F
35
36 #define STK8312_MODE_ACTIVE BIT(0)
37 #define STK8312_MODE_STANDBY 0x00
38 #define STK8312_MODE_INT_AH_PP 0xC0
39 #define STK8312_DREADY_BIT BIT(4)
40 #define STK8312_RNG_6G 1
41 #define STK8312_RNG_SHIFT 6
42 #define STK8312_RNG_MASK GENMASK(7, 6)
43 #define STK8312_SR_MASK GENMASK(2, 0)
44 #define STK8312_SR_400HZ_IDX 0
45 #define STK8312_ALL_CHANNEL_MASK GENMASK(2, 0)
46 #define STK8312_ALL_CHANNEL_SIZE 3
47
48 #define STK8312_DRIVER_NAME "stk8312"
49 #define STK8312_IRQ_NAME "stk8312_event"
50
51
52
53
54
55
56
57
58
59
60 #define STK8312_SCALE_AVAIL "0.4616 1.2311"
61
62 static const int stk8312_scale_table[][2] = {
63 {0, 461600}, {1, 231100}
64 };
65
66 static const struct {
67 int val;
68 int val2;
69 } stk8312_samp_freq_table[] = {
70 {400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0},
71 {12, 500000}, {6, 250000}, {3, 125000}
72 };
73
74 #define STK8312_ACCEL_CHANNEL(index, reg, axis) { \
75 .type = IIO_ACCEL, \
76 .address = reg, \
77 .modified = 1, \
78 .channel2 = IIO_MOD_##axis, \
79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
80 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
81 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
82 .scan_index = index, \
83 .scan_type = { \
84 .sign = 's', \
85 .realbits = 8, \
86 .storagebits = 8, \
87 .endianness = IIO_CPU, \
88 }, \
89 }
90
91 static const struct iio_chan_spec stk8312_channels[] = {
92 STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT, X),
93 STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT, Y),
94 STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT, Z),
95 IIO_CHAN_SOFT_TIMESTAMP(3),
96 };
97
98 struct stk8312_data {
99 struct i2c_client *client;
100 struct mutex lock;
101 u8 range;
102 u8 sample_rate_idx;
103 u8 mode;
104 struct iio_trigger *dready_trig;
105 bool dready_trigger_on;
106 s8 buffer[16];
107 };
108
109 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL);
110
111 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400");
112
113 static struct attribute *stk8312_attributes[] = {
114 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
115 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
116 NULL,
117 };
118
119 static const struct attribute_group stk8312_attribute_group = {
120 .attrs = stk8312_attributes
121 };
122
123 static int stk8312_otp_init(struct stk8312_data *data)
124 {
125 int ret;
126 int count = 10;
127 struct i2c_client *client = data->client;
128
129 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70);
130 if (ret < 0)
131 goto exit_err;
132 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02);
133 if (ret < 0)
134 goto exit_err;
135
136 do {
137 usleep_range(1000, 5000);
138 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL);
139 if (ret < 0)
140 goto exit_err;
141 count--;
142 } while (!(ret & BIT(7)) && count > 0);
143
144 if (count == 0) {
145 ret = -ETIMEDOUT;
146 goto exit_err;
147 }
148
149 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA);
150 if (ret == 0)
151 ret = -EINVAL;
152 if (ret < 0)
153 goto exit_err;
154
155 ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_AFECTRL, ret);
156 if (ret < 0)
157 goto exit_err;
158 msleep(150);
159
160 return 0;
161
162 exit_err:
163 dev_err(&client->dev, "failed to initialize sensor\n");
164 return ret;
165 }
166
167 static int stk8312_set_mode(struct stk8312_data *data, u8 mode)
168 {
169 int ret;
170 struct i2c_client *client = data->client;
171
172 if (mode == data->mode)
173 return 0;
174
175 ret = i2c_smbus_write_byte_data(client, STK8312_REG_MODE, mode);
176 if (ret < 0) {
177 dev_err(&client->dev, "failed to change sensor mode\n");
178 return ret;
179 }
180
181 data->mode = mode;
182 if (mode & STK8312_MODE_ACTIVE) {
183
184 usleep_range(1000, 5000);
185 ret = stk8312_otp_init(data);
186 }
187
188 return ret;
189 }
190
191 static int stk8312_set_interrupts(struct stk8312_data *data, u8 int_mask)
192 {
193 int ret;
194 u8 mode;
195 struct i2c_client *client = data->client;
196
197 mode = data->mode;
198
199 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
200 if (ret < 0)
201 return ret;
202
203 ret = i2c_smbus_write_byte_data(client, STK8312_REG_INTSU, int_mask);
204 if (ret < 0) {
205 dev_err(&client->dev, "failed to set interrupts\n");
206 stk8312_set_mode(data, mode);
207 return ret;
208 }
209
210 return stk8312_set_mode(data, mode);
211 }
212
213 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger *trig,
214 bool state)
215 {
216 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
217 struct stk8312_data *data = iio_priv(indio_dev);
218 int ret;
219
220 if (state)
221 ret = stk8312_set_interrupts(data, STK8312_DREADY_BIT);
222 else
223 ret = stk8312_set_interrupts(data, 0x00);
224
225 if (ret < 0) {
226 dev_err(&data->client->dev, "failed to set trigger state\n");
227 return ret;
228 }
229
230 data->dready_trigger_on = state;
231
232 return 0;
233 }
234
235 static const struct iio_trigger_ops stk8312_trigger_ops = {
236 .set_trigger_state = stk8312_data_rdy_trigger_set_state,
237 };
238
239 static int stk8312_set_sample_rate(struct stk8312_data *data, u8 rate)
240 {
241 int ret;
242 u8 masked_reg;
243 u8 mode;
244 struct i2c_client *client = data->client;
245
246 if (rate == data->sample_rate_idx)
247 return 0;
248
249 mode = data->mode;
250
251 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
252 if (ret < 0)
253 return ret;
254
255 ret = i2c_smbus_read_byte_data(client, STK8312_REG_SR);
256 if (ret < 0)
257 goto err_activate;
258
259 masked_reg = (ret & (~STK8312_SR_MASK)) | rate;
260
261 ret = i2c_smbus_write_byte_data(client, STK8312_REG_SR, masked_reg);
262 if (ret < 0)
263 goto err_activate;
264
265 data->sample_rate_idx = rate;
266
267 return stk8312_set_mode(data, mode);
268
269 err_activate:
270 dev_err(&client->dev, "failed to set sampling rate\n");
271 stk8312_set_mode(data, mode);
272
273 return ret;
274 }
275
276 static int stk8312_set_range(struct stk8312_data *data, u8 range)
277 {
278 int ret;
279 u8 masked_reg;
280 u8 mode;
281 struct i2c_client *client = data->client;
282
283 if (range != 1 && range != 2)
284 return -EINVAL;
285 else if (range == data->range)
286 return 0;
287
288 mode = data->mode;
289
290 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY);
291 if (ret < 0)
292 return ret;
293
294 ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH);
295 if (ret < 0)
296 goto err_activate;
297
298 masked_reg = ret & (~STK8312_RNG_MASK);
299 masked_reg |= range << STK8312_RNG_SHIFT;
300
301 ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg);
302 if (ret < 0)
303 goto err_activate;
304
305 data->range = range;
306
307 return stk8312_set_mode(data, mode);
308
309 err_activate:
310 dev_err(&client->dev, "failed to change sensor range\n");
311 stk8312_set_mode(data, mode);
312
313 return ret;
314 }
315
316 static int stk8312_read_accel(struct stk8312_data *data, u8 address)
317 {
318 int ret;
319 struct i2c_client *client = data->client;
320
321 if (address > 2)
322 return -EINVAL;
323
324 ret = i2c_smbus_read_byte_data(client, address);
325 if (ret < 0)
326 dev_err(&client->dev, "register read failed\n");
327
328 return ret;
329 }
330
331 static int stk8312_read_raw(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *chan,
333 int *val, int *val2, long mask)
334 {
335 struct stk8312_data *data = iio_priv(indio_dev);
336 int ret;
337
338 switch (mask) {
339 case IIO_CHAN_INFO_RAW:
340 if (iio_buffer_enabled(indio_dev))
341 return -EBUSY;
342 mutex_lock(&data->lock);
343 ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
344 if (ret < 0) {
345 mutex_unlock(&data->lock);
346 return ret;
347 }
348 ret = stk8312_read_accel(data, chan->address);
349 if (ret < 0) {
350 stk8312_set_mode(data,
351 data->mode & (~STK8312_MODE_ACTIVE));
352 mutex_unlock(&data->lock);
353 return ret;
354 }
355 *val = sign_extend32(ret, 7);
356 ret = stk8312_set_mode(data,
357 data->mode & (~STK8312_MODE_ACTIVE));
358 mutex_unlock(&data->lock);
359 if (ret < 0)
360 return ret;
361 return IIO_VAL_INT;
362 case IIO_CHAN_INFO_SCALE:
363 *val = stk8312_scale_table[data->range - 1][0];
364 *val2 = stk8312_scale_table[data->range - 1][1];
365 return IIO_VAL_INT_PLUS_MICRO;
366 case IIO_CHAN_INFO_SAMP_FREQ:
367 *val = stk8312_samp_freq_table[data->sample_rate_idx].val;
368 *val2 = stk8312_samp_freq_table[data->sample_rate_idx].val2;
369 return IIO_VAL_INT_PLUS_MICRO;
370 }
371
372 return -EINVAL;
373 }
374
375 static int stk8312_write_raw(struct iio_dev *indio_dev,
376 struct iio_chan_spec const *chan,
377 int val, int val2, long mask)
378 {
379 int i;
380 int index = -1;
381 int ret;
382 struct stk8312_data *data = iio_priv(indio_dev);
383
384 switch (mask) {
385 case IIO_CHAN_INFO_SCALE:
386 for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++)
387 if (val == stk8312_scale_table[i][0] &&
388 val2 == stk8312_scale_table[i][1]) {
389 index = i + 1;
390 break;
391 }
392 if (index < 0)
393 return -EINVAL;
394
395 mutex_lock(&data->lock);
396 ret = stk8312_set_range(data, index);
397 mutex_unlock(&data->lock);
398
399 return ret;
400 case IIO_CHAN_INFO_SAMP_FREQ:
401 for (i = 0; i < ARRAY_SIZE(stk8312_samp_freq_table); i++)
402 if (val == stk8312_samp_freq_table[i].val &&
403 val2 == stk8312_samp_freq_table[i].val2) {
404 index = i;
405 break;
406 }
407 if (index < 0)
408 return -EINVAL;
409 mutex_lock(&data->lock);
410 ret = stk8312_set_sample_rate(data, index);
411 mutex_unlock(&data->lock);
412
413 return ret;
414 }
415
416 return -EINVAL;
417 }
418
419 static const struct iio_info stk8312_info = {
420 .read_raw = stk8312_read_raw,
421 .write_raw = stk8312_write_raw,
422 .attrs = &stk8312_attribute_group,
423 };
424
425 static irqreturn_t stk8312_trigger_handler(int irq, void *p)
426 {
427 struct iio_poll_func *pf = p;
428 struct iio_dev *indio_dev = pf->indio_dev;
429 struct stk8312_data *data = iio_priv(indio_dev);
430 int bit, ret, i = 0;
431
432 mutex_lock(&data->lock);
433
434
435
436
437 if (*(indio_dev->active_scan_mask) == STK8312_ALL_CHANNEL_MASK) {
438 ret = i2c_smbus_read_i2c_block_data(data->client,
439 STK8312_REG_XOUT,
440 STK8312_ALL_CHANNEL_SIZE,
441 data->buffer);
442 if (ret < STK8312_ALL_CHANNEL_SIZE) {
443 dev_err(&data->client->dev, "register read failed\n");
444 mutex_unlock(&data->lock);
445 goto err;
446 }
447 } else {
448 for_each_set_bit(bit, indio_dev->active_scan_mask,
449 indio_dev->masklength) {
450 ret = stk8312_read_accel(data, bit);
451 if (ret < 0) {
452 mutex_unlock(&data->lock);
453 goto err;
454 }
455 data->buffer[i++] = ret;
456 }
457 }
458 mutex_unlock(&data->lock);
459
460 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
461 pf->timestamp);
462 err:
463 iio_trigger_notify_done(indio_dev->trig);
464
465 return IRQ_HANDLED;
466 }
467
468 static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private)
469 {
470 struct iio_dev *indio_dev = private;
471 struct stk8312_data *data = iio_priv(indio_dev);
472
473 if (data->dready_trigger_on)
474 iio_trigger_poll(data->dready_trig);
475
476 return IRQ_HANDLED;
477 }
478
479 static int stk8312_buffer_preenable(struct iio_dev *indio_dev)
480 {
481 struct stk8312_data *data = iio_priv(indio_dev);
482
483 return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
484 }
485
486 static int stk8312_buffer_postdisable(struct iio_dev *indio_dev)
487 {
488 struct stk8312_data *data = iio_priv(indio_dev);
489
490 return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE));
491 }
492
493 static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops = {
494 .preenable = stk8312_buffer_preenable,
495 .postenable = iio_triggered_buffer_postenable,
496 .predisable = iio_triggered_buffer_predisable,
497 .postdisable = stk8312_buffer_postdisable,
498 };
499
500 static int stk8312_probe(struct i2c_client *client,
501 const struct i2c_device_id *id)
502 {
503 int ret;
504 struct iio_dev *indio_dev;
505 struct stk8312_data *data;
506
507 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
508 if (!indio_dev) {
509 dev_err(&client->dev, "iio allocation failed!\n");
510 return -ENOMEM;
511 }
512
513 data = iio_priv(indio_dev);
514 data->client = client;
515 i2c_set_clientdata(client, indio_dev);
516 mutex_init(&data->lock);
517
518 indio_dev->dev.parent = &client->dev;
519 indio_dev->info = &stk8312_info;
520 indio_dev->name = STK8312_DRIVER_NAME;
521 indio_dev->modes = INDIO_DIRECT_MODE;
522 indio_dev->channels = stk8312_channels;
523 indio_dev->num_channels = ARRAY_SIZE(stk8312_channels);
524
525
526 ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00);
527 if (ret < 0) {
528 dev_err(&client->dev, "failed to reset sensor\n");
529 return ret;
530 }
531 data->sample_rate_idx = STK8312_SR_400HZ_IDX;
532 ret = stk8312_set_range(data, STK8312_RNG_6G);
533 if (ret < 0)
534 return ret;
535
536 ret = stk8312_set_mode(data,
537 STK8312_MODE_INT_AH_PP | STK8312_MODE_ACTIVE);
538 if (ret < 0)
539 return ret;
540
541 if (client->irq > 0) {
542 ret = devm_request_threaded_irq(&client->dev, client->irq,
543 stk8312_data_rdy_trig_poll,
544 NULL,
545 IRQF_TRIGGER_RISING |
546 IRQF_ONESHOT,
547 STK8312_IRQ_NAME,
548 indio_dev);
549 if (ret < 0) {
550 dev_err(&client->dev, "request irq %d failed\n",
551 client->irq);
552 goto err_power_off;
553 }
554
555 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
556 "%s-dev%d",
557 indio_dev->name,
558 indio_dev->id);
559 if (!data->dready_trig) {
560 ret = -ENOMEM;
561 goto err_power_off;
562 }
563
564 data->dready_trig->dev.parent = &client->dev;
565 data->dready_trig->ops = &stk8312_trigger_ops;
566 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
567 ret = iio_trigger_register(data->dready_trig);
568 if (ret) {
569 dev_err(&client->dev, "iio trigger register failed\n");
570 goto err_power_off;
571 }
572 }
573
574 ret = iio_triggered_buffer_setup(indio_dev,
575 iio_pollfunc_store_time,
576 stk8312_trigger_handler,
577 &stk8312_buffer_setup_ops);
578 if (ret < 0) {
579 dev_err(&client->dev, "iio triggered buffer setup failed\n");
580 goto err_trigger_unregister;
581 }
582
583 ret = iio_device_register(indio_dev);
584 if (ret < 0) {
585 dev_err(&client->dev, "device_register failed\n");
586 goto err_buffer_cleanup;
587 }
588
589 return 0;
590
591 err_buffer_cleanup:
592 iio_triggered_buffer_cleanup(indio_dev);
593 err_trigger_unregister:
594 if (data->dready_trig)
595 iio_trigger_unregister(data->dready_trig);
596 err_power_off:
597 stk8312_set_mode(data, STK8312_MODE_STANDBY);
598 return ret;
599 }
600
601 static int stk8312_remove(struct i2c_client *client)
602 {
603 struct iio_dev *indio_dev = i2c_get_clientdata(client);
604 struct stk8312_data *data = iio_priv(indio_dev);
605
606 iio_device_unregister(indio_dev);
607 iio_triggered_buffer_cleanup(indio_dev);
608
609 if (data->dready_trig)
610 iio_trigger_unregister(data->dready_trig);
611
612 return stk8312_set_mode(data, STK8312_MODE_STANDBY);
613 }
614
615 #ifdef CONFIG_PM_SLEEP
616 static int stk8312_suspend(struct device *dev)
617 {
618 struct stk8312_data *data;
619
620 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
621
622 return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE));
623 }
624
625 static int stk8312_resume(struct device *dev)
626 {
627 struct stk8312_data *data;
628
629 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
630
631 return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE);
632 }
633
634 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume);
635
636 #define STK8312_PM_OPS (&stk8312_pm_ops)
637 #else
638 #define STK8312_PM_OPS NULL
639 #endif
640
641 static const struct i2c_device_id stk8312_i2c_id[] = {
642 {"STK8312", 0},
643 {}
644 };
645 MODULE_DEVICE_TABLE(i2c, stk8312_i2c_id);
646
647 static const struct acpi_device_id stk8312_acpi_id[] = {
648 {"STK8312", 0},
649 {}
650 };
651
652 MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id);
653
654 static struct i2c_driver stk8312_driver = {
655 .driver = {
656 .name = STK8312_DRIVER_NAME,
657 .pm = STK8312_PM_OPS,
658 .acpi_match_table = ACPI_PTR(stk8312_acpi_id),
659 },
660 .probe = stk8312_probe,
661 .remove = stk8312_remove,
662 .id_table = stk8312_i2c_id,
663 };
664
665 module_i2c_driver(stk8312_driver);
666
667 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
668 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver");
669 MODULE_LICENSE("GPL v2");