xref: /openbmc/linux/drivers/iio/gyro/st_gyro_core.c (revision e285d5bf)
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_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
42 
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)
57 };
58 
59 static const struct st_sensor_settings st_gyro_sensors_settings[] = {
60 	{
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,
66 		},
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, },
76 			},
77 		},
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,
83 		},
84 		.enable_axis = {
85 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
86 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
87 		},
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),
96 				},
97 				[1] = {
98 					.num = ST_GYRO_FS_AVL_500DPS,
99 					.value = 0x01,
100 					.gain = IIO_DEGREE_TO_RAD(17500),
101 				},
102 				[2] = {
103 					.num = ST_GYRO_FS_AVL_2000DPS,
104 					.value = 0x02,
105 					.gain = IIO_DEGREE_TO_RAD(70000),
106 				},
107 			},
108 		},
109 		.bdu = {
110 			.addr = 0x23,
111 			.mask = 0x80,
112 		},
113 		.drdy_irq = {
114 			.int2 = {
115 				.addr = 0x22,
116 				.mask = 0x08,
117 			},
118 			/*
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.
122 			 */
123 			.stat_drdy = {
124 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
125 				.mask = 0x07,
126 			},
127 		},
128 		.sim = {
129 			.addr = 0x23,
130 			.value = BIT(0),
131 		},
132 		.multi_read_bit = true,
133 		.bootime = 2,
134 	},
135 	{
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,
144 			[5] = LSM9DS0_GYRO_DEV_NAME,
145 		},
146 		.ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
147 		.odr = {
148 			.addr = 0x20,
149 			.mask = 0xc0,
150 			.odr_avl = {
151 				{ .hz = 95, .value = 0x00, },
152 				{ .hz = 190, .value = 0x01, },
153 				{ .hz = 380, .value = 0x02, },
154 				{ .hz = 760, .value = 0x03, },
155 			},
156 		},
157 		.pw = {
158 			.addr = 0x20,
159 			.mask = 0x08,
160 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
161 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
162 		},
163 		.enable_axis = {
164 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
165 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
166 		},
167 		.fs = {
168 			.addr = 0x23,
169 			.mask = 0x30,
170 			.fs_avl = {
171 				[0] = {
172 					.num = ST_GYRO_FS_AVL_250DPS,
173 					.value = 0x00,
174 					.gain = IIO_DEGREE_TO_RAD(8750),
175 				},
176 				[1] = {
177 					.num = ST_GYRO_FS_AVL_500DPS,
178 					.value = 0x01,
179 					.gain = IIO_DEGREE_TO_RAD(17500),
180 				},
181 				[2] = {
182 					.num = ST_GYRO_FS_AVL_2000DPS,
183 					.value = 0x02,
184 					.gain = IIO_DEGREE_TO_RAD(70000),
185 				},
186 			},
187 		},
188 		.bdu = {
189 			.addr = 0x23,
190 			.mask = 0x80,
191 		},
192 		.drdy_irq = {
193 			.int2 = {
194 				.addr = 0x22,
195 				.mask = 0x08,
196 			},
197 			/*
198 			 * The sensor has IHL (active low) and open
199 			 * drain settings, but only for INT1 and not
200 			 * for the DRDY line on INT2.
201 			 */
202 			.stat_drdy = {
203 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
204 				.mask = 0x07,
205 			},
206 		},
207 		.sim = {
208 			.addr = 0x23,
209 			.value = BIT(0),
210 		},
211 		.multi_read_bit = true,
212 		.bootime = 2,
213 	},
214 	{
215 		.wai = 0xd7,
216 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
217 		.sensors_supported = {
218 			[0] = L3GD20H_GYRO_DEV_NAME,
219 		},
220 		.ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
221 		.odr = {
222 			.addr = 0x20,
223 			.mask = 0xc0,
224 			.odr_avl = {
225 				{ .hz = 100, .value = 0x00, },
226 				{ .hz = 200, .value = 0x01, },
227 				{ .hz = 400, .value = 0x02, },
228 				{ .hz = 800, .value = 0x03, },
229 			},
230 		},
231 		.pw = {
232 			.addr = 0x20,
233 			.mask = 0x08,
234 			.value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
235 			.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
236 		},
237 		.enable_axis = {
238 			.addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
239 			.mask = ST_SENSORS_DEFAULT_AXIS_MASK,
240 		},
241 		.fs = {
242 			.addr = 0x23,
243 			.mask = 0x30,
244 			.fs_avl = {
245 				[0] = {
246 					.num = ST_GYRO_FS_AVL_245DPS,
247 					.value = 0x00,
248 					.gain = IIO_DEGREE_TO_RAD(8750),
249 				},
250 				[1] = {
251 					.num = ST_GYRO_FS_AVL_500DPS,
252 					.value = 0x01,
253 					.gain = IIO_DEGREE_TO_RAD(17500),
254 				},
255 				[2] = {
256 					.num = ST_GYRO_FS_AVL_2000DPS,
257 					.value = 0x02,
258 					.gain = IIO_DEGREE_TO_RAD(70000),
259 				},
260 			},
261 		},
262 		.bdu = {
263 			.addr = 0x23,
264 			.mask = 0x80,
265 		},
266 		.drdy_irq = {
267 			.int2 = {
268 				.addr = 0x22,
269 				.mask = 0x08,
270 			},
271 			/*
272 			 * The sensor has IHL (active low) and open
273 			 * drain settings, but only for INT1 and not
274 			 * for the DRDY line on INT2.
275 			 */
276 			.stat_drdy = {
277 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
278 				.mask = 0x07,
279 			},
280 		},
281 		.sim = {
282 			.addr = 0x23,
283 			.value = BIT(0),
284 		},
285 		.multi_read_bit = true,
286 		.bootime = 2,
287 	},
288 };
289 
290 static int st_gyro_read_raw(struct iio_dev *indio_dev,
291 			struct iio_chan_spec const *ch, int *val,
292 							int *val2, long mask)
293 {
294 	int err;
295 	struct st_sensor_data *gdata = iio_priv(indio_dev);
296 
297 	switch (mask) {
298 	case IIO_CHAN_INFO_RAW:
299 		err = st_sensors_read_info_raw(indio_dev, ch, val);
300 		if (err < 0)
301 			goto read_error;
302 
303 		return IIO_VAL_INT;
304 	case IIO_CHAN_INFO_SCALE:
305 		*val = 0;
306 		*val2 = gdata->current_fullscale->gain;
307 		return IIO_VAL_INT_PLUS_MICRO;
308 	case IIO_CHAN_INFO_SAMP_FREQ:
309 		*val = gdata->odr;
310 		return IIO_VAL_INT;
311 	default:
312 		return -EINVAL;
313 	}
314 
315 read_error:
316 	return err;
317 }
318 
319 static int st_gyro_write_raw(struct iio_dev *indio_dev,
320 		struct iio_chan_spec const *chan, int val, int val2, long mask)
321 {
322 	int err;
323 
324 	switch (mask) {
325 	case IIO_CHAN_INFO_SCALE:
326 		err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
327 		break;
328 	case IIO_CHAN_INFO_SAMP_FREQ:
329 		if (val2)
330 			return -EINVAL;
331 		mutex_lock(&indio_dev->mlock);
332 		err = st_sensors_set_odr(indio_dev, val);
333 		mutex_unlock(&indio_dev->mlock);
334 		return err;
335 	default:
336 		err = -EINVAL;
337 	}
338 
339 	return err;
340 }
341 
342 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
343 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
344 
345 static struct attribute *st_gyro_attributes[] = {
346 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
347 	&iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
348 	NULL,
349 };
350 
351 static const struct attribute_group st_gyro_attribute_group = {
352 	.attrs = st_gyro_attributes,
353 };
354 
355 static const struct iio_info gyro_info = {
356 	.attrs = &st_gyro_attribute_group,
357 	.read_raw = &st_gyro_read_raw,
358 	.write_raw = &st_gyro_write_raw,
359 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
360 };
361 
362 #ifdef CONFIG_IIO_TRIGGER
363 static const struct iio_trigger_ops st_gyro_trigger_ops = {
364 	.set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
365 	.validate_device = st_sensors_validate_device,
366 };
367 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
368 #else
369 #define ST_GYRO_TRIGGER_OPS NULL
370 #endif
371 
372 int st_gyro_common_probe(struct iio_dev *indio_dev)
373 {
374 	struct st_sensor_data *gdata = iio_priv(indio_dev);
375 	int irq = gdata->get_irq_data_ready(indio_dev);
376 	int err;
377 
378 	indio_dev->modes = INDIO_DIRECT_MODE;
379 	indio_dev->info = &gyro_info;
380 	mutex_init(&gdata->tb.buf_lock);
381 
382 	err = st_sensors_power_enable(indio_dev);
383 	if (err)
384 		return err;
385 
386 	err = st_sensors_check_device_support(indio_dev,
387 					ARRAY_SIZE(st_gyro_sensors_settings),
388 					st_gyro_sensors_settings);
389 	if (err < 0)
390 		goto st_gyro_power_off;
391 
392 	gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
393 	gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
394 	indio_dev->channels = gdata->sensor_settings->ch;
395 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
396 
397 	gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
398 					&gdata->sensor_settings->fs.fs_avl[0];
399 	gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
400 
401 	err = st_sensors_init_sensor(indio_dev,
402 				(struct st_sensors_platform_data *)&gyro_pdata);
403 	if (err < 0)
404 		goto st_gyro_power_off;
405 
406 	err = st_gyro_allocate_ring(indio_dev);
407 	if (err < 0)
408 		goto st_gyro_power_off;
409 
410 	if (irq > 0) {
411 		err = st_sensors_allocate_trigger(indio_dev,
412 						  ST_GYRO_TRIGGER_OPS);
413 		if (err < 0)
414 			goto st_gyro_probe_trigger_error;
415 	}
416 
417 	err = iio_device_register(indio_dev);
418 	if (err)
419 		goto st_gyro_device_register_error;
420 
421 	dev_info(&indio_dev->dev, "registered gyroscope %s\n",
422 		 indio_dev->name);
423 
424 	return 0;
425 
426 st_gyro_device_register_error:
427 	if (irq > 0)
428 		st_sensors_deallocate_trigger(indio_dev);
429 st_gyro_probe_trigger_error:
430 	st_gyro_deallocate_ring(indio_dev);
431 st_gyro_power_off:
432 	st_sensors_power_disable(indio_dev);
433 
434 	return err;
435 }
436 EXPORT_SYMBOL(st_gyro_common_probe);
437 
438 void st_gyro_common_remove(struct iio_dev *indio_dev)
439 {
440 	struct st_sensor_data *gdata = iio_priv(indio_dev);
441 
442 	st_sensors_power_disable(indio_dev);
443 
444 	iio_device_unregister(indio_dev);
445 	if (gdata->get_irq_data_ready(indio_dev) > 0)
446 		st_sensors_deallocate_trigger(indio_dev);
447 
448 	st_gyro_deallocate_ring(indio_dev);
449 }
450 EXPORT_SYMBOL(st_gyro_common_remove);
451 
452 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
453 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
454 MODULE_LICENSE("GPL v2");
455