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