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