Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / iio / gyro / st_gyro_core.c
blob110f95b6e52fa1a085523ba438b470be861edb35
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_250DPS 250
39 #define ST_GYRO_FS_AVL_500DPS 500
40 #define ST_GYRO_FS_AVL_2000DPS 2000
42 /* CUSTOM VALUES FOR SENSOR 1 */
43 #define ST_GYRO_1_WAI_EXP 0xd3
44 #define ST_GYRO_1_ODR_ADDR 0x20
45 #define ST_GYRO_1_ODR_MASK 0xc0
46 #define ST_GYRO_1_ODR_AVL_100HZ_VAL 0x00
47 #define ST_GYRO_1_ODR_AVL_200HZ_VAL 0x01
48 #define ST_GYRO_1_ODR_AVL_400HZ_VAL 0x02
49 #define ST_GYRO_1_ODR_AVL_800HZ_VAL 0x03
50 #define ST_GYRO_1_PW_ADDR 0x20
51 #define ST_GYRO_1_PW_MASK 0x08
52 #define ST_GYRO_1_FS_ADDR 0x23
53 #define ST_GYRO_1_FS_MASK 0x30
54 #define ST_GYRO_1_FS_AVL_250_VAL 0x00
55 #define ST_GYRO_1_FS_AVL_500_VAL 0x01
56 #define ST_GYRO_1_FS_AVL_2000_VAL 0x02
57 #define ST_GYRO_1_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
58 #define ST_GYRO_1_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
59 #define ST_GYRO_1_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
60 #define ST_GYRO_1_BDU_ADDR 0x23
61 #define ST_GYRO_1_BDU_MASK 0x80
62 #define ST_GYRO_1_DRDY_IRQ_ADDR 0x22
63 #define ST_GYRO_1_DRDY_IRQ_INT2_MASK 0x08
64 #define ST_GYRO_1_MULTIREAD_BIT true
66 /* CUSTOM VALUES FOR SENSOR 2 */
67 #define ST_GYRO_2_WAI_EXP 0xd4
68 #define ST_GYRO_2_ODR_ADDR 0x20
69 #define ST_GYRO_2_ODR_MASK 0xc0
70 #define ST_GYRO_2_ODR_AVL_95HZ_VAL 0x00
71 #define ST_GYRO_2_ODR_AVL_190HZ_VAL 0x01
72 #define ST_GYRO_2_ODR_AVL_380HZ_VAL 0x02
73 #define ST_GYRO_2_ODR_AVL_760HZ_VAL 0x03
74 #define ST_GYRO_2_PW_ADDR 0x20
75 #define ST_GYRO_2_PW_MASK 0x08
76 #define ST_GYRO_2_FS_ADDR 0x23
77 #define ST_GYRO_2_FS_MASK 0x30
78 #define ST_GYRO_2_FS_AVL_250_VAL 0x00
79 #define ST_GYRO_2_FS_AVL_500_VAL 0x01
80 #define ST_GYRO_2_FS_AVL_2000_VAL 0x02
81 #define ST_GYRO_2_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
82 #define ST_GYRO_2_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
83 #define ST_GYRO_2_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
84 #define ST_GYRO_2_BDU_ADDR 0x23
85 #define ST_GYRO_2_BDU_MASK 0x80
86 #define ST_GYRO_2_DRDY_IRQ_ADDR 0x22
87 #define ST_GYRO_2_DRDY_IRQ_INT2_MASK 0x08
88 #define ST_GYRO_2_MULTIREAD_BIT true
90 /* CUSTOM VALUES FOR SENSOR 3 */
91 #define ST_GYRO_3_WAI_EXP 0xd7
92 #define ST_GYRO_3_ODR_ADDR 0x20
93 #define ST_GYRO_3_ODR_MASK 0xc0
94 #define ST_GYRO_3_ODR_AVL_95HZ_VAL 0x00
95 #define ST_GYRO_3_ODR_AVL_190HZ_VAL 0x01
96 #define ST_GYRO_3_ODR_AVL_380HZ_VAL 0x02
97 #define ST_GYRO_3_ODR_AVL_760HZ_VAL 0x03
98 #define ST_GYRO_3_PW_ADDR 0x20
99 #define ST_GYRO_3_PW_MASK 0x08
100 #define ST_GYRO_3_FS_ADDR 0x23
101 #define ST_GYRO_3_FS_MASK 0x30
102 #define ST_GYRO_3_FS_AVL_250_VAL 0x00
103 #define ST_GYRO_3_FS_AVL_500_VAL 0x01
104 #define ST_GYRO_3_FS_AVL_2000_VAL 0x02
105 #define ST_GYRO_3_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
106 #define ST_GYRO_3_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
107 #define ST_GYRO_3_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
108 #define ST_GYRO_3_BDU_ADDR 0x23
109 #define ST_GYRO_3_BDU_MASK 0x80
110 #define ST_GYRO_3_DRDY_IRQ_ADDR 0x22
111 #define ST_GYRO_3_DRDY_IRQ_INT2_MASK 0x08
112 #define ST_GYRO_3_MULTIREAD_BIT true
115 static const struct iio_chan_spec st_gyro_16bit_channels[] = {
116 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
117 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
118 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
119 ST_GYRO_DEFAULT_OUT_X_L_ADDR),
120 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
121 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
122 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
123 ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
124 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
125 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
126 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
127 ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
128 IIO_CHAN_SOFT_TIMESTAMP(3)
131 static const struct st_sensor_settings st_gyro_sensors_settings[] = {
133 .wai = ST_GYRO_1_WAI_EXP,
134 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
135 .sensors_supported = {
136 [0] = L3G4200D_GYRO_DEV_NAME,
137 [1] = LSM330DL_GYRO_DEV_NAME,
139 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
140 .odr = {
141 .addr = ST_GYRO_1_ODR_ADDR,
142 .mask = ST_GYRO_1_ODR_MASK,
143 .odr_avl = {
144 { 100, ST_GYRO_1_ODR_AVL_100HZ_VAL, },
145 { 200, ST_GYRO_1_ODR_AVL_200HZ_VAL, },
146 { 400, ST_GYRO_1_ODR_AVL_400HZ_VAL, },
147 { 800, ST_GYRO_1_ODR_AVL_800HZ_VAL, },
150 .pw = {
151 .addr = ST_GYRO_1_PW_ADDR,
152 .mask = ST_GYRO_1_PW_MASK,
153 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
154 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
156 .enable_axis = {
157 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
158 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
160 .fs = {
161 .addr = ST_GYRO_1_FS_ADDR,
162 .mask = ST_GYRO_1_FS_MASK,
163 .fs_avl = {
164 [0] = {
165 .num = ST_GYRO_FS_AVL_250DPS,
166 .value = ST_GYRO_1_FS_AVL_250_VAL,
167 .gain = ST_GYRO_1_FS_AVL_250_GAIN,
169 [1] = {
170 .num = ST_GYRO_FS_AVL_500DPS,
171 .value = ST_GYRO_1_FS_AVL_500_VAL,
172 .gain = ST_GYRO_1_FS_AVL_500_GAIN,
174 [2] = {
175 .num = ST_GYRO_FS_AVL_2000DPS,
176 .value = ST_GYRO_1_FS_AVL_2000_VAL,
177 .gain = ST_GYRO_1_FS_AVL_2000_GAIN,
181 .bdu = {
182 .addr = ST_GYRO_1_BDU_ADDR,
183 .mask = ST_GYRO_1_BDU_MASK,
185 .drdy_irq = {
186 .addr = ST_GYRO_1_DRDY_IRQ_ADDR,
187 .mask_int2 = ST_GYRO_1_DRDY_IRQ_INT2_MASK,
189 * The sensor has IHL (active low) and open
190 * drain settings, but only for INT1 and not
191 * for the DRDY line on INT2.
194 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
195 .bootime = 2,
198 .wai = ST_GYRO_2_WAI_EXP,
199 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
200 .sensors_supported = {
201 [0] = L3GD20_GYRO_DEV_NAME,
202 [1] = LSM330D_GYRO_DEV_NAME,
203 [2] = LSM330DLC_GYRO_DEV_NAME,
204 [3] = L3G4IS_GYRO_DEV_NAME,
205 [4] = LSM330_GYRO_DEV_NAME,
207 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
208 .odr = {
209 .addr = ST_GYRO_2_ODR_ADDR,
210 .mask = ST_GYRO_2_ODR_MASK,
211 .odr_avl = {
212 { 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, },
213 { 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, },
214 { 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, },
215 { 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, },
218 .pw = {
219 .addr = ST_GYRO_2_PW_ADDR,
220 .mask = ST_GYRO_2_PW_MASK,
221 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
222 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
224 .enable_axis = {
225 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
226 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
228 .fs = {
229 .addr = ST_GYRO_2_FS_ADDR,
230 .mask = ST_GYRO_2_FS_MASK,
231 .fs_avl = {
232 [0] = {
233 .num = ST_GYRO_FS_AVL_250DPS,
234 .value = ST_GYRO_2_FS_AVL_250_VAL,
235 .gain = ST_GYRO_2_FS_AVL_250_GAIN,
237 [1] = {
238 .num = ST_GYRO_FS_AVL_500DPS,
239 .value = ST_GYRO_2_FS_AVL_500_VAL,
240 .gain = ST_GYRO_2_FS_AVL_500_GAIN,
242 [2] = {
243 .num = ST_GYRO_FS_AVL_2000DPS,
244 .value = ST_GYRO_2_FS_AVL_2000_VAL,
245 .gain = ST_GYRO_2_FS_AVL_2000_GAIN,
249 .bdu = {
250 .addr = ST_GYRO_2_BDU_ADDR,
251 .mask = ST_GYRO_2_BDU_MASK,
253 .drdy_irq = {
254 .addr = ST_GYRO_2_DRDY_IRQ_ADDR,
255 .mask_int2 = ST_GYRO_2_DRDY_IRQ_INT2_MASK,
257 * The sensor has IHL (active low) and open
258 * drain settings, but only for INT1 and not
259 * for the DRDY line on INT2.
262 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
263 .bootime = 2,
266 .wai = ST_GYRO_3_WAI_EXP,
267 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
268 .sensors_supported = {
269 [0] = L3GD20_GYRO_DEV_NAME,
271 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
272 .odr = {
273 .addr = ST_GYRO_3_ODR_ADDR,
274 .mask = ST_GYRO_3_ODR_MASK,
275 .odr_avl = {
276 { 95, ST_GYRO_3_ODR_AVL_95HZ_VAL, },
277 { 190, ST_GYRO_3_ODR_AVL_190HZ_VAL, },
278 { 380, ST_GYRO_3_ODR_AVL_380HZ_VAL, },
279 { 760, ST_GYRO_3_ODR_AVL_760HZ_VAL, },
282 .pw = {
283 .addr = ST_GYRO_3_PW_ADDR,
284 .mask = ST_GYRO_3_PW_MASK,
285 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
286 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
288 .enable_axis = {
289 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
290 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
292 .fs = {
293 .addr = ST_GYRO_3_FS_ADDR,
294 .mask = ST_GYRO_3_FS_MASK,
295 .fs_avl = {
296 [0] = {
297 .num = ST_GYRO_FS_AVL_250DPS,
298 .value = ST_GYRO_3_FS_AVL_250_VAL,
299 .gain = ST_GYRO_3_FS_AVL_250_GAIN,
301 [1] = {
302 .num = ST_GYRO_FS_AVL_500DPS,
303 .value = ST_GYRO_3_FS_AVL_500_VAL,
304 .gain = ST_GYRO_3_FS_AVL_500_GAIN,
306 [2] = {
307 .num = ST_GYRO_FS_AVL_2000DPS,
308 .value = ST_GYRO_3_FS_AVL_2000_VAL,
309 .gain = ST_GYRO_3_FS_AVL_2000_GAIN,
313 .bdu = {
314 .addr = ST_GYRO_3_BDU_ADDR,
315 .mask = ST_GYRO_3_BDU_MASK,
317 .drdy_irq = {
318 .addr = ST_GYRO_3_DRDY_IRQ_ADDR,
319 .mask_int2 = ST_GYRO_3_DRDY_IRQ_INT2_MASK,
321 * The sensor has IHL (active low) and open
322 * drain settings, but only for INT1 and not
323 * for the DRDY line on INT2.
326 .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT,
327 .bootime = 2,
331 static int st_gyro_read_raw(struct iio_dev *indio_dev,
332 struct iio_chan_spec const *ch, int *val,
333 int *val2, long mask)
335 int err;
336 struct st_sensor_data *gdata = iio_priv(indio_dev);
338 switch (mask) {
339 case IIO_CHAN_INFO_RAW:
340 err = st_sensors_read_info_raw(indio_dev, ch, val);
341 if (err < 0)
342 goto read_error;
344 return IIO_VAL_INT;
345 case IIO_CHAN_INFO_SCALE:
346 *val = 0;
347 *val2 = gdata->current_fullscale->gain;
348 return IIO_VAL_INT_PLUS_MICRO;
349 case IIO_CHAN_INFO_SAMP_FREQ:
350 *val = gdata->odr;
351 return IIO_VAL_INT;
352 default:
353 return -EINVAL;
356 read_error:
357 return err;
360 static int st_gyro_write_raw(struct iio_dev *indio_dev,
361 struct iio_chan_spec const *chan, int val, int val2, long mask)
363 int err;
365 switch (mask) {
366 case IIO_CHAN_INFO_SCALE:
367 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
368 break;
369 case IIO_CHAN_INFO_SAMP_FREQ:
370 if (val2)
371 return -EINVAL;
372 mutex_lock(&indio_dev->mlock);
373 err = st_sensors_set_odr(indio_dev, val);
374 mutex_unlock(&indio_dev->mlock);
375 return err;
376 default:
377 err = -EINVAL;
380 return err;
383 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
384 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
386 static struct attribute *st_gyro_attributes[] = {
387 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
388 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
389 NULL,
392 static const struct attribute_group st_gyro_attribute_group = {
393 .attrs = st_gyro_attributes,
396 static const struct iio_info gyro_info = {
397 .driver_module = THIS_MODULE,
398 .attrs = &st_gyro_attribute_group,
399 .read_raw = &st_gyro_read_raw,
400 .write_raw = &st_gyro_write_raw,
401 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
404 #ifdef CONFIG_IIO_TRIGGER
405 static const struct iio_trigger_ops st_gyro_trigger_ops = {
406 .owner = THIS_MODULE,
407 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
409 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
410 #else
411 #define ST_GYRO_TRIGGER_OPS NULL
412 #endif
414 int st_gyro_common_probe(struct iio_dev *indio_dev)
416 struct st_sensor_data *gdata = iio_priv(indio_dev);
417 int irq = gdata->get_irq_data_ready(indio_dev);
418 int err;
420 indio_dev->modes = INDIO_DIRECT_MODE;
421 indio_dev->info = &gyro_info;
422 mutex_init(&gdata->tb.buf_lock);
424 st_sensors_power_enable(indio_dev);
426 err = st_sensors_check_device_support(indio_dev,
427 ARRAY_SIZE(st_gyro_sensors_settings),
428 st_gyro_sensors_settings);
429 if (err < 0)
430 return err;
432 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
433 gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
434 indio_dev->channels = gdata->sensor_settings->ch;
435 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
437 gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
438 &gdata->sensor_settings->fs.fs_avl[0];
439 gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
441 err = st_sensors_init_sensor(indio_dev,
442 (struct st_sensors_platform_data *)&gyro_pdata);
443 if (err < 0)
444 return err;
446 err = st_gyro_allocate_ring(indio_dev);
447 if (err < 0)
448 return err;
450 if (irq > 0) {
451 err = st_sensors_allocate_trigger(indio_dev,
452 ST_GYRO_TRIGGER_OPS);
453 if (err < 0)
454 goto st_gyro_probe_trigger_error;
457 err = iio_device_register(indio_dev);
458 if (err)
459 goto st_gyro_device_register_error;
461 dev_info(&indio_dev->dev, "registered gyroscope %s\n",
462 indio_dev->name);
464 return 0;
466 st_gyro_device_register_error:
467 if (irq > 0)
468 st_sensors_deallocate_trigger(indio_dev);
469 st_gyro_probe_trigger_error:
470 st_gyro_deallocate_ring(indio_dev);
472 return err;
474 EXPORT_SYMBOL(st_gyro_common_probe);
476 void st_gyro_common_remove(struct iio_dev *indio_dev)
478 struct st_sensor_data *gdata = iio_priv(indio_dev);
480 st_sensors_power_disable(indio_dev);
482 iio_device_unregister(indio_dev);
483 if (gdata->get_irq_data_ready(indio_dev) > 0)
484 st_sensors_deallocate_trigger(indio_dev);
486 st_gyro_deallocate_ring(indio_dev);
488 EXPORT_SYMBOL(st_gyro_common_remove);
490 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
491 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
492 MODULE_LICENSE("GPL v2");