GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / iio / imu / bmi160 / bmi160_core.c
1 /*
2  * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
3  *
4  * Copyright (c) 2016, 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 core driver for BMI160, with support for I2C/SPI busses
11  *
12  * TODO: magnetometer, interrupts, hardware FIFO
13  */
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/acpi.h>
17 #include <linux/delay.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/sysfs.h>
24
25 #include "bmi160.h"
26
27 #define BMI160_REG_CHIP_ID      0x00
28 #define BMI160_CHIP_ID_VAL      0xD1
29
30 #define BMI160_REG_PMU_STATUS   0x03
31
32 /* X axis data low byte address, the rest can be obtained using axis offset */
33 #define BMI160_REG_DATA_MAGN_XOUT_L     0x04
34 #define BMI160_REG_DATA_GYRO_XOUT_L     0x0C
35 #define BMI160_REG_DATA_ACCEL_XOUT_L    0x12
36
37 #define BMI160_REG_ACCEL_CONFIG         0x40
38 #define BMI160_ACCEL_CONFIG_ODR_MASK    GENMASK(3, 0)
39 #define BMI160_ACCEL_CONFIG_BWP_MASK    GENMASK(6, 4)
40
41 #define BMI160_REG_ACCEL_RANGE          0x41
42 #define BMI160_ACCEL_RANGE_2G           0x03
43 #define BMI160_ACCEL_RANGE_4G           0x05
44 #define BMI160_ACCEL_RANGE_8G           0x08
45 #define BMI160_ACCEL_RANGE_16G          0x0C
46
47 #define BMI160_REG_GYRO_CONFIG          0x42
48 #define BMI160_GYRO_CONFIG_ODR_MASK     GENMASK(3, 0)
49 #define BMI160_GYRO_CONFIG_BWP_MASK     GENMASK(5, 4)
50
51 #define BMI160_REG_GYRO_RANGE           0x43
52 #define BMI160_GYRO_RANGE_2000DPS       0x00
53 #define BMI160_GYRO_RANGE_1000DPS       0x01
54 #define BMI160_GYRO_RANGE_500DPS        0x02
55 #define BMI160_GYRO_RANGE_250DPS        0x03
56 #define BMI160_GYRO_RANGE_125DPS        0x04
57
58 #define BMI160_REG_CMD                  0x7E
59 #define BMI160_CMD_ACCEL_PM_SUSPEND     0x10
60 #define BMI160_CMD_ACCEL_PM_NORMAL      0x11
61 #define BMI160_CMD_ACCEL_PM_LOW_POWER   0x12
62 #define BMI160_CMD_GYRO_PM_SUSPEND      0x14
63 #define BMI160_CMD_GYRO_PM_NORMAL       0x15
64 #define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17
65 #define BMI160_CMD_SOFTRESET            0xB6
66
67 #define BMI160_REG_DUMMY                0x7F
68
69 #define BMI160_ACCEL_PMU_MIN_USLEEP     3800
70 #define BMI160_GYRO_PMU_MIN_USLEEP      80000
71 #define BMI160_SOFTRESET_USLEEP         1000
72
73 #define BMI160_CHANNEL(_type, _axis, _index) {                  \
74         .type = _type,                                          \
75         .modified = 1,                                          \
76         .channel2 = IIO_MOD_##_axis,                            \
77         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
78         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |  \
79                 BIT(IIO_CHAN_INFO_SAMP_FREQ),                   \
80         .scan_index = _index,                                   \
81         .scan_type = {                                          \
82                 .sign = 's',                                    \
83                 .realbits = 16,                                 \
84                 .storagebits = 16,                              \
85                 .endianness = IIO_LE,                           \
86         },                                                      \
87 }
88
89 /* scan indexes follow DATA register order */
90 enum bmi160_scan_axis {
91         BMI160_SCAN_EXT_MAGN_X = 0,
92         BMI160_SCAN_EXT_MAGN_Y,
93         BMI160_SCAN_EXT_MAGN_Z,
94         BMI160_SCAN_RHALL,
95         BMI160_SCAN_GYRO_X,
96         BMI160_SCAN_GYRO_Y,
97         BMI160_SCAN_GYRO_Z,
98         BMI160_SCAN_ACCEL_X,
99         BMI160_SCAN_ACCEL_Y,
100         BMI160_SCAN_ACCEL_Z,
101         BMI160_SCAN_TIMESTAMP,
102 };
103
104 enum bmi160_sensor_type {
105         BMI160_ACCEL    = 0,
106         BMI160_GYRO,
107         BMI160_EXT_MAGN,
108         BMI160_NUM_SENSORS /* must be last */
109 };
110
111 struct bmi160_data {
112         struct regmap *regmap;
113         /*
114          * Ensure natural alignment for timestamp if present.
115          * Max length needed: 2 * 3 channels + 4 bytes padding + 8 byte ts.
116          * If fewer channels are enabled, less space may be needed, as
117          * long as the timestamp is still aligned to 8 bytes.
118          */
119         __le16 buf[12] __aligned(8);
120 };
121
122 const struct regmap_config bmi160_regmap_config = {
123         .reg_bits = 8,
124         .val_bits = 8,
125 };
126 EXPORT_SYMBOL(bmi160_regmap_config);
127
128 struct bmi160_regs {
129         u8 data; /* LSB byte register for X-axis */
130         u8 config;
131         u8 config_odr_mask;
132         u8 config_bwp_mask;
133         u8 range;
134         u8 pmu_cmd_normal;
135         u8 pmu_cmd_suspend;
136 };
137
138 static struct bmi160_regs bmi160_regs[] = {
139         [BMI160_ACCEL] = {
140                 .data   = BMI160_REG_DATA_ACCEL_XOUT_L,
141                 .config = BMI160_REG_ACCEL_CONFIG,
142                 .config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK,
143                 .config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK,
144                 .range  = BMI160_REG_ACCEL_RANGE,
145                 .pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL,
146                 .pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND,
147         },
148         [BMI160_GYRO] = {
149                 .data   = BMI160_REG_DATA_GYRO_XOUT_L,
150                 .config = BMI160_REG_GYRO_CONFIG,
151                 .config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK,
152                 .config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK,
153                 .range  = BMI160_REG_GYRO_RANGE,
154                 .pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL,
155                 .pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND,
156         },
157 };
158
159 static unsigned long bmi160_pmu_time[] = {
160         [BMI160_ACCEL] = BMI160_ACCEL_PMU_MIN_USLEEP,
161         [BMI160_GYRO] = BMI160_GYRO_PMU_MIN_USLEEP,
162 };
163
164 struct bmi160_scale {
165         u8 bits;
166         int uscale;
167 };
168
169 struct bmi160_odr {
170         u8 bits;
171         int odr;
172         int uodr;
173 };
174
175 static const struct bmi160_scale bmi160_accel_scale[] = {
176         { BMI160_ACCEL_RANGE_2G, 598},
177         { BMI160_ACCEL_RANGE_4G, 1197},
178         { BMI160_ACCEL_RANGE_8G, 2394},
179         { BMI160_ACCEL_RANGE_16G, 4788},
180 };
181
182 static const struct bmi160_scale bmi160_gyro_scale[] = {
183         { BMI160_GYRO_RANGE_2000DPS, 1065},
184         { BMI160_GYRO_RANGE_1000DPS, 532},
185         { BMI160_GYRO_RANGE_500DPS, 266},
186         { BMI160_GYRO_RANGE_250DPS, 133},
187         { BMI160_GYRO_RANGE_125DPS, 66},
188 };
189
190 struct bmi160_scale_item {
191         const struct bmi160_scale *tbl;
192         int num;
193 };
194
195 static const struct  bmi160_scale_item bmi160_scale_table[] = {
196         [BMI160_ACCEL] = {
197                 .tbl    = bmi160_accel_scale,
198                 .num    = ARRAY_SIZE(bmi160_accel_scale),
199         },
200         [BMI160_GYRO] = {
201                 .tbl    = bmi160_gyro_scale,
202                 .num    = ARRAY_SIZE(bmi160_gyro_scale),
203         },
204 };
205
206 static const struct bmi160_odr bmi160_accel_odr[] = {
207         {0x01, 0, 781250},
208         {0x02, 1, 562500},
209         {0x03, 3, 125000},
210         {0x04, 6, 250000},
211         {0x05, 12, 500000},
212         {0x06, 25, 0},
213         {0x07, 50, 0},
214         {0x08, 100, 0},
215         {0x09, 200, 0},
216         {0x0A, 400, 0},
217         {0x0B, 800, 0},
218         {0x0C, 1600, 0},
219 };
220
221 static const struct bmi160_odr bmi160_gyro_odr[] = {
222         {0x06, 25, 0},
223         {0x07, 50, 0},
224         {0x08, 100, 0},
225         {0x09, 200, 0},
226         {0x0A, 400, 0},
227         {0x0B, 800, 0},
228         {0x0C, 1600, 0},
229         {0x0D, 3200, 0},
230 };
231
232 struct bmi160_odr_item {
233         const struct bmi160_odr *tbl;
234         int num;
235 };
236
237 static const struct  bmi160_odr_item bmi160_odr_table[] = {
238         [BMI160_ACCEL] = {
239                 .tbl    = bmi160_accel_odr,
240                 .num    = ARRAY_SIZE(bmi160_accel_odr),
241         },
242         [BMI160_GYRO] = {
243                 .tbl    = bmi160_gyro_odr,
244                 .num    = ARRAY_SIZE(bmi160_gyro_odr),
245         },
246 };
247
248 static const struct iio_chan_spec bmi160_channels[] = {
249         BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X),
250         BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y),
251         BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z),
252         BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X),
253         BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y),
254         BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z),
255         IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP),
256 };
257
258 static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type)
259 {
260         switch (iio_type) {
261         case IIO_ACCEL:
262                 return BMI160_ACCEL;
263         case IIO_ANGL_VEL:
264                 return BMI160_GYRO;
265         default:
266                 return -EINVAL;
267         }
268 }
269
270 static
271 int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
272                     bool mode)
273 {
274         int ret;
275         u8 cmd;
276
277         if (mode)
278                 cmd = bmi160_regs[t].pmu_cmd_normal;
279         else
280                 cmd = bmi160_regs[t].pmu_cmd_suspend;
281
282         ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
283         if (ret < 0)
284                 return ret;
285
286         usleep_range(bmi160_pmu_time[t], bmi160_pmu_time[t] + 1000);
287
288         return 0;
289 }
290
291 static
292 int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
293                      int uscale)
294 {
295         int i;
296
297         for (i = 0; i < bmi160_scale_table[t].num; i++)
298                 if (bmi160_scale_table[t].tbl[i].uscale == uscale)
299                         break;
300
301         if (i == bmi160_scale_table[t].num)
302                 return -EINVAL;
303
304         return regmap_write(data->regmap, bmi160_regs[t].range,
305                             bmi160_scale_table[t].tbl[i].bits);
306 }
307
308 static
309 int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
310                      int *uscale)
311 {
312         int i, ret, val;
313
314         ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
315         if (ret < 0)
316                 return ret;
317
318         for (i = 0; i < bmi160_scale_table[t].num; i++)
319                 if (bmi160_scale_table[t].tbl[i].bits == val) {
320                         *uscale = bmi160_scale_table[t].tbl[i].uscale;
321                         return 0;
322                 }
323
324         return -EINVAL;
325 }
326
327 static int bmi160_get_data(struct bmi160_data *data, int chan_type,
328                            int axis, int *val)
329 {
330         u8 reg;
331         int ret;
332         __le16 sample;
333         enum bmi160_sensor_type t = bmi160_to_sensor(chan_type);
334
335         reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(sample);
336
337         ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(sample));
338         if (ret < 0)
339                 return ret;
340
341         *val = sign_extend32(le16_to_cpu(sample), 15);
342
343         return 0;
344 }
345
346 static
347 int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
348                    int odr, int uodr)
349 {
350         int i;
351
352         for (i = 0; i < bmi160_odr_table[t].num; i++)
353                 if (bmi160_odr_table[t].tbl[i].odr == odr &&
354                     bmi160_odr_table[t].tbl[i].uodr == uodr)
355                         break;
356
357         if (i >= bmi160_odr_table[t].num)
358                 return -EINVAL;
359
360         return regmap_update_bits(data->regmap,
361                                   bmi160_regs[t].config,
362                                   bmi160_regs[t].config_odr_mask,
363                                   bmi160_odr_table[t].tbl[i].bits);
364 }
365
366 static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
367                           int *odr, int *uodr)
368 {
369         int i, val, ret;
370
371         ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
372         if (ret < 0)
373                 return ret;
374
375         val &= bmi160_regs[t].config_odr_mask;
376
377         for (i = 0; i < bmi160_odr_table[t].num; i++)
378                 if (val == bmi160_odr_table[t].tbl[i].bits)
379                         break;
380
381         if (i >= bmi160_odr_table[t].num)
382                 return -EINVAL;
383
384         *odr = bmi160_odr_table[t].tbl[i].odr;
385         *uodr = bmi160_odr_table[t].tbl[i].uodr;
386
387         return 0;
388 }
389
390 static irqreturn_t bmi160_trigger_handler(int irq, void *p)
391 {
392         struct iio_poll_func *pf = p;
393         struct iio_dev *indio_dev = pf->indio_dev;
394         struct bmi160_data *data = iio_priv(indio_dev);
395         int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
396         __le16 sample;
397
398         for_each_set_bit(i, indio_dev->active_scan_mask,
399                          indio_dev->masklength) {
400                 ret = regmap_bulk_read(data->regmap, base + i * sizeof(sample),
401                                        &sample, sizeof(sample));
402                 if (ret < 0)
403                         goto done;
404                 data->buf[j++] = sample;
405         }
406
407         iio_push_to_buffers_with_timestamp(indio_dev, data->buf,
408                                            iio_get_time_ns(indio_dev));
409 done:
410         iio_trigger_notify_done(indio_dev->trig);
411         return IRQ_HANDLED;
412 }
413
414 static int bmi160_read_raw(struct iio_dev *indio_dev,
415                            struct iio_chan_spec const *chan,
416                            int *val, int *val2, long mask)
417 {
418         int ret;
419         struct bmi160_data *data = iio_priv(indio_dev);
420
421         switch (mask) {
422         case IIO_CHAN_INFO_RAW:
423                 ret = bmi160_get_data(data, chan->type, chan->channel2, val);
424                 if (ret < 0)
425                         return ret;
426                 return IIO_VAL_INT;
427         case IIO_CHAN_INFO_SCALE:
428                 *val = 0;
429                 ret = bmi160_get_scale(data,
430                                        bmi160_to_sensor(chan->type), val2);
431                 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
432         case IIO_CHAN_INFO_SAMP_FREQ:
433                 ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
434                                      val, val2);
435                 return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO;
436         default:
437                 return -EINVAL;
438         }
439
440         return 0;
441 }
442
443 static int bmi160_write_raw(struct iio_dev *indio_dev,
444                             struct iio_chan_spec const *chan,
445                             int val, int val2, long mask)
446 {
447         struct bmi160_data *data = iio_priv(indio_dev);
448
449         switch (mask) {
450         case IIO_CHAN_INFO_SCALE:
451                 return bmi160_set_scale(data,
452                                         bmi160_to_sensor(chan->type), val2);
453                 break;
454         case IIO_CHAN_INFO_SAMP_FREQ:
455                 return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
456                                       val, val2);
457         default:
458                 return -EINVAL;
459         }
460
461         return 0;
462 }
463
464 static
465 IIO_CONST_ATTR(in_accel_sampling_frequency_available,
466                "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600");
467 static
468 IIO_CONST_ATTR(in_anglvel_sampling_frequency_available,
469                "25 50 100 200 400 800 1600 3200");
470 static
471 IIO_CONST_ATTR(in_accel_scale_available,
472                "0.000598 0.001197 0.002394 0.004788");
473 static
474 IIO_CONST_ATTR(in_anglvel_scale_available,
475                "0.001065 0.000532 0.000266 0.000133 0.000066");
476
477 static struct attribute *bmi160_attrs[] = {
478         &iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr,
479         &iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr,
480         &iio_const_attr_in_accel_scale_available.dev_attr.attr,
481         &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
482         NULL,
483 };
484
485 static const struct attribute_group bmi160_attrs_group = {
486         .attrs = bmi160_attrs,
487 };
488
489 static const struct iio_info bmi160_info = {
490         .driver_module = THIS_MODULE,
491         .read_raw = bmi160_read_raw,
492         .write_raw = bmi160_write_raw,
493         .attrs = &bmi160_attrs_group,
494 };
495
496 static const char *bmi160_match_acpi_device(struct device *dev)
497 {
498         const struct acpi_device_id *id;
499
500         id = acpi_match_device(dev->driver->acpi_match_table, dev);
501         if (!id)
502                 return NULL;
503
504         return dev_name(dev);
505 }
506
507 static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
508 {
509         int ret;
510         unsigned int val;
511         struct device *dev = regmap_get_device(data->regmap);
512
513         ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
514         if (ret < 0)
515                 return ret;
516
517         usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
518
519         /*
520          * CS rising edge is needed before starting SPI, so do a dummy read
521          * See Section 3.2.1, page 86 of the datasheet
522          */
523         if (use_spi) {
524                 ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
525                 if (ret < 0)
526                         return ret;
527         }
528
529         ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
530         if (ret < 0) {
531                 dev_err(dev, "Error reading chip id\n");
532                 return ret;
533         }
534         if (val != BMI160_CHIP_ID_VAL) {
535                 dev_err(dev, "Wrong chip id, got %x expected %x\n",
536                         val, BMI160_CHIP_ID_VAL);
537                 return -ENODEV;
538         }
539
540         ret = bmi160_set_mode(data, BMI160_ACCEL, true);
541         if (ret < 0)
542                 return ret;
543
544         ret = bmi160_set_mode(data, BMI160_GYRO, true);
545         if (ret < 0)
546                 return ret;
547
548         return 0;
549 }
550
551 static void bmi160_chip_uninit(struct bmi160_data *data)
552 {
553         bmi160_set_mode(data, BMI160_GYRO, false);
554         bmi160_set_mode(data, BMI160_ACCEL, false);
555 }
556
557 int bmi160_core_probe(struct device *dev, struct regmap *regmap,
558                       const char *name, bool use_spi)
559 {
560         struct iio_dev *indio_dev;
561         struct bmi160_data *data;
562         int ret;
563
564         indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
565         if (!indio_dev)
566                 return -ENOMEM;
567
568         data = iio_priv(indio_dev);
569         dev_set_drvdata(dev, indio_dev);
570         data->regmap = regmap;
571
572         ret = bmi160_chip_init(data, use_spi);
573         if (ret < 0)
574                 return ret;
575
576         if (!name && ACPI_HANDLE(dev))
577                 name = bmi160_match_acpi_device(dev);
578
579         indio_dev->dev.parent = dev;
580         indio_dev->channels = bmi160_channels;
581         indio_dev->num_channels = ARRAY_SIZE(bmi160_channels);
582         indio_dev->name = name;
583         indio_dev->modes = INDIO_DIRECT_MODE;
584         indio_dev->info = &bmi160_info;
585
586         ret = iio_triggered_buffer_setup(indio_dev, NULL,
587                                          bmi160_trigger_handler, NULL);
588         if (ret < 0)
589                 goto uninit;
590
591         ret = iio_device_register(indio_dev);
592         if (ret < 0)
593                 goto buffer_cleanup;
594
595         return 0;
596 buffer_cleanup:
597         iio_triggered_buffer_cleanup(indio_dev);
598 uninit:
599         bmi160_chip_uninit(data);
600         return ret;
601 }
602 EXPORT_SYMBOL_GPL(bmi160_core_probe);
603
604 void bmi160_core_remove(struct device *dev)
605 {
606         struct iio_dev *indio_dev = dev_get_drvdata(dev);
607         struct bmi160_data *data = iio_priv(indio_dev);
608
609         iio_device_unregister(indio_dev);
610         iio_triggered_buffer_cleanup(indio_dev);
611         bmi160_chip_uninit(data);
612 }
613 EXPORT_SYMBOL_GPL(bmi160_core_remove);
614
615 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
616 MODULE_DESCRIPTION("Bosch BMI160 driver");
617 MODULE_LICENSE("GPL v2");