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