powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / drivers / iio / gyro / st_gyro_core.c
blob2a42b3d583e85b45d9f93c9b1400e8551eac034a
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 static const struct iio_chan_spec st_gyro_16bit_channels[] = {
43 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
44 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
45 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
46 ST_GYRO_DEFAULT_OUT_X_L_ADDR),
47 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
48 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
49 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
50 ST_GYRO_DEFAULT_OUT_Y_L_ADDR),
51 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
52 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
53 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
54 ST_GYRO_DEFAULT_OUT_Z_L_ADDR),
55 IIO_CHAN_SOFT_TIMESTAMP(3)
58 static const struct st_sensor_settings st_gyro_sensors_settings[] = {
60 .wai = 0xd3,
61 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
62 .sensors_supported = {
63 [0] = L3G4200D_GYRO_DEV_NAME,
64 [1] = LSM330DL_GYRO_DEV_NAME,
66 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
67 .odr = {
68 .addr = 0x20,
69 .mask = 0xc0,
70 .odr_avl = {
71 { .hz = 100, .value = 0x00, },
72 { .hz = 200, .value = 0x01, },
73 { .hz = 400, .value = 0x02, },
74 { .hz = 800, .value = 0x03, },
77 .pw = {
78 .addr = 0x20,
79 .mask = 0x08,
80 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
81 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
83 .enable_axis = {
84 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
85 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
87 .fs = {
88 .addr = 0x23,
89 .mask = 0x30,
90 .fs_avl = {
91 [0] = {
92 .num = ST_GYRO_FS_AVL_250DPS,
93 .value = 0x00,
94 .gain = IIO_DEGREE_TO_RAD(8750),
96 [1] = {
97 .num = ST_GYRO_FS_AVL_500DPS,
98 .value = 0x01,
99 .gain = IIO_DEGREE_TO_RAD(17500),
101 [2] = {
102 .num = ST_GYRO_FS_AVL_2000DPS,
103 .value = 0x02,
104 .gain = IIO_DEGREE_TO_RAD(70000),
108 .bdu = {
109 .addr = 0x23,
110 .mask = 0x80,
112 .drdy_irq = {
113 .addr = 0x22,
114 .mask_int2 = 0x08,
116 * The sensor has IHL (active low) and open
117 * drain settings, but only for INT1 and not
118 * for the DRDY line on INT2.
120 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
122 .multi_read_bit = true,
123 .bootime = 2,
126 .wai = 0xd4,
127 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
128 .sensors_supported = {
129 [0] = L3GD20_GYRO_DEV_NAME,
130 [1] = LSM330D_GYRO_DEV_NAME,
131 [2] = LSM330DLC_GYRO_DEV_NAME,
132 [3] = L3G4IS_GYRO_DEV_NAME,
133 [4] = LSM330_GYRO_DEV_NAME,
134 [5] = LSM9DS0_GYRO_DEV_NAME,
136 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
137 .odr = {
138 .addr = 0x20,
139 .mask = 0xc0,
140 .odr_avl = {
141 { .hz = 95, .value = 0x00, },
142 { .hz = 190, .value = 0x01, },
143 { .hz = 380, .value = 0x02, },
144 { .hz = 760, .value = 0x03, },
147 .pw = {
148 .addr = 0x20,
149 .mask = 0x08,
150 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
151 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
153 .enable_axis = {
154 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
155 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
157 .fs = {
158 .addr = 0x23,
159 .mask = 0x30,
160 .fs_avl = {
161 [0] = {
162 .num = ST_GYRO_FS_AVL_250DPS,
163 .value = 0x00,
164 .gain = IIO_DEGREE_TO_RAD(8750),
166 [1] = {
167 .num = ST_GYRO_FS_AVL_500DPS,
168 .value = 0x01,
169 .gain = IIO_DEGREE_TO_RAD(17500),
171 [2] = {
172 .num = ST_GYRO_FS_AVL_2000DPS,
173 .value = 0x02,
174 .gain = IIO_DEGREE_TO_RAD(70000),
178 .bdu = {
179 .addr = 0x23,
180 .mask = 0x80,
182 .drdy_irq = {
183 .addr = 0x22,
184 .mask_int2 = 0x08,
186 * The sensor has IHL (active low) and open
187 * drain settings, but only for INT1 and not
188 * for the DRDY line on INT2.
190 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
192 .multi_read_bit = true,
193 .bootime = 2,
196 .wai = 0xd7,
197 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
198 .sensors_supported = {
199 [0] = L3GD20_GYRO_DEV_NAME,
201 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
202 .odr = {
203 .addr = 0x20,
204 .mask = 0xc0,
205 .odr_avl = {
206 { .hz = 95, .value = 0x00, },
207 { .hz = 190, .value = 0x01, },
208 { .hz = 380, .value = 0x02, },
209 { .hz = 760, .value = 0x03, },
212 .pw = {
213 .addr = 0x20,
214 .mask = 0x08,
215 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
216 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
218 .enable_axis = {
219 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
220 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
222 .fs = {
223 .addr = 0x23,
224 .mask = 0x30,
225 .fs_avl = {
226 [0] = {
227 .num = ST_GYRO_FS_AVL_250DPS,
228 .value = 0x00,
229 .gain = IIO_DEGREE_TO_RAD(8750),
231 [1] = {
232 .num = ST_GYRO_FS_AVL_500DPS,
233 .value = 0x01,
234 .gain = IIO_DEGREE_TO_RAD(17500),
236 [2] = {
237 .num = ST_GYRO_FS_AVL_2000DPS,
238 .value = 0x02,
239 .gain = IIO_DEGREE_TO_RAD(70000),
243 .bdu = {
244 .addr = 0x23,
245 .mask = 0x80,
247 .drdy_irq = {
248 .addr = 0x22,
249 .mask_int2 = 0x08,
251 * The sensor has IHL (active low) and open
252 * drain settings, but only for INT1 and not
253 * for the DRDY line on INT2.
255 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
257 .multi_read_bit = true,
258 .bootime = 2,
262 static int st_gyro_read_raw(struct iio_dev *indio_dev,
263 struct iio_chan_spec const *ch, int *val,
264 int *val2, long mask)
266 int err;
267 struct st_sensor_data *gdata = iio_priv(indio_dev);
269 switch (mask) {
270 case IIO_CHAN_INFO_RAW:
271 err = st_sensors_read_info_raw(indio_dev, ch, val);
272 if (err < 0)
273 goto read_error;
275 return IIO_VAL_INT;
276 case IIO_CHAN_INFO_SCALE:
277 *val = 0;
278 *val2 = gdata->current_fullscale->gain;
279 return IIO_VAL_INT_PLUS_MICRO;
280 case IIO_CHAN_INFO_SAMP_FREQ:
281 *val = gdata->odr;
282 return IIO_VAL_INT;
283 default:
284 return -EINVAL;
287 read_error:
288 return err;
291 static int st_gyro_write_raw(struct iio_dev *indio_dev,
292 struct iio_chan_spec const *chan, int val, int val2, long mask)
294 int err;
296 switch (mask) {
297 case IIO_CHAN_INFO_SCALE:
298 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
299 break;
300 case IIO_CHAN_INFO_SAMP_FREQ:
301 if (val2)
302 return -EINVAL;
303 mutex_lock(&indio_dev->mlock);
304 err = st_sensors_set_odr(indio_dev, val);
305 mutex_unlock(&indio_dev->mlock);
306 return err;
307 default:
308 err = -EINVAL;
311 return err;
314 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
315 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
317 static struct attribute *st_gyro_attributes[] = {
318 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
319 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
320 NULL,
323 static const struct attribute_group st_gyro_attribute_group = {
324 .attrs = st_gyro_attributes,
327 static const struct iio_info gyro_info = {
328 .driver_module = THIS_MODULE,
329 .attrs = &st_gyro_attribute_group,
330 .read_raw = &st_gyro_read_raw,
331 .write_raw = &st_gyro_write_raw,
332 .debugfs_reg_access = &st_sensors_debugfs_reg_access,
335 #ifdef CONFIG_IIO_TRIGGER
336 static const struct iio_trigger_ops st_gyro_trigger_ops = {
337 .owner = THIS_MODULE,
338 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE,
339 .validate_device = st_sensors_validate_device,
341 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops)
342 #else
343 #define ST_GYRO_TRIGGER_OPS NULL
344 #endif
346 int st_gyro_common_probe(struct iio_dev *indio_dev)
348 struct st_sensor_data *gdata = iio_priv(indio_dev);
349 int irq = gdata->get_irq_data_ready(indio_dev);
350 int err;
352 indio_dev->modes = INDIO_DIRECT_MODE;
353 indio_dev->info = &gyro_info;
354 mutex_init(&gdata->tb.buf_lock);
356 err = st_sensors_power_enable(indio_dev);
357 if (err)
358 return err;
360 err = st_sensors_check_device_support(indio_dev,
361 ARRAY_SIZE(st_gyro_sensors_settings),
362 st_gyro_sensors_settings);
363 if (err < 0)
364 goto st_gyro_power_off;
366 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS;
367 gdata->multiread_bit = gdata->sensor_settings->multi_read_bit;
368 indio_dev->channels = gdata->sensor_settings->ch;
369 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
371 gdata->current_fullscale = (struct st_sensor_fullscale_avl *)
372 &gdata->sensor_settings->fs.fs_avl[0];
373 gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz;
375 err = st_sensors_init_sensor(indio_dev,
376 (struct st_sensors_platform_data *)&gyro_pdata);
377 if (err < 0)
378 goto st_gyro_power_off;
380 err = st_gyro_allocate_ring(indio_dev);
381 if (err < 0)
382 goto st_gyro_power_off;
384 if (irq > 0) {
385 err = st_sensors_allocate_trigger(indio_dev,
386 ST_GYRO_TRIGGER_OPS);
387 if (err < 0)
388 goto st_gyro_probe_trigger_error;
391 err = iio_device_register(indio_dev);
392 if (err)
393 goto st_gyro_device_register_error;
395 dev_info(&indio_dev->dev, "registered gyroscope %s\n",
396 indio_dev->name);
398 return 0;
400 st_gyro_device_register_error:
401 if (irq > 0)
402 st_sensors_deallocate_trigger(indio_dev);
403 st_gyro_probe_trigger_error:
404 st_gyro_deallocate_ring(indio_dev);
405 st_gyro_power_off:
406 st_sensors_power_disable(indio_dev);
408 return err;
410 EXPORT_SYMBOL(st_gyro_common_probe);
412 void st_gyro_common_remove(struct iio_dev *indio_dev)
414 struct st_sensor_data *gdata = iio_priv(indio_dev);
416 st_sensors_power_disable(indio_dev);
418 iio_device_unregister(indio_dev);
419 if (gdata->get_irq_data_ready(indio_dev) > 0)
420 st_sensors_deallocate_trigger(indio_dev);
422 st_gyro_deallocate_ring(indio_dev);
424 EXPORT_SYMBOL(st_gyro_common_remove);
426 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
427 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
428 MODULE_LICENSE("GPL v2");