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