Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / gyro / st_gyro_core.c
blob02f6f263e3384cdc6267247262906abebfeb1c7e
1 /*
2 * STMicroelectronics gyroscopes driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/irq.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/buffer.h>
27 #include <linux/iio/common/st_sensors.h>
28 #include "st_gyro.h"
30 #define ST_GYRO_NUMBER_DATA_CHANNELS 3
32 /* DEFAULT VALUE FOR SENSORS */
33 #define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28
34 #define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a
35 #define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c
37 /* FULLSCALE */
38 #define ST_GYRO_FS_AVL_245DPS 245
39 #define ST_GYRO_FS_AVL_250DPS 250
40 #define ST_GYRO_FS_AVL_500DPS 500
41 #define ST_GYRO_FS_AVL_2000DPS 2000
43 static const struct iio_chan_spec st_gyro_16bit_channels[] = {
44 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
45 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
46 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
47 ST_GYRO_DEFAULT_OUT_X_L_ADDR),
48 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
49 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
50 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
51 ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
52 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
53 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
54 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
55 ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
56 IIO_CHAN_SOFT_TIMESTAMP(3)
59 static const struct st_sensor_settings st_gyro_sensors_settings[] = {
61 .wai = 0xd3,
62 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
63 .sensors_supported = {
64 [0] = L3G4200D_GYRO_DEV_NAME,
65 [1] = LSM330DL_GYRO_DEV_NAME,
67 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
68 .odr = {
69 .addr = 0x20,
70 .mask = 0xc0,
71 .odr_avl = {
72 { .hz = 100, .value = 0x00, },
73 { .hz = 200, .value = 0x01, },
74 { .hz = 400, .value = 0x02, },
75 { .hz = 800, .value = 0x03, },
78 .pw = {
79 .addr = 0x20,
80 .mask = 0x08,
81 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
82 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
84 .enable_axis = {
85 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
86 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
88 .fs = {
89 .addr = 0x23,
90 .mask = 0x30,
91 .fs_avl = {
92 [0] = {
93 .num = ST_GYRO_FS_AVL_250DPS,
94 .value = 0x00,
95 .gain = IIO_DEGREE_TO_RAD(8750),
97 [1] = {
98 .num = ST_GYRO_FS_AVL_500DPS,
99 .value = 0x01,
100 .gain = IIO_DEGREE_TO_RAD(17500),
102 [2] = {
103 .num = ST_GYRO_FS_AVL_2000DPS,
104 .value = 0x02,
105 .gain = IIO_DEGREE_TO_RAD(70000),
109 .bdu = {
110 .addr = 0x23,
111 .mask = 0x80,
113 .drdy_irq = {
114 .int2 = {
115 .addr = 0x22,
116 .mask = 0x08,
119 * The sensor has IHL (active low) and open
120 * drain settings, but only for INT1 and not
121 * for the DRDY line on INT2.
123 .stat_drdy = {
124 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
125 .mask = 0x07,
128 .sim = {
129 .addr = 0x23,
130 .value = BIT(0),
132 .multi_read_bit = true,
133 .bootime = 2,
136 .wai = 0xd4,
137 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
138 .sensors_supported = {
139 [0] = L3GD20_GYRO_DEV_NAME,
140 [1] = LSM330D_GYRO_DEV_NAME,
141 [2] = LSM330DLC_GYRO_DEV_NAME,
142 [3] = L3G4IS_GYRO_DEV_NAME,
143 [4] = LSM330_GYRO_DEV_NAME,
145 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
146 .odr = {
147 .addr = 0x20,
148 .mask = 0xc0,
149 .odr_avl = {
150 { .hz = 95, .value = 0x00, },
151 { .hz = 190, .value = 0x01, },
152 { .hz = 380, .value = 0x02, },
153 { .hz = 760, .value = 0x03, },
156 .pw = {
157 .addr = 0x20,
158 .mask = 0x08,
159 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
160 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
162 .enable_axis = {
163 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
164 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
166 .fs = {
167 .addr = 0x23,
168 .mask = 0x30,
169 .fs_avl = {
170 [0] = {
171 .num = ST_GYRO_FS_AVL_250DPS,
172 .value = 0x00,
173 .gain = IIO_DEGREE_TO_RAD(8750),
175 [1] = {
176 .num = ST_GYRO_FS_AVL_500DPS,
177 .value = 0x01,
178 .gain = IIO_DEGREE_TO_RAD(17500),
180 [2] = {
181 .num = ST_GYRO_FS_AVL_2000DPS,
182 .value = 0x02,
183 .gain = IIO_DEGREE_TO_RAD(70000),
187 .bdu = {
188 .addr = 0x23,
189 .mask = 0x80,
191 .drdy_irq = {
192 .int2 = {
193 .addr = 0x22,
194 .mask = 0x08,
197 * The sensor has IHL (active low) and open
198 * drain settings, but only for INT1 and not
199 * for the DRDY line on INT2.
201 .stat_drdy = {
202 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
203 .mask = 0x07,
206 .sim = {
207 .addr = 0x23,
208 .value = BIT(0),
210 .multi_read_bit = true,
211 .bootime = 2,
214 .wai = 0xd4,
215 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
216 .sensors_supported = {
217 [0] = LSM9DS0_GYRO_DEV_NAME,
219 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
220 .odr = {
221 .addr = 0x20,
222 .mask = GENMASK(7, 6),
223 .odr_avl = {
224 { .hz = 95, .value = 0x00, },
225 { .hz = 190, .value = 0x01, },
226 { .hz = 380, .value = 0x02, },
227 { .hz = 760, .value = 0x03, },
230 .pw = {
231 .addr = 0x20,
232 .mask = BIT(3),
233 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
234 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
236 .enable_axis = {
237 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
238 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
240 .fs = {
241 .addr = 0x23,
242 .mask = GENMASK(5, 4),
243 .fs_avl = {
244 [0] = {
245 .num = ST_GYRO_FS_AVL_245DPS,
246 .value = 0x00,
247 .gain = IIO_DEGREE_TO_RAD(8750),
249 [1] = {
250 .num = ST_GYRO_FS_AVL_500DPS,
251 .value = 0x01,
252 .gain = IIO_DEGREE_TO_RAD(17500),
254 [2] = {
255 .num = ST_GYRO_FS_AVL_2000DPS,
256 .value = 0x02,
257 .gain = IIO_DEGREE_TO_RAD(70000),
261 .bdu = {
262 .addr = 0x23,
263 .mask = BIT(7),
265 .drdy_irq = {
266 .int2 = {
267 .addr = 0x22,
268 .mask = BIT(3),
271 * The sensor has IHL (active low) and open
272 * drain settings, but only for INT1 and not
273 * for the DRDY line on INT2.
275 .stat_drdy = {
276 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
277 .mask = GENMASK(2, 0),
280 .sim = {
281 .addr = 0x23,
282 .value = BIT(0),
284 .multi_read_bit = true,
285 .bootime = 2,
288 .wai = 0xd7,
289 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
290 .sensors_supported = {
291 [0] = L3GD20H_GYRO_DEV_NAME,
293 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
294 .odr = {
295 .addr = 0x20,
296 .mask = 0xc0,
297 .odr_avl = {
298 { .hz = 100, .value = 0x00, },
299 { .hz = 200, .value = 0x01, },
300 { .hz = 400, .value = 0x02, },
301 { .hz = 800, .value = 0x03, },
304 .pw = {
305 .addr = 0x20,
306 .mask = 0x08,
307 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
308 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
310 .enable_axis = {
311 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
312 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
314 .fs = {
315 .addr = 0x23,
316 .mask = 0x30,
317 .fs_avl = {
318 [0] = {
319 .num = ST_GYRO_FS_AVL_245DPS,
320 .value = 0x00,
321 .gain = IIO_DEGREE_TO_RAD(8750),
323 [1] = {
324 .num = ST_GYRO_FS_AVL_500DPS,
325 .value = 0x01,
326 .gain = IIO_DEGREE_TO_RAD(17500),
328 [2] = {
329 .num = ST_GYRO_FS_AVL_2000DPS,
330 .value = 0x02,
331 .gain = IIO_DEGREE_TO_RAD(70000),
335 .bdu = {
336 .addr = 0x23,
337 .mask = 0x80,
339 .drdy_irq = {
340 .int2 = {
341 .addr = 0x22,
342 .mask = 0x08,
345 * The sensor has IHL (active low) and open
346 * drain settings, but only for INT1 and not
347 * for the DRDY line on INT2.
349 .stat_drdy = {
350 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
351 .mask = 0x07,
354 .sim = {
355 .addr = 0x23,
356 .value = BIT(0),
358 .multi_read_bit = true,
359 .bootime = 2,
363 static int st_gyro_read_raw(struct iio_dev *indio_dev,
364 struct iio_chan_spec const *ch, int *val,
365 int *val2, long mask)
367 int err;
368 struct st_sensor_data *gdata = iio_priv(indio_dev);
370 switch (mask) {
371 case IIO_CHAN_INFO_RAW:
372 err = st_sensors_read_info_raw(indio_dev, ch, val);
373 if (err < 0)
374 goto read_error;
376 return IIO_VAL_INT;
377 case IIO_CHAN_INFO_SCALE:
378 *val = 0;
379 *val2 = gdata->current_fullscale->gain;
380 return IIO_VAL_INT_PLUS_MICRO;
381 case IIO_CHAN_INFO_SAMP_FREQ:
382 *val = gdata->odr;
383 return IIO_VAL_INT;
384 default:
385 return -EINVAL;
388 read_error:
389 return err;
392 static int st_gyro_write_raw(struct iio_dev *indio_dev,
393 struct iio_chan_spec const *chan, int val, int val2, long mask)
395 int err;
397 switch (mask) {
398 case IIO_CHAN_INFO_SCALE:
399 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
400 break;
401 case IIO_CHAN_INFO_SAMP_FREQ:
402 if (val2)
403 return -EINVAL;
404 mutex_lock(&indio_dev->mlock);
405 err = st_sensors_set_odr(indio_dev, val);
406 mutex_unlock(&indio_dev->mlock);
407 return err;
408 default:
409 err = -EINVAL;
412 return err;
415 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
416 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
418 static struct attribute *st_gyro_attributes[] = {
419 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
420 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
421 NULL,
424 static const struct attribute_group st_gyro_attribute_group = {
425 .attrs = st_gyro_attributes,
428 static const struct iio_info gyro_info = {
429 .attrs = &st_gyro_attribute_group,
430 .read_raw = &st_gyro_read_raw,
431 .write_raw = &st_gyro_write_raw,
432 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
435 #ifdef CONFIG_IIO_TRIGGER
436 static const struct iio_trigger_ops st_gyro_trigger_ops = {
437 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
438 .validate_device = st_sensors_validate_device,
440 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
441 #else
442 #define ST_GYRO_TRIGGER_OPS NULL
443 #endif
445 int st_gyro_common_probe(struct iio_dev *indio_dev)
447 struct st_sensor_data *gdata = iio_priv(indio_dev);
448 int irq = gdata->get_irq_data_ready(indio_dev);
449 int err;
451 indio_dev->modes = INDIO_DIRECT_MODE;
452 indio_dev->info = &gyro_info;
453 mutex_init(&gdata->tb.buf_lock);
455 err = st_sensors_power_enable(indio_dev);
456 if (err)
457 return err;
459 err = st_sensors_check_device_support(indio_dev,
460 ARRAY_SIZE(st_gyro_sensors_settings),
461 st_gyro_sensors_settings);
462 if (err < 0)
463 goto st_gyro_power_off;
465 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
466 gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
467 indio_dev->channels = gdata->sensor_settings->ch;
468 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
470 gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
471 &gdata->sensor_settings->fs.fs_avl[0];
472 gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
474 err = st_sensors_init_sensor(indio_dev,
475 (struct st_sensors_platform_data *)&gyro_pdata);
476 if (err < 0)
477 goto st_gyro_power_off;
479 err = st_gyro_allocate_ring(indio_dev);
480 if (err < 0)
481 goto st_gyro_power_off;
483 if (irq > 0) {
484 err = st_sensors_allocate_trigger(indio_dev,
485 ST_GYRO_TRIGGER_OPS);
486 if (err < 0)
487 goto st_gyro_probe_trigger_error;
490 err = iio_device_register(indio_dev);
491 if (err)
492 goto st_gyro_device_register_error;
494 dev_info(&indio_dev->dev, "registered gyroscope %s\n",
495 indio_dev->name);
497 return 0;
499 st_gyro_device_register_error:
500 if (irq > 0)
501 st_sensors_deallocate_trigger(indio_dev);
502 st_gyro_probe_trigger_error:
503 st_gyro_deallocate_ring(indio_dev);
504 st_gyro_power_off:
505 st_sensors_power_disable(indio_dev);
507 return err;
509 EXPORT_SYMBOL(st_gyro_common_probe);
511 void st_gyro_common_remove(struct iio_dev *indio_dev)
513 struct st_sensor_data *gdata = iio_priv(indio_dev);
515 st_sensors_power_disable(indio_dev);
517 iio_device_unregister(indio_dev);
518 if (gdata->get_irq_data_ready(indio_dev) > 0)
519 st_sensors_deallocate_trigger(indio_dev);
521 st_gyro_deallocate_ring(indio_dev);
523 EXPORT_SYMBOL(st_gyro_common_remove);
525 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
526 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
527 MODULE_LICENSE("GPL v2");