1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2021 Analog Devices, Inc. 4 * Author: Cosmin Tanislav <cosmin.tanislav@analog.com> 5 */ 6 7 #include <linux/bitfield.h> 8 #include <linux/bitops.h> 9 #include <linux/iio/buffer.h> 10 #include <linux/iio/events.h> 11 #include <linux/iio/iio.h> 12 #include <linux/iio/kfifo_buf.h> 13 #include <linux/iio/sysfs.h> 14 #include <linux/interrupt.h> 15 #include <linux/irq.h> 16 #include <linux/property.h> 17 #include <linux/regmap.h> 18 #include <linux/regulator/consumer.h> 19 #include <linux/unaligned.h> 20 21 #include "adxl367.h" 22 23 #define ADXL367_REG_DEVID 0x00 24 #define ADXL367_DEVID_AD 0xAD 25 26 #define ADXL367_REG_STATUS 0x0B 27 #define ADXL367_STATUS_INACT_MASK BIT(5) 28 #define ADXL367_STATUS_ACT_MASK BIT(4) 29 #define ADXL367_STATUS_FIFO_FULL_MASK BIT(2) 30 31 #define ADXL367_FIFO_ENT_H_MASK GENMASK(1, 0) 32 33 #define ADXL367_REG_X_DATA_H 0x0E 34 #define ADXL367_REG_Y_DATA_H 0x10 35 #define ADXL367_REG_Z_DATA_H 0x12 36 #define ADXL367_REG_TEMP_DATA_H 0x14 37 #define ADXL367_REG_EX_ADC_DATA_H 0x16 38 #define ADXL367_DATA_MASK GENMASK(15, 2) 39 40 #define ADXL367_TEMP_25C 165 41 #define ADXL367_TEMP_PER_C 54 42 43 #define ADXL367_VOLTAGE_OFFSET 8192 44 #define ADXL367_VOLTAGE_MAX_MV 1000 45 #define ADXL367_VOLTAGE_MAX_RAW GENMASK(13, 0) 46 47 #define ADXL367_REG_RESET 0x1F 48 #define ADXL367_RESET_CODE 0x52 49 50 #define ADXL367_REG_THRESH_ACT_H 0x20 51 #define ADXL367_REG_THRESH_INACT_H 0x23 52 #define ADXL367_THRESH_MAX GENMASK(12, 0) 53 #define ADXL367_THRESH_VAL_H_MASK GENMASK(12, 6) 54 #define ADXL367_THRESH_H_MASK GENMASK(6, 0) 55 #define ADXL367_THRESH_VAL_L_MASK GENMASK(5, 0) 56 #define ADXL367_THRESH_L_MASK GENMASK(7, 2) 57 58 #define ADXL367_REG_TIME_ACT 0x22 59 #define ADXL367_REG_TIME_INACT_H 0x25 60 #define ADXL367_TIME_ACT_MAX GENMASK(7, 0) 61 #define ADXL367_TIME_INACT_MAX GENMASK(15, 0) 62 #define ADXL367_TIME_INACT_VAL_H_MASK GENMASK(15, 8) 63 #define ADXL367_TIME_INACT_H_MASK GENMASK(7, 0) 64 #define ADXL367_TIME_INACT_VAL_L_MASK GENMASK(7, 0) 65 #define ADXL367_TIME_INACT_L_MASK GENMASK(7, 0) 66 67 #define ADXL367_REG_ACT_INACT_CTL 0x27 68 #define ADXL367_ACT_EN_MASK GENMASK(1, 0) 69 #define ADXL367_ACT_LINKLOOP_MASK GENMASK(5, 4) 70 71 #define ADXL367_REG_FIFO_CTL 0x28 72 #define ADXL367_FIFO_CTL_FORMAT_MASK GENMASK(6, 3) 73 #define ADXL367_FIFO_CTL_MODE_MASK GENMASK(1, 0) 74 75 #define ADXL367_REG_FIFO_SAMPLES 0x29 76 #define ADXL367_FIFO_SIZE 512 77 #define ADXL367_FIFO_MAX_WATERMARK 511 78 79 #define ADXL367_SAMPLES_VAL_H_MASK BIT(8) 80 #define ADXL367_SAMPLES_H_MASK BIT(2) 81 #define ADXL367_SAMPLES_VAL_L_MASK GENMASK(7, 0) 82 #define ADXL367_SAMPLES_L_MASK GENMASK(7, 0) 83 84 #define ADXL367_REG_INT1_MAP 0x2A 85 #define ADXL367_REG_INT2_MAP 0x2B 86 #define ADXL367_INT_INACT_MASK BIT(5) 87 #define ADXL367_INT_ACT_MASK BIT(4) 88 #define ADXL367_INT_FIFO_WATERMARK_MASK BIT(2) 89 90 #define ADXL367_REG_FILTER_CTL 0x2C 91 #define ADXL367_FILTER_CTL_RANGE_MASK GENMASK(7, 6) 92 #define ADXL367_2G_RANGE_1G 4095 93 #define ADXL367_2G_RANGE_100MG 409 94 #define ADXL367_FILTER_CTL_ODR_MASK GENMASK(2, 0) 95 96 #define ADXL367_REG_POWER_CTL 0x2D 97 #define ADXL367_POWER_CTL_MODE_MASK GENMASK(1, 0) 98 99 #define ADXL367_REG_ADC_CTL 0x3C 100 #define ADXL367_REG_TEMP_CTL 0x3D 101 #define ADXL367_ADC_EN_MASK BIT(0) 102 103 enum adxl367_range { 104 ADXL367_2G_RANGE, 105 ADXL367_4G_RANGE, 106 ADXL367_8G_RANGE, 107 }; 108 109 enum adxl367_fifo_mode { 110 ADXL367_FIFO_MODE_DISABLED = 0b00, 111 ADXL367_FIFO_MODE_STREAM = 0b10, 112 }; 113 114 enum adxl367_fifo_format { 115 ADXL367_FIFO_FORMAT_XYZ, 116 ADXL367_FIFO_FORMAT_X, 117 ADXL367_FIFO_FORMAT_Y, 118 ADXL367_FIFO_FORMAT_Z, 119 ADXL367_FIFO_FORMAT_XYZT, 120 ADXL367_FIFO_FORMAT_XT, 121 ADXL367_FIFO_FORMAT_YT, 122 ADXL367_FIFO_FORMAT_ZT, 123 ADXL367_FIFO_FORMAT_XYZA, 124 ADXL367_FIFO_FORMAT_XA, 125 ADXL367_FIFO_FORMAT_YA, 126 ADXL367_FIFO_FORMAT_ZA, 127 }; 128 129 enum adxl367_op_mode { 130 ADXL367_OP_STANDBY = 0b00, 131 ADXL367_OP_MEASURE = 0b10, 132 }; 133 134 enum adxl367_act_proc_mode { 135 ADXL367_LOOPED = 0b11, 136 }; 137 138 enum adxl367_act_en_mode { 139 ADXL367_ACT_DISABLED = 0b00, 140 ADCL367_ACT_REF_ENABLED = 0b11, 141 }; 142 143 enum adxl367_activity_type { 144 ADXL367_ACTIVITY, 145 ADXL367_INACTIVITY, 146 }; 147 148 enum adxl367_odr { 149 ADXL367_ODR_12P5HZ, 150 ADXL367_ODR_25HZ, 151 ADXL367_ODR_50HZ, 152 ADXL367_ODR_100HZ, 153 ADXL367_ODR_200HZ, 154 ADXL367_ODR_400HZ, 155 }; 156 157 struct adxl367_state { 158 const struct adxl367_ops *ops; 159 void *context; 160 161 struct device *dev; 162 struct regmap *regmap; 163 164 /* 165 * Synchronize access to members of driver state, and ensure atomicity 166 * of consecutive regmap operations. 167 */ 168 struct mutex lock; 169 170 enum adxl367_odr odr; 171 enum adxl367_range range; 172 173 u8 int_map_reg; 174 175 unsigned int act_threshold; 176 unsigned int act_time_ms; 177 unsigned int inact_threshold; 178 unsigned int inact_time_ms; 179 180 unsigned int fifo_set_size; 181 unsigned int fifo_watermark; 182 183 __be16 fifo_buf[ADXL367_FIFO_SIZE] __aligned(IIO_DMA_MINALIGN); 184 __be16 sample_buf; 185 u8 act_threshold_buf[2]; 186 u8 inact_time_buf[2]; 187 u8 status_buf[3]; 188 }; 189 190 static const unsigned int adxl367_threshold_h_reg_tbl[] = { 191 [ADXL367_ACTIVITY] = ADXL367_REG_THRESH_ACT_H, 192 [ADXL367_INACTIVITY] = ADXL367_REG_THRESH_INACT_H, 193 }; 194 195 static const unsigned int adxl367_act_en_shift_tbl[] = { 196 [ADXL367_ACTIVITY] = 0, 197 [ADXL367_INACTIVITY] = 2, 198 }; 199 200 static const unsigned int adxl367_act_int_mask_tbl[] = { 201 [ADXL367_ACTIVITY] = ADXL367_INT_ACT_MASK, 202 [ADXL367_INACTIVITY] = ADXL367_INT_INACT_MASK, 203 }; 204 205 static const int adxl367_samp_freq_tbl[][2] = { 206 [ADXL367_ODR_12P5HZ] = {12, 500000}, 207 [ADXL367_ODR_25HZ] = {25, 0}, 208 [ADXL367_ODR_50HZ] = {50, 0}, 209 [ADXL367_ODR_100HZ] = {100, 0}, 210 [ADXL367_ODR_200HZ] = {200, 0}, 211 [ADXL367_ODR_400HZ] = {400, 0}, 212 }; 213 214 /* (g * 2) * 9.80665 * 1000000 / (2^14 - 1) */ 215 static const int adxl367_range_scale_tbl[][2] = { 216 [ADXL367_2G_RANGE] = {0, 2394347}, 217 [ADXL367_4G_RANGE] = {0, 4788695}, 218 [ADXL367_8G_RANGE] = {0, 9577391}, 219 }; 220 221 static const int adxl367_range_scale_factor_tbl[] = { 222 [ADXL367_2G_RANGE] = 1, 223 [ADXL367_4G_RANGE] = 2, 224 [ADXL367_8G_RANGE] = 4, 225 }; 226 227 enum { 228 ADXL367_X_CHANNEL_INDEX, 229 ADXL367_Y_CHANNEL_INDEX, 230 ADXL367_Z_CHANNEL_INDEX, 231 ADXL367_TEMP_CHANNEL_INDEX, 232 ADXL367_EX_ADC_CHANNEL_INDEX 233 }; 234 235 #define ADXL367_X_CHANNEL_MASK BIT(ADXL367_X_CHANNEL_INDEX) 236 #define ADXL367_Y_CHANNEL_MASK BIT(ADXL367_Y_CHANNEL_INDEX) 237 #define ADXL367_Z_CHANNEL_MASK BIT(ADXL367_Z_CHANNEL_INDEX) 238 #define ADXL367_TEMP_CHANNEL_MASK BIT(ADXL367_TEMP_CHANNEL_INDEX) 239 #define ADXL367_EX_ADC_CHANNEL_MASK BIT(ADXL367_EX_ADC_CHANNEL_INDEX) 240 241 static const enum adxl367_fifo_format adxl367_fifo_formats[] = { 242 ADXL367_FIFO_FORMAT_X, 243 ADXL367_FIFO_FORMAT_Y, 244 ADXL367_FIFO_FORMAT_Z, 245 ADXL367_FIFO_FORMAT_XT, 246 ADXL367_FIFO_FORMAT_YT, 247 ADXL367_FIFO_FORMAT_ZT, 248 ADXL367_FIFO_FORMAT_XA, 249 ADXL367_FIFO_FORMAT_YA, 250 ADXL367_FIFO_FORMAT_ZA, 251 ADXL367_FIFO_FORMAT_XYZ, 252 ADXL367_FIFO_FORMAT_XYZT, 253 ADXL367_FIFO_FORMAT_XYZA, 254 }; 255 256 static const unsigned long adxl367_channel_masks[] = { 257 ADXL367_X_CHANNEL_MASK, 258 ADXL367_Y_CHANNEL_MASK, 259 ADXL367_Z_CHANNEL_MASK, 260 ADXL367_X_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK, 261 ADXL367_Y_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK, 262 ADXL367_Z_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK, 263 ADXL367_X_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK, 264 ADXL367_Y_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK, 265 ADXL367_Z_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK, 266 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK, 267 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK | 268 ADXL367_TEMP_CHANNEL_MASK, 269 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK | 270 ADXL367_EX_ADC_CHANNEL_MASK, 271 0, 272 }; 273 274 static int adxl367_set_measure_en(struct adxl367_state *st, bool en) 275 { 276 enum adxl367_op_mode op_mode = en ? ADXL367_OP_MEASURE 277 : ADXL367_OP_STANDBY; 278 int ret; 279 280 ret = regmap_update_bits(st->regmap, ADXL367_REG_POWER_CTL, 281 ADXL367_POWER_CTL_MODE_MASK, 282 FIELD_PREP(ADXL367_POWER_CTL_MODE_MASK, 283 op_mode)); 284 if (ret) 285 return ret; 286 287 /* 288 * Wait for acceleration output to settle after entering 289 * measure mode. 290 */ 291 if (en) 292 msleep(100); 293 294 return 0; 295 } 296 297 static void adxl367_scale_act_thresholds(struct adxl367_state *st, 298 enum adxl367_range old_range, 299 enum adxl367_range new_range) 300 { 301 st->act_threshold = st->act_threshold 302 * adxl367_range_scale_factor_tbl[old_range] 303 / adxl367_range_scale_factor_tbl[new_range]; 304 st->inact_threshold = st->inact_threshold 305 * adxl367_range_scale_factor_tbl[old_range] 306 / adxl367_range_scale_factor_tbl[new_range]; 307 } 308 309 static int _adxl367_set_act_threshold(struct adxl367_state *st, 310 enum adxl367_activity_type act, 311 unsigned int threshold) 312 { 313 u8 reg = adxl367_threshold_h_reg_tbl[act]; 314 int ret; 315 316 if (threshold > ADXL367_THRESH_MAX) 317 return -EINVAL; 318 319 st->act_threshold_buf[0] = FIELD_PREP(ADXL367_THRESH_H_MASK, 320 FIELD_GET(ADXL367_THRESH_VAL_H_MASK, 321 threshold)); 322 st->act_threshold_buf[1] = FIELD_PREP(ADXL367_THRESH_L_MASK, 323 FIELD_GET(ADXL367_THRESH_VAL_L_MASK, 324 threshold)); 325 326 ret = regmap_bulk_write(st->regmap, reg, st->act_threshold_buf, 327 sizeof(st->act_threshold_buf)); 328 if (ret) 329 return ret; 330 331 if (act == ADXL367_ACTIVITY) 332 st->act_threshold = threshold; 333 else 334 st->inact_threshold = threshold; 335 336 return 0; 337 } 338 339 static int adxl367_set_act_threshold(struct adxl367_state *st, 340 enum adxl367_activity_type act, 341 unsigned int threshold) 342 { 343 int ret; 344 345 guard(mutex)(&st->lock); 346 347 ret = adxl367_set_measure_en(st, false); 348 if (ret) 349 return ret; 350 351 ret = _adxl367_set_act_threshold(st, act, threshold); 352 if (ret) 353 return ret; 354 355 return adxl367_set_measure_en(st, true); 356 } 357 358 static int adxl367_set_act_proc_mode(struct adxl367_state *st, 359 enum adxl367_act_proc_mode mode) 360 { 361 return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL, 362 ADXL367_ACT_LINKLOOP_MASK, 363 FIELD_PREP(ADXL367_ACT_LINKLOOP_MASK, 364 mode)); 365 } 366 367 static int adxl367_set_act_interrupt_en(struct adxl367_state *st, 368 enum adxl367_activity_type act, 369 bool en) 370 { 371 unsigned int mask = adxl367_act_int_mask_tbl[act]; 372 373 return regmap_assign_bits(st->regmap, st->int_map_reg, mask, en); 374 } 375 376 static int adxl367_get_act_interrupt_en(struct adxl367_state *st, 377 enum adxl367_activity_type act, 378 bool *en) 379 { 380 unsigned int mask = adxl367_act_int_mask_tbl[act]; 381 unsigned int val; 382 int ret; 383 384 ret = regmap_read(st->regmap, st->int_map_reg, &val); 385 if (ret) 386 return ret; 387 388 *en = !!(val & mask); 389 390 return 0; 391 } 392 393 static int adxl367_set_act_en(struct adxl367_state *st, 394 enum adxl367_activity_type act, 395 enum adxl367_act_en_mode en) 396 { 397 unsigned int ctl_shift = adxl367_act_en_shift_tbl[act]; 398 399 return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL, 400 ADXL367_ACT_EN_MASK << ctl_shift, 401 en << ctl_shift); 402 } 403 404 static int adxl367_set_fifo_watermark_interrupt_en(struct adxl367_state *st, 405 bool en) 406 { 407 return regmap_assign_bits(st->regmap, st->int_map_reg, 408 ADXL367_INT_FIFO_WATERMARK_MASK, en); 409 } 410 411 static int adxl367_get_fifo_mode(struct adxl367_state *st, 412 enum adxl367_fifo_mode *fifo_mode) 413 { 414 unsigned int val; 415 int ret; 416 417 ret = regmap_read(st->regmap, ADXL367_REG_FIFO_CTL, &val); 418 if (ret) 419 return ret; 420 421 *fifo_mode = FIELD_GET(ADXL367_FIFO_CTL_MODE_MASK, val); 422 423 return 0; 424 } 425 426 static int adxl367_set_fifo_mode(struct adxl367_state *st, 427 enum adxl367_fifo_mode fifo_mode) 428 { 429 return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL, 430 ADXL367_FIFO_CTL_MODE_MASK, 431 FIELD_PREP(ADXL367_FIFO_CTL_MODE_MASK, 432 fifo_mode)); 433 } 434 435 static int adxl367_set_fifo_format(struct adxl367_state *st, 436 enum adxl367_fifo_format fifo_format) 437 { 438 return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL, 439 ADXL367_FIFO_CTL_FORMAT_MASK, 440 FIELD_PREP(ADXL367_FIFO_CTL_FORMAT_MASK, 441 fifo_format)); 442 } 443 444 static int adxl367_set_fifo_watermark(struct adxl367_state *st, 445 unsigned int fifo_watermark) 446 { 447 unsigned int fifo_samples = fifo_watermark * st->fifo_set_size; 448 unsigned int fifo_samples_h, fifo_samples_l; 449 int ret; 450 451 if (fifo_samples > ADXL367_FIFO_MAX_WATERMARK) 452 fifo_samples = ADXL367_FIFO_MAX_WATERMARK; 453 454 fifo_samples /= st->fifo_set_size; 455 456 fifo_samples_h = FIELD_PREP(ADXL367_SAMPLES_H_MASK, 457 FIELD_GET(ADXL367_SAMPLES_VAL_H_MASK, 458 fifo_samples)); 459 fifo_samples_l = FIELD_PREP(ADXL367_SAMPLES_L_MASK, 460 FIELD_GET(ADXL367_SAMPLES_VAL_L_MASK, 461 fifo_samples)); 462 463 ret = regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL, 464 ADXL367_SAMPLES_H_MASK, fifo_samples_h); 465 if (ret) 466 return ret; 467 468 ret = regmap_update_bits(st->regmap, ADXL367_REG_FIFO_SAMPLES, 469 ADXL367_SAMPLES_L_MASK, fifo_samples_l); 470 if (ret) 471 return ret; 472 473 st->fifo_watermark = fifo_watermark; 474 475 return 0; 476 } 477 478 static int adxl367_set_range(struct iio_dev *indio_dev, 479 enum adxl367_range range) 480 { 481 struct adxl367_state *st = iio_priv(indio_dev); 482 int ret; 483 484 guard(mutex)(&st->lock); 485 486 ret = adxl367_set_measure_en(st, false); 487 if (ret) 488 return ret; 489 490 ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL, 491 ADXL367_FILTER_CTL_RANGE_MASK, 492 FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK, 493 range)); 494 if (ret) 495 return ret; 496 497 adxl367_scale_act_thresholds(st, st->range, range); 498 499 /* Activity thresholds depend on range */ 500 ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY, 501 st->act_threshold); 502 if (ret) 503 return ret; 504 505 ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY, 506 st->inact_threshold); 507 if (ret) 508 return ret; 509 510 ret = adxl367_set_measure_en(st, true); 511 if (ret) 512 return ret; 513 514 st->range = range; 515 516 return 0; 517 } 518 519 static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms) 520 { 521 int freq_hz = adxl367_samp_freq_tbl[st->odr][0]; 522 int freq_microhz = adxl367_samp_freq_tbl[st->odr][1]; 523 /* Scale to decihertz to prevent precision loss in 12.5Hz case. */ 524 int freq_dhz = freq_hz * 10 + freq_microhz / 100000; 525 526 return DIV_ROUND_CLOSEST(ms * freq_dhz, 10000); 527 } 528 529 static int _adxl367_set_act_time_ms(struct adxl367_state *st, unsigned int ms) 530 { 531 unsigned int val = adxl367_time_ms_to_samples(st, ms); 532 int ret; 533 534 if (val > ADXL367_TIME_ACT_MAX) 535 val = ADXL367_TIME_ACT_MAX; 536 537 ret = regmap_write(st->regmap, ADXL367_REG_TIME_ACT, val); 538 if (ret) 539 return ret; 540 541 st->act_time_ms = ms; 542 543 return 0; 544 } 545 546 static int _adxl367_set_inact_time_ms(struct adxl367_state *st, unsigned int ms) 547 { 548 unsigned int val = adxl367_time_ms_to_samples(st, ms); 549 int ret; 550 551 if (val > ADXL367_TIME_INACT_MAX) 552 val = ADXL367_TIME_INACT_MAX; 553 554 st->inact_time_buf[0] = FIELD_PREP(ADXL367_TIME_INACT_H_MASK, 555 FIELD_GET(ADXL367_TIME_INACT_VAL_H_MASK, 556 val)); 557 st->inact_time_buf[1] = FIELD_PREP(ADXL367_TIME_INACT_L_MASK, 558 FIELD_GET(ADXL367_TIME_INACT_VAL_L_MASK, 559 val)); 560 561 ret = regmap_bulk_write(st->regmap, ADXL367_REG_TIME_INACT_H, 562 st->inact_time_buf, sizeof(st->inact_time_buf)); 563 if (ret) 564 return ret; 565 566 st->inact_time_ms = ms; 567 568 return 0; 569 } 570 571 static int adxl367_set_act_time_ms(struct adxl367_state *st, 572 enum adxl367_activity_type act, 573 unsigned int ms) 574 { 575 int ret; 576 577 guard(mutex)(&st->lock); 578 579 ret = adxl367_set_measure_en(st, false); 580 if (ret) 581 return ret; 582 583 if (act == ADXL367_ACTIVITY) 584 ret = _adxl367_set_act_time_ms(st, ms); 585 else 586 ret = _adxl367_set_inact_time_ms(st, ms); 587 588 if (ret) 589 return ret; 590 591 return adxl367_set_measure_en(st, true); 592 } 593 594 static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr) 595 { 596 int ret; 597 598 ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL, 599 ADXL367_FILTER_CTL_ODR_MASK, 600 FIELD_PREP(ADXL367_FILTER_CTL_ODR_MASK, 601 odr)); 602 if (ret) 603 return ret; 604 605 st->odr = odr; 606 607 /* Activity timers depend on ODR */ 608 ret = _adxl367_set_act_time_ms(st, st->act_time_ms); 609 if (ret) 610 return ret; 611 612 return _adxl367_set_inact_time_ms(st, st->inact_time_ms); 613 } 614 615 static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr) 616 { 617 struct adxl367_state *st = iio_priv(indio_dev); 618 int ret; 619 620 guard(mutex)(&st->lock); 621 622 ret = adxl367_set_measure_en(st, false); 623 if (ret) 624 return ret; 625 626 ret = _adxl367_set_odr(st, odr); 627 if (ret) 628 return ret; 629 630 return adxl367_set_measure_en(st, true); 631 } 632 633 static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg, 634 bool en) 635 { 636 return regmap_assign_bits(st->regmap, reg, ADXL367_ADC_EN_MASK, en); 637 } 638 639 static int adxl367_set_temp_adc_reg_en(struct adxl367_state *st, 640 unsigned int reg, bool en) 641 { 642 int ret; 643 644 switch (reg) { 645 case ADXL367_REG_TEMP_DATA_H: 646 ret = adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en); 647 break; 648 case ADXL367_REG_EX_ADC_DATA_H: 649 ret = adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en); 650 break; 651 default: 652 return 0; 653 } 654 655 if (ret) 656 return ret; 657 658 if (en) 659 msleep(100); 660 661 return 0; 662 } 663 664 static int adxl367_set_temp_adc_mask_en(struct adxl367_state *st, 665 const unsigned long *active_scan_mask, 666 bool en) 667 { 668 if (*active_scan_mask & ADXL367_TEMP_CHANNEL_MASK) 669 return adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en); 670 else if (*active_scan_mask & ADXL367_EX_ADC_CHANNEL_MASK) 671 return adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en); 672 673 return 0; 674 } 675 676 static int adxl367_find_odr(struct adxl367_state *st, int val, int val2, 677 enum adxl367_odr *odr) 678 { 679 size_t size = ARRAY_SIZE(adxl367_samp_freq_tbl); 680 int i; 681 682 for (i = 0; i < size; i++) 683 if (val == adxl367_samp_freq_tbl[i][0] && 684 val2 == adxl367_samp_freq_tbl[i][1]) 685 break; 686 687 if (i == size) 688 return -EINVAL; 689 690 *odr = i; 691 692 return 0; 693 } 694 695 static int adxl367_find_range(struct adxl367_state *st, int val, int val2, 696 enum adxl367_range *range) 697 { 698 size_t size = ARRAY_SIZE(adxl367_range_scale_tbl); 699 int i; 700 701 for (i = 0; i < size; i++) 702 if (val == adxl367_range_scale_tbl[i][0] && 703 val2 == adxl367_range_scale_tbl[i][1]) 704 break; 705 706 if (i == size) 707 return -EINVAL; 708 709 *range = i; 710 711 return 0; 712 } 713 714 static int adxl367_read_sample(struct iio_dev *indio_dev, 715 struct iio_chan_spec const *chan, 716 int *val) 717 { 718 struct adxl367_state *st = iio_priv(indio_dev); 719 u16 sample; 720 int ret; 721 722 guard(mutex)(&st->lock); 723 724 ret = adxl367_set_temp_adc_reg_en(st, chan->address, true); 725 if (ret) 726 return ret; 727 728 ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf, 729 sizeof(st->sample_buf)); 730 if (ret) 731 return ret; 732 733 sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf)); 734 *val = sign_extend32(sample, chan->scan_type.realbits - 1); 735 736 ret = adxl367_set_temp_adc_reg_en(st, chan->address, false); 737 if (ret) 738 return ret; 739 740 return IIO_VAL_INT; 741 } 742 743 static int adxl367_get_status(struct adxl367_state *st, u8 *status, 744 u16 *fifo_entries) 745 { 746 int ret; 747 748 /* Read STATUS, FIFO_ENT_L and FIFO_ENT_H */ 749 ret = regmap_bulk_read(st->regmap, ADXL367_REG_STATUS, 750 st->status_buf, sizeof(st->status_buf)); 751 if (ret) 752 return ret; 753 754 st->status_buf[2] &= ADXL367_FIFO_ENT_H_MASK; 755 756 *status = st->status_buf[0]; 757 *fifo_entries = get_unaligned_le16(&st->status_buf[1]); 758 759 return 0; 760 } 761 762 static bool adxl367_push_event(struct iio_dev *indio_dev, u8 status) 763 { 764 unsigned int ev_dir; 765 766 if (FIELD_GET(ADXL367_STATUS_ACT_MASK, status)) 767 ev_dir = IIO_EV_DIR_RISING; 768 else if (FIELD_GET(ADXL367_STATUS_INACT_MASK, status)) 769 ev_dir = IIO_EV_DIR_FALLING; 770 else 771 return false; 772 773 iio_push_event(indio_dev, 774 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 775 IIO_EV_TYPE_THRESH, ev_dir), 776 iio_get_time_ns(indio_dev)); 777 778 return true; 779 } 780 781 static bool adxl367_push_fifo_data(struct iio_dev *indio_dev, u8 status, 782 u16 fifo_entries) 783 { 784 struct adxl367_state *st = iio_priv(indio_dev); 785 int ret; 786 int i; 787 788 if (!FIELD_GET(ADXL367_STATUS_FIFO_FULL_MASK, status)) 789 return false; 790 791 if (fifo_entries > ADXL367_FIFO_SIZE) { 792 dev_err_ratelimited(st->dev, 793 "FIFO entry count %u exceeds FIFO size %u\n", 794 fifo_entries, ADXL367_FIFO_SIZE); 795 return true; 796 } 797 798 fifo_entries -= fifo_entries % st->fifo_set_size; 799 800 ret = st->ops->read_fifo(st->context, st->fifo_buf, fifo_entries); 801 if (ret) { 802 dev_err(st->dev, "Failed to read FIFO: %d\n", ret); 803 return true; 804 } 805 806 for (i = 0; i < fifo_entries; i += st->fifo_set_size) 807 iio_push_to_buffers(indio_dev, &st->fifo_buf[i]); 808 809 return true; 810 } 811 812 static irqreturn_t adxl367_irq_handler(int irq, void *private) 813 { 814 struct iio_dev *indio_dev = private; 815 struct adxl367_state *st = iio_priv(indio_dev); 816 u16 fifo_entries; 817 bool handled; 818 u8 status; 819 int ret; 820 821 ret = adxl367_get_status(st, &status, &fifo_entries); 822 if (ret) 823 return IRQ_NONE; 824 825 handled = adxl367_push_event(indio_dev, status); 826 handled |= adxl367_push_fifo_data(indio_dev, status, fifo_entries); 827 828 return handled ? IRQ_HANDLED : IRQ_NONE; 829 } 830 831 static int adxl367_reg_access(struct iio_dev *indio_dev, 832 unsigned int reg, 833 unsigned int writeval, 834 unsigned int *readval) 835 { 836 struct adxl367_state *st = iio_priv(indio_dev); 837 838 if (readval) 839 return regmap_read(st->regmap, reg, readval); 840 else 841 return regmap_write(st->regmap, reg, writeval); 842 } 843 844 static int adxl367_read_raw(struct iio_dev *indio_dev, 845 struct iio_chan_spec const *chan, 846 int *val, int *val2, long info) 847 { 848 struct adxl367_state *st = iio_priv(indio_dev); 849 int ret; 850 851 switch (info) { 852 case IIO_CHAN_INFO_RAW: 853 if (!iio_device_claim_direct(indio_dev)) 854 return -EBUSY; 855 ret = adxl367_read_sample(indio_dev, chan, val); 856 iio_device_release_direct(indio_dev); 857 return ret; 858 case IIO_CHAN_INFO_SCALE: 859 switch (chan->type) { 860 case IIO_ACCEL: { 861 guard(mutex)(&st->lock); 862 *val = adxl367_range_scale_tbl[st->range][0]; 863 *val2 = adxl367_range_scale_tbl[st->range][1]; 864 return IIO_VAL_INT_PLUS_NANO; 865 } 866 case IIO_TEMP: 867 *val = 1000; 868 *val2 = ADXL367_TEMP_PER_C; 869 return IIO_VAL_FRACTIONAL; 870 case IIO_VOLTAGE: 871 *val = ADXL367_VOLTAGE_MAX_MV; 872 *val2 = ADXL367_VOLTAGE_MAX_RAW; 873 return IIO_VAL_FRACTIONAL; 874 default: 875 return -EINVAL; 876 } 877 case IIO_CHAN_INFO_OFFSET: 878 switch (chan->type) { 879 case IIO_TEMP: 880 *val = 25 * ADXL367_TEMP_PER_C - ADXL367_TEMP_25C; 881 return IIO_VAL_INT; 882 case IIO_VOLTAGE: 883 *val = ADXL367_VOLTAGE_OFFSET; 884 return IIO_VAL_INT; 885 default: 886 return -EINVAL; 887 } 888 case IIO_CHAN_INFO_SAMP_FREQ: { 889 guard(mutex)(&st->lock); 890 *val = adxl367_samp_freq_tbl[st->odr][0]; 891 *val2 = adxl367_samp_freq_tbl[st->odr][1]; 892 return IIO_VAL_INT_PLUS_MICRO; 893 } 894 default: 895 return -EINVAL; 896 } 897 } 898 899 static int adxl367_write_raw(struct iio_dev *indio_dev, 900 struct iio_chan_spec const *chan, 901 int val, int val2, long info) 902 { 903 struct adxl367_state *st = iio_priv(indio_dev); 904 int ret; 905 906 switch (info) { 907 case IIO_CHAN_INFO_SAMP_FREQ: { 908 enum adxl367_odr odr; 909 910 ret = adxl367_find_odr(st, val, val2, &odr); 911 if (ret) 912 return ret; 913 914 if (!iio_device_claim_direct(indio_dev)) 915 return -EBUSY; 916 917 ret = adxl367_set_odr(indio_dev, odr); 918 iio_device_release_direct(indio_dev); 919 return ret; 920 } 921 case IIO_CHAN_INFO_SCALE: { 922 enum adxl367_range range; 923 924 ret = adxl367_find_range(st, val, val2, &range); 925 if (ret) 926 return ret; 927 928 if (!iio_device_claim_direct(indio_dev)) 929 return -EBUSY; 930 931 ret = adxl367_set_range(indio_dev, range); 932 iio_device_release_direct(indio_dev); 933 return ret; 934 } 935 default: 936 return -EINVAL; 937 } 938 } 939 940 static int adxl367_write_raw_get_fmt(struct iio_dev *indio_dev, 941 struct iio_chan_spec const *chan, 942 long info) 943 { 944 switch (info) { 945 case IIO_CHAN_INFO_SCALE: 946 if (chan->type != IIO_ACCEL) 947 return -EINVAL; 948 949 return IIO_VAL_INT_PLUS_NANO; 950 default: 951 return IIO_VAL_INT_PLUS_MICRO; 952 } 953 } 954 955 static int adxl367_read_avail(struct iio_dev *indio_dev, 956 struct iio_chan_spec const *chan, 957 const int **vals, int *type, int *length, 958 long info) 959 { 960 switch (info) { 961 case IIO_CHAN_INFO_SCALE: 962 if (chan->type != IIO_ACCEL) 963 return -EINVAL; 964 965 *vals = (int *)adxl367_range_scale_tbl; 966 *type = IIO_VAL_INT_PLUS_NANO; 967 *length = ARRAY_SIZE(adxl367_range_scale_tbl) * 2; 968 return IIO_AVAIL_LIST; 969 case IIO_CHAN_INFO_SAMP_FREQ: 970 *vals = (int *)adxl367_samp_freq_tbl; 971 *type = IIO_VAL_INT_PLUS_MICRO; 972 *length = ARRAY_SIZE(adxl367_samp_freq_tbl) * 2; 973 return IIO_AVAIL_LIST; 974 default: 975 return -EINVAL; 976 } 977 } 978 979 static int adxl367_read_event_value(struct iio_dev *indio_dev, 980 const struct iio_chan_spec *chan, 981 enum iio_event_type type, 982 enum iio_event_direction dir, 983 enum iio_event_info info, 984 int *val, int *val2) 985 { 986 struct adxl367_state *st = iio_priv(indio_dev); 987 988 guard(mutex)(&st->lock); 989 switch (info) { 990 case IIO_EV_INFO_VALUE: { 991 switch (dir) { 992 case IIO_EV_DIR_RISING: 993 *val = st->act_threshold; 994 return IIO_VAL_INT; 995 case IIO_EV_DIR_FALLING: 996 *val = st->inact_threshold; 997 return IIO_VAL_INT; 998 default: 999 return -EINVAL; 1000 } 1001 } 1002 case IIO_EV_INFO_PERIOD: 1003 switch (dir) { 1004 case IIO_EV_DIR_RISING: 1005 *val = st->act_time_ms; 1006 *val2 = 1000; 1007 return IIO_VAL_FRACTIONAL; 1008 case IIO_EV_DIR_FALLING: 1009 *val = st->inact_time_ms; 1010 *val2 = 1000; 1011 return IIO_VAL_FRACTIONAL; 1012 default: 1013 return -EINVAL; 1014 } 1015 default: 1016 return -EINVAL; 1017 } 1018 } 1019 1020 static int adxl367_write_event_value(struct iio_dev *indio_dev, 1021 const struct iio_chan_spec *chan, 1022 enum iio_event_type type, 1023 enum iio_event_direction dir, 1024 enum iio_event_info info, 1025 int val, int val2) 1026 { 1027 struct adxl367_state *st = iio_priv(indio_dev); 1028 1029 switch (info) { 1030 case IIO_EV_INFO_VALUE: 1031 if (val < 0) 1032 return -EINVAL; 1033 1034 switch (dir) { 1035 case IIO_EV_DIR_RISING: 1036 return adxl367_set_act_threshold(st, ADXL367_ACTIVITY, val); 1037 case IIO_EV_DIR_FALLING: 1038 return adxl367_set_act_threshold(st, ADXL367_INACTIVITY, val); 1039 default: 1040 return -EINVAL; 1041 } 1042 case IIO_EV_INFO_PERIOD: 1043 if (val < 0) 1044 return -EINVAL; 1045 1046 val = val * 1000 + DIV_ROUND_UP(val2, 1000); 1047 switch (dir) { 1048 case IIO_EV_DIR_RISING: 1049 return adxl367_set_act_time_ms(st, ADXL367_ACTIVITY, val); 1050 case IIO_EV_DIR_FALLING: 1051 return adxl367_set_act_time_ms(st, ADXL367_INACTIVITY, val); 1052 default: 1053 return -EINVAL; 1054 } 1055 default: 1056 return -EINVAL; 1057 } 1058 } 1059 1060 static int adxl367_read_event_config(struct iio_dev *indio_dev, 1061 const struct iio_chan_spec *chan, 1062 enum iio_event_type type, 1063 enum iio_event_direction dir) 1064 { 1065 struct adxl367_state *st = iio_priv(indio_dev); 1066 bool en; 1067 int ret; 1068 1069 switch (dir) { 1070 case IIO_EV_DIR_RISING: 1071 ret = adxl367_get_act_interrupt_en(st, ADXL367_ACTIVITY, &en); 1072 return ret ?: en; 1073 case IIO_EV_DIR_FALLING: 1074 ret = adxl367_get_act_interrupt_en(st, ADXL367_INACTIVITY, &en); 1075 return ret ?: en; 1076 default: 1077 return -EINVAL; 1078 } 1079 } 1080 1081 static int __adxl367_write_event_config(struct iio_dev *indio_dev, 1082 const struct iio_chan_spec *chan, 1083 enum iio_event_type type, 1084 enum iio_event_direction dir, 1085 bool state) 1086 { 1087 struct adxl367_state *st = iio_priv(indio_dev); 1088 enum adxl367_activity_type act; 1089 int ret; 1090 1091 switch (dir) { 1092 case IIO_EV_DIR_RISING: 1093 act = ADXL367_ACTIVITY; 1094 break; 1095 case IIO_EV_DIR_FALLING: 1096 act = ADXL367_INACTIVITY; 1097 break; 1098 default: 1099 return -EINVAL; 1100 } 1101 1102 guard(mutex)(&st->lock); 1103 1104 ret = adxl367_set_measure_en(st, false); 1105 if (ret) 1106 return ret; 1107 1108 ret = adxl367_set_act_interrupt_en(st, act, state); 1109 if (ret) 1110 return ret; 1111 1112 ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED 1113 : ADXL367_ACT_DISABLED); 1114 if (ret) 1115 return ret; 1116 1117 return adxl367_set_measure_en(st, true); 1118 } 1119 1120 static int adxl367_write_event_config(struct iio_dev *indio_dev, 1121 const struct iio_chan_spec *chan, 1122 enum iio_event_type type, 1123 enum iio_event_direction dir, 1124 bool state) 1125 { 1126 int ret; 1127 1128 if (!iio_device_claim_direct(indio_dev)) 1129 return -EBUSY; 1130 1131 ret = __adxl367_write_event_config(indio_dev, chan, type, dir, state); 1132 iio_device_release_direct(indio_dev); 1133 return ret; 1134 } 1135 1136 static ssize_t adxl367_get_fifo_enabled(struct device *dev, 1137 struct device_attribute *attr, 1138 char *buf) 1139 { 1140 struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev)); 1141 enum adxl367_fifo_mode fifo_mode; 1142 int ret; 1143 1144 ret = adxl367_get_fifo_mode(st, &fifo_mode); 1145 if (ret) 1146 return ret; 1147 1148 return sysfs_emit(buf, "%d\n", fifo_mode != ADXL367_FIFO_MODE_DISABLED); 1149 } 1150 1151 static ssize_t adxl367_get_fifo_watermark(struct device *dev, 1152 struct device_attribute *attr, 1153 char *buf) 1154 { 1155 struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev)); 1156 unsigned int fifo_watermark; 1157 1158 guard(mutex)(&st->lock); 1159 fifo_watermark = st->fifo_watermark; 1160 1161 return sysfs_emit(buf, "%d\n", fifo_watermark); 1162 } 1163 1164 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1"); 1165 IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max, 1166 __stringify(ADXL367_FIFO_MAX_WATERMARK)); 1167 static IIO_DEVICE_ATTR(hwfifo_watermark, 0444, 1168 adxl367_get_fifo_watermark, NULL, 0); 1169 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, 1170 adxl367_get_fifo_enabled, NULL, 0); 1171 1172 static const struct iio_dev_attr *adxl367_fifo_attributes[] = { 1173 &iio_dev_attr_hwfifo_watermark_min, 1174 &iio_dev_attr_hwfifo_watermark_max, 1175 &iio_dev_attr_hwfifo_watermark, 1176 &iio_dev_attr_hwfifo_enabled, 1177 NULL, 1178 }; 1179 1180 static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val) 1181 { 1182 struct adxl367_state *st = iio_priv(indio_dev); 1183 int ret; 1184 1185 if (val > ADXL367_FIFO_MAX_WATERMARK) 1186 return -EINVAL; 1187 1188 guard(mutex)(&st->lock); 1189 1190 ret = adxl367_set_measure_en(st, false); 1191 if (ret) 1192 return ret; 1193 1194 ret = adxl367_set_fifo_watermark(st, val); 1195 if (ret) 1196 return ret; 1197 1198 return adxl367_set_measure_en(st, true); 1199 } 1200 1201 static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask, 1202 enum adxl367_fifo_format *fifo_format) 1203 { 1204 size_t size = ARRAY_SIZE(adxl367_fifo_formats); 1205 int i; 1206 1207 for (i = 0; i < size; i++) 1208 if (*scan_mask == adxl367_channel_masks[i]) 1209 break; 1210 1211 if (i == size) 1212 return false; 1213 1214 *fifo_format = adxl367_fifo_formats[i]; 1215 1216 return true; 1217 } 1218 1219 static int adxl367_update_scan_mode(struct iio_dev *indio_dev, 1220 const unsigned long *active_scan_mask) 1221 { 1222 struct adxl367_state *st = iio_priv(indio_dev); 1223 enum adxl367_fifo_format fifo_format; 1224 int ret; 1225 1226 if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format)) 1227 return -EINVAL; 1228 1229 guard(mutex)(&st->lock); 1230 1231 ret = adxl367_set_measure_en(st, false); 1232 if (ret) 1233 return ret; 1234 1235 ret = adxl367_set_fifo_format(st, fifo_format); 1236 if (ret) 1237 return ret; 1238 1239 ret = adxl367_set_measure_en(st, true); 1240 if (ret) 1241 return ret; 1242 1243 st->fifo_set_size = bitmap_weight(active_scan_mask, 1244 iio_get_masklength(indio_dev)); 1245 1246 return 0; 1247 } 1248 1249 static int adxl367_buffer_postenable(struct iio_dev *indio_dev) 1250 { 1251 struct adxl367_state *st = iio_priv(indio_dev); 1252 int ret; 1253 1254 guard(mutex)(&st->lock); 1255 1256 ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask, 1257 true); 1258 if (ret) 1259 return ret; 1260 1261 ret = adxl367_set_measure_en(st, false); 1262 if (ret) 1263 return ret; 1264 1265 ret = adxl367_set_fifo_watermark_interrupt_en(st, true); 1266 if (ret) 1267 return ret; 1268 1269 ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM); 1270 if (ret) 1271 return ret; 1272 1273 return adxl367_set_measure_en(st, true); 1274 } 1275 1276 static int adxl367_buffer_predisable(struct iio_dev *indio_dev) 1277 { 1278 struct adxl367_state *st = iio_priv(indio_dev); 1279 int ret; 1280 1281 guard(mutex)(&st->lock); 1282 1283 ret = adxl367_set_measure_en(st, false); 1284 if (ret) 1285 return ret; 1286 1287 ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED); 1288 if (ret) 1289 return ret; 1290 1291 ret = adxl367_set_fifo_watermark_interrupt_en(st, false); 1292 if (ret) 1293 return ret; 1294 1295 ret = adxl367_set_measure_en(st, true); 1296 if (ret) 1297 return ret; 1298 1299 return adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask, 1300 false); 1301 } 1302 1303 static const struct iio_buffer_setup_ops adxl367_buffer_ops = { 1304 .postenable = adxl367_buffer_postenable, 1305 .predisable = adxl367_buffer_predisable, 1306 }; 1307 1308 static const struct iio_info adxl367_info = { 1309 .read_raw = adxl367_read_raw, 1310 .write_raw = adxl367_write_raw, 1311 .write_raw_get_fmt = adxl367_write_raw_get_fmt, 1312 .read_avail = adxl367_read_avail, 1313 .read_event_config = adxl367_read_event_config, 1314 .write_event_config = adxl367_write_event_config, 1315 .read_event_value = adxl367_read_event_value, 1316 .write_event_value = adxl367_write_event_value, 1317 .debugfs_reg_access = adxl367_reg_access, 1318 .hwfifo_set_watermark = adxl367_set_watermark, 1319 .update_scan_mode = adxl367_update_scan_mode, 1320 }; 1321 1322 static const struct iio_event_spec adxl367_events[] = { 1323 { 1324 .type = IIO_EV_TYPE_MAG_REFERENCED, 1325 .dir = IIO_EV_DIR_RISING, 1326 .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | 1327 BIT(IIO_EV_INFO_PERIOD) | 1328 BIT(IIO_EV_INFO_VALUE), 1329 }, 1330 { 1331 .type = IIO_EV_TYPE_MAG_REFERENCED, 1332 .dir = IIO_EV_DIR_FALLING, 1333 .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | 1334 BIT(IIO_EV_INFO_PERIOD) | 1335 BIT(IIO_EV_INFO_VALUE), 1336 }, 1337 }; 1338 1339 #define ADXL367_ACCEL_CHANNEL(index, reg, axis) { \ 1340 .type = IIO_ACCEL, \ 1341 .address = (reg), \ 1342 .modified = 1, \ 1343 .channel2 = IIO_MOD_##axis, \ 1344 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 1345 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 1346 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \ 1347 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1348 .info_mask_shared_by_all_available = \ 1349 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1350 .event_spec = adxl367_events, \ 1351 .num_event_specs = ARRAY_SIZE(adxl367_events), \ 1352 .scan_index = (index), \ 1353 .scan_type = { \ 1354 .sign = 's', \ 1355 .realbits = 14, \ 1356 .storagebits = 16, \ 1357 .endianness = IIO_BE, \ 1358 }, \ 1359 } 1360 1361 #define ADXL367_CHANNEL(index, reg, _type) { \ 1362 .type = (_type), \ 1363 .address = (reg), \ 1364 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 1365 BIT(IIO_CHAN_INFO_OFFSET) | \ 1366 BIT(IIO_CHAN_INFO_SCALE), \ 1367 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1368 .scan_index = (index), \ 1369 .scan_type = { \ 1370 .sign = 's', \ 1371 .realbits = 14, \ 1372 .storagebits = 16, \ 1373 .endianness = IIO_BE, \ 1374 }, \ 1375 } 1376 1377 static const struct iio_chan_spec adxl367_channels[] = { 1378 ADXL367_ACCEL_CHANNEL(ADXL367_X_CHANNEL_INDEX, ADXL367_REG_X_DATA_H, X), 1379 ADXL367_ACCEL_CHANNEL(ADXL367_Y_CHANNEL_INDEX, ADXL367_REG_Y_DATA_H, Y), 1380 ADXL367_ACCEL_CHANNEL(ADXL367_Z_CHANNEL_INDEX, ADXL367_REG_Z_DATA_H, Z), 1381 ADXL367_CHANNEL(ADXL367_TEMP_CHANNEL_INDEX, ADXL367_REG_TEMP_DATA_H, 1382 IIO_TEMP), 1383 ADXL367_CHANNEL(ADXL367_EX_ADC_CHANNEL_INDEX, ADXL367_REG_EX_ADC_DATA_H, 1384 IIO_VOLTAGE), 1385 }; 1386 1387 static int adxl367_verify_devid(struct adxl367_state *st) 1388 { 1389 unsigned int val; 1390 int ret; 1391 1392 ret = regmap_read(st->regmap, ADXL367_REG_DEVID, &val); 1393 if (ret) 1394 return dev_err_probe(st->dev, ret, "Failed to read dev id\n"); 1395 1396 if (val != ADXL367_DEVID_AD) 1397 return dev_err_probe(st->dev, -ENODEV, 1398 "Invalid dev id 0x%02X, expected 0x%02X\n", 1399 val, ADXL367_DEVID_AD); 1400 1401 return 0; 1402 } 1403 1404 static int adxl367_setup(struct adxl367_state *st) 1405 { 1406 int ret; 1407 1408 ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY, 1409 ADXL367_2G_RANGE_1G); 1410 if (ret) 1411 return ret; 1412 1413 ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY, 1414 ADXL367_2G_RANGE_100MG); 1415 if (ret) 1416 return ret; 1417 1418 ret = adxl367_set_act_proc_mode(st, ADXL367_LOOPED); 1419 if (ret) 1420 return ret; 1421 1422 ret = _adxl367_set_odr(st, ADXL367_ODR_400HZ); 1423 if (ret) 1424 return ret; 1425 1426 ret = _adxl367_set_act_time_ms(st, 10); 1427 if (ret) 1428 return ret; 1429 1430 ret = _adxl367_set_inact_time_ms(st, 10000); 1431 if (ret) 1432 return ret; 1433 1434 return adxl367_set_measure_en(st, true); 1435 } 1436 1437 static int adxl367_set_int_map_reg(struct adxl367_state *st, int irq) 1438 { 1439 int ret; 1440 1441 /* 1442 * Route the mappings to whichever of INT1/INT2 is named in the 1443 * interrupt-names property. -EINVAL (property absent) and -ENODATA 1444 * (name not listed) are not errors; anything else is. 1445 */ 1446 ret = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT1"); 1447 if (ret > 0) { 1448 st->int_map_reg = ADXL367_REG_INT1_MAP; 1449 return ret; 1450 } 1451 if (ret != -ENODATA && ret != -EINVAL) 1452 return ret; 1453 1454 ret = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT2"); 1455 if (ret > 0) { 1456 st->int_map_reg = ADXL367_REG_INT2_MAP; 1457 return ret; 1458 } 1459 if (ret != -ENODATA && ret != -EINVAL) 1460 return ret; 1461 1462 /* No INT1/INT2 named: default to INT1 and the bus interrupt. */ 1463 st->int_map_reg = ADXL367_REG_INT1_MAP; 1464 return irq; 1465 } 1466 1467 int adxl367_probe(struct device *dev, const struct adxl367_ops *ops, 1468 void *context, struct regmap *regmap, int irq) 1469 { 1470 static const char * const regulator_names[] = { "vdd", "vddio" }; 1471 struct iio_dev *indio_dev; 1472 struct adxl367_state *st; 1473 int ret; 1474 1475 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 1476 if (!indio_dev) 1477 return -ENOMEM; 1478 1479 st = iio_priv(indio_dev); 1480 st->dev = dev; 1481 st->regmap = regmap; 1482 st->context = context; 1483 st->ops = ops; 1484 1485 ret = devm_mutex_init(dev, &st->lock); 1486 if (ret) 1487 return ret; 1488 1489 indio_dev->channels = adxl367_channels; 1490 indio_dev->num_channels = ARRAY_SIZE(adxl367_channels); 1491 indio_dev->available_scan_masks = adxl367_channel_masks; 1492 indio_dev->name = "adxl367"; 1493 indio_dev->info = &adxl367_info; 1494 indio_dev->modes = INDIO_DIRECT_MODE; 1495 1496 ret = adxl367_set_int_map_reg(st, irq); 1497 if (ret < 0) 1498 return dev_err_probe(st->dev, ret, "Failed to get interrupt\n"); 1499 irq = ret; 1500 1501 ret = devm_regulator_bulk_get_enable(st->dev, 1502 ARRAY_SIZE(regulator_names), 1503 regulator_names); 1504 if (ret) 1505 return dev_err_probe(st->dev, ret, 1506 "Failed to get regulators\n"); 1507 1508 ret = regmap_write(st->regmap, ADXL367_REG_RESET, ADXL367_RESET_CODE); 1509 if (ret) 1510 return ret; 1511 1512 fsleep(15000); 1513 1514 ret = adxl367_verify_devid(st); 1515 if (ret) 1516 return ret; 1517 1518 ret = adxl367_setup(st); 1519 if (ret) 1520 return ret; 1521 1522 ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev, 1523 &adxl367_buffer_ops, 1524 adxl367_fifo_attributes); 1525 if (ret) 1526 return ret; 1527 1528 ret = devm_request_threaded_irq(st->dev, irq, NULL, 1529 adxl367_irq_handler, IRQF_ONESHOT, 1530 indio_dev->name, indio_dev); 1531 if (ret) 1532 return ret; 1533 1534 return devm_iio_device_register(dev, indio_dev); 1535 } 1536 EXPORT_SYMBOL_NS_GPL(adxl367_probe, "IIO_ADXL367"); 1537 1538 MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>"); 1539 MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer driver"); 1540 MODULE_LICENSE("GPL"); 1541