1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * STMicroelectronics gyroscopes driver 4 * 5 * Copyright 2012-2013 STMicroelectronics Inc. 6 * 7 * Denis Ciocca <denis.ciocca@st.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/errno.h> 14 #include <linux/types.h> 15 #include <linux/interrupt.h> 16 #include <linux/i2c.h> 17 #include <linux/irq.h> 18 #include <linux/delay.h> 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 #include <linux/iio/trigger.h> 22 #include <linux/iio/buffer.h> 23 24 #include <linux/iio/common/st_sensors.h> 25 #include "st_gyro.h" 26 27 #define ST_GYRO_NUMBER_DATA_CHANNELS 3 28 29 /* DEFAULT VALUE FOR SENSORS */ 30 #define ST_GYRO_DEFAULT_OUT_X_L_ADDR 0x28 31 #define ST_GYRO_DEFAULT_OUT_Y_L_ADDR 0x2a 32 #define ST_GYRO_DEFAULT_OUT_Z_L_ADDR 0x2c 33 34 /* FULLSCALE */ 35 #define ST_GYRO_FS_AVL_245DPS 245 36 #define ST_GYRO_FS_AVL_250DPS 250 37 #define ST_GYRO_FS_AVL_500DPS 500 38 #define ST_GYRO_FS_AVL_2000DPS 2000 39 40 static const struct iio_mount_matrix * 41 st_gyro_get_mount_matrix(const struct iio_dev *indio_dev, 42 const struct iio_chan_spec *chan) 43 { 44 struct st_sensor_data *gdata = iio_priv(indio_dev); 45 46 return &gdata->mount_matrix; 47 } 48 49 static const struct iio_chan_spec_ext_info st_gyro_mount_matrix_ext_info[] = { 50 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_gyro_get_mount_matrix), 51 { } 52 }; 53 54 static const struct iio_chan_spec st_gyro_16bit_channels[] = { 55 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ANGL_VEL, 56 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 57 ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16, 58 ST_GYRO_DEFAULT_OUT_X_L_ADDR, 59 st_gyro_mount_matrix_ext_info), 60 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ANGL_VEL, 61 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 62 ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16, 63 ST_GYRO_DEFAULT_OUT_Y_L_ADDR, 64 st_gyro_mount_matrix_ext_info), 65 ST_SENSORS_LSM_CHANNELS_EXT(IIO_ANGL_VEL, 66 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 67 ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16, 68 ST_GYRO_DEFAULT_OUT_Z_L_ADDR, 69 st_gyro_mount_matrix_ext_info), 70 IIO_CHAN_SOFT_TIMESTAMP(3) 71 }; 72 73 static const struct st_sensor_settings st_gyro_sensors_settings[] = { 74 { 75 .wai = 0xd3, 76 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 77 .sensors_supported = { 78 [0] = L3G4200D_GYRO_DEV_NAME, 79 [1] = LSM330DL_GYRO_DEV_NAME, 80 }, 81 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 82 .odr = { 83 .addr = 0x20, 84 .mask = 0xc0, 85 .odr_avl = { 86 { .hz = 100, .value = 0x00, }, 87 { .hz = 200, .value = 0x01, }, 88 { .hz = 400, .value = 0x02, }, 89 { .hz = 800, .value = 0x03, }, 90 }, 91 }, 92 .pw = { 93 .addr = 0x20, 94 .mask = 0x08, 95 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 96 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 97 }, 98 .enable_axis = { 99 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 100 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 101 }, 102 .fs = { 103 .addr = 0x23, 104 .mask = 0x30, 105 .fs_avl = { 106 [0] = { 107 .num = ST_GYRO_FS_AVL_250DPS, 108 .value = 0x00, 109 .gain = IIO_DEGREE_TO_RAD(8750), 110 }, 111 [1] = { 112 .num = ST_GYRO_FS_AVL_500DPS, 113 .value = 0x01, 114 .gain = IIO_DEGREE_TO_RAD(17500), 115 }, 116 [2] = { 117 .num = ST_GYRO_FS_AVL_2000DPS, 118 .value = 0x02, 119 .gain = IIO_DEGREE_TO_RAD(70000), 120 }, 121 }, 122 }, 123 .bdu = { 124 .addr = 0x23, 125 .mask = 0x80, 126 }, 127 .drdy_irq = { 128 .int2 = { 129 .addr = 0x22, 130 .mask = 0x08, 131 }, 132 /* 133 * The sensor has IHL (active low) and open 134 * drain settings, but only for INT1 and not 135 * for the DRDY line on INT2. 136 */ 137 .stat_drdy = { 138 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 139 .mask = 0x07, 140 }, 141 }, 142 .sim = { 143 .addr = 0x23, 144 .value = BIT(0), 145 }, 146 .multi_read_bit = true, 147 .bootime = 2, 148 }, 149 { 150 .wai = 0xd4, 151 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 152 .sensors_supported = { 153 [0] = L3GD20_GYRO_DEV_NAME, 154 [1] = LSM330D_GYRO_DEV_NAME, 155 [2] = LSM330DLC_GYRO_DEV_NAME, 156 [3] = L3G4IS_GYRO_DEV_NAME, 157 [4] = LSM330_GYRO_DEV_NAME, 158 }, 159 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 160 .odr = { 161 .addr = 0x20, 162 .mask = 0xc0, 163 .odr_avl = { 164 { .hz = 95, .value = 0x00, }, 165 { .hz = 190, .value = 0x01, }, 166 { .hz = 380, .value = 0x02, }, 167 { .hz = 760, .value = 0x03, }, 168 }, 169 }, 170 .pw = { 171 .addr = 0x20, 172 .mask = 0x08, 173 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 174 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 175 }, 176 .enable_axis = { 177 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 178 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 179 }, 180 .fs = { 181 .addr = 0x23, 182 .mask = 0x30, 183 .fs_avl = { 184 [0] = { 185 .num = ST_GYRO_FS_AVL_250DPS, 186 .value = 0x00, 187 .gain = IIO_DEGREE_TO_RAD(8750), 188 }, 189 [1] = { 190 .num = ST_GYRO_FS_AVL_500DPS, 191 .value = 0x01, 192 .gain = IIO_DEGREE_TO_RAD(17500), 193 }, 194 [2] = { 195 .num = ST_GYRO_FS_AVL_2000DPS, 196 .value = 0x02, 197 .gain = IIO_DEGREE_TO_RAD(70000), 198 }, 199 }, 200 }, 201 .bdu = { 202 .addr = 0x23, 203 .mask = 0x80, 204 }, 205 .drdy_irq = { 206 .int2 = { 207 .addr = 0x22, 208 .mask = 0x08, 209 }, 210 /* 211 * The sensor has IHL (active low) and open 212 * drain settings, but only for INT1 and not 213 * for the DRDY line on INT2. 214 */ 215 .stat_drdy = { 216 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 217 .mask = 0x07, 218 }, 219 }, 220 .sim = { 221 .addr = 0x23, 222 .value = BIT(0), 223 }, 224 .multi_read_bit = true, 225 .bootime = 2, 226 }, 227 { 228 .wai = 0xd4, 229 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 230 .sensors_supported = { 231 [0] = LSM9DS0_GYRO_DEV_NAME, 232 }, 233 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 234 .odr = { 235 .addr = 0x20, 236 .mask = GENMASK(7, 6), 237 .odr_avl = { 238 { .hz = 95, .value = 0x00, }, 239 { .hz = 190, .value = 0x01, }, 240 { .hz = 380, .value = 0x02, }, 241 { .hz = 760, .value = 0x03, }, 242 }, 243 }, 244 .pw = { 245 .addr = 0x20, 246 .mask = BIT(3), 247 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 248 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 249 }, 250 .enable_axis = { 251 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 252 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 253 }, 254 .fs = { 255 .addr = 0x23, 256 .mask = GENMASK(5, 4), 257 .fs_avl = { 258 [0] = { 259 .num = ST_GYRO_FS_AVL_245DPS, 260 .value = 0x00, 261 .gain = IIO_DEGREE_TO_RAD(8750), 262 }, 263 [1] = { 264 .num = ST_GYRO_FS_AVL_500DPS, 265 .value = 0x01, 266 .gain = IIO_DEGREE_TO_RAD(17500), 267 }, 268 [2] = { 269 .num = ST_GYRO_FS_AVL_2000DPS, 270 .value = 0x02, 271 .gain = IIO_DEGREE_TO_RAD(70000), 272 }, 273 }, 274 }, 275 .bdu = { 276 .addr = 0x23, 277 .mask = BIT(7), 278 }, 279 .drdy_irq = { 280 .int2 = { 281 .addr = 0x22, 282 .mask = BIT(3), 283 }, 284 /* 285 * The sensor has IHL (active low) and open 286 * drain settings, but only for INT1 and not 287 * for the DRDY line on INT2. 288 */ 289 .stat_drdy = { 290 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 291 .mask = GENMASK(2, 0), 292 }, 293 }, 294 .sim = { 295 .addr = 0x23, 296 .value = BIT(0), 297 }, 298 .multi_read_bit = true, 299 .bootime = 2, 300 }, 301 { 302 .wai = 0xd7, 303 .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS, 304 .sensors_supported = { 305 [0] = L3GD20H_GYRO_DEV_NAME, 306 }, 307 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, 308 .odr = { 309 .addr = 0x20, 310 .mask = 0xc0, 311 .odr_avl = { 312 { .hz = 100, .value = 0x00, }, 313 { .hz = 200, .value = 0x01, }, 314 { .hz = 400, .value = 0x02, }, 315 { .hz = 800, .value = 0x03, }, 316 }, 317 }, 318 .pw = { 319 .addr = 0x20, 320 .mask = 0x08, 321 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE, 322 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, 323 }, 324 .enable_axis = { 325 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR, 326 .mask = ST_SENSORS_DEFAULT_AXIS_MASK, 327 }, 328 .fs = { 329 .addr = 0x23, 330 .mask = 0x30, 331 .fs_avl = { 332 [0] = { 333 .num = ST_GYRO_FS_AVL_245DPS, 334 .value = 0x00, 335 .gain = IIO_DEGREE_TO_RAD(8750), 336 }, 337 [1] = { 338 .num = ST_GYRO_FS_AVL_500DPS, 339 .value = 0x01, 340 .gain = IIO_DEGREE_TO_RAD(17500), 341 }, 342 [2] = { 343 .num = ST_GYRO_FS_AVL_2000DPS, 344 .value = 0x02, 345 .gain = IIO_DEGREE_TO_RAD(70000), 346 }, 347 }, 348 }, 349 .bdu = { 350 .addr = 0x23, 351 .mask = 0x80, 352 }, 353 .drdy_irq = { 354 .int2 = { 355 .addr = 0x22, 356 .mask = 0x08, 357 }, 358 /* 359 * The sensor has IHL (active low) and open 360 * drain settings, but only for INT1 and not 361 * for the DRDY line on INT2. 362 */ 363 .stat_drdy = { 364 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 365 .mask = 0x07, 366 }, 367 }, 368 .sim = { 369 .addr = 0x23, 370 .value = BIT(0), 371 }, 372 .multi_read_bit = true, 373 .bootime = 2, 374 }, 375 }; 376 377 /* DRDY on gyros is available only on INT2 pin */ 378 static const struct st_sensors_platform_data gyro_pdata = { 379 .drdy_int_pin = 2, 380 }; 381 382 static int st_gyro_read_raw(struct iio_dev *indio_dev, 383 struct iio_chan_spec const *ch, int *val, 384 int *val2, long mask) 385 { 386 int err; 387 struct st_sensor_data *gdata = iio_priv(indio_dev); 388 389 switch (mask) { 390 case IIO_CHAN_INFO_RAW: 391 err = st_sensors_read_info_raw(indio_dev, ch, val); 392 if (err < 0) 393 goto read_error; 394 395 return IIO_VAL_INT; 396 case IIO_CHAN_INFO_SCALE: 397 *val = 0; 398 *val2 = gdata->current_fullscale->gain; 399 return IIO_VAL_INT_PLUS_MICRO; 400 case IIO_CHAN_INFO_SAMP_FREQ: 401 *val = gdata->odr; 402 return IIO_VAL_INT; 403 default: 404 return -EINVAL; 405 } 406 407 read_error: 408 return err; 409 } 410 411 static int st_gyro_write_raw(struct iio_dev *indio_dev, 412 struct iio_chan_spec const *chan, int val, int val2, long mask) 413 { 414 int err; 415 416 switch (mask) { 417 case IIO_CHAN_INFO_SCALE: 418 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 419 break; 420 case IIO_CHAN_INFO_SAMP_FREQ: 421 if (val2) 422 return -EINVAL; 423 mutex_lock(&indio_dev->mlock); 424 err = st_sensors_set_odr(indio_dev, val); 425 mutex_unlock(&indio_dev->mlock); 426 return err; 427 default: 428 err = -EINVAL; 429 } 430 431 return err; 432 } 433 434 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 435 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available); 436 437 static struct attribute *st_gyro_attributes[] = { 438 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 439 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr, 440 NULL, 441 }; 442 443 static const struct attribute_group st_gyro_attribute_group = { 444 .attrs = st_gyro_attributes, 445 }; 446 447 static const struct iio_info gyro_info = { 448 .attrs = &st_gyro_attribute_group, 449 .read_raw = &st_gyro_read_raw, 450 .write_raw = &st_gyro_write_raw, 451 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 452 }; 453 454 #ifdef CONFIG_IIO_TRIGGER 455 static const struct iio_trigger_ops st_gyro_trigger_ops = { 456 .set_trigger_state = ST_GYRO_TRIGGER_SET_STATE, 457 .validate_device = st_sensors_validate_device, 458 }; 459 #define ST_GYRO_TRIGGER_OPS (&st_gyro_trigger_ops) 460 #else 461 #define ST_GYRO_TRIGGER_OPS NULL 462 #endif 463 464 /* 465 * st_gyro_get_settings() - get sensor settings from device name 466 * @name: device name buffer reference. 467 * 468 * Return: valid reference on success, NULL otherwise. 469 */ 470 const struct st_sensor_settings *st_gyro_get_settings(const char *name) 471 { 472 int index = st_sensors_get_settings_index(name, 473 st_gyro_sensors_settings, 474 ARRAY_SIZE(st_gyro_sensors_settings)); 475 if (index < 0) 476 return NULL; 477 478 return &st_gyro_sensors_settings[index]; 479 } 480 EXPORT_SYMBOL(st_gyro_get_settings); 481 482 int st_gyro_common_probe(struct iio_dev *indio_dev) 483 { 484 struct st_sensor_data *gdata = iio_priv(indio_dev); 485 struct st_sensors_platform_data *pdata; 486 int err; 487 488 indio_dev->modes = INDIO_DIRECT_MODE; 489 indio_dev->info = &gyro_info; 490 491 err = st_sensors_verify_id(indio_dev); 492 if (err < 0) 493 return err; 494 495 gdata->num_data_channels = ST_GYRO_NUMBER_DATA_CHANNELS; 496 indio_dev->channels = gdata->sensor_settings->ch; 497 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 498 499 err = iio_read_mount_matrix(gdata->dev, &gdata->mount_matrix); 500 if (err) 501 return err; 502 503 gdata->current_fullscale = &gdata->sensor_settings->fs.fs_avl[0]; 504 gdata->odr = gdata->sensor_settings->odr.odr_avl[0].hz; 505 506 pdata = (struct st_sensors_platform_data *)&gyro_pdata; 507 508 err = st_sensors_init_sensor(indio_dev, pdata); 509 if (err < 0) 510 return err; 511 512 err = st_gyro_allocate_ring(indio_dev); 513 if (err < 0) 514 return err; 515 516 if (gdata->irq > 0) { 517 err = st_sensors_allocate_trigger(indio_dev, 518 ST_GYRO_TRIGGER_OPS); 519 if (err < 0) 520 goto st_gyro_probe_trigger_error; 521 } 522 523 err = iio_device_register(indio_dev); 524 if (err) 525 goto st_gyro_device_register_error; 526 527 dev_info(&indio_dev->dev, "registered gyroscope %s\n", 528 indio_dev->name); 529 530 return 0; 531 532 st_gyro_device_register_error: 533 if (gdata->irq > 0) 534 st_sensors_deallocate_trigger(indio_dev); 535 st_gyro_probe_trigger_error: 536 st_gyro_deallocate_ring(indio_dev); 537 return err; 538 } 539 EXPORT_SYMBOL(st_gyro_common_probe); 540 541 void st_gyro_common_remove(struct iio_dev *indio_dev) 542 { 543 struct st_sensor_data *gdata = iio_priv(indio_dev); 544 545 iio_device_unregister(indio_dev); 546 if (gdata->irq > 0) 547 st_sensors_deallocate_trigger(indio_dev); 548 549 st_gyro_deallocate_ring(indio_dev); 550 } 551 EXPORT_SYMBOL(st_gyro_common_remove); 552 553 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 554 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver"); 555 MODULE_LICENSE("GPL v2"); 556