1 /** 2 * Sensortek STK8BA50 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 * STK8BA50 7-bit I2C address: 0x18. 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/iio/buffer.h> 20 #include <linux/iio/iio.h> 21 #include <linux/iio/sysfs.h> 22 #include <linux/iio/trigger.h> 23 #include <linux/iio/triggered_buffer.h> 24 #include <linux/iio/trigger_consumer.h> 25 26 #define STK8BA50_REG_XOUT 0x02 27 #define STK8BA50_REG_YOUT 0x04 28 #define STK8BA50_REG_ZOUT 0x06 29 #define STK8BA50_REG_RANGE 0x0F 30 #define STK8BA50_REG_BWSEL 0x10 31 #define STK8BA50_REG_POWMODE 0x11 32 #define STK8BA50_REG_SWRST 0x14 33 #define STK8BA50_REG_INTEN2 0x17 34 #define STK8BA50_REG_INTMAP2 0x1A 35 36 #define STK8BA50_MODE_NORMAL 0 37 #define STK8BA50_MODE_SUSPEND 1 38 #define STK8BA50_MODE_POWERBIT BIT(7) 39 #define STK8BA50_DATA_SHIFT 6 40 #define STK8BA50_RESET_CMD 0xB6 41 #define STK8BA50_SR_1792HZ_IDX 7 42 #define STK8BA50_DREADY_INT_MASK 0x10 43 #define STK8BA50_DREADY_INT_MAP 0x81 44 #define STK8BA50_ALL_CHANNEL_MASK 7 45 #define STK8BA50_ALL_CHANNEL_SIZE 6 46 47 #define STK8BA50_DRIVER_NAME "stk8ba50" 48 #define STK8BA50_GPIO "stk8ba50_gpio" 49 #define STK8BA50_IRQ_NAME "stk8ba50_event" 50 51 #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069" 52 53 /* 54 * The accelerometer has four measurement ranges: 55 * +/-2g; +/-4g; +/-8g; +/-16g 56 * 57 * Acceleration values are 10-bit, 2's complement. 58 * Scales are calculated as following: 59 * 60 * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384 61 * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767 62 * etc. 63 * 64 * Scales are stored in this format: 65 * { <register value>, <scale value> } 66 * 67 * Locally, the range is stored as a table index. 68 */ 69 static const struct { 70 u8 reg_val; 71 u32 scale_val; 72 } stk8ba50_scale_table[] = { 73 {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900} 74 }; 75 76 /* Sample rates are stored as { <register value>, <Hz value> } */ 77 static const struct { 78 u8 reg_val; 79 u16 samp_freq; 80 } stk8ba50_samp_freq_table[] = { 81 {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112}, 82 {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792} 83 }; 84 85 /* Used to map scan mask bits to their corresponding channel register. */ 86 static const int stk8ba50_channel_table[] = { 87 STK8BA50_REG_XOUT, 88 STK8BA50_REG_YOUT, 89 STK8BA50_REG_ZOUT 90 }; 91 92 struct stk8ba50_data { 93 struct i2c_client *client; 94 struct mutex lock; 95 int range; 96 u8 sample_rate_idx; 97 struct iio_trigger *dready_trig; 98 bool dready_trigger_on; 99 /* 100 * 3 x 16-bit channels (10-bit data, 6-bit padding) + 101 * 1 x 16 padding + 102 * 4 x 16 64-bit timestamp 103 */ 104 s16 buffer[8]; 105 }; 106 107 #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \ 108 .type = IIO_ACCEL, \ 109 .address = reg, \ 110 .modified = 1, \ 111 .channel2 = IIO_MOD_##axis, \ 112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 113 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 114 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 115 .scan_index = index, \ 116 .scan_type = { \ 117 .sign = 's', \ 118 .realbits = 10, \ 119 .storagebits = 16, \ 120 .shift = STK8BA50_DATA_SHIFT, \ 121 .endianness = IIO_CPU, \ 122 }, \ 123 } 124 125 static const struct iio_chan_spec stk8ba50_channels[] = { 126 STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X), 127 STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y), 128 STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z), 129 IIO_CHAN_SOFT_TIMESTAMP(3), 130 }; 131 132 static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL); 133 134 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792"); 135 136 static struct attribute *stk8ba50_attributes[] = { 137 &iio_const_attr_in_accel_scale_available.dev_attr.attr, 138 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 139 NULL, 140 }; 141 142 static const struct attribute_group stk8ba50_attribute_group = { 143 .attrs = stk8ba50_attributes 144 }; 145 146 static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg) 147 { 148 int ret; 149 struct i2c_client *client = data->client; 150 151 ret = i2c_smbus_read_word_data(client, reg); 152 if (ret < 0) { 153 dev_err(&client->dev, "register read failed\n"); 154 return ret; 155 } 156 157 return ret; 158 } 159 160 static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig, 161 bool state) 162 { 163 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 164 struct stk8ba50_data *data = iio_priv(indio_dev); 165 int ret; 166 167 if (state) 168 ret = i2c_smbus_write_byte_data(data->client, 169 STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); 170 else 171 ret = i2c_smbus_write_byte_data(data->client, 172 STK8BA50_REG_INTEN2, 0x00); 173 174 if (ret < 0) 175 dev_err(&data->client->dev, "failed to set trigger state\n"); 176 else 177 data->dready_trigger_on = state; 178 179 return ret; 180 } 181 182 static const struct iio_trigger_ops stk8ba50_trigger_ops = { 183 .set_trigger_state = stk8ba50_data_rdy_trigger_set_state, 184 .owner = THIS_MODULE, 185 }; 186 187 static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode) 188 { 189 int ret; 190 u8 masked_reg; 191 struct i2c_client *client = data->client; 192 193 ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE); 194 if (ret < 0) 195 goto exit_err; 196 197 if (mode) 198 masked_reg = ret | STK8BA50_MODE_POWERBIT; 199 else 200 masked_reg = ret & (~STK8BA50_MODE_POWERBIT); 201 202 ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE, 203 masked_reg); 204 if (ret < 0) 205 goto exit_err; 206 207 return ret; 208 209 exit_err: 210 dev_err(&client->dev, "failed to change sensor mode\n"); 211 return ret; 212 } 213 214 static int stk8ba50_read_raw(struct iio_dev *indio_dev, 215 struct iio_chan_spec const *chan, 216 int *val, int *val2, long mask) 217 { 218 struct stk8ba50_data *data = iio_priv(indio_dev); 219 int ret; 220 221 switch (mask) { 222 case IIO_CHAN_INFO_RAW: 223 if (iio_buffer_enabled(indio_dev)) 224 return -EBUSY; 225 mutex_lock(&data->lock); 226 ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 227 if (ret < 0) { 228 mutex_unlock(&data->lock); 229 return -EINVAL; 230 } 231 ret = stk8ba50_read_accel(data, chan->address); 232 if (ret < 0) { 233 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 234 mutex_unlock(&data->lock); 235 return -EINVAL; 236 } 237 *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9); 238 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 239 mutex_unlock(&data->lock); 240 return IIO_VAL_INT; 241 case IIO_CHAN_INFO_SCALE: 242 *val = 0; 243 *val2 = stk8ba50_scale_table[data->range].scale_val; 244 return IIO_VAL_INT_PLUS_MICRO; 245 case IIO_CHAN_INFO_SAMP_FREQ: 246 *val = stk8ba50_samp_freq_table 247 [data->sample_rate_idx].samp_freq; 248 *val2 = 0; 249 return IIO_VAL_INT; 250 } 251 252 return -EINVAL; 253 } 254 255 static int stk8ba50_write_raw(struct iio_dev *indio_dev, 256 struct iio_chan_spec const *chan, 257 int val, int val2, long mask) 258 { 259 int ret; 260 int i; 261 int index = -1; 262 struct stk8ba50_data *data = iio_priv(indio_dev); 263 264 switch (mask) { 265 case IIO_CHAN_INFO_SCALE: 266 if (val != 0) 267 return -EINVAL; 268 269 for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++) 270 if (val2 == stk8ba50_scale_table[i].scale_val) { 271 index = i; 272 break; 273 } 274 if (index < 0) 275 return -EINVAL; 276 277 ret = i2c_smbus_write_byte_data(data->client, 278 STK8BA50_REG_RANGE, 279 stk8ba50_scale_table[index].reg_val); 280 if (ret < 0) 281 dev_err(&data->client->dev, 282 "failed to set measurement range\n"); 283 else 284 data->range = index; 285 286 return ret; 287 case IIO_CHAN_INFO_SAMP_FREQ: 288 for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++) 289 if (val == stk8ba50_samp_freq_table[i].samp_freq) { 290 index = i; 291 break; 292 } 293 if (index < 0) 294 return -EINVAL; 295 296 ret = i2c_smbus_write_byte_data(data->client, 297 STK8BA50_REG_BWSEL, 298 stk8ba50_samp_freq_table[index].reg_val); 299 if (ret < 0) 300 dev_err(&data->client->dev, 301 "failed to set sampling rate\n"); 302 else 303 data->sample_rate_idx = index; 304 305 return ret; 306 } 307 308 return -EINVAL; 309 } 310 311 static const struct iio_info stk8ba50_info = { 312 .driver_module = THIS_MODULE, 313 .read_raw = stk8ba50_read_raw, 314 .write_raw = stk8ba50_write_raw, 315 .attrs = &stk8ba50_attribute_group, 316 }; 317 318 static irqreturn_t stk8ba50_trigger_handler(int irq, void *p) 319 { 320 struct iio_poll_func *pf = p; 321 struct iio_dev *indio_dev = pf->indio_dev; 322 struct stk8ba50_data *data = iio_priv(indio_dev); 323 int bit, ret, i = 0; 324 325 mutex_lock(&data->lock); 326 /* 327 * Do a bulk read if all channels are requested, 328 * from 0x02 (XOUT1) to 0x07 (ZOUT2) 329 */ 330 if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) { 331 ret = i2c_smbus_read_i2c_block_data(data->client, 332 STK8BA50_REG_XOUT, 333 STK8BA50_ALL_CHANNEL_SIZE, 334 (u8 *)data->buffer); 335 if (ret < STK8BA50_ALL_CHANNEL_SIZE) { 336 dev_err(&data->client->dev, "register read failed\n"); 337 goto err; 338 } 339 } else { 340 for_each_set_bit(bit, indio_dev->active_scan_mask, 341 indio_dev->masklength) { 342 ret = stk8ba50_read_accel(data, 343 stk8ba50_channel_table[bit]); 344 if (ret < 0) 345 goto err; 346 347 data->buffer[i++] = ret; 348 } 349 } 350 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 351 pf->timestamp); 352 err: 353 mutex_unlock(&data->lock); 354 iio_trigger_notify_done(indio_dev->trig); 355 356 return IRQ_HANDLED; 357 } 358 359 static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private) 360 { 361 struct iio_dev *indio_dev = private; 362 struct stk8ba50_data *data = iio_priv(indio_dev); 363 364 if (data->dready_trigger_on) 365 iio_trigger_poll(data->dready_trig); 366 367 return IRQ_HANDLED; 368 } 369 370 static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev) 371 { 372 struct stk8ba50_data *data = iio_priv(indio_dev); 373 374 return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 375 } 376 377 static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev) 378 { 379 struct stk8ba50_data *data = iio_priv(indio_dev); 380 381 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 382 } 383 384 static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = { 385 .preenable = stk8ba50_buffer_preenable, 386 .postenable = iio_triggered_buffer_postenable, 387 .predisable = iio_triggered_buffer_predisable, 388 .postdisable = stk8ba50_buffer_postdisable, 389 }; 390 391 static int stk8ba50_gpio_probe(struct i2c_client *client) 392 { 393 struct device *dev; 394 struct gpio_desc *gpio; 395 int ret; 396 397 if (!client) 398 return -EINVAL; 399 400 dev = &client->dev; 401 402 /* data ready gpio interrupt pin */ 403 gpio = devm_gpiod_get_index(dev, STK8BA50_GPIO, 0, GPIOD_IN); 404 if (IS_ERR(gpio)) { 405 dev_err(dev, "acpi gpio get index failed\n"); 406 return PTR_ERR(gpio); 407 } 408 409 ret = gpiod_to_irq(gpio); 410 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 411 412 return ret; 413 } 414 415 static int stk8ba50_probe(struct i2c_client *client, 416 const struct i2c_device_id *id) 417 { 418 int ret; 419 struct iio_dev *indio_dev; 420 struct stk8ba50_data *data; 421 422 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 423 if (!indio_dev) { 424 dev_err(&client->dev, "iio allocation failed!\n"); 425 return -ENOMEM; 426 } 427 428 data = iio_priv(indio_dev); 429 data->client = client; 430 i2c_set_clientdata(client, indio_dev); 431 mutex_init(&data->lock); 432 433 indio_dev->dev.parent = &client->dev; 434 indio_dev->info = &stk8ba50_info; 435 indio_dev->name = STK8BA50_DRIVER_NAME; 436 indio_dev->modes = INDIO_DIRECT_MODE; 437 indio_dev->channels = stk8ba50_channels; 438 indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels); 439 440 /* Reset all registers on startup */ 441 ret = i2c_smbus_write_byte_data(client, 442 STK8BA50_REG_SWRST, STK8BA50_RESET_CMD); 443 if (ret < 0) { 444 dev_err(&client->dev, "failed to reset sensor\n"); 445 goto err_power_off; 446 } 447 448 /* The default range is +/-2g */ 449 data->range = 0; 450 451 /* The default sampling rate is 1792 Hz (maximum) */ 452 data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX; 453 454 /* Set up interrupts */ 455 ret = i2c_smbus_write_byte_data(client, 456 STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); 457 if (ret < 0) { 458 dev_err(&client->dev, "failed to set up interrupts\n"); 459 goto err_power_off; 460 } 461 ret = i2c_smbus_write_byte_data(client, 462 STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP); 463 if (ret < 0) { 464 dev_err(&client->dev, "failed to set up interrupts\n"); 465 goto err_power_off; 466 } 467 468 if (client->irq < 0) 469 client->irq = stk8ba50_gpio_probe(client); 470 471 if (client->irq >= 0) { 472 ret = devm_request_threaded_irq(&client->dev, client->irq, 473 stk8ba50_data_rdy_trig_poll, 474 NULL, 475 IRQF_TRIGGER_RISING | 476 IRQF_ONESHOT, 477 STK8BA50_IRQ_NAME, 478 indio_dev); 479 if (ret < 0) { 480 dev_err(&client->dev, "request irq %d failed\n", 481 client->irq); 482 goto err_power_off; 483 } 484 485 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 486 "%s-dev%d", 487 indio_dev->name, 488 indio_dev->id); 489 if (!data->dready_trig) { 490 ret = -ENOMEM; 491 goto err_power_off; 492 } 493 494 data->dready_trig->dev.parent = &client->dev; 495 data->dready_trig->ops = &stk8ba50_trigger_ops; 496 iio_trigger_set_drvdata(data->dready_trig, indio_dev); 497 ret = iio_trigger_register(data->dready_trig); 498 if (ret) { 499 dev_err(&client->dev, "iio trigger register failed\n"); 500 goto err_power_off; 501 } 502 } 503 504 ret = iio_triggered_buffer_setup(indio_dev, 505 iio_pollfunc_store_time, 506 stk8ba50_trigger_handler, 507 &stk8ba50_buffer_setup_ops); 508 if (ret < 0) { 509 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 510 goto err_trigger_unregister; 511 } 512 513 ret = iio_device_register(indio_dev); 514 if (ret < 0) { 515 dev_err(&client->dev, "device_register failed\n"); 516 goto err_buffer_cleanup; 517 } 518 519 return ret; 520 521 err_buffer_cleanup: 522 iio_triggered_buffer_cleanup(indio_dev); 523 err_trigger_unregister: 524 if (data->dready_trig) 525 iio_trigger_unregister(data->dready_trig); 526 err_power_off: 527 stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 528 return ret; 529 } 530 531 static int stk8ba50_remove(struct i2c_client *client) 532 { 533 struct iio_dev *indio_dev = i2c_get_clientdata(client); 534 struct stk8ba50_data *data = iio_priv(indio_dev); 535 536 iio_device_unregister(indio_dev); 537 iio_triggered_buffer_cleanup(indio_dev); 538 539 if (data->dready_trig) 540 iio_trigger_unregister(data->dready_trig); 541 542 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 543 } 544 545 #ifdef CONFIG_PM_SLEEP 546 static int stk8ba50_suspend(struct device *dev) 547 { 548 struct stk8ba50_data *data; 549 550 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 551 552 return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); 553 } 554 555 static int stk8ba50_resume(struct device *dev) 556 { 557 struct stk8ba50_data *data; 558 559 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 560 561 return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); 562 } 563 564 static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume); 565 566 #define STK8BA50_PM_OPS (&stk8ba50_pm_ops) 567 #else 568 #define STK8BA50_PM_OPS NULL 569 #endif 570 571 static const struct i2c_device_id stk8ba50_i2c_id[] = { 572 {"stk8ba50", 0}, 573 {} 574 }; 575 MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id); 576 577 static const struct acpi_device_id stk8ba50_acpi_id[] = { 578 {"STK8BA50", 0}, 579 {} 580 }; 581 582 MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id); 583 584 static struct i2c_driver stk8ba50_driver = { 585 .driver = { 586 .name = "stk8ba50", 587 .pm = STK8BA50_PM_OPS, 588 .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id), 589 }, 590 .probe = stk8ba50_probe, 591 .remove = stk8ba50_remove, 592 .id_table = stk8ba50_i2c_id, 593 }; 594 595 module_i2c_driver(stk8ba50_driver); 596 597 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); 598 MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver"); 599 MODULE_LICENSE("GPL v2"); 600