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