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