xref: /linux/drivers/iio/gyro/st_gyro_core.c (revision 93df8a1ed6231727c5db94a80b1a6bd5ee67cec3)
1 /*
2  * STMicroelectronics gyroscopes driver
3  *
4  * Copyright 2012-2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
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>
26 
27 #include <linux/iio/common/st_sensors.h>
28 #include "st_gyro.h"
29 
30 #define ST_GYRO_NUMBER_DATA_CHANNELS		3
31 
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
36 
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
41 
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
65 
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
89 
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
113 
114 
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)
129 };
130 
131 static const struct st_sensor_settings st_gyro_sensors_settings[] = {
132 	{
133 		.wai = ST_GYRO_1_WAI_EXP,
134 		.sensors_supported = {
135 			[0] = L3G4200D_GYRO_DEV_NAME,
136 			[1] = LSM330DL_GYRO_DEV_NAME,
137 		},
138 		.ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
139 		.odr = {
140 			.addr = ST_GYRO_1_ODR_ADDR,
141 			.mask = ST_GYRO_1_ODR_MASK,
142 			.odr_avl = {
143 				{ 100, ST_GYRO_1_ODR_AVL_100HZ_VAL, },
144 				{ 200, ST_GYRO_1_ODR_AVL_200HZ_VAL, },
145 				{ 400, ST_GYRO_1_ODR_AVL_400HZ_VAL, },
146 				{ 800, ST_GYRO_1_ODR_AVL_800HZ_VAL, },
147 			},
148 		},
149 		.pw = {
150 			.addr = ST_GYRO_1_PW_ADDR,
151 			.mask = ST_GYRO_1_PW_MASK,
152 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
153 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
154 		},
155 		.enable_axis = {
156 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
157 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
158 		},
159 		.fs = {
160 			.addr = ST_GYRO_1_FS_ADDR,
161 			.mask = ST_GYRO_1_FS_MASK,
162 			.fs_avl = {
163 				[0] = {
164 					.num = ST_GYRO_FS_AVL_250DPS,
165 					.value = ST_GYRO_1_FS_AVL_250_VAL,
166 					.gain = ST_GYRO_1_FS_AVL_250_GAIN,
167 				},
168 				[1] = {
169 					.num = ST_GYRO_FS_AVL_500DPS,
170 					.value = ST_GYRO_1_FS_AVL_500_VAL,
171 					.gain = ST_GYRO_1_FS_AVL_500_GAIN,
172 				},
173 				[2] = {
174 					.num = ST_GYRO_FS_AVL_2000DPS,
175 					.value = ST_GYRO_1_FS_AVL_2000_VAL,
176 					.gain = ST_GYRO_1_FS_AVL_2000_GAIN,
177 				},
178 			},
179 		},
180 		.bdu = {
181 			.addr = ST_GYRO_1_BDU_ADDR,
182 			.mask = ST_GYRO_1_BDU_MASK,
183 		},
184 		.drdy_irq = {
185 			.addr = ST_GYRO_1_DRDY_IRQ_ADDR,
186 			.mask_int2 = ST_GYRO_1_DRDY_IRQ_INT2_MASK,
187 		},
188 		.multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
189 		.bootime = 2,
190 	},
191 	{
192 		.wai = ST_GYRO_2_WAI_EXP,
193 		.sensors_supported = {
194 			[0] = L3GD20_GYRO_DEV_NAME,
195 			[1] = LSM330D_GYRO_DEV_NAME,
196 			[2] = LSM330DLC_GYRO_DEV_NAME,
197 			[3] = L3G4IS_GYRO_DEV_NAME,
198 			[4] = LSM330_GYRO_DEV_NAME,
199 		},
200 		.ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
201 		.odr = {
202 			.addr = ST_GYRO_2_ODR_ADDR,
203 			.mask = ST_GYRO_2_ODR_MASK,
204 			.odr_avl = {
205 				{ 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, },
206 				{ 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, },
207 				{ 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, },
208 				{ 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, },
209 			},
210 		},
211 		.pw = {
212 			.addr = ST_GYRO_2_PW_ADDR,
213 			.mask = ST_GYRO_2_PW_MASK,
214 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
215 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
216 		},
217 		.enable_axis = {
218 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
219 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
220 		},
221 		.fs = {
222 			.addr = ST_GYRO_2_FS_ADDR,
223 			.mask = ST_GYRO_2_FS_MASK,
224 			.fs_avl = {
225 				[0] = {
226 					.num = ST_GYRO_FS_AVL_250DPS,
227 					.value = ST_GYRO_2_FS_AVL_250_VAL,
228 					.gain = ST_GYRO_2_FS_AVL_250_GAIN,
229 				},
230 				[1] = {
231 					.num = ST_GYRO_FS_AVL_500DPS,
232 					.value = ST_GYRO_2_FS_AVL_500_VAL,
233 					.gain = ST_GYRO_2_FS_AVL_500_GAIN,
234 				},
235 				[2] = {
236 					.num = ST_GYRO_FS_AVL_2000DPS,
237 					.value = ST_GYRO_2_FS_AVL_2000_VAL,
238 					.gain = ST_GYRO_2_FS_AVL_2000_GAIN,
239 				},
240 			},
241 		},
242 		.bdu = {
243 			.addr = ST_GYRO_2_BDU_ADDR,
244 			.mask = ST_GYRO_2_BDU_MASK,
245 		},
246 		.drdy_irq = {
247 			.addr = ST_GYRO_2_DRDY_IRQ_ADDR,
248 			.mask_int2 = ST_GYRO_2_DRDY_IRQ_INT2_MASK,
249 		},
250 		.multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
251 		.bootime = 2,
252 	},
253 	{
254 		.wai = ST_GYRO_3_WAI_EXP,
255 		.sensors_supported = {
256 			[0] = L3GD20_GYRO_DEV_NAME,
257 		},
258 		.ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
259 		.odr = {
260 			.addr = ST_GYRO_3_ODR_ADDR,
261 			.mask = ST_GYRO_3_ODR_MASK,
262 			.odr_avl = {
263 				{ 95, ST_GYRO_3_ODR_AVL_95HZ_VAL, },
264 				{ 190, ST_GYRO_3_ODR_AVL_190HZ_VAL, },
265 				{ 380, ST_GYRO_3_ODR_AVL_380HZ_VAL, },
266 				{ 760, ST_GYRO_3_ODR_AVL_760HZ_VAL, },
267 			},
268 		},
269 		.pw = {
270 			.addr = ST_GYRO_3_PW_ADDR,
271 			.mask = ST_GYRO_3_PW_MASK,
272 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
273 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
274 		},
275 		.enable_axis = {
276 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
277 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
278 		},
279 		.fs = {
280 			.addr = ST_GYRO_3_FS_ADDR,
281 			.mask = ST_GYRO_3_FS_MASK,
282 			.fs_avl = {
283 				[0] = {
284 					.num = ST_GYRO_FS_AVL_250DPS,
285 					.value = ST_GYRO_3_FS_AVL_250_VAL,
286 					.gain = ST_GYRO_3_FS_AVL_250_GAIN,
287 				},
288 				[1] = {
289 					.num = ST_GYRO_FS_AVL_500DPS,
290 					.value = ST_GYRO_3_FS_AVL_500_VAL,
291 					.gain = ST_GYRO_3_FS_AVL_500_GAIN,
292 				},
293 				[2] = {
294 					.num = ST_GYRO_FS_AVL_2000DPS,
295 					.value = ST_GYRO_3_FS_AVL_2000_VAL,
296 					.gain = ST_GYRO_3_FS_AVL_2000_GAIN,
297 				},
298 			},
299 		},
300 		.bdu = {
301 			.addr = ST_GYRO_3_BDU_ADDR,
302 			.mask = ST_GYRO_3_BDU_MASK,
303 		},
304 		.drdy_irq = {
305 			.addr = ST_GYRO_3_DRDY_IRQ_ADDR,
306 			.mask_int2 = ST_GYRO_3_DRDY_IRQ_INT2_MASK,
307 		},
308 		.multi_read_bit = ST_GYRO_3_MULTIREAD_BIT,
309 		.bootime = 2,
310 	},
311 };
312 
313 static int st_gyro_read_raw(struct iio_dev *indio_dev,
314 			struct iio_chan_spec const *ch, int *val,
315 							int *val2, long mask)
316 {
317 	int err;
318 	struct st_sensor_data *gdata = iio_priv(indio_dev);
319 
320 	switch (mask) {
321 	case IIO_CHAN_INFO_RAW:
322 		err = st_sensors_read_info_raw(indio_dev, ch, val);
323 		if (err < 0)
324 			goto read_error;
325 
326 		return IIO_VAL_INT;
327 	case IIO_CHAN_INFO_SCALE:
328 		*val = 0;
329 		*val2 = gdata->current_fullscale->gain;
330 		return IIO_VAL_INT_PLUS_MICRO;
331 	case IIO_CHAN_INFO_SAMP_FREQ:
332 		*val = gdata->odr;
333 		return IIO_VAL_INT;
334 	default:
335 		return -EINVAL;
336 	}
337 
338 read_error:
339 	return err;
340 }
341 
342 static int st_gyro_write_raw(struct iio_dev *indio_dev,
343 		struct iio_chan_spec const *chan, int val, int val2, long mask)
344 {
345 	int err;
346 
347 	switch (mask) {
348 	case IIO_CHAN_INFO_SCALE:
349 		err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
350 		break;
351 	case IIO_CHAN_INFO_SAMP_FREQ:
352 		if (val2)
353 			return -EINVAL;
354 		mutex_lock(&indio_dev->mlock);
355 		err = st_sensors_set_odr(indio_dev, val);
356 		mutex_unlock(&indio_dev->mlock);
357 		return err;
358 	default:
359 		err = -EINVAL;
360 	}
361 
362 	return err;
363 }
364 
365 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
366 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
367 
368 static struct attribute *st_gyro_attributes[] = {
369 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
370 	&iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
371 	NULL,
372 };
373 
374 static const struct attribute_group st_gyro_attribute_group = {
375 	.attrs = st_gyro_attributes,
376 };
377 
378 static const struct iio_info gyro_info = {
379 	.driver_module = THIS_MODULE,
380 	.attrs = &st_gyro_attribute_group,
381 	.read_raw = &st_gyro_read_raw,
382 	.write_raw = &st_gyro_write_raw,
383 };
384 
385 #ifdef CONFIG_IIO_TRIGGER
386 static const struct iio_trigger_ops st_gyro_trigger_ops = {
387 	.owner = THIS_MODULE,
388 	.set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
389 };
390 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
391 #else
392 #define ST_GYRO_TRIGGER_OPS NULL
393 #endif
394 
395 int st_gyro_common_probe(struct iio_dev *indio_dev)
396 {
397 	struct st_sensor_data *gdata = iio_priv(indio_dev);
398 	int irq = gdata->get_irq_data_ready(indio_dev);
399 	int err;
400 
401 	indio_dev->modes = INDIO_DIRECT_MODE;
402 	indio_dev->info = &gyro_info;
403 	mutex_init(&gdata->tb.buf_lock);
404 
405 	st_sensors_power_enable(indio_dev);
406 
407 	err = st_sensors_check_device_support(indio_dev,
408 					ARRAY_SIZE(st_gyro_sensors_settings),
409 					st_gyro_sensors_settings);
410 	if (err < 0)
411 		return err;
412 
413 	gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
414 	gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
415 	indio_dev->channels = gdata->sensor_settings->ch;
416 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
417 
418 	gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
419 					&gdata->sensor_settings->fs.fs_avl[0];
420 	gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
421 
422 	err = st_sensors_init_sensor(indio_dev,
423 				(struct st_sensors_platform_data *)&gyro_pdata);
424 	if (err < 0)
425 		return err;
426 
427 	err = st_gyro_allocate_ring(indio_dev);
428 	if (err < 0)
429 		return err;
430 
431 	if (irq > 0) {
432 		err = st_sensors_allocate_trigger(indio_dev,
433 						  ST_GYRO_TRIGGER_OPS);
434 		if (err < 0)
435 			goto st_gyro_probe_trigger_error;
436 	}
437 
438 	err = iio_device_register(indio_dev);
439 	if (err)
440 		goto st_gyro_device_register_error;
441 
442 	dev_info(&indio_dev->dev, "registered gyroscope %s\n",
443 		 indio_dev->name);
444 
445 	return 0;
446 
447 st_gyro_device_register_error:
448 	if (irq > 0)
449 		st_sensors_deallocate_trigger(indio_dev);
450 st_gyro_probe_trigger_error:
451 	st_gyro_deallocate_ring(indio_dev);
452 
453 	return err;
454 }
455 EXPORT_SYMBOL(st_gyro_common_probe);
456 
457 void st_gyro_common_remove(struct iio_dev *indio_dev)
458 {
459 	struct st_sensor_data *gdata = iio_priv(indio_dev);
460 
461 	st_sensors_power_disable(indio_dev);
462 
463 	iio_device_unregister(indio_dev);
464 	if (gdata->get_irq_data_ready(indio_dev) > 0)
465 		st_sensors_deallocate_trigger(indio_dev);
466 
467 	st_gyro_deallocate_ring(indio_dev);
468 }
469 EXPORT_SYMBOL(st_gyro_common_remove);
470 
471 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
472 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
473 MODULE_LICENSE("GPL v2");
474