1 /* 2 * Freescale MMA9551L Intelligent Motion-Sensing Platform driver 3 * Copyright (c) 2014, Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 */ 14 15 #include <linux/module.h> 16 #include <linux/i2c.h> 17 #include <linux/interrupt.h> 18 #include <linux/slab.h> 19 #include <linux/acpi.h> 20 #include <linux/delay.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/iio/iio.h> 23 #include <linux/iio/sysfs.h> 24 #include <linux/iio/events.h> 25 #include <linux/pm_runtime.h> 26 27 #define MMA9551_DRV_NAME "mma9551" 28 #define MMA9551_IRQ_NAME "mma9551_event" 29 #define MMA9551_GPIO_NAME "mma9551_int" 30 #define MMA9551_GPIO_COUNT 4 31 32 /* Applications IDs */ 33 #define MMA9551_APPID_VERSION 0x00 34 #define MMA9551_APPID_GPIO 0x03 35 #define MMA9551_APPID_AFE 0x06 36 #define MMA9551_APPID_TILT 0x0B 37 #define MMA9551_APPID_SLEEP_WAKE 0x12 38 #define MMA9551_APPID_RESET 0x17 39 #define MMA9551_APPID_NONE 0xff 40 41 /* Command masks for mailbox write command */ 42 #define MMA9551_CMD_READ_VERSION_INFO 0x00 43 #define MMA9551_CMD_READ_CONFIG 0x10 44 #define MMA9551_CMD_WRITE_CONFIG 0x20 45 #define MMA9551_CMD_READ_STATUS 0x30 46 47 enum mma9551_gpio_pin { 48 mma9551_gpio6 = 0, 49 mma9551_gpio7, 50 mma9551_gpio8, 51 mma9551_gpio9, 52 mma9551_gpio_max = mma9551_gpio9, 53 }; 54 55 /* Mailbox read command */ 56 #define MMA9551_RESPONSE_COCO BIT(7) 57 58 /* Error-Status codes returned in mailbox read command */ 59 #define MMA9551_MCI_ERROR_NONE 0x00 60 #define MMA9551_MCI_ERROR_PARAM 0x04 61 #define MMA9551_MCI_INVALID_COUNT 0x19 62 #define MMA9551_MCI_ERROR_COMMAND 0x1C 63 #define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21 64 #define MMA9551_MCI_ERROR_FIFO_BUSY 0x22 65 #define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23 66 #define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24 67 68 /* GPIO Application */ 69 #define MMA9551_GPIO_POL_MSB 0x08 70 #define MMA9551_GPIO_POL_LSB 0x09 71 72 /* Sleep/Wake application */ 73 #define MMA9551_SLEEP_CFG 0x06 74 #define MMA9551_SLEEP_CFG_SNCEN BIT(0) 75 #define MMA9551_SLEEP_CFG_FLEEN BIT(1) 76 #define MMA9551_SLEEP_CFG_SCHEN BIT(2) 77 78 /* AFE application */ 79 #define MMA9551_AFE_X_ACCEL_REG 0x00 80 #define MMA9551_AFE_Y_ACCEL_REG 0x02 81 #define MMA9551_AFE_Z_ACCEL_REG 0x04 82 83 /* Tilt application (inclination in IIO terms). */ 84 #define MMA9551_TILT_XZ_ANG_REG 0x00 85 #define MMA9551_TILT_YZ_ANG_REG 0x01 86 #define MMA9551_TILT_XY_ANG_REG 0x02 87 #define MMA9551_TILT_ANGFLG BIT(7) 88 #define MMA9551_TILT_QUAD_REG 0x03 89 #define MMA9551_TILT_XY_QUAD_SHIFT 0 90 #define MMA9551_TILT_YZ_QUAD_SHIFT 2 91 #define MMA9551_TILT_XZ_QUAD_SHIFT 4 92 #define MMA9551_TILT_CFG_REG 0x01 93 #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0) 94 95 /* Tilt events are mapped to the first three GPIO pins. */ 96 enum mma9551_tilt_axis { 97 mma9551_x = 0, 98 mma9551_y, 99 mma9551_z, 100 }; 101 102 /* 103 * A response is composed of: 104 * - control registers: MB0-3 105 * - data registers: MB4-31 106 * 107 * A request is composed of: 108 * - mbox to write to (always 0) 109 * - control registers: MB1-4 110 * - data registers: MB5-31 111 */ 112 #define MMA9551_MAILBOX_CTRL_REGS 4 113 #define MMA9551_MAX_MAILBOX_DATA_REGS 28 114 #define MMA9551_MAILBOX_REGS 32 115 116 #define MMA9551_I2C_READ_RETRIES 5 117 #define MMA9551_I2C_READ_DELAY 50 /* us */ 118 119 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */ 120 #define MMA9551_AUTO_SUSPEND_DELAY_MS 2000 121 122 struct mma9551_mbox_request { 123 u8 start_mbox; /* Always 0. */ 124 u8 app_id; 125 /* 126 * See Section 5.3.1 of the MMA955xL Software Reference Manual. 127 * 128 * Bit 7: reserved, always 0 129 * Bits 6-4: command 130 * Bits 3-0: upper bits of register offset 131 */ 132 u8 cmd_off; 133 u8 lower_off; 134 u8 nbytes; 135 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS - 1]; 136 } __packed; 137 138 struct mma9551_mbox_response { 139 u8 app_id; 140 /* 141 * See Section 5.3.3 of the MMA955xL Software Reference Manual. 142 * 143 * Bit 7: COCO 144 * Bits 6-0: Error code. 145 */ 146 u8 coco_err; 147 u8 nbytes; 148 u8 req_bytes; 149 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS]; 150 } __packed; 151 152 struct mma9551_version_info { 153 __be32 device_id; 154 u8 rom_version[2]; 155 u8 fw_version[2]; 156 u8 hw_version[2]; 157 u8 fw_build[2]; 158 }; 159 160 struct mma9551_data { 161 struct i2c_client *client; 162 struct mutex mutex; 163 int event_enabled[3]; 164 int irqs[MMA9551_GPIO_COUNT]; 165 }; 166 167 static int mma9551_transfer(struct i2c_client *client, 168 u8 app_id, u8 command, u16 offset, 169 u8 *inbytes, int num_inbytes, 170 u8 *outbytes, int num_outbytes) 171 { 172 struct mma9551_mbox_request req; 173 struct mma9551_mbox_response rsp; 174 struct i2c_msg in, out; 175 u8 req_len, err_code; 176 int ret, retries; 177 178 if (offset >= 1 << 12) { 179 dev_err(&client->dev, "register offset too large\n"); 180 return -EINVAL; 181 } 182 183 req_len = 1 + MMA9551_MAILBOX_CTRL_REGS + num_inbytes; 184 req.start_mbox = 0; 185 req.app_id = app_id; 186 req.cmd_off = command | (offset >> 8); 187 req.lower_off = offset; 188 189 if (command == MMA9551_CMD_WRITE_CONFIG) 190 req.nbytes = num_inbytes; 191 else 192 req.nbytes = num_outbytes; 193 if (num_inbytes) 194 memcpy(req.buf, inbytes, num_inbytes); 195 196 out.addr = client->addr; 197 out.flags = 0; 198 out.len = req_len; 199 out.buf = (u8 *)&req; 200 201 ret = i2c_transfer(client->adapter, &out, 1); 202 if (ret < 0) { 203 dev_err(&client->dev, "i2c write failed\n"); 204 return ret; 205 } 206 207 retries = MMA9551_I2C_READ_RETRIES; 208 do { 209 udelay(MMA9551_I2C_READ_DELAY); 210 211 in.addr = client->addr; 212 in.flags = I2C_M_RD; 213 in.len = sizeof(rsp); 214 in.buf = (u8 *)&rsp; 215 216 ret = i2c_transfer(client->adapter, &in, 1); 217 if (ret < 0) { 218 dev_err(&client->dev, "i2c read failed\n"); 219 return ret; 220 } 221 222 if (rsp.coco_err & MMA9551_RESPONSE_COCO) 223 break; 224 } while (--retries > 0); 225 226 if (retries == 0) { 227 dev_err(&client->dev, 228 "timed out while waiting for command response\n"); 229 return -ETIMEDOUT; 230 } 231 232 if (rsp.app_id != app_id) { 233 dev_err(&client->dev, 234 "app_id mismatch in response got %02x expected %02x\n", 235 rsp.app_id, app_id); 236 return -EINVAL; 237 } 238 239 err_code = rsp.coco_err & ~MMA9551_RESPONSE_COCO; 240 if (err_code != MMA9551_MCI_ERROR_NONE) { 241 dev_err(&client->dev, "read returned error %x\n", err_code); 242 return -EINVAL; 243 } 244 245 if (rsp.nbytes != rsp.req_bytes) { 246 dev_err(&client->dev, 247 "output length mismatch got %d expected %d\n", 248 rsp.nbytes, rsp.req_bytes); 249 return -EINVAL; 250 } 251 252 if (num_outbytes) 253 memcpy(outbytes, rsp.buf, num_outbytes); 254 255 return 0; 256 } 257 258 static int mma9551_read_config_byte(struct i2c_client *client, u8 app_id, 259 u16 reg, u8 *val) 260 { 261 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG, 262 reg, NULL, 0, val, 1); 263 } 264 265 static int mma9551_write_config_byte(struct i2c_client *client, u8 app_id, 266 u16 reg, u8 val) 267 { 268 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg, 269 &val, 1, NULL, 0); 270 } 271 272 static int mma9551_read_status_byte(struct i2c_client *client, u8 app_id, 273 u16 reg, u8 *val) 274 { 275 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 276 reg, NULL, 0, val, 1); 277 } 278 279 static int mma9551_read_status_word(struct i2c_client *client, u8 app_id, 280 u16 reg, u16 *val) 281 { 282 int ret; 283 __be16 v; 284 285 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 286 reg, NULL, 0, (u8 *)&v, 2); 287 *val = be16_to_cpu(v); 288 289 return ret; 290 } 291 292 static int mma9551_update_config_bits(struct i2c_client *client, u8 app_id, 293 u16 reg, u8 mask, u8 val) 294 { 295 int ret; 296 u8 tmp, orig; 297 298 ret = mma9551_read_config_byte(client, app_id, reg, &orig); 299 if (ret < 0) 300 return ret; 301 302 tmp = orig & ~mask; 303 tmp |= val & mask; 304 305 if (tmp == orig) 306 return 0; 307 308 return mma9551_write_config_byte(client, app_id, reg, tmp); 309 } 310 311 /* 312 * The polarity parameter is described in section 6.2.2, page 66, of the 313 * Software Reference Manual. Basically, polarity=0 means the interrupt 314 * line has the same value as the selected bit, while polarity=1 means 315 * the line is inverted. 316 */ 317 static int mma9551_gpio_config(struct i2c_client *client, 318 enum mma9551_gpio_pin pin, 319 u8 app_id, u8 bitnum, int polarity) 320 { 321 u8 reg, pol_mask, pol_val; 322 int ret; 323 324 if (pin > mma9551_gpio_max) { 325 dev_err(&client->dev, "bad GPIO pin\n"); 326 return -EINVAL; 327 } 328 329 /* 330 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and 331 * 0x03, and so on. 332 */ 333 reg = pin * 2; 334 335 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO, 336 reg, app_id); 337 if (ret < 0) { 338 dev_err(&client->dev, "error setting GPIO app_id\n"); 339 return ret; 340 } 341 342 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO, 343 reg + 1, bitnum); 344 if (ret < 0) { 345 dev_err(&client->dev, "error setting GPIO bit number\n"); 346 return ret; 347 } 348 349 switch (pin) { 350 case mma9551_gpio6: 351 reg = MMA9551_GPIO_POL_LSB; 352 pol_mask = 1 << 6; 353 break; 354 case mma9551_gpio7: 355 reg = MMA9551_GPIO_POL_LSB; 356 pol_mask = 1 << 7; 357 break; 358 case mma9551_gpio8: 359 reg = MMA9551_GPIO_POL_MSB; 360 pol_mask = 1 << 0; 361 break; 362 case mma9551_gpio9: 363 reg = MMA9551_GPIO_POL_MSB; 364 pol_mask = 1 << 1; 365 break; 366 } 367 pol_val = polarity ? pol_mask : 0; 368 369 ret = mma9551_update_config_bits(client, MMA9551_APPID_GPIO, reg, 370 pol_mask, pol_val); 371 if (ret < 0) 372 dev_err(&client->dev, "error setting GPIO polarity\n"); 373 374 return ret; 375 } 376 377 static int mma9551_read_version(struct i2c_client *client) 378 { 379 struct mma9551_version_info info; 380 int ret; 381 382 ret = mma9551_transfer(client, MMA9551_APPID_VERSION, 0x00, 0x00, 383 NULL, 0, (u8 *)&info, sizeof(info)); 384 if (ret < 0) 385 return ret; 386 387 dev_info(&client->dev, "Device ID 0x%x, firmware version %02x.%02x\n", 388 be32_to_cpu(info.device_id), info.fw_version[0], 389 info.fw_version[1]); 390 391 return 0; 392 } 393 394 /* 395 * Power on chip and enable doze mode. 396 * Use 'false' as the second parameter to cause the device to enter 397 * sleep. 398 */ 399 static int mma9551_set_device_state(struct i2c_client *client, bool enable) 400 { 401 return mma9551_update_config_bits(client, MMA9551_APPID_SLEEP_WAKE, 402 MMA9551_SLEEP_CFG, 403 MMA9551_SLEEP_CFG_SNCEN | 404 MMA9551_SLEEP_CFG_FLEEN | 405 MMA9551_SLEEP_CFG_SCHEN, 406 enable ? MMA9551_SLEEP_CFG_SCHEN | 407 MMA9551_SLEEP_CFG_FLEEN : 408 MMA9551_SLEEP_CFG_SNCEN); 409 } 410 411 static int mma9551_set_power_state(struct i2c_client *client, bool on) 412 { 413 #ifdef CONFIG_PM 414 int ret; 415 416 if (on) 417 ret = pm_runtime_get_sync(&client->dev); 418 else { 419 pm_runtime_mark_last_busy(&client->dev); 420 ret = pm_runtime_put_autosuspend(&client->dev); 421 } 422 423 if (ret < 0) { 424 dev_err(&client->dev, 425 "failed to change power state to %d\n", on); 426 if (on) 427 pm_runtime_put_noidle(&client->dev); 428 429 return ret; 430 } 431 #endif 432 433 return 0; 434 } 435 436 static void mma9551_sleep(int freq) 437 { 438 int sleep_val = 1000 / freq; 439 440 if (sleep_val < 20) 441 usleep_range(sleep_val * 1000, 20000); 442 else 443 msleep_interruptible(sleep_val); 444 } 445 446 static int mma9551_read_incli_chan(struct i2c_client *client, 447 const struct iio_chan_spec *chan, 448 int *val) 449 { 450 u8 quad_shift, angle, quadrant; 451 u16 reg_addr; 452 int ret; 453 454 switch (chan->channel2) { 455 case IIO_MOD_X: 456 reg_addr = MMA9551_TILT_YZ_ANG_REG; 457 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT; 458 break; 459 case IIO_MOD_Y: 460 reg_addr = MMA9551_TILT_XZ_ANG_REG; 461 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT; 462 break; 463 case IIO_MOD_Z: 464 reg_addr = MMA9551_TILT_XY_ANG_REG; 465 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT; 466 break; 467 default: 468 return -EINVAL; 469 } 470 471 ret = mma9551_set_power_state(client, true); 472 if (ret < 0) 473 return ret; 474 475 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT, 476 reg_addr, &angle); 477 if (ret < 0) 478 goto out_poweroff; 479 480 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT, 481 MMA9551_TILT_QUAD_REG, &quadrant); 482 if (ret < 0) 483 goto out_poweroff; 484 485 angle &= ~MMA9551_TILT_ANGFLG; 486 quadrant = (quadrant >> quad_shift) & 0x03; 487 488 if (quadrant == 1 || quadrant == 3) 489 *val = 90 * (quadrant + 1) - angle; 490 else 491 *val = angle + 90 * quadrant; 492 493 ret = IIO_VAL_INT; 494 495 out_poweroff: 496 mma9551_set_power_state(client, false); 497 return ret; 498 } 499 500 static int mma9551_read_accel_chan(struct i2c_client *client, 501 const struct iio_chan_spec *chan, 502 int *val, int *val2) 503 { 504 u16 reg_addr; 505 s16 raw_accel; 506 int ret; 507 508 switch (chan->channel2) { 509 case IIO_MOD_X: 510 reg_addr = MMA9551_AFE_X_ACCEL_REG; 511 break; 512 case IIO_MOD_Y: 513 reg_addr = MMA9551_AFE_Y_ACCEL_REG; 514 break; 515 case IIO_MOD_Z: 516 reg_addr = MMA9551_AFE_Z_ACCEL_REG; 517 break; 518 default: 519 return -EINVAL; 520 } 521 522 ret = mma9551_set_power_state(client, true); 523 if (ret < 0) 524 return ret; 525 526 ret = mma9551_read_status_word(client, MMA9551_APPID_AFE, 527 reg_addr, &raw_accel); 528 if (ret < 0) 529 goto out_poweroff; 530 531 *val = raw_accel; 532 533 ret = IIO_VAL_INT; 534 535 out_poweroff: 536 mma9551_set_power_state(client, false); 537 return ret; 538 } 539 540 static int mma9551_read_raw(struct iio_dev *indio_dev, 541 struct iio_chan_spec const *chan, 542 int *val, int *val2, long mask) 543 { 544 struct mma9551_data *data = iio_priv(indio_dev); 545 int ret; 546 547 switch (mask) { 548 case IIO_CHAN_INFO_PROCESSED: 549 switch (chan->type) { 550 case IIO_INCLI: 551 mutex_lock(&data->mutex); 552 ret = mma9551_read_incli_chan(data->client, chan, val); 553 mutex_unlock(&data->mutex); 554 return ret; 555 default: 556 return -EINVAL; 557 } 558 case IIO_CHAN_INFO_RAW: 559 switch (chan->type) { 560 case IIO_ACCEL: 561 mutex_lock(&data->mutex); 562 ret = mma9551_read_accel_chan(data->client, 563 chan, val, val2); 564 mutex_unlock(&data->mutex); 565 return ret; 566 default: 567 return -EINVAL; 568 } 569 case IIO_CHAN_INFO_SCALE: 570 switch (chan->type) { 571 case IIO_ACCEL: 572 *val = 0; 573 *val2 = 2440; 574 return IIO_VAL_INT_PLUS_MICRO; 575 default: 576 return -EINVAL; 577 } 578 default: 579 return -EINVAL; 580 } 581 } 582 583 static int mma9551_read_event_config(struct iio_dev *indio_dev, 584 const struct iio_chan_spec *chan, 585 enum iio_event_type type, 586 enum iio_event_direction dir) 587 { 588 struct mma9551_data *data = iio_priv(indio_dev); 589 590 switch (chan->type) { 591 case IIO_INCLI: 592 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */ 593 return data->event_enabled[chan->channel2 - 1]; 594 default: 595 return -EINVAL; 596 } 597 } 598 599 static int mma9551_config_incli_event(struct iio_dev *indio_dev, 600 enum iio_modifier axis, 601 int state) 602 { 603 struct mma9551_data *data = iio_priv(indio_dev); 604 enum mma9551_tilt_axis mma_axis; 605 int ret; 606 607 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */ 608 mma_axis = axis - 1; 609 610 if (data->event_enabled[mma_axis] == state) 611 return 0; 612 613 if (state == 0) { 614 ret = mma9551_gpio_config(data->client, 615 (enum mma9551_gpio_pin)mma_axis, 616 MMA9551_APPID_NONE, 0, 0); 617 if (ret < 0) 618 return ret; 619 620 ret = mma9551_set_power_state(data->client, false); 621 if (ret < 0) 622 return ret; 623 } else { 624 int bitnum; 625 626 /* Bit 7 of each angle register holds the angle flag. */ 627 switch (axis) { 628 case IIO_MOD_X: 629 bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG; 630 break; 631 case IIO_MOD_Y: 632 bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG; 633 break; 634 case IIO_MOD_Z: 635 bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG; 636 break; 637 default: 638 return -EINVAL; 639 } 640 641 642 ret = mma9551_set_power_state(data->client, true); 643 if (ret < 0) 644 return ret; 645 646 ret = mma9551_gpio_config(data->client, 647 (enum mma9551_gpio_pin)mma_axis, 648 MMA9551_APPID_TILT, bitnum, 0); 649 if (ret < 0) { 650 mma9551_set_power_state(data->client, false); 651 return ret; 652 } 653 } 654 655 data->event_enabled[mma_axis] = state; 656 657 return ret; 658 } 659 660 static int mma9551_write_event_config(struct iio_dev *indio_dev, 661 const struct iio_chan_spec *chan, 662 enum iio_event_type type, 663 enum iio_event_direction dir, 664 int state) 665 { 666 struct mma9551_data *data = iio_priv(indio_dev); 667 int ret; 668 669 switch (chan->type) { 670 case IIO_INCLI: 671 mutex_lock(&data->mutex); 672 ret = mma9551_config_incli_event(indio_dev, 673 chan->channel2, state); 674 mutex_unlock(&data->mutex); 675 return ret; 676 default: 677 return -EINVAL; 678 } 679 } 680 681 static int mma9551_write_event_value(struct iio_dev *indio_dev, 682 const struct iio_chan_spec *chan, 683 enum iio_event_type type, 684 enum iio_event_direction dir, 685 enum iio_event_info info, 686 int val, int val2) 687 { 688 struct mma9551_data *data = iio_priv(indio_dev); 689 int ret; 690 691 switch (chan->type) { 692 case IIO_INCLI: 693 if (val2 != 0 || val < 1 || val > 10) 694 return -EINVAL; 695 mutex_lock(&data->mutex); 696 ret = mma9551_update_config_bits(data->client, 697 MMA9551_APPID_TILT, 698 MMA9551_TILT_CFG_REG, 699 MMA9551_TILT_ANG_THRESH_MASK, 700 val); 701 mutex_unlock(&data->mutex); 702 return ret; 703 default: 704 return -EINVAL; 705 } 706 } 707 708 static int mma9551_read_event_value(struct iio_dev *indio_dev, 709 const struct iio_chan_spec *chan, 710 enum iio_event_type type, 711 enum iio_event_direction dir, 712 enum iio_event_info info, 713 int *val, int *val2) 714 { 715 struct mma9551_data *data = iio_priv(indio_dev); 716 int ret; 717 u8 tmp; 718 719 switch (chan->type) { 720 case IIO_INCLI: 721 mutex_lock(&data->mutex); 722 ret = mma9551_read_config_byte(data->client, 723 MMA9551_APPID_TILT, 724 MMA9551_TILT_CFG_REG, &tmp); 725 mutex_unlock(&data->mutex); 726 if (ret < 0) 727 return ret; 728 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK; 729 *val2 = 0; 730 return IIO_VAL_INT; 731 default: 732 return -EINVAL; 733 } 734 } 735 736 static const struct iio_event_spec mma9551_incli_event = { 737 .type = IIO_EV_TYPE_ROC, 738 .dir = IIO_EV_DIR_RISING, 739 .mask_separate = BIT(IIO_EV_INFO_ENABLE), 740 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE), 741 }; 742 743 #define MMA9551_ACCEL_CHANNEL(axis) { \ 744 .type = IIO_ACCEL, \ 745 .modified = 1, \ 746 .channel2 = axis, \ 747 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 748 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 749 } 750 751 #define MMA9551_INCLI_CHANNEL(axis) { \ 752 .type = IIO_INCLI, \ 753 .modified = 1, \ 754 .channel2 = axis, \ 755 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 756 .event_spec = &mma9551_incli_event, \ 757 .num_event_specs = 1, \ 758 } 759 760 static const struct iio_chan_spec mma9551_channels[] = { 761 MMA9551_ACCEL_CHANNEL(IIO_MOD_X), 762 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y), 763 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z), 764 765 MMA9551_INCLI_CHANNEL(IIO_MOD_X), 766 MMA9551_INCLI_CHANNEL(IIO_MOD_Y), 767 MMA9551_INCLI_CHANNEL(IIO_MOD_Z), 768 }; 769 770 static const struct iio_info mma9551_info = { 771 .driver_module = THIS_MODULE, 772 .read_raw = mma9551_read_raw, 773 .read_event_config = mma9551_read_event_config, 774 .write_event_config = mma9551_write_event_config, 775 .read_event_value = mma9551_read_event_value, 776 .write_event_value = mma9551_write_event_value, 777 }; 778 779 static irqreturn_t mma9551_event_handler(int irq, void *private) 780 { 781 struct iio_dev *indio_dev = private; 782 struct mma9551_data *data = iio_priv(indio_dev); 783 int i, ret, mma_axis = -1; 784 u16 reg; 785 u8 val; 786 787 mutex_lock(&data->mutex); 788 789 for (i = 0; i < 3; i++) 790 if (irq == data->irqs[i]) { 791 mma_axis = i; 792 break; 793 } 794 795 if (mma_axis == -1) { 796 /* IRQ was triggered on 4th line, which we don't use. */ 797 dev_warn(&data->client->dev, 798 "irq triggered on unused line %d\n", data->irqs[3]); 799 goto out; 800 } 801 802 switch (mma_axis) { 803 case mma9551_x: 804 reg = MMA9551_TILT_YZ_ANG_REG; 805 break; 806 case mma9551_y: 807 reg = MMA9551_TILT_XZ_ANG_REG; 808 break; 809 case mma9551_z: 810 reg = MMA9551_TILT_XY_ANG_REG; 811 break; 812 } 813 814 /* 815 * Read the angle even though we don't use it, otherwise we 816 * won't get any further interrupts. 817 */ 818 ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT, 819 reg, &val); 820 if (ret < 0) { 821 dev_err(&data->client->dev, 822 "error %d reading tilt register in IRQ\n", ret); 823 goto out; 824 } 825 826 iio_push_event(indio_dev, 827 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1), 828 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING), 829 iio_get_time_ns()); 830 831 out: 832 mutex_unlock(&data->mutex); 833 834 return IRQ_HANDLED; 835 } 836 837 static int mma9551_init(struct mma9551_data *data) 838 { 839 int ret; 840 841 ret = mma9551_read_version(data->client); 842 if (ret) 843 return ret; 844 845 return mma9551_set_device_state(data->client, true); 846 } 847 848 static int mma9551_gpio_probe(struct iio_dev *indio_dev) 849 { 850 struct gpio_desc *gpio; 851 int i, ret; 852 struct mma9551_data *data = iio_priv(indio_dev); 853 struct device *dev = &data->client->dev; 854 855 for (i = 0; i < MMA9551_GPIO_COUNT; i++) { 856 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i); 857 if (IS_ERR(gpio)) { 858 dev_err(dev, "acpi gpio get index failed\n"); 859 return PTR_ERR(gpio); 860 } 861 862 ret = gpiod_direction_input(gpio); 863 if (ret) 864 return ret; 865 866 data->irqs[i] = gpiod_to_irq(gpio); 867 ret = devm_request_threaded_irq(dev, data->irqs[i], 868 NULL, mma9551_event_handler, 869 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 870 MMA9551_IRQ_NAME, indio_dev); 871 if (ret < 0) { 872 dev_err(dev, "request irq %d failed\n", data->irqs[i]); 873 return ret; 874 } 875 876 dev_dbg(dev, "gpio resource, no:%d irq:%d\n", 877 desc_to_gpio(gpio), data->irqs[i]); 878 } 879 880 return 0; 881 } 882 883 static const char *mma9551_match_acpi_device(struct device *dev) 884 { 885 const struct acpi_device_id *id; 886 887 id = acpi_match_device(dev->driver->acpi_match_table, dev); 888 if (!id) 889 return NULL; 890 891 return dev_name(dev); 892 } 893 894 static int mma9551_probe(struct i2c_client *client, 895 const struct i2c_device_id *id) 896 { 897 struct mma9551_data *data; 898 struct iio_dev *indio_dev; 899 const char *name = NULL; 900 int ret; 901 902 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 903 if (!indio_dev) 904 return -ENOMEM; 905 906 data = iio_priv(indio_dev); 907 i2c_set_clientdata(client, indio_dev); 908 data->client = client; 909 910 if (id) 911 name = id->name; 912 else if (ACPI_HANDLE(&client->dev)) 913 name = mma9551_match_acpi_device(&client->dev); 914 915 ret = mma9551_init(data); 916 if (ret < 0) 917 return ret; 918 919 mutex_init(&data->mutex); 920 921 indio_dev->dev.parent = &client->dev; 922 indio_dev->channels = mma9551_channels; 923 indio_dev->num_channels = ARRAY_SIZE(mma9551_channels); 924 indio_dev->name = name; 925 indio_dev->modes = INDIO_DIRECT_MODE; 926 indio_dev->info = &mma9551_info; 927 928 ret = mma9551_gpio_probe(indio_dev); 929 if (ret < 0) 930 goto out_poweroff; 931 932 ret = iio_device_register(indio_dev); 933 if (ret < 0) { 934 dev_err(&client->dev, "unable to register iio device\n"); 935 goto out_poweroff; 936 } 937 938 ret = pm_runtime_set_active(&client->dev); 939 if (ret < 0) 940 goto out_iio_unregister; 941 942 pm_runtime_enable(&client->dev); 943 pm_runtime_set_autosuspend_delay(&client->dev, 944 MMA9551_AUTO_SUSPEND_DELAY_MS); 945 pm_runtime_use_autosuspend(&client->dev); 946 947 return 0; 948 949 out_iio_unregister: 950 iio_device_unregister(indio_dev); 951 out_poweroff: 952 mma9551_set_device_state(client, false); 953 954 return ret; 955 } 956 957 static int mma9551_remove(struct i2c_client *client) 958 { 959 struct iio_dev *indio_dev = i2c_get_clientdata(client); 960 struct mma9551_data *data = iio_priv(indio_dev); 961 962 pm_runtime_disable(&client->dev); 963 pm_runtime_set_suspended(&client->dev); 964 pm_runtime_put_noidle(&client->dev); 965 966 iio_device_unregister(indio_dev); 967 mutex_lock(&data->mutex); 968 mma9551_set_device_state(data->client, false); 969 mutex_unlock(&data->mutex); 970 971 return 0; 972 } 973 974 #ifdef CONFIG_PM 975 static int mma9551_runtime_suspend(struct device *dev) 976 { 977 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 978 struct mma9551_data *data = iio_priv(indio_dev); 979 int ret; 980 981 mutex_lock(&data->mutex); 982 ret = mma9551_set_device_state(data->client, false); 983 mutex_unlock(&data->mutex); 984 if (ret < 0) { 985 dev_err(&data->client->dev, "powering off device failed\n"); 986 return -EAGAIN; 987 } 988 989 return 0; 990 } 991 992 static int mma9551_runtime_resume(struct device *dev) 993 { 994 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 995 struct mma9551_data *data = iio_priv(indio_dev); 996 int ret; 997 998 ret = mma9551_set_device_state(data->client, true); 999 if (ret < 0) 1000 return ret; 1001 1002 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE); 1003 1004 return 0; 1005 } 1006 #endif 1007 1008 #ifdef CONFIG_PM_SLEEP 1009 static int mma9551_suspend(struct device *dev) 1010 { 1011 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1012 struct mma9551_data *data = iio_priv(indio_dev); 1013 int ret; 1014 1015 mutex_lock(&data->mutex); 1016 ret = mma9551_set_device_state(data->client, false); 1017 mutex_unlock(&data->mutex); 1018 1019 return ret; 1020 } 1021 1022 static int mma9551_resume(struct device *dev) 1023 { 1024 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1025 struct mma9551_data *data = iio_priv(indio_dev); 1026 int ret; 1027 1028 mutex_lock(&data->mutex); 1029 ret = mma9551_set_device_state(data->client, true); 1030 mutex_unlock(&data->mutex); 1031 1032 return ret; 1033 } 1034 #endif 1035 1036 static const struct dev_pm_ops mma9551_pm_ops = { 1037 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume) 1038 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend, 1039 mma9551_runtime_resume, NULL) 1040 }; 1041 1042 static const struct acpi_device_id mma9551_acpi_match[] = { 1043 {"MMA9551", 0}, 1044 {}, 1045 }; 1046 1047 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match); 1048 1049 static const struct i2c_device_id mma9551_id[] = { 1050 {"mma9551", 0}, 1051 {} 1052 }; 1053 1054 MODULE_DEVICE_TABLE(i2c, mma9551_id); 1055 1056 static struct i2c_driver mma9551_driver = { 1057 .driver = { 1058 .name = MMA9551_DRV_NAME, 1059 .acpi_match_table = ACPI_PTR(mma9551_acpi_match), 1060 .pm = &mma9551_pm_ops, 1061 }, 1062 .probe = mma9551_probe, 1063 .remove = mma9551_remove, 1064 .id_table = mma9551_id, 1065 }; 1066 1067 module_i2c_driver(mma9551_driver); 1068 1069 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 1070 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 1071 MODULE_LICENSE("GPL v2"); 1072 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver"); 1073