1 // SPDX-License-Identifier: GPL-2.0-only 2 /** 3 * Sensortek STK8312 3-Axis Accelerometer 4 * 5 * Copyright (c) 2015, Intel Corporation. 6 * 7 * IIO driver for STK8312; 7-bit I2C address: 0x3D. 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/i2c.h> 12 #include <linux/interrupt.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/delay.h> 16 #include <linux/iio/buffer.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 #include <linux/iio/trigger.h> 20 #include <linux/iio/triggered_buffer.h> 21 #include <linux/iio/trigger_consumer.h> 22 23 #define STK8312_REG_XOUT 0x00 24 #define STK8312_REG_YOUT 0x01 25 #define STK8312_REG_ZOUT 0x02 26 #define STK8312_REG_INTSU 0x06 27 #define STK8312_REG_MODE 0x07 28 #define STK8312_REG_SR 0x08 29 #define STK8312_REG_STH 0x13 30 #define STK8312_REG_RESET 0x20 31 #define STK8312_REG_AFECTRL 0x24 32 #define STK8312_REG_OTPADDR 0x3D 33 #define STK8312_REG_OTPDATA 0x3E 34 #define STK8312_REG_OTPCTRL 0x3F 35 36 #define STK8312_MODE_ACTIVE BIT(0) 37 #define STK8312_MODE_STANDBY 0x00 38 #define STK8312_MODE_INT_AH_PP 0xC0 /* active-high, push-pull */ 39 #define STK8312_DREADY_BIT BIT(4) 40 #define STK8312_RNG_6G 1 41 #define STK8312_RNG_SHIFT 6 42 #define STK8312_RNG_MASK GENMASK(7, 6) 43 #define STK8312_SR_MASK GENMASK(2, 0) 44 #define STK8312_SR_400HZ_IDX 0 45 #define STK8312_ALL_CHANNEL_MASK GENMASK(2, 0) 46 #define STK8312_ALL_CHANNEL_SIZE 3 47 48 #define STK8312_DRIVER_NAME "stk8312" 49 #define STK8312_IRQ_NAME "stk8312_event" 50 51 /* 52 * The accelerometer has two measurement ranges: 53 * 54 * -6g - +6g (8-bit, signed) 55 * -16g - +16g (8-bit, signed) 56 * 57 * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616 58 * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311 59 */ 60 #define STK8312_SCALE_AVAIL "0.4616 1.2311" 61 62 static const int stk8312_scale_table[][2] = { 63 {0, 461600}, {1, 231100} 64 }; 65 66 static const struct { 67 int val; 68 int val2; 69 } stk8312_samp_freq_table[] = { 70 {400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0}, 71 {12, 500000}, {6, 250000}, {3, 125000} 72 }; 73 74 #define STK8312_ACCEL_CHANNEL(index, reg, axis) { \ 75 .type = IIO_ACCEL, \ 76 .address = reg, \ 77 .modified = 1, \ 78 .channel2 = IIO_MOD_##axis, \ 79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 80 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 81 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 82 .scan_index = index, \ 83 .scan_type = { \ 84 .sign = 's', \ 85 .realbits = 8, \ 86 .storagebits = 8, \ 87 .endianness = IIO_CPU, \ 88 }, \ 89 } 90 91 static const struct iio_chan_spec stk8312_channels[] = { 92 STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT, X), 93 STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT, Y), 94 STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT, Z), 95 IIO_CHAN_SOFT_TIMESTAMP(3), 96 }; 97 98 struct stk8312_data { 99 struct i2c_client *client; 100 struct mutex lock; 101 u8 range; 102 u8 sample_rate_idx; 103 u8 mode; 104 struct iio_trigger *dready_trig; 105 bool dready_trigger_on; 106 s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 64-bit timestamp */ 107 }; 108 109 static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL); 110 111 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400"); 112 113 static struct attribute *stk8312_attributes[] = { 114 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 115 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 116 NULL, 117 }; 118 119 static const struct attribute_group stk8312_attribute_group = { 120 .attrs = stk8312_attributes 121 }; 122 123 static int stk8312_otp_init(struct stk8312_data *data) 124 { 125 int ret; 126 int count = 10; 127 struct i2c_client *client = data->client; 128 129 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70); 130 if (ret < 0) 131 goto exit_err; 132 ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02); 133 if (ret < 0) 134 goto exit_err; 135 136 do { 137 usleep_range(1000, 5000); 138 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL); 139 if (ret < 0) 140 goto exit_err; 141 count--; 142 } while (!(ret & BIT(7)) && count > 0); 143 144 if (count == 0) { 145 ret = -ETIMEDOUT; 146 goto exit_err; 147 } 148 149 ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA); 150 if (ret == 0) 151 ret = -EINVAL; 152 if (ret < 0) 153 goto exit_err; 154 155 ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_AFECTRL, ret); 156 if (ret < 0) 157 goto exit_err; 158 msleep(150); 159 160 return 0; 161 162 exit_err: 163 dev_err(&client->dev, "failed to initialize sensor\n"); 164 return ret; 165 } 166 167 static int stk8312_set_mode(struct stk8312_data *data, u8 mode) 168 { 169 int ret; 170 struct i2c_client *client = data->client; 171 172 if (mode == data->mode) 173 return 0; 174 175 ret = i2c_smbus_write_byte_data(client, STK8312_REG_MODE, mode); 176 if (ret < 0) { 177 dev_err(&client->dev, "failed to change sensor mode\n"); 178 return ret; 179 } 180 181 data->mode = mode; 182 if (mode & STK8312_MODE_ACTIVE) { 183 /* Need to run OTP sequence before entering active mode */ 184 usleep_range(1000, 5000); 185 ret = stk8312_otp_init(data); 186 } 187 188 return ret; 189 } 190 191 static int stk8312_set_interrupts(struct stk8312_data *data, u8 int_mask) 192 { 193 int ret; 194 u8 mode; 195 struct i2c_client *client = data->client; 196 197 mode = data->mode; 198 /* We need to go in standby mode to modify registers */ 199 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); 200 if (ret < 0) 201 return ret; 202 203 ret = i2c_smbus_write_byte_data(client, STK8312_REG_INTSU, int_mask); 204 if (ret < 0) { 205 dev_err(&client->dev, "failed to set interrupts\n"); 206 stk8312_set_mode(data, mode); 207 return ret; 208 } 209 210 return stk8312_set_mode(data, mode); 211 } 212 213 static int stk8312_data_rdy_trigger_set_state(struct iio_trigger *trig, 214 bool state) 215 { 216 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 217 struct stk8312_data *data = iio_priv(indio_dev); 218 int ret; 219 220 if (state) 221 ret = stk8312_set_interrupts(data, STK8312_DREADY_BIT); 222 else 223 ret = stk8312_set_interrupts(data, 0x00); 224 225 if (ret < 0) { 226 dev_err(&data->client->dev, "failed to set trigger state\n"); 227 return ret; 228 } 229 230 data->dready_trigger_on = state; 231 232 return 0; 233 } 234 235 static const struct iio_trigger_ops stk8312_trigger_ops = { 236 .set_trigger_state = stk8312_data_rdy_trigger_set_state, 237 }; 238 239 static int stk8312_set_sample_rate(struct stk8312_data *data, u8 rate) 240 { 241 int ret; 242 u8 masked_reg; 243 u8 mode; 244 struct i2c_client *client = data->client; 245 246 if (rate == data->sample_rate_idx) 247 return 0; 248 249 mode = data->mode; 250 /* We need to go in standby mode to modify registers */ 251 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); 252 if (ret < 0) 253 return ret; 254 255 ret = i2c_smbus_read_byte_data(client, STK8312_REG_SR); 256 if (ret < 0) 257 goto err_activate; 258 259 masked_reg = (ret & (~STK8312_SR_MASK)) | rate; 260 261 ret = i2c_smbus_write_byte_data(client, STK8312_REG_SR, masked_reg); 262 if (ret < 0) 263 goto err_activate; 264 265 data->sample_rate_idx = rate; 266 267 return stk8312_set_mode(data, mode); 268 269 err_activate: 270 dev_err(&client->dev, "failed to set sampling rate\n"); 271 stk8312_set_mode(data, mode); 272 273 return ret; 274 } 275 276 static int stk8312_set_range(struct stk8312_data *data, u8 range) 277 { 278 int ret; 279 u8 masked_reg; 280 u8 mode; 281 struct i2c_client *client = data->client; 282 283 if (range != 1 && range != 2) 284 return -EINVAL; 285 else if (range == data->range) 286 return 0; 287 288 mode = data->mode; 289 /* We need to go in standby mode to modify registers */ 290 ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); 291 if (ret < 0) 292 return ret; 293 294 ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH); 295 if (ret < 0) 296 goto err_activate; 297 298 masked_reg = ret & (~STK8312_RNG_MASK); 299 masked_reg |= range << STK8312_RNG_SHIFT; 300 301 ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg); 302 if (ret < 0) 303 goto err_activate; 304 305 data->range = range; 306 307 return stk8312_set_mode(data, mode); 308 309 err_activate: 310 dev_err(&client->dev, "failed to change sensor range\n"); 311 stk8312_set_mode(data, mode); 312 313 return ret; 314 } 315 316 static int stk8312_read_accel(struct stk8312_data *data, u8 address) 317 { 318 int ret; 319 struct i2c_client *client = data->client; 320 321 if (address > 2) 322 return -EINVAL; 323 324 ret = i2c_smbus_read_byte_data(client, address); 325 if (ret < 0) 326 dev_err(&client->dev, "register read failed\n"); 327 328 return ret; 329 } 330 331 static int stk8312_read_raw(struct iio_dev *indio_dev, 332 struct iio_chan_spec const *chan, 333 int *val, int *val2, long mask) 334 { 335 struct stk8312_data *data = iio_priv(indio_dev); 336 int ret; 337 338 switch (mask) { 339 case IIO_CHAN_INFO_RAW: 340 if (iio_buffer_enabled(indio_dev)) 341 return -EBUSY; 342 mutex_lock(&data->lock); 343 ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); 344 if (ret < 0) { 345 mutex_unlock(&data->lock); 346 return ret; 347 } 348 ret = stk8312_read_accel(data, chan->address); 349 if (ret < 0) { 350 stk8312_set_mode(data, 351 data->mode & (~STK8312_MODE_ACTIVE)); 352 mutex_unlock(&data->lock); 353 return ret; 354 } 355 *val = sign_extend32(ret, 7); 356 ret = stk8312_set_mode(data, 357 data->mode & (~STK8312_MODE_ACTIVE)); 358 mutex_unlock(&data->lock); 359 if (ret < 0) 360 return ret; 361 return IIO_VAL_INT; 362 case IIO_CHAN_INFO_SCALE: 363 *val = stk8312_scale_table[data->range - 1][0]; 364 *val2 = stk8312_scale_table[data->range - 1][1]; 365 return IIO_VAL_INT_PLUS_MICRO; 366 case IIO_CHAN_INFO_SAMP_FREQ: 367 *val = stk8312_samp_freq_table[data->sample_rate_idx].val; 368 *val2 = stk8312_samp_freq_table[data->sample_rate_idx].val2; 369 return IIO_VAL_INT_PLUS_MICRO; 370 } 371 372 return -EINVAL; 373 } 374 375 static int stk8312_write_raw(struct iio_dev *indio_dev, 376 struct iio_chan_spec const *chan, 377 int val, int val2, long mask) 378 { 379 int i; 380 int index = -1; 381 int ret; 382 struct stk8312_data *data = iio_priv(indio_dev); 383 384 switch (mask) { 385 case IIO_CHAN_INFO_SCALE: 386 for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++) 387 if (val == stk8312_scale_table[i][0] && 388 val2 == stk8312_scale_table[i][1]) { 389 index = i + 1; 390 break; 391 } 392 if (index < 0) 393 return -EINVAL; 394 395 mutex_lock(&data->lock); 396 ret = stk8312_set_range(data, index); 397 mutex_unlock(&data->lock); 398 399 return ret; 400 case IIO_CHAN_INFO_SAMP_FREQ: 401 for (i = 0; i < ARRAY_SIZE(stk8312_samp_freq_table); i++) 402 if (val == stk8312_samp_freq_table[i].val && 403 val2 == stk8312_samp_freq_table[i].val2) { 404 index = i; 405 break; 406 } 407 if (index < 0) 408 return -EINVAL; 409 mutex_lock(&data->lock); 410 ret = stk8312_set_sample_rate(data, index); 411 mutex_unlock(&data->lock); 412 413 return ret; 414 } 415 416 return -EINVAL; 417 } 418 419 static const struct iio_info stk8312_info = { 420 .read_raw = stk8312_read_raw, 421 .write_raw = stk8312_write_raw, 422 .attrs = &stk8312_attribute_group, 423 }; 424 425 static irqreturn_t stk8312_trigger_handler(int irq, void *p) 426 { 427 struct iio_poll_func *pf = p; 428 struct iio_dev *indio_dev = pf->indio_dev; 429 struct stk8312_data *data = iio_priv(indio_dev); 430 int bit, ret, i = 0; 431 432 mutex_lock(&data->lock); 433 /* 434 * Do a bulk read if all channels are requested, 435 * from 0x00 (XOUT) to 0x02 (ZOUT) 436 */ 437 if (*(indio_dev->active_scan_mask) == STK8312_ALL_CHANNEL_MASK) { 438 ret = i2c_smbus_read_i2c_block_data(data->client, 439 STK8312_REG_XOUT, 440 STK8312_ALL_CHANNEL_SIZE, 441 data->buffer); 442 if (ret < STK8312_ALL_CHANNEL_SIZE) { 443 dev_err(&data->client->dev, "register read failed\n"); 444 mutex_unlock(&data->lock); 445 goto err; 446 } 447 } else { 448 for_each_set_bit(bit, indio_dev->active_scan_mask, 449 indio_dev->masklength) { 450 ret = stk8312_read_accel(data, bit); 451 if (ret < 0) { 452 mutex_unlock(&data->lock); 453 goto err; 454 } 455 data->buffer[i++] = ret; 456 } 457 } 458 mutex_unlock(&data->lock); 459 460 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 461 pf->timestamp); 462 err: 463 iio_trigger_notify_done(indio_dev->trig); 464 465 return IRQ_HANDLED; 466 } 467 468 static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private) 469 { 470 struct iio_dev *indio_dev = private; 471 struct stk8312_data *data = iio_priv(indio_dev); 472 473 if (data->dready_trigger_on) 474 iio_trigger_poll(data->dready_trig); 475 476 return IRQ_HANDLED; 477 } 478 479 static int stk8312_buffer_preenable(struct iio_dev *indio_dev) 480 { 481 struct stk8312_data *data = iio_priv(indio_dev); 482 483 return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); 484 } 485 486 static int stk8312_buffer_postdisable(struct iio_dev *indio_dev) 487 { 488 struct stk8312_data *data = iio_priv(indio_dev); 489 490 return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE)); 491 } 492 493 static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops = { 494 .preenable = stk8312_buffer_preenable, 495 .postdisable = stk8312_buffer_postdisable, 496 }; 497 498 static int stk8312_probe(struct i2c_client *client, 499 const struct i2c_device_id *id) 500 { 501 int ret; 502 struct iio_dev *indio_dev; 503 struct stk8312_data *data; 504 505 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 506 if (!indio_dev) { 507 dev_err(&client->dev, "iio allocation failed!\n"); 508 return -ENOMEM; 509 } 510 511 data = iio_priv(indio_dev); 512 data->client = client; 513 i2c_set_clientdata(client, indio_dev); 514 mutex_init(&data->lock); 515 516 indio_dev->info = &stk8312_info; 517 indio_dev->name = STK8312_DRIVER_NAME; 518 indio_dev->modes = INDIO_DIRECT_MODE; 519 indio_dev->channels = stk8312_channels; 520 indio_dev->num_channels = ARRAY_SIZE(stk8312_channels); 521 522 /* A software reset is recommended at power-on */ 523 ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00); 524 if (ret < 0) { 525 dev_err(&client->dev, "failed to reset sensor\n"); 526 return ret; 527 } 528 data->sample_rate_idx = STK8312_SR_400HZ_IDX; 529 ret = stk8312_set_range(data, STK8312_RNG_6G); 530 if (ret < 0) 531 return ret; 532 533 ret = stk8312_set_mode(data, 534 STK8312_MODE_INT_AH_PP | STK8312_MODE_ACTIVE); 535 if (ret < 0) 536 return ret; 537 538 if (client->irq > 0) { 539 ret = devm_request_threaded_irq(&client->dev, client->irq, 540 stk8312_data_rdy_trig_poll, 541 NULL, 542 IRQF_TRIGGER_RISING | 543 IRQF_ONESHOT, 544 STK8312_IRQ_NAME, 545 indio_dev); 546 if (ret < 0) { 547 dev_err(&client->dev, "request irq %d failed\n", 548 client->irq); 549 goto err_power_off; 550 } 551 552 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 553 "%s-dev%d", 554 indio_dev->name, 555 indio_dev->id); 556 if (!data->dready_trig) { 557 ret = -ENOMEM; 558 goto err_power_off; 559 } 560 561 data->dready_trig->dev.parent = &client->dev; 562 data->dready_trig->ops = &stk8312_trigger_ops; 563 iio_trigger_set_drvdata(data->dready_trig, indio_dev); 564 ret = iio_trigger_register(data->dready_trig); 565 if (ret) { 566 dev_err(&client->dev, "iio trigger register failed\n"); 567 goto err_power_off; 568 } 569 } 570 571 ret = iio_triggered_buffer_setup(indio_dev, 572 iio_pollfunc_store_time, 573 stk8312_trigger_handler, 574 &stk8312_buffer_setup_ops); 575 if (ret < 0) { 576 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 577 goto err_trigger_unregister; 578 } 579 580 ret = iio_device_register(indio_dev); 581 if (ret < 0) { 582 dev_err(&client->dev, "device_register failed\n"); 583 goto err_buffer_cleanup; 584 } 585 586 return 0; 587 588 err_buffer_cleanup: 589 iio_triggered_buffer_cleanup(indio_dev); 590 err_trigger_unregister: 591 if (data->dready_trig) 592 iio_trigger_unregister(data->dready_trig); 593 err_power_off: 594 stk8312_set_mode(data, STK8312_MODE_STANDBY); 595 return ret; 596 } 597 598 static int stk8312_remove(struct i2c_client *client) 599 { 600 struct iio_dev *indio_dev = i2c_get_clientdata(client); 601 struct stk8312_data *data = iio_priv(indio_dev); 602 603 iio_device_unregister(indio_dev); 604 iio_triggered_buffer_cleanup(indio_dev); 605 606 if (data->dready_trig) 607 iio_trigger_unregister(data->dready_trig); 608 609 return stk8312_set_mode(data, STK8312_MODE_STANDBY); 610 } 611 612 #ifdef CONFIG_PM_SLEEP 613 static int stk8312_suspend(struct device *dev) 614 { 615 struct stk8312_data *data; 616 617 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 618 619 return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE)); 620 } 621 622 static int stk8312_resume(struct device *dev) 623 { 624 struct stk8312_data *data; 625 626 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 627 628 return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); 629 } 630 631 static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume); 632 633 #define STK8312_PM_OPS (&stk8312_pm_ops) 634 #else 635 #define STK8312_PM_OPS NULL 636 #endif 637 638 static const struct i2c_device_id stk8312_i2c_id[] = { 639 {"STK8312", 0}, 640 {} 641 }; 642 MODULE_DEVICE_TABLE(i2c, stk8312_i2c_id); 643 644 static const struct acpi_device_id stk8312_acpi_id[] = { 645 {"STK8312", 0}, 646 {} 647 }; 648 649 MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id); 650 651 static struct i2c_driver stk8312_driver = { 652 .driver = { 653 .name = STK8312_DRIVER_NAME, 654 .pm = STK8312_PM_OPS, 655 .acpi_match_table = ACPI_PTR(stk8312_acpi_id), 656 }, 657 .probe = stk8312_probe, 658 .remove = stk8312_remove, 659 .id_table = stk8312_i2c_id, 660 }; 661 662 module_i2c_driver(stk8312_driver); 663 664 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 665 MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver"); 666 MODULE_LICENSE("GPL v2"); 667