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 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 135 .sensors_supported = { 136 [0] = L3G4200D_GYRO_DEV_NAME, 137 [1] = LSM330DL_GYRO_DEV_NAME, 138 }, 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, }, 148 }, 149 }, 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, 155 }, 156 .enable_axis = { 157 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 158 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 159 }, 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, 168 }, 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, 173 }, 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, 178 }, 179 }, 180 }, 181 .bdu = { 182 .addr = ST_GYRO_1_BDU_ADDR, 183 .mask = ST_GYRO_1_BDU_MASK, 184 }, 185 .drdy_irq = { 186 .addr = ST_GYRO_1_DRDY_IRQ_ADDR, 187 .mask_int2 = ST_GYRO_1_DRDY_IRQ_INT2_MASK, 188 /* 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. 192 */ 193 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 194 }, 195 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT, 196 .bootime = 2, 197 }, 198 { 199 .wai = ST_GYRO_2_WAI_EXP, 200 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 201 .sensors_supported = { 202 [0] = L3GD20_GYRO_DEV_NAME, 203 [1] = LSM330D_GYRO_DEV_NAME, 204 [2] = LSM330DLC_GYRO_DEV_NAME, 205 [3] = L3G4IS_GYRO_DEV_NAME, 206 [4] = LSM330_GYRO_DEV_NAME, 207 [5] = LSM9DS0_GYRO_DEV_NAME, 208 }, 209 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 210 .odr = { 211 .addr = ST_GYRO_2_ODR_ADDR, 212 .mask = ST_GYRO_2_ODR_MASK, 213 .odr_avl = { 214 { 95, ST_GYRO_2_ODR_AVL_95HZ_VAL, }, 215 { 190, ST_GYRO_2_ODR_AVL_190HZ_VAL, }, 216 { 380, ST_GYRO_2_ODR_AVL_380HZ_VAL, }, 217 { 760, ST_GYRO_2_ODR_AVL_760HZ_VAL, }, 218 }, 219 }, 220 .pw = { 221 .addr = ST_GYRO_2_PW_ADDR, 222 .mask = ST_GYRO_2_PW_MASK, 223 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 224 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 225 }, 226 .enable_axis = { 227 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 228 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 229 }, 230 .fs = { 231 .addr = ST_GYRO_2_FS_ADDR, 232 .mask = ST_GYRO_2_FS_MASK, 233 .fs_avl = { 234 [0] = { 235 .num = ST_GYRO_FS_AVL_250DPS, 236 .value = ST_GYRO_2_FS_AVL_250_VAL, 237 .gain = ST_GYRO_2_FS_AVL_250_GAIN, 238 }, 239 [1] = { 240 .num = ST_GYRO_FS_AVL_500DPS, 241 .value = ST_GYRO_2_FS_AVL_500_VAL, 242 .gain = ST_GYRO_2_FS_AVL_500_GAIN, 243 }, 244 [2] = { 245 .num = ST_GYRO_FS_AVL_2000DPS, 246 .value = ST_GYRO_2_FS_AVL_2000_VAL, 247 .gain = ST_GYRO_2_FS_AVL_2000_GAIN, 248 }, 249 }, 250 }, 251 .bdu = { 252 .addr = ST_GYRO_2_BDU_ADDR, 253 .mask = ST_GYRO_2_BDU_MASK, 254 }, 255 .drdy_irq = { 256 .addr = ST_GYRO_2_DRDY_IRQ_ADDR, 257 .mask_int2 = ST_GYRO_2_DRDY_IRQ_INT2_MASK, 258 /* 259 * The sensor has IHL (active low) and open 260 * drain settings, but only for INT1 and not 261 * for the DRDY line on INT2. 262 */ 263 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 264 }, 265 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, 266 .bootime = 2, 267 }, 268 { 269 .wai = ST_GYRO_3_WAI_EXP, 270 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 271 .sensors_supported = { 272 [0] = L3GD20_GYRO_DEV_NAME, 273 }, 274 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 275 .odr = { 276 .addr = ST_GYRO_3_ODR_ADDR, 277 .mask = ST_GYRO_3_ODR_MASK, 278 .odr_avl = { 279 { 95, ST_GYRO_3_ODR_AVL_95HZ_VAL, }, 280 { 190, ST_GYRO_3_ODR_AVL_190HZ_VAL, }, 281 { 380, ST_GYRO_3_ODR_AVL_380HZ_VAL, }, 282 { 760, ST_GYRO_3_ODR_AVL_760HZ_VAL, }, 283 }, 284 }, 285 .pw = { 286 .addr = ST_GYRO_3_PW_ADDR, 287 .mask = ST_GYRO_3_PW_MASK, 288 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 289 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 290 }, 291 .enable_axis = { 292 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 293 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 294 }, 295 .fs = { 296 .addr = ST_GYRO_3_FS_ADDR, 297 .mask = ST_GYRO_3_FS_MASK, 298 .fs_avl = { 299 [0] = { 300 .num = ST_GYRO_FS_AVL_250DPS, 301 .value = ST_GYRO_3_FS_AVL_250_VAL, 302 .gain = ST_GYRO_3_FS_AVL_250_GAIN, 303 }, 304 [1] = { 305 .num = ST_GYRO_FS_AVL_500DPS, 306 .value = ST_GYRO_3_FS_AVL_500_VAL, 307 .gain = ST_GYRO_3_FS_AVL_500_GAIN, 308 }, 309 [2] = { 310 .num = ST_GYRO_FS_AVL_2000DPS, 311 .value = ST_GYRO_3_FS_AVL_2000_VAL, 312 .gain = ST_GYRO_3_FS_AVL_2000_GAIN, 313 }, 314 }, 315 }, 316 .bdu = { 317 .addr = ST_GYRO_3_BDU_ADDR, 318 .mask = ST_GYRO_3_BDU_MASK, 319 }, 320 .drdy_irq = { 321 .addr = ST_GYRO_3_DRDY_IRQ_ADDR, 322 .mask_int2 = ST_GYRO_3_DRDY_IRQ_INT2_MASK, 323 /* 324 * The sensor has IHL (active low) and open 325 * drain settings, but only for INT1 and not 326 * for the DRDY line on INT2. 327 */ 328 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, 329 }, 330 .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT, 331 .bootime = 2, 332 }, 333 }; 334 335 static int st_gyro_read_raw(struct iio_dev *indio_dev, 336 struct iio_chan_spec const *ch, int *val, 337 int *val2, long mask) 338 { 339 int err; 340 struct st_sensor_data *gdata = iio_priv(indio_dev); 341 342 switch (mask) { 343 case IIO_CHAN_INFO_RAW: 344 err = st_sensors_read_info_raw(indio_dev, ch, val); 345 if (err < 0) 346 goto read_error; 347 348 return IIO_VAL_INT; 349 case IIO_CHAN_INFO_SCALE: 350 *val = 0; 351 *val2 = gdata->current_fullscale->gain; 352 return IIO_VAL_INT_PLUS_MICRO; 353 case IIO_CHAN_INFO_SAMP_FREQ: 354 *val = gdata->odr; 355 return IIO_VAL_INT; 356 default: 357 return -EINVAL; 358 } 359 360 read_error: 361 return err; 362 } 363 364 static int st_gyro_write_raw(struct iio_dev *indio_dev, 365 struct iio_chan_spec const *chan, int val, int val2, long mask) 366 { 367 int err; 368 369 switch (mask) { 370 case IIO_CHAN_INFO_SCALE: 371 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 372 break; 373 case IIO_CHAN_INFO_SAMP_FREQ: 374 if (val2) 375 return -EINVAL; 376 mutex_lock(&indio_dev->mlock); 377 err = st_sensors_set_odr(indio_dev, val); 378 mutex_unlock(&indio_dev->mlock); 379 return err; 380 default: 381 err = -EINVAL; 382 } 383 384 return err; 385 } 386 387 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 388 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available); 389 390 static struct attribute *st_gyro_attributes[] = { 391 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 392 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr, 393 NULL, 394 }; 395 396 static const struct attribute_group st_gyro_attribute_group = { 397 .attrs = st_gyro_attributes, 398 }; 399 400 static const struct iio_info gyro_info = { 401 .driver_module = THIS_MODULE, 402 .attrs = &st_gyro_attribute_group, 403 .read_raw = &st_gyro_read_raw, 404 .write_raw = &st_gyro_write_raw, 405 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 406 }; 407 408 #ifdef CONFIG_IIO_TRIGGER 409 static const struct iio_trigger_ops st_gyro_trigger_ops = { 410 .owner = THIS_MODULE, 411 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE, 412 }; 413 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops) 414 #else 415 #define ST_GYRO_TRIGGER_OPS NULL 416 #endif 417 418 int st_gyro_common_probe(struct iio_dev *indio_dev) 419 { 420 struct st_sensor_data *gdata = iio_priv(indio_dev); 421 int irq = gdata->get_irq_data_ready(indio_dev); 422 int err; 423 424 indio_dev->modes = INDIO_DIRECT_MODE; 425 indio_dev->info = &gyro_info; 426 mutex_init(&gdata->tb.buf_lock); 427 428 st_sensors_power_enable(indio_dev); 429 430 err = st_sensors_check_device_support(indio_dev, 431 ARRAY_SIZE(st_gyro_sensors_settings), 432 st_gyro_sensors_settings); 433 if (err < 0) 434 return err; 435 436 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS; 437 gdata->multiread_bit = gdata->sensor_settings->multi_read_bit; 438 indio_dev->channels = gdata->sensor_settings->ch; 439 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 440 441 gdata->current_fullscale = (struct st_sensor_fullscale_avl *) 442 &gdata->sensor_settings->fs.fs_avl[0]; 443 gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz; 444 445 err = st_sensors_init_sensor(indio_dev, 446 (struct st_sensors_platform_data *)&gyro_pdata); 447 if (err < 0) 448 return err; 449 450 err = st_gyro_allocate_ring(indio_dev); 451 if (err < 0) 452 return err; 453 454 if (irq > 0) { 455 err = st_sensors_allocate_trigger(indio_dev, 456 ST_GYRO_TRIGGER_OPS); 457 if (err < 0) 458 goto st_gyro_probe_trigger_error; 459 } 460 461 err = iio_device_register(indio_dev); 462 if (err) 463 goto st_gyro_device_register_error; 464 465 dev_info(&indio_dev->dev, "registered gyroscope %s\n", 466 indio_dev->name); 467 468 return 0; 469 470 st_gyro_device_register_error: 471 if (irq > 0) 472 st_sensors_deallocate_trigger(indio_dev); 473 st_gyro_probe_trigger_error: 474 st_gyro_deallocate_ring(indio_dev); 475 476 return err; 477 } 478 EXPORT_SYMBOL(st_gyro_common_probe); 479 480 void st_gyro_common_remove(struct iio_dev *indio_dev) 481 { 482 struct st_sensor_data *gdata = iio_priv(indio_dev); 483 484 st_sensors_power_disable(indio_dev); 485 486 iio_device_unregister(indio_dev); 487 if (gdata->get_irq_data_ready(indio_dev) > 0) 488 st_sensors_deallocate_trigger(indio_dev); 489 490 st_gyro_deallocate_ring(indio_dev); 491 } 492 EXPORT_SYMBOL(st_gyro_common_remove); 493 494 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 495 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver"); 496 MODULE_LICENSE("GPL v2"); 497