1/* 2 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer 3 * 4 * Copyright (c) 2014, 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 KMX61 (7-bit I2C slave address 0x0E or 0x0F). 11 * 12 */ 13 14#include <linux/module.h> 15#include <linux/i2c.h> 16#include <linux/acpi.h> 17#include <linux/gpio/consumer.h> 18#include <linux/interrupt.h> 19#include <linux/pm.h> 20#include <linux/pm_runtime.h> 21#include <linux/iio/iio.h> 22#include <linux/iio/sysfs.h> 23#include <linux/iio/events.h> 24#include <linux/iio/trigger.h> 25#include <linux/iio/buffer.h> 26#include <linux/iio/triggered_buffer.h> 27#include <linux/iio/trigger_consumer.h> 28 29#define KMX61_DRV_NAME "kmx61" 30#define KMX61_IRQ_NAME "kmx61_event" 31 32#define KMX61_REG_WHO_AM_I 0x00 33#define KMX61_REG_INS1 0x01 34#define KMX61_REG_INS2 0x02 35 36/* 37 * three 16-bit accelerometer output registers for X/Y/Z axis 38 * we use only XOUT_L as a base register, all other addresses 39 * can be obtained by applying an offset and are provided here 40 * only for clarity. 41 */ 42#define KMX61_ACC_XOUT_L 0x0A 43#define KMX61_ACC_XOUT_H 0x0B 44#define KMX61_ACC_YOUT_L 0x0C 45#define KMX61_ACC_YOUT_H 0x0D 46#define KMX61_ACC_ZOUT_L 0x0E 47#define KMX61_ACC_ZOUT_H 0x0F 48 49/* 50 * one 16-bit temperature output register 51 */ 52#define KMX61_TEMP_L 0x10 53#define KMX61_TEMP_H 0x11 54 55/* 56 * three 16-bit magnetometer output registers for X/Y/Z axis 57 */ 58#define KMX61_MAG_XOUT_L 0x12 59#define KMX61_MAG_XOUT_H 0x13 60#define KMX61_MAG_YOUT_L 0x14 61#define KMX61_MAG_YOUT_H 0x15 62#define KMX61_MAG_ZOUT_L 0x16 63#define KMX61_MAG_ZOUT_H 0x17 64 65#define KMX61_REG_INL 0x28 66#define KMX61_REG_STBY 0x29 67#define KMX61_REG_CTRL1 0x2A 68#define KMX61_REG_CTRL2 0x2B 69#define KMX61_REG_ODCNTL 0x2C 70#define KMX61_REG_INC1 0x2D 71 72#define KMX61_REG_WUF_THRESH 0x3D 73#define KMX61_REG_WUF_TIMER 0x3E 74 75#define KMX61_ACC_STBY_BIT BIT(0) 76#define KMX61_MAG_STBY_BIT BIT(1) 77#define KMX61_ACT_STBY_BIT BIT(7) 78 79#define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT) 80 81#define KMX61_REG_INS1_BIT_WUFS BIT(1) 82 83#define KMX61_REG_INS2_BIT_ZP BIT(0) 84#define KMX61_REG_INS2_BIT_ZN BIT(1) 85#define KMX61_REG_INS2_BIT_YP BIT(2) 86#define KMX61_REG_INS2_BIT_YN BIT(3) 87#define KMX61_REG_INS2_BIT_XP BIT(4) 88#define KMX61_REG_INS2_BIT_XN BIT(5) 89 90#define KMX61_REG_CTRL1_GSEL_MASK 0x03 91 92#define KMX61_REG_CTRL1_BIT_RES BIT(4) 93#define KMX61_REG_CTRL1_BIT_DRDYE BIT(5) 94#define KMX61_REG_CTRL1_BIT_WUFE BIT(6) 95#define KMX61_REG_CTRL1_BIT_BTSE BIT(7) 96 97#define KMX61_REG_INC1_BIT_WUFS BIT(0) 98#define KMX61_REG_INC1_BIT_DRDYM BIT(1) 99#define KMX61_REG_INC1_BIT_DRDYA BIT(2) 100#define KMX61_REG_INC1_BIT_IEN BIT(5) 101 102#define KMX61_ACC_ODR_SHIFT 0 103#define KMX61_MAG_ODR_SHIFT 4 104#define KMX61_ACC_ODR_MASK 0x0F 105#define KMX61_MAG_ODR_MASK 0xF0 106 107#define KMX61_OWUF_MASK 0x7 108 109#define KMX61_DEFAULT_WAKE_THRESH 1 110#define KMX61_DEFAULT_WAKE_DURATION 1 111 112#define KMX61_SLEEP_DELAY_MS 2000 113 114#define KMX61_CHIP_ID 0x12 115 116/* KMX61 devices */ 117#define KMX61_ACC 0x01 118#define KMX61_MAG 0x02 119 120struct kmx61_data { 121 struct i2c_client *client; 122 123 /* serialize access to non-atomic ops, e.g set_mode */ 124 struct mutex lock; 125 126 /* standby state */ 127 bool acc_stby; 128 bool mag_stby; 129 130 /* power state */ 131 bool acc_ps; 132 bool mag_ps; 133 134 /* config bits */ 135 u8 range; 136 u8 odr_bits; 137 u8 wake_thresh; 138 u8 wake_duration; 139 140 /* accelerometer specific data */ 141 struct iio_dev *acc_indio_dev; 142 struct iio_trigger *acc_dready_trig; 143 struct iio_trigger *motion_trig; 144 bool acc_dready_trig_on; 145 bool motion_trig_on; 146 bool ev_enable_state; 147 148 /* magnetometer specific data */ 149 struct iio_dev *mag_indio_dev; 150 struct iio_trigger *mag_dready_trig; 151 bool mag_dready_trig_on; 152}; 153 154enum kmx61_range { 155 KMX61_RANGE_2G, 156 KMX61_RANGE_4G, 157 KMX61_RANGE_8G, 158}; 159 160enum kmx61_axis { 161 KMX61_AXIS_X, 162 KMX61_AXIS_Y, 163 KMX61_AXIS_Z, 164}; 165 166static const u16 kmx61_uscale_table[] = {9582, 19163, 38326}; 167 168static const struct { 169 int val; 170 int val2; 171} kmx61_samp_freq_table[] = { {12, 500000}, 172 {25, 0}, 173 {50, 0}, 174 {100, 0}, 175 {200, 0}, 176 {400, 0}, 177 {800, 0}, 178 {1600, 0}, 179 {0, 781000}, 180 {1, 563000}, 181 {3, 125000}, 182 {6, 250000} }; 183 184static const struct { 185 int val; 186 int val2; 187 int odr_bits; 188} kmx61_wake_up_odr_table[] = { {0, 781000, 0x00}, 189 {1, 563000, 0x01}, 190 {3, 125000, 0x02}, 191 {6, 250000, 0x03}, 192 {12, 500000, 0x04}, 193 {25, 0, 0x05}, 194 {50, 0, 0x06}, 195 {100, 0, 0x06}, 196 {200, 0, 0x06}, 197 {400, 0, 0x06}, 198 {800, 0, 0x06}, 199 {1600, 0, 0x06} }; 200 201static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326"); 202static IIO_CONST_ATTR(magn_scale_available, "0.001465"); 203static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 204 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800"); 205 206static struct attribute *kmx61_acc_attributes[] = { 207 &iio_const_attr_accel_scale_available.dev_attr.attr, 208 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 209 NULL, 210}; 211 212static struct attribute *kmx61_mag_attributes[] = { 213 &iio_const_attr_magn_scale_available.dev_attr.attr, 214 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 215 NULL, 216}; 217 218static const struct attribute_group kmx61_acc_attribute_group = { 219 .attrs = kmx61_acc_attributes, 220}; 221 222static const struct attribute_group kmx61_mag_attribute_group = { 223 .attrs = kmx61_mag_attributes, 224}; 225 226static const struct iio_event_spec kmx61_event = { 227 .type = IIO_EV_TYPE_THRESH, 228 .dir = IIO_EV_DIR_EITHER, 229 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 230 BIT(IIO_EV_INFO_ENABLE) | 231 BIT(IIO_EV_INFO_PERIOD), 232}; 233 234#define KMX61_ACC_CHAN(_axis) { \ 235 .type = IIO_ACCEL, \ 236 .modified = 1, \ 237 .channel2 = IIO_MOD_ ## _axis, \ 238 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 239 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 240 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 241 .address = KMX61_ACC, \ 242 .scan_index = KMX61_AXIS_ ## _axis, \ 243 .scan_type = { \ 244 .sign = 's', \ 245 .realbits = 12, \ 246 .storagebits = 16, \ 247 .shift = 4, \ 248 .endianness = IIO_LE, \ 249 }, \ 250 .event_spec = &kmx61_event, \ 251 .num_event_specs = 1 \ 252} 253 254#define KMX61_MAG_CHAN(_axis) { \ 255 .type = IIO_MAGN, \ 256 .modified = 1, \ 257 .channel2 = IIO_MOD_ ## _axis, \ 258 .address = KMX61_MAG, \ 259 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 260 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 261 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 262 .scan_index = KMX61_AXIS_ ## _axis, \ 263 .scan_type = { \ 264 .sign = 's', \ 265 .realbits = 14, \ 266 .storagebits = 16, \ 267 .shift = 2, \ 268 .endianness = IIO_LE, \ 269 }, \ 270} 271 272static const struct iio_chan_spec kmx61_acc_channels[] = { 273 KMX61_ACC_CHAN(X), 274 KMX61_ACC_CHAN(Y), 275 KMX61_ACC_CHAN(Z), 276}; 277 278static const struct iio_chan_spec kmx61_mag_channels[] = { 279 KMX61_MAG_CHAN(X), 280 KMX61_MAG_CHAN(Y), 281 KMX61_MAG_CHAN(Z), 282}; 283 284static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data) 285{ 286 struct kmx61_data **priv = iio_priv(indio_dev); 287 288 *priv = data; 289} 290 291static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev) 292{ 293 return *(struct kmx61_data **)iio_priv(indio_dev); 294} 295 296static int kmx61_convert_freq_to_bit(int val, int val2) 297{ 298 int i; 299 300 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++) 301 if (val == kmx61_samp_freq_table[i].val && 302 val2 == kmx61_samp_freq_table[i].val2) 303 return i; 304 return -EINVAL; 305} 306 307static int kmx61_convert_wake_up_odr_to_bit(int val, int val2) 308{ 309 int i; 310 311 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i) 312 if (kmx61_wake_up_odr_table[i].val == val && 313 kmx61_wake_up_odr_table[i].val2 == val2) 314 return kmx61_wake_up_odr_table[i].odr_bits; 315 return -EINVAL; 316} 317 318/** 319 * kmx61_set_mode() - set KMX61 device operating mode 320 * @data - kmx61 device private data pointer 321 * @mode - bitmask, indicating operating mode for @device 322 * @device - bitmask, indicating device for which @mode needs to be set 323 * @update - update stby bits stored in device's private @data 324 * 325 * For each sensor (accelerometer/magnetometer) there are two operating modes 326 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently 327 * if they are both enabled. Internal sensors state is saved in acc_stby and 328 * mag_stby members of driver's private @data. 329 */ 330static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device, 331 bool update) 332{ 333 int ret; 334 int acc_stby = -1, mag_stby = -1; 335 336 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY); 337 if (ret < 0) { 338 dev_err(&data->client->dev, "Error reading reg_stby\n"); 339 return ret; 340 } 341 if (device & KMX61_ACC) { 342 if (mode & KMX61_ACC_STBY_BIT) { 343 ret |= KMX61_ACC_STBY_BIT; 344 acc_stby = 1; 345 } else { 346 ret &= ~KMX61_ACC_STBY_BIT; 347 acc_stby = 0; 348 } 349 } 350 351 if (device & KMX61_MAG) { 352 if (mode & KMX61_MAG_STBY_BIT) { 353 ret |= KMX61_MAG_STBY_BIT; 354 mag_stby = 1; 355 } else { 356 ret &= ~KMX61_MAG_STBY_BIT; 357 mag_stby = 0; 358 } 359 } 360 361 if (mode & KMX61_ACT_STBY_BIT) 362 ret |= KMX61_ACT_STBY_BIT; 363 364 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret); 365 if (ret < 0) { 366 dev_err(&data->client->dev, "Error writing reg_stby\n"); 367 return ret; 368 } 369 370 if (acc_stby != -1 && update) 371 data->acc_stby = acc_stby; 372 if (mag_stby != -1 && update) 373 data->mag_stby = mag_stby; 374 375 return 0; 376} 377 378static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device) 379{ 380 int ret; 381 382 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY); 383 if (ret < 0) { 384 dev_err(&data->client->dev, "Error reading reg_stby\n"); 385 return ret; 386 } 387 *mode = 0; 388 389 if (device & KMX61_ACC) { 390 if (ret & KMX61_ACC_STBY_BIT) 391 *mode |= KMX61_ACC_STBY_BIT; 392 else 393 *mode &= ~KMX61_ACC_STBY_BIT; 394 } 395 396 if (device & KMX61_MAG) { 397 if (ret & KMX61_MAG_STBY_BIT) 398 *mode |= KMX61_MAG_STBY_BIT; 399 else 400 *mode &= ~KMX61_MAG_STBY_BIT; 401 } 402 403 return 0; 404} 405 406static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2) 407{ 408 int ret, odr_bits; 409 410 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2); 411 if (odr_bits < 0) 412 return odr_bits; 413 414 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2, 415 odr_bits); 416 if (ret < 0) 417 dev_err(&data->client->dev, "Error writing reg_ctrl2\n"); 418 return ret; 419} 420 421static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device) 422{ 423 int ret; 424 u8 mode; 425 int lodr_bits, odr_bits; 426 427 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG); 428 if (ret < 0) 429 return ret; 430 431 lodr_bits = kmx61_convert_freq_to_bit(val, val2); 432 if (lodr_bits < 0) 433 return lodr_bits; 434 435 /* To change ODR, accel and magn must be in STDBY */ 436 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, 437 true); 438 if (ret < 0) 439 return ret; 440 441 odr_bits = 0; 442 if (device & KMX61_ACC) 443 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT; 444 if (device & KMX61_MAG) 445 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT; 446 447 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL, 448 odr_bits); 449 if (ret < 0) 450 return ret; 451 452 data->odr_bits = odr_bits; 453 454 if (device & KMX61_ACC) { 455 ret = kmx61_set_wake_up_odr(data, val, val2); 456 if (ret) 457 return ret; 458 } 459 460 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true); 461} 462 463static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, 464 u8 device) 465{ 466 u8 lodr_bits; 467 468 if (device & KMX61_ACC) 469 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) & 470 KMX61_ACC_ODR_MASK; 471 else if (device & KMX61_MAG) 472 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) & 473 KMX61_MAG_ODR_MASK; 474 else 475 return -EINVAL; 476 477 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table)) 478 return -EINVAL; 479 480 *val = kmx61_samp_freq_table[lodr_bits].val; 481 *val2 = kmx61_samp_freq_table[lodr_bits].val2; 482 483 return 0; 484} 485 486static int kmx61_set_range(struct kmx61_data *data, u8 range) 487{ 488 int ret; 489 490 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); 491 if (ret < 0) { 492 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 493 return ret; 494 } 495 496 ret &= ~KMX61_REG_CTRL1_GSEL_MASK; 497 ret |= range & KMX61_REG_CTRL1_GSEL_MASK; 498 499 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); 500 if (ret < 0) { 501 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 502 return ret; 503 } 504 505 data->range = range; 506 507 return 0; 508} 509 510static int kmx61_set_scale(struct kmx61_data *data, u16 uscale) 511{ 512 int ret, i; 513 u8 mode; 514 515 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) { 516 if (kmx61_uscale_table[i] == uscale) { 517 ret = kmx61_get_mode(data, &mode, 518 KMX61_ACC | KMX61_MAG); 519 if (ret < 0) 520 return ret; 521 522 ret = kmx61_set_mode(data, KMX61_ALL_STBY, 523 KMX61_ACC | KMX61_MAG, true); 524 if (ret < 0) 525 return ret; 526 527 ret = kmx61_set_range(data, i); 528 if (ret < 0) 529 return ret; 530 531 return kmx61_set_mode(data, mode, 532 KMX61_ACC | KMX61_MAG, true); 533 } 534 } 535 return -EINVAL; 536} 537 538static int kmx61_chip_init(struct kmx61_data *data) 539{ 540 int ret, val, val2; 541 542 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I); 543 if (ret < 0) { 544 dev_err(&data->client->dev, "Error reading who_am_i\n"); 545 return ret; 546 } 547 548 if (ret != KMX61_CHIP_ID) { 549 dev_err(&data->client->dev, 550 "Wrong chip id, got %x expected %x\n", 551 ret, KMX61_CHIP_ID); 552 return -EINVAL; 553 } 554 555 /* set accel 12bit, 4g range */ 556 ret = kmx61_set_range(data, KMX61_RANGE_4G); 557 if (ret < 0) 558 return ret; 559 560 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL); 561 if (ret < 0) { 562 dev_err(&data->client->dev, "Error reading reg_odcntl\n"); 563 return ret; 564 } 565 data->odr_bits = ret; 566 567 /* 568 * set output data rate for wake up (motion detection) function 569 * to match data rate for accelerometer sampling 570 */ 571 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC); 572 if (ret < 0) 573 return ret; 574 575 ret = kmx61_set_wake_up_odr(data, val, val2); 576 if (ret < 0) 577 return ret; 578 579 /* set acc/magn to OPERATION mode */ 580 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true); 581 if (ret < 0) 582 return ret; 583 584 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH; 585 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION; 586 587 return 0; 588} 589 590static int kmx61_setup_new_data_interrupt(struct kmx61_data *data, 591 bool status, u8 device) 592{ 593 u8 mode; 594 int ret; 595 596 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG); 597 if (ret < 0) 598 return ret; 599 600 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true); 601 if (ret < 0) 602 return ret; 603 604 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1); 605 if (ret < 0) { 606 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 607 return ret; 608 } 609 610 if (status) { 611 ret |= KMX61_REG_INC1_BIT_IEN; 612 if (device & KMX61_ACC) 613 ret |= KMX61_REG_INC1_BIT_DRDYA; 614 if (device & KMX61_MAG) 615 ret |= KMX61_REG_INC1_BIT_DRDYM; 616 } else { 617 ret &= ~KMX61_REG_INC1_BIT_IEN; 618 if (device & KMX61_ACC) 619 ret &= ~KMX61_REG_INC1_BIT_DRDYA; 620 if (device & KMX61_MAG) 621 ret &= ~KMX61_REG_INC1_BIT_DRDYM; 622 } 623 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret); 624 if (ret < 0) { 625 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); 626 return ret; 627 } 628 629 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); 630 if (ret < 0) { 631 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 632 return ret; 633 } 634 635 if (status) 636 ret |= KMX61_REG_CTRL1_BIT_DRDYE; 637 else 638 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE; 639 640 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); 641 if (ret < 0) { 642 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 643 return ret; 644 } 645 646 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true); 647} 648 649static int kmx61_chip_update_thresholds(struct kmx61_data *data) 650{ 651 int ret; 652 653 ret = i2c_smbus_write_byte_data(data->client, 654 KMX61_REG_WUF_TIMER, 655 data->wake_duration); 656 if (ret < 0) { 657 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n"); 658 return ret; 659 } 660 661 ret = i2c_smbus_write_byte_data(data->client, 662 KMX61_REG_WUF_THRESH, 663 data->wake_thresh); 664 if (ret < 0) 665 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n"); 666 667 return ret; 668} 669 670static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data, 671 bool status) 672{ 673 u8 mode; 674 int ret; 675 676 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG); 677 if (ret < 0) 678 return ret; 679 680 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true); 681 if (ret < 0) 682 return ret; 683 684 ret = kmx61_chip_update_thresholds(data); 685 if (ret < 0) 686 return ret; 687 688 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1); 689 if (ret < 0) { 690 dev_err(&data->client->dev, "Error reading reg_inc1\n"); 691 return ret; 692 } 693 if (status) 694 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS); 695 else 696 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS); 697 698 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret); 699 if (ret < 0) { 700 dev_err(&data->client->dev, "Error writing reg_inc1\n"); 701 return ret; 702 } 703 704 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); 705 if (ret < 0) { 706 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 707 return ret; 708 } 709 710 if (status) 711 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE; 712 else 713 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE); 714 715 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); 716 if (ret < 0) { 717 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 718 return ret; 719 } 720 mode |= KMX61_ACT_STBY_BIT; 721 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true); 722} 723 724/** 725 * kmx61_set_power_state() - set power state for kmx61 @device 726 * @data - kmx61 device private pointer 727 * @on - power state to be set for @device 728 * @device - bitmask indicating device for which @on state needs to be set 729 * 730 * Notice that when ACC power state needs to be set to ON and MAG is in 731 * OPERATION then we know that kmx61_runtime_resume was already called 732 * so we must set ACC OPERATION mode here. The same happens when MAG power 733 * state needs to be set to ON and ACC is in OPERATION. 734 */ 735static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device) 736{ 737#ifdef CONFIG_PM 738 int ret; 739 740 if (device & KMX61_ACC) { 741 if (on && !data->acc_ps && !data->mag_stby) { 742 ret = kmx61_set_mode(data, 0, KMX61_ACC, true); 743 if (ret < 0) 744 return ret; 745 } 746 data->acc_ps = on; 747 } 748 if (device & KMX61_MAG) { 749 if (on && !data->mag_ps && !data->acc_stby) { 750 ret = kmx61_set_mode(data, 0, KMX61_MAG, true); 751 if (ret < 0) 752 return ret; 753 } 754 data->mag_ps = on; 755 } 756 757 if (on) { 758 ret = pm_runtime_get_sync(&data->client->dev); 759 } else { 760 pm_runtime_mark_last_busy(&data->client->dev); 761 ret = pm_runtime_put_autosuspend(&data->client->dev); 762 } 763 if (ret < 0) { 764 dev_err(&data->client->dev, 765 "Failed: kmx61_set_power_state for %d, ret %d\n", 766 on, ret); 767 if (on) 768 pm_runtime_put_noidle(&data->client->dev); 769 770 return ret; 771 } 772#endif 773 return 0; 774} 775 776static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset) 777{ 778 int ret; 779 u8 reg = base + offset * 2; 780 781 ret = i2c_smbus_read_word_data(data->client, reg); 782 if (ret < 0) 783 dev_err(&data->client->dev, "failed to read reg at %x\n", reg); 784 785 return ret; 786} 787 788static int kmx61_read_raw(struct iio_dev *indio_dev, 789 struct iio_chan_spec const *chan, int *val, 790 int *val2, long mask) 791{ 792 int ret; 793 u8 base_reg; 794 struct kmx61_data *data = kmx61_get_data(indio_dev); 795 796 switch (mask) { 797 case IIO_CHAN_INFO_RAW: 798 switch (chan->type) { 799 case IIO_ACCEL: 800 base_reg = KMX61_ACC_XOUT_L; 801 break; 802 case IIO_MAGN: 803 base_reg = KMX61_MAG_XOUT_L; 804 break; 805 default: 806 return -EINVAL; 807 } 808 mutex_lock(&data->lock); 809 810 ret = kmx61_set_power_state(data, true, chan->address); 811 if (ret) { 812 mutex_unlock(&data->lock); 813 return ret; 814 } 815 816 ret = kmx61_read_measurement(data, base_reg, chan->scan_index); 817 if (ret < 0) { 818 kmx61_set_power_state(data, false, chan->address); 819 mutex_unlock(&data->lock); 820 return ret; 821 } 822 *val = sign_extend32(ret >> chan->scan_type.shift, 823 chan->scan_type.realbits - 1); 824 ret = kmx61_set_power_state(data, false, chan->address); 825 826 mutex_unlock(&data->lock); 827 if (ret) 828 return ret; 829 return IIO_VAL_INT; 830 case IIO_CHAN_INFO_SCALE: 831 switch (chan->type) { 832 case IIO_ACCEL: 833 *val = 0; 834 *val2 = kmx61_uscale_table[data->range]; 835 return IIO_VAL_INT_PLUS_MICRO; 836 case IIO_MAGN: 837 /* 14 bits res, 1465 microGauss per magn count */ 838 *val = 0; 839 *val2 = 1465; 840 return IIO_VAL_INT_PLUS_MICRO; 841 default: 842 return -EINVAL; 843 } 844 case IIO_CHAN_INFO_SAMP_FREQ: 845 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN) 846 return -EINVAL; 847 848 mutex_lock(&data->lock); 849 ret = kmx61_get_odr(data, val, val2, chan->address); 850 mutex_unlock(&data->lock); 851 if (ret) 852 return -EINVAL; 853 return IIO_VAL_INT_PLUS_MICRO; 854 } 855 return -EINVAL; 856} 857 858static int kmx61_write_raw(struct iio_dev *indio_dev, 859 struct iio_chan_spec const *chan, int val, 860 int val2, long mask) 861{ 862 int ret; 863 struct kmx61_data *data = kmx61_get_data(indio_dev); 864 865 switch (mask) { 866 case IIO_CHAN_INFO_SAMP_FREQ: 867 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN) 868 return -EINVAL; 869 870 mutex_lock(&data->lock); 871 ret = kmx61_set_odr(data, val, val2, chan->address); 872 mutex_unlock(&data->lock); 873 return ret; 874 case IIO_CHAN_INFO_SCALE: 875 switch (chan->type) { 876 case IIO_ACCEL: 877 if (val != 0) 878 return -EINVAL; 879 mutex_lock(&data->lock); 880 ret = kmx61_set_scale(data, val2); 881 mutex_unlock(&data->lock); 882 return ret; 883 default: 884 return -EINVAL; 885 } 886 default: 887 return -EINVAL; 888 } 889} 890 891static int kmx61_read_event(struct iio_dev *indio_dev, 892 const struct iio_chan_spec *chan, 893 enum iio_event_type type, 894 enum iio_event_direction dir, 895 enum iio_event_info info, 896 int *val, int *val2) 897{ 898 struct kmx61_data *data = kmx61_get_data(indio_dev); 899 900 *val2 = 0; 901 switch (info) { 902 case IIO_EV_INFO_VALUE: 903 *val = data->wake_thresh; 904 return IIO_VAL_INT; 905 case IIO_EV_INFO_PERIOD: 906 *val = data->wake_duration; 907 return IIO_VAL_INT; 908 default: 909 return -EINVAL; 910 } 911} 912 913static int kmx61_write_event(struct iio_dev *indio_dev, 914 const struct iio_chan_spec *chan, 915 enum iio_event_type type, 916 enum iio_event_direction dir, 917 enum iio_event_info info, 918 int val, int val2) 919{ 920 struct kmx61_data *data = kmx61_get_data(indio_dev); 921 922 if (data->ev_enable_state) 923 return -EBUSY; 924 925 switch (info) { 926 case IIO_EV_INFO_VALUE: 927 data->wake_thresh = val; 928 return IIO_VAL_INT; 929 case IIO_EV_INFO_PERIOD: 930 data->wake_duration = val; 931 return IIO_VAL_INT; 932 default: 933 return -EINVAL; 934 } 935} 936 937static int kmx61_read_event_config(struct iio_dev *indio_dev, 938 const struct iio_chan_spec *chan, 939 enum iio_event_type type, 940 enum iio_event_direction dir) 941{ 942 struct kmx61_data *data = kmx61_get_data(indio_dev); 943 944 return data->ev_enable_state; 945} 946 947static int kmx61_write_event_config(struct iio_dev *indio_dev, 948 const struct iio_chan_spec *chan, 949 enum iio_event_type type, 950 enum iio_event_direction dir, 951 int state) 952{ 953 struct kmx61_data *data = kmx61_get_data(indio_dev); 954 int ret = 0; 955 956 if (state && data->ev_enable_state) 957 return 0; 958 959 mutex_lock(&data->lock); 960 961 if (!state && data->motion_trig_on) { 962 data->ev_enable_state = false; 963 goto err_unlock; 964 } 965 966 ret = kmx61_set_power_state(data, state, KMX61_ACC); 967 if (ret < 0) 968 goto err_unlock; 969 970 ret = kmx61_setup_any_motion_interrupt(data, state); 971 if (ret < 0) { 972 kmx61_set_power_state(data, false, KMX61_ACC); 973 goto err_unlock; 974 } 975 976 data->ev_enable_state = state; 977 978err_unlock: 979 mutex_unlock(&data->lock); 980 981 return ret; 982} 983 984static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev, 985 struct iio_trigger *trig) 986{ 987 struct kmx61_data *data = kmx61_get_data(indio_dev); 988 989 if (data->acc_dready_trig != trig && data->motion_trig != trig) 990 return -EINVAL; 991 992 return 0; 993} 994 995static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev, 996 struct iio_trigger *trig) 997{ 998 struct kmx61_data *data = kmx61_get_data(indio_dev); 999 1000 if (data->mag_dready_trig != trig) 1001 return -EINVAL; 1002 1003 return 0; 1004} 1005 1006static const struct iio_info kmx61_acc_info = { 1007 .driver_module = THIS_MODULE, 1008 .read_raw = kmx61_read_raw, 1009 .write_raw = kmx61_write_raw, 1010 .attrs = &kmx61_acc_attribute_group, 1011 .read_event_value = kmx61_read_event, 1012 .write_event_value = kmx61_write_event, 1013 .read_event_config = kmx61_read_event_config, 1014 .write_event_config = kmx61_write_event_config, 1015 .validate_trigger = kmx61_acc_validate_trigger, 1016}; 1017 1018static const struct iio_info kmx61_mag_info = { 1019 .driver_module = THIS_MODULE, 1020 .read_raw = kmx61_read_raw, 1021 .write_raw = kmx61_write_raw, 1022 .attrs = &kmx61_mag_attribute_group, 1023 .validate_trigger = kmx61_mag_validate_trigger, 1024}; 1025 1026 1027static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig, 1028 bool state) 1029{ 1030 int ret = 0; 1031 u8 device; 1032 1033 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1034 struct kmx61_data *data = kmx61_get_data(indio_dev); 1035 1036 mutex_lock(&data->lock); 1037 1038 if (!state && data->ev_enable_state && data->motion_trig_on) { 1039 data->motion_trig_on = false; 1040 goto err_unlock; 1041 } 1042 1043 if (data->acc_dready_trig == trig || data->motion_trig == trig) 1044 device = KMX61_ACC; 1045 else 1046 device = KMX61_MAG; 1047 1048 ret = kmx61_set_power_state(data, state, device); 1049 if (ret < 0) 1050 goto err_unlock; 1051 1052 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig) 1053 ret = kmx61_setup_new_data_interrupt(data, state, device); 1054 else 1055 ret = kmx61_setup_any_motion_interrupt(data, state); 1056 if (ret < 0) { 1057 kmx61_set_power_state(data, false, device); 1058 goto err_unlock; 1059 } 1060 1061 if (data->acc_dready_trig == trig) 1062 data->acc_dready_trig_on = state; 1063 else if (data->mag_dready_trig == trig) 1064 data->mag_dready_trig_on = state; 1065 else 1066 data->motion_trig_on = state; 1067err_unlock: 1068 mutex_unlock(&data->lock); 1069 1070 return ret; 1071} 1072 1073static int kmx61_trig_try_reenable(struct iio_trigger *trig) 1074{ 1075 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1076 struct kmx61_data *data = kmx61_get_data(indio_dev); 1077 int ret; 1078 1079 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL); 1080 if (ret < 0) { 1081 dev_err(&data->client->dev, "Error reading reg_inl\n"); 1082 return ret; 1083 } 1084 1085 return 0; 1086} 1087 1088static const struct iio_trigger_ops kmx61_trigger_ops = { 1089 .set_trigger_state = kmx61_data_rdy_trigger_set_state, 1090 .try_reenable = kmx61_trig_try_reenable, 1091 .owner = THIS_MODULE, 1092}; 1093 1094static irqreturn_t kmx61_event_handler(int irq, void *private) 1095{ 1096 struct kmx61_data *data = private; 1097 struct iio_dev *indio_dev = data->acc_indio_dev; 1098 int ret; 1099 1100 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1); 1101 if (ret < 0) { 1102 dev_err(&data->client->dev, "Error reading reg_ins1\n"); 1103 goto ack_intr; 1104 } 1105 1106 if (ret & KMX61_REG_INS1_BIT_WUFS) { 1107 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2); 1108 if (ret < 0) { 1109 dev_err(&data->client->dev, "Error reading reg_ins2\n"); 1110 goto ack_intr; 1111 } 1112 1113 if (ret & KMX61_REG_INS2_BIT_XN) 1114 iio_push_event(indio_dev, 1115 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1116 0, 1117 IIO_MOD_X, 1118 IIO_EV_TYPE_THRESH, 1119 IIO_EV_DIR_FALLING), 1120 0); 1121 1122 if (ret & KMX61_REG_INS2_BIT_XP) 1123 iio_push_event(indio_dev, 1124 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1125 0, 1126 IIO_MOD_X, 1127 IIO_EV_TYPE_THRESH, 1128 IIO_EV_DIR_RISING), 1129 0); 1130 1131 if (ret & KMX61_REG_INS2_BIT_YN) 1132 iio_push_event(indio_dev, 1133 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1134 0, 1135 IIO_MOD_Y, 1136 IIO_EV_TYPE_THRESH, 1137 IIO_EV_DIR_FALLING), 1138 0); 1139 1140 if (ret & KMX61_REG_INS2_BIT_YP) 1141 iio_push_event(indio_dev, 1142 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1143 0, 1144 IIO_MOD_Y, 1145 IIO_EV_TYPE_THRESH, 1146 IIO_EV_DIR_RISING), 1147 0); 1148 1149 if (ret & KMX61_REG_INS2_BIT_ZN) 1150 iio_push_event(indio_dev, 1151 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1152 0, 1153 IIO_MOD_Z, 1154 IIO_EV_TYPE_THRESH, 1155 IIO_EV_DIR_FALLING), 1156 0); 1157 1158 if (ret & KMX61_REG_INS2_BIT_ZP) 1159 iio_push_event(indio_dev, 1160 IIO_MOD_EVENT_CODE(IIO_ACCEL, 1161 0, 1162 IIO_MOD_Z, 1163 IIO_EV_TYPE_THRESH, 1164 IIO_EV_DIR_RISING), 1165 0); 1166 } 1167 1168ack_intr: 1169 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1); 1170 if (ret < 0) 1171 dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); 1172 1173 ret |= KMX61_REG_CTRL1_BIT_RES; 1174 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret); 1175 if (ret < 0) 1176 dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); 1177 1178 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL); 1179 if (ret < 0) 1180 dev_err(&data->client->dev, "Error reading reg_inl\n"); 1181 1182 return IRQ_HANDLED; 1183} 1184 1185static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private) 1186{ 1187 struct kmx61_data *data = private; 1188 1189 if (data->acc_dready_trig_on) 1190 iio_trigger_poll(data->acc_dready_trig); 1191 if (data->mag_dready_trig_on) 1192 iio_trigger_poll(data->mag_dready_trig); 1193 1194 if (data->motion_trig_on) 1195 iio_trigger_poll(data->motion_trig); 1196 1197 if (data->ev_enable_state) 1198 return IRQ_WAKE_THREAD; 1199 return IRQ_HANDLED; 1200} 1201 1202static irqreturn_t kmx61_trigger_handler(int irq, void *p) 1203{ 1204 struct iio_poll_func *pf = p; 1205 struct iio_dev *indio_dev = pf->indio_dev; 1206 struct kmx61_data *data = kmx61_get_data(indio_dev); 1207 int bit, ret, i = 0; 1208 u8 base; 1209 s16 buffer[8]; 1210 1211 if (indio_dev == data->acc_indio_dev) 1212 base = KMX61_ACC_XOUT_L; 1213 else 1214 base = KMX61_MAG_XOUT_L; 1215 1216 mutex_lock(&data->lock); 1217 for_each_set_bit(bit, indio_dev->active_scan_mask, 1218 indio_dev->masklength) { 1219 ret = kmx61_read_measurement(data, base, bit); 1220 if (ret < 0) { 1221 mutex_unlock(&data->lock); 1222 goto err; 1223 } 1224 buffer[i++] = ret; 1225 } 1226 mutex_unlock(&data->lock); 1227 1228 iio_push_to_buffers(indio_dev, buffer); 1229err: 1230 iio_trigger_notify_done(indio_dev->trig); 1231 1232 return IRQ_HANDLED; 1233} 1234 1235static const char *kmx61_match_acpi_device(struct device *dev) 1236{ 1237 const struct acpi_device_id *id; 1238 1239 id = acpi_match_device(dev->driver->acpi_match_table, dev); 1240 if (!id) 1241 return NULL; 1242 return dev_name(dev); 1243} 1244 1245static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data, 1246 const struct iio_info *info, 1247 const struct iio_chan_spec *chan, 1248 int num_channels, 1249 const char *name) 1250{ 1251 struct iio_dev *indio_dev; 1252 1253 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data)); 1254 if (!indio_dev) 1255 return ERR_PTR(-ENOMEM); 1256 1257 kmx61_set_data(indio_dev, data); 1258 1259 indio_dev->dev.parent = &data->client->dev; 1260 indio_dev->channels = chan; 1261 indio_dev->num_channels = num_channels; 1262 indio_dev->name = name; 1263 indio_dev->modes = INDIO_DIRECT_MODE; 1264 indio_dev->info = info; 1265 1266 return indio_dev; 1267} 1268 1269static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data, 1270 struct iio_dev *indio_dev, 1271 const char *tag) 1272{ 1273 struct iio_trigger *trig; 1274 int ret; 1275 1276 trig = devm_iio_trigger_alloc(&data->client->dev, 1277 "%s-%s-dev%d", 1278 indio_dev->name, 1279 tag, 1280 indio_dev->id); 1281 if (!trig) 1282 return ERR_PTR(-ENOMEM); 1283 1284 trig->dev.parent = &data->client->dev; 1285 trig->ops = &kmx61_trigger_ops; 1286 iio_trigger_set_drvdata(trig, indio_dev); 1287 1288 ret = iio_trigger_register(trig); 1289 if (ret) 1290 return ERR_PTR(ret); 1291 1292 return trig; 1293} 1294 1295static int kmx61_probe(struct i2c_client *client, 1296 const struct i2c_device_id *id) 1297{ 1298 int ret; 1299 struct kmx61_data *data; 1300 const char *name = NULL; 1301 1302 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 1303 if (!data) 1304 return -ENOMEM; 1305 1306 i2c_set_clientdata(client, data); 1307 data->client = client; 1308 1309 mutex_init(&data->lock); 1310 1311 if (id) 1312 name = id->name; 1313 else if (ACPI_HANDLE(&client->dev)) 1314 name = kmx61_match_acpi_device(&client->dev); 1315 else 1316 return -ENODEV; 1317 1318 data->acc_indio_dev = 1319 kmx61_indiodev_setup(data, &kmx61_acc_info, 1320 kmx61_acc_channels, 1321 ARRAY_SIZE(kmx61_acc_channels), 1322 name); 1323 if (IS_ERR(data->acc_indio_dev)) 1324 return PTR_ERR(data->acc_indio_dev); 1325 1326 data->mag_indio_dev = 1327 kmx61_indiodev_setup(data, &kmx61_mag_info, 1328 kmx61_mag_channels, 1329 ARRAY_SIZE(kmx61_mag_channels), 1330 name); 1331 if (IS_ERR(data->mag_indio_dev)) 1332 return PTR_ERR(data->mag_indio_dev); 1333 1334 ret = kmx61_chip_init(data); 1335 if (ret < 0) 1336 return ret; 1337 1338 if (client->irq > 0) { 1339 ret = devm_request_threaded_irq(&client->dev, client->irq, 1340 kmx61_data_rdy_trig_poll, 1341 kmx61_event_handler, 1342 IRQF_TRIGGER_RISING, 1343 KMX61_IRQ_NAME, 1344 data); 1345 if (ret) 1346 goto err_chip_uninit; 1347 1348 data->acc_dready_trig = 1349 kmx61_trigger_setup(data, data->acc_indio_dev, 1350 "dready"); 1351 if (IS_ERR(data->acc_dready_trig)) { 1352 ret = PTR_ERR(data->acc_dready_trig); 1353 goto err_chip_uninit; 1354 } 1355 1356 data->mag_dready_trig = 1357 kmx61_trigger_setup(data, data->mag_indio_dev, 1358 "dready"); 1359 if (IS_ERR(data->mag_dready_trig)) { 1360 ret = PTR_ERR(data->mag_dready_trig); 1361 goto err_trigger_unregister_acc_dready; 1362 } 1363 1364 data->motion_trig = 1365 kmx61_trigger_setup(data, data->acc_indio_dev, 1366 "any-motion"); 1367 if (IS_ERR(data->motion_trig)) { 1368 ret = PTR_ERR(data->motion_trig); 1369 goto err_trigger_unregister_mag_dready; 1370 } 1371 1372 ret = iio_triggered_buffer_setup(data->acc_indio_dev, 1373 &iio_pollfunc_store_time, 1374 kmx61_trigger_handler, 1375 NULL); 1376 if (ret < 0) { 1377 dev_err(&data->client->dev, 1378 "Failed to setup acc triggered buffer\n"); 1379 goto err_trigger_unregister_motion; 1380 } 1381 1382 ret = iio_triggered_buffer_setup(data->mag_indio_dev, 1383 &iio_pollfunc_store_time, 1384 kmx61_trigger_handler, 1385 NULL); 1386 if (ret < 0) { 1387 dev_err(&data->client->dev, 1388 "Failed to setup mag triggered buffer\n"); 1389 goto err_buffer_cleanup_acc; 1390 } 1391 } 1392 1393 ret = iio_device_register(data->acc_indio_dev); 1394 if (ret < 0) { 1395 dev_err(&client->dev, "Failed to register acc iio device\n"); 1396 goto err_buffer_cleanup_mag; 1397 } 1398 1399 ret = iio_device_register(data->mag_indio_dev); 1400 if (ret < 0) { 1401 dev_err(&client->dev, "Failed to register mag iio device\n"); 1402 goto err_iio_unregister_acc; 1403 } 1404 1405 ret = pm_runtime_set_active(&client->dev); 1406 if (ret < 0) 1407 goto err_iio_unregister_mag; 1408 1409 pm_runtime_enable(&client->dev); 1410 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS); 1411 pm_runtime_use_autosuspend(&client->dev); 1412 1413 return 0; 1414 1415err_iio_unregister_mag: 1416 iio_device_unregister(data->mag_indio_dev); 1417err_iio_unregister_acc: 1418 iio_device_unregister(data->acc_indio_dev); 1419err_buffer_cleanup_mag: 1420 if (client->irq > 0) 1421 iio_triggered_buffer_cleanup(data->mag_indio_dev); 1422err_buffer_cleanup_acc: 1423 if (client->irq > 0) 1424 iio_triggered_buffer_cleanup(data->acc_indio_dev); 1425err_trigger_unregister_motion: 1426 iio_trigger_unregister(data->motion_trig); 1427err_trigger_unregister_mag_dready: 1428 iio_trigger_unregister(data->mag_dready_trig); 1429err_trigger_unregister_acc_dready: 1430 iio_trigger_unregister(data->acc_dready_trig); 1431err_chip_uninit: 1432 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true); 1433 return ret; 1434} 1435 1436static int kmx61_remove(struct i2c_client *client) 1437{ 1438 struct kmx61_data *data = i2c_get_clientdata(client); 1439 1440 pm_runtime_disable(&client->dev); 1441 pm_runtime_set_suspended(&client->dev); 1442 pm_runtime_put_noidle(&client->dev); 1443 1444 iio_device_unregister(data->acc_indio_dev); 1445 iio_device_unregister(data->mag_indio_dev); 1446 1447 if (client->irq > 0) { 1448 iio_triggered_buffer_cleanup(data->acc_indio_dev); 1449 iio_triggered_buffer_cleanup(data->mag_indio_dev); 1450 iio_trigger_unregister(data->acc_dready_trig); 1451 iio_trigger_unregister(data->mag_dready_trig); 1452 iio_trigger_unregister(data->motion_trig); 1453 } 1454 1455 mutex_lock(&data->lock); 1456 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true); 1457 mutex_unlock(&data->lock); 1458 1459 return 0; 1460} 1461 1462#ifdef CONFIG_PM_SLEEP 1463static int kmx61_suspend(struct device *dev) 1464{ 1465 int ret; 1466 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev)); 1467 1468 mutex_lock(&data->lock); 1469 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, 1470 false); 1471 mutex_unlock(&data->lock); 1472 1473 return ret; 1474} 1475 1476static int kmx61_resume(struct device *dev) 1477{ 1478 u8 stby = 0; 1479 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev)); 1480 1481 if (data->acc_stby) 1482 stby |= KMX61_ACC_STBY_BIT; 1483 if (data->mag_stby) 1484 stby |= KMX61_MAG_STBY_BIT; 1485 1486 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true); 1487} 1488#endif 1489 1490#ifdef CONFIG_PM 1491static int kmx61_runtime_suspend(struct device *dev) 1492{ 1493 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev)); 1494 int ret; 1495 1496 mutex_lock(&data->lock); 1497 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true); 1498 mutex_unlock(&data->lock); 1499 1500 return ret; 1501} 1502 1503static int kmx61_runtime_resume(struct device *dev) 1504{ 1505 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev)); 1506 u8 stby = 0; 1507 1508 if (!data->acc_ps) 1509 stby |= KMX61_ACC_STBY_BIT; 1510 if (!data->mag_ps) 1511 stby |= KMX61_MAG_STBY_BIT; 1512 1513 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true); 1514} 1515#endif 1516 1517static const struct dev_pm_ops kmx61_pm_ops = { 1518 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume) 1519 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL) 1520}; 1521 1522static const struct acpi_device_id kmx61_acpi_match[] = { 1523 {"KMX61021", 0}, 1524 {} 1525}; 1526 1527MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match); 1528 1529static const struct i2c_device_id kmx61_id[] = { 1530 {"kmx611021", 0}, 1531 {} 1532}; 1533 1534MODULE_DEVICE_TABLE(i2c, kmx61_id); 1535 1536static struct i2c_driver kmx61_driver = { 1537 .driver = { 1538 .name = KMX61_DRV_NAME, 1539 .acpi_match_table = ACPI_PTR(kmx61_acpi_match), 1540 .pm = &kmx61_pm_ops, 1541 }, 1542 .probe = kmx61_probe, 1543 .remove = kmx61_remove, 1544 .id_table = kmx61_id, 1545}; 1546 1547module_i2c_driver(kmx61_driver); 1548 1549MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); 1550MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver"); 1551MODULE_LICENSE("GPL v2"); 1552