1 /* 2 * Common code for Freescale MMA955x Intelligent Sensor Platform drivers 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/delay.h> 18 #include <linux/iio/iio.h> 19 #include <linux/pm_runtime.h> 20 #include "mma9551_core.h" 21 22 /* Command masks for mailbox write command */ 23 #define MMA9551_CMD_READ_VERSION_INFO 0x00 24 #define MMA9551_CMD_READ_CONFIG 0x10 25 #define MMA9551_CMD_WRITE_CONFIG 0x20 26 #define MMA9551_CMD_READ_STATUS 0x30 27 28 /* Mailbox read command */ 29 #define MMA9551_RESPONSE_COCO BIT(7) 30 31 /* Error-Status codes returned in mailbox read command */ 32 #define MMA9551_MCI_ERROR_NONE 0x00 33 #define MMA9551_MCI_ERROR_PARAM 0x04 34 #define MMA9551_MCI_INVALID_COUNT 0x19 35 #define MMA9551_MCI_ERROR_COMMAND 0x1C 36 #define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21 37 #define MMA9551_MCI_ERROR_FIFO_BUSY 0x22 38 #define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23 39 #define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24 40 41 /* GPIO Application */ 42 #define MMA9551_GPIO_POL_MSB 0x08 43 #define MMA9551_GPIO_POL_LSB 0x09 44 45 /* Sleep/Wake application */ 46 #define MMA9551_SLEEP_CFG 0x06 47 #define MMA9551_SLEEP_CFG_SNCEN BIT(0) 48 #define MMA9551_SLEEP_CFG_FLEEN BIT(1) 49 #define MMA9551_SLEEP_CFG_SCHEN BIT(2) 50 51 /* AFE application */ 52 #define MMA9551_AFE_X_ACCEL_REG 0x00 53 #define MMA9551_AFE_Y_ACCEL_REG 0x02 54 #define MMA9551_AFE_Z_ACCEL_REG 0x04 55 56 /* Reset/Suspend/Clear application */ 57 #define MMA9551_RSC_RESET 0x00 58 #define MMA9551_RSC_OFFSET(mask) (3 - (ffs(mask) - 1) / 8) 59 #define MMA9551_RSC_VAL(mask) (mask >> (((ffs(mask) - 1) / 8) * 8)) 60 61 /* 62 * A response is composed of: 63 * - control registers: MB0-3 64 * - data registers: MB4-31 65 * 66 * A request is composed of: 67 * - mbox to write to (always 0) 68 * - control registers: MB1-4 69 * - data registers: MB5-31 70 */ 71 #define MMA9551_MAILBOX_CTRL_REGS 4 72 #define MMA9551_MAX_MAILBOX_DATA_REGS 28 73 #define MMA9551_MAILBOX_REGS 32 74 75 #define MMA9551_I2C_READ_RETRIES 5 76 #define MMA9551_I2C_READ_DELAY 50 /* us */ 77 78 struct mma9551_mbox_request { 79 u8 start_mbox; /* Always 0. */ 80 u8 app_id; 81 /* 82 * See Section 5.3.1 of the MMA955xL Software Reference Manual. 83 * 84 * Bit 7: reserved, always 0 85 * Bits 6-4: command 86 * Bits 3-0: upper bits of register offset 87 */ 88 u8 cmd_off; 89 u8 lower_off; 90 u8 nbytes; 91 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS - 1]; 92 } __packed; 93 94 struct mma9551_mbox_response { 95 u8 app_id; 96 /* 97 * See Section 5.3.3 of the MMA955xL Software Reference Manual. 98 * 99 * Bit 7: COCO 100 * Bits 6-0: Error code. 101 */ 102 u8 coco_err; 103 u8 nbytes; 104 u8 req_bytes; 105 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS]; 106 } __packed; 107 108 struct mma9551_version_info { 109 __be32 device_id; 110 u8 rom_version[2]; 111 u8 fw_version[2]; 112 u8 hw_version[2]; 113 u8 fw_build[2]; 114 }; 115 116 static int mma9551_transfer(struct i2c_client *client, 117 u8 app_id, u8 command, u16 offset, 118 u8 *inbytes, int num_inbytes, 119 u8 *outbytes, int num_outbytes) 120 { 121 struct mma9551_mbox_request req; 122 struct mma9551_mbox_response rsp; 123 struct i2c_msg in, out; 124 u8 req_len, err_code; 125 int ret, retries; 126 127 if (offset >= 1 << 12) { 128 dev_err(&client->dev, "register offset too large\n"); 129 return -EINVAL; 130 } 131 132 req_len = 1 + MMA9551_MAILBOX_CTRL_REGS + num_inbytes; 133 req.start_mbox = 0; 134 req.app_id = app_id; 135 req.cmd_off = command | (offset >> 8); 136 req.lower_off = offset; 137 138 if (command == MMA9551_CMD_WRITE_CONFIG) 139 req.nbytes = num_inbytes; 140 else 141 req.nbytes = num_outbytes; 142 if (num_inbytes) 143 memcpy(req.buf, inbytes, num_inbytes); 144 145 out.addr = client->addr; 146 out.flags = 0; 147 out.len = req_len; 148 out.buf = (u8 *)&req; 149 150 ret = i2c_transfer(client->adapter, &out, 1); 151 if (ret < 0) { 152 dev_err(&client->dev, "i2c write failed\n"); 153 return ret; 154 } 155 156 retries = MMA9551_I2C_READ_RETRIES; 157 do { 158 udelay(MMA9551_I2C_READ_DELAY); 159 160 in.addr = client->addr; 161 in.flags = I2C_M_RD; 162 in.len = sizeof(rsp); 163 in.buf = (u8 *)&rsp; 164 165 ret = i2c_transfer(client->adapter, &in, 1); 166 if (ret < 0) { 167 dev_err(&client->dev, "i2c read failed\n"); 168 return ret; 169 } 170 171 if (rsp.coco_err & MMA9551_RESPONSE_COCO) 172 break; 173 } while (--retries > 0); 174 175 if (retries == 0) { 176 dev_err(&client->dev, 177 "timed out while waiting for command response\n"); 178 return -ETIMEDOUT; 179 } 180 181 if (rsp.app_id != app_id) { 182 dev_err(&client->dev, 183 "app_id mismatch in response got %02x expected %02x\n", 184 rsp.app_id, app_id); 185 return -EINVAL; 186 } 187 188 err_code = rsp.coco_err & ~MMA9551_RESPONSE_COCO; 189 if (err_code != MMA9551_MCI_ERROR_NONE) { 190 dev_err(&client->dev, "read returned error %x\n", err_code); 191 return -EINVAL; 192 } 193 194 if (rsp.nbytes != rsp.req_bytes) { 195 dev_err(&client->dev, 196 "output length mismatch got %d expected %d\n", 197 rsp.nbytes, rsp.req_bytes); 198 return -EINVAL; 199 } 200 201 if (num_outbytes) 202 memcpy(outbytes, rsp.buf, num_outbytes); 203 204 return 0; 205 } 206 207 /** 208 * mma9551_read_config_byte() - read 1 configuration byte 209 * @client: I2C client 210 * @app_id: Application ID 211 * @reg: Application register 212 * @val: Pointer to store value read 213 * 214 * Read one configuration byte from the device using MMA955xL command format. 215 * Commands to the MMA955xL platform consist of a write followed 216 * by one or more reads. 217 * 218 * Locking note: This function must be called with the device lock held. 219 * Locking is not handled inside the function. Callers should ensure they 220 * serialize access to the HW. 221 * 222 * Returns: 0 on success, negative value on failure. 223 */ 224 int mma9551_read_config_byte(struct i2c_client *client, u8 app_id, 225 u16 reg, u8 *val) 226 { 227 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG, 228 reg, NULL, 0, val, 1); 229 } 230 EXPORT_SYMBOL(mma9551_read_config_byte); 231 232 /** 233 * mma9551_write_config_byte() - write 1 configuration byte 234 * @client: I2C client 235 * @app_id: Application ID 236 * @reg: Application register 237 * @val: Value to write 238 * 239 * Write one configuration byte from the device using MMA955xL command format. 240 * Commands to the MMA955xL platform consist of a write followed by one or 241 * more reads. 242 * 243 * Locking note: This function must be called with the device lock held. 244 * Locking is not handled inside the function. Callers should ensure they 245 * serialize access to the HW. 246 * 247 * Returns: 0 on success, negative value on failure. 248 */ 249 int mma9551_write_config_byte(struct i2c_client *client, u8 app_id, 250 u16 reg, u8 val) 251 { 252 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg, 253 &val, 1, NULL, 0); 254 } 255 EXPORT_SYMBOL(mma9551_write_config_byte); 256 257 /** 258 * mma9551_read_status_byte() - read 1 status byte 259 * @client: I2C client 260 * @app_id: Application ID 261 * @reg: Application register 262 * @val: Pointer to store value read 263 * 264 * Read one status byte from the device using MMA955xL command format. 265 * Commands to the MMA955xL platform consist of a write followed by one or 266 * more reads. 267 * 268 * Locking note: This function must be called with the device lock held. 269 * Locking is not handled inside the function. Callers should ensure they 270 * serialize access to the HW. 271 * 272 * Returns: 0 on success, negative value on failure. 273 */ 274 int mma9551_read_status_byte(struct i2c_client *client, u8 app_id, 275 u16 reg, u8 *val) 276 { 277 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 278 reg, NULL, 0, val, 1); 279 } 280 EXPORT_SYMBOL(mma9551_read_status_byte); 281 282 /** 283 * mma9551_read_config_word() - read 1 config word 284 * @client: I2C client 285 * @app_id: Application ID 286 * @reg: Application register 287 * @val: Pointer to store value read 288 * 289 * Read one configuration word from the device using MMA955xL command format. 290 * Commands to the MMA955xL platform consist of a write followed by one or 291 * more reads. 292 * 293 * Locking note: This function must be called with the device lock held. 294 * Locking is not handled inside the function. Callers should ensure they 295 * serialize access to the HW. 296 * 297 * Returns: 0 on success, negative value on failure. 298 */ 299 int mma9551_read_config_word(struct i2c_client *client, u8 app_id, 300 u16 reg, u16 *val) 301 { 302 int ret; 303 __be16 v; 304 305 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG, 306 reg, NULL, 0, (u8 *)&v, 2); 307 *val = be16_to_cpu(v); 308 309 return ret; 310 } 311 EXPORT_SYMBOL(mma9551_read_config_word); 312 313 /** 314 * mma9551_write_config_word() - write 1 config word 315 * @client: I2C client 316 * @app_id: Application ID 317 * @reg: Application register 318 * @val: Value to write 319 * 320 * Write one configuration word from the device using MMA955xL command format. 321 * Commands to the MMA955xL platform consist of a write followed by one or 322 * more reads. 323 * 324 * Locking note: This function must be called with the device lock held. 325 * Locking is not handled inside the function. Callers should ensure they 326 * serialize access to the HW. 327 * 328 * Returns: 0 on success, negative value on failure. 329 */ 330 int mma9551_write_config_word(struct i2c_client *client, u8 app_id, 331 u16 reg, u16 val) 332 { 333 __be16 v = cpu_to_be16(val); 334 335 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg, 336 (u8 *) &v, 2, NULL, 0); 337 } 338 EXPORT_SYMBOL(mma9551_write_config_word); 339 340 /** 341 * mma9551_read_status_word() - read 1 status word 342 * @client: I2C client 343 * @app_id: Application ID 344 * @reg: Application register 345 * @val: Pointer to store value read 346 * 347 * Read one status word from the device using MMA955xL command format. 348 * Commands to the MMA955xL platform consist of a write followed by one or 349 * more reads. 350 * 351 * Locking note: This function must be called with the device lock held. 352 * Locking is not handled inside the function. Callers should ensure they 353 * serialize access to the HW. 354 * 355 * Returns: 0 on success, negative value on failure. 356 */ 357 int mma9551_read_status_word(struct i2c_client *client, u8 app_id, 358 u16 reg, u16 *val) 359 { 360 int ret; 361 __be16 v; 362 363 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 364 reg, NULL, 0, (u8 *)&v, 2); 365 *val = be16_to_cpu(v); 366 367 return ret; 368 } 369 EXPORT_SYMBOL(mma9551_read_status_word); 370 371 /** 372 * mma9551_read_config_words() - read multiple config words 373 * @client: I2C client 374 * @app_id: Application ID 375 * @reg: Application register 376 * @len: Length of array to read in bytes 377 * @val: Array of words to read 378 * 379 * Read multiple configuration registers (word-sized registers). 380 * 381 * Locking note: This function must be called with the device lock held. 382 * Locking is not handled inside the function. Callers should ensure they 383 * serialize access to the HW. 384 * 385 * Returns: 0 on success, negative value on failure. 386 */ 387 int mma9551_read_config_words(struct i2c_client *client, u8 app_id, 388 u16 reg, u8 len, u16 *buf) 389 { 390 int ret, i; 391 int len_words = len / sizeof(u16); 392 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS]; 393 394 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG, 395 reg, NULL, 0, (u8 *) be_buf, len); 396 if (ret < 0) 397 return ret; 398 399 for (i = 0; i < len_words; i++) 400 buf[i] = be16_to_cpu(be_buf[i]); 401 402 return 0; 403 } 404 EXPORT_SYMBOL(mma9551_read_config_words); 405 406 /** 407 * mma9551_read_status_words() - read multiple status words 408 * @client: I2C client 409 * @app_id: Application ID 410 * @reg: Application register 411 * @len: Length of array to read in bytes 412 * @val: Array of words to read 413 * 414 * Read multiple status registers (word-sized registers). 415 * 416 * Locking note: This function must be called with the device lock held. 417 * Locking is not handled inside the function. Callers should ensure they 418 * serialize access to the HW. 419 * 420 * Returns: 0 on success, negative value on failure. 421 */ 422 int mma9551_read_status_words(struct i2c_client *client, u8 app_id, 423 u16 reg, u8 len, u16 *buf) 424 { 425 int ret, i; 426 int len_words = len / sizeof(u16); 427 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS]; 428 429 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 430 reg, NULL, 0, (u8 *) be_buf, len); 431 if (ret < 0) 432 return ret; 433 434 for (i = 0; i < len_words; i++) 435 buf[i] = be16_to_cpu(be_buf[i]); 436 437 return 0; 438 } 439 EXPORT_SYMBOL(mma9551_read_status_words); 440 441 /** 442 * mma9551_write_config_words() - write multiple config words 443 * @client: I2C client 444 * @app_id: Application ID 445 * @reg: Application register 446 * @len: Length of array to write in bytes 447 * @val: Array of words to write 448 * 449 * Write multiple configuration registers (word-sized registers). 450 * 451 * Locking note: This function must be called with the device lock held. 452 * Locking is not handled inside the function. Callers should ensure they 453 * serialize access to the HW. 454 * 455 * Returns: 0 on success, negative value on failure. 456 */ 457 int mma9551_write_config_words(struct i2c_client *client, u8 app_id, 458 u16 reg, u8 len, u16 *buf) 459 { 460 int i; 461 int len_words = len / sizeof(u16); 462 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS]; 463 464 for (i = 0; i < len_words; i++) 465 be_buf[i] = cpu_to_be16(buf[i]); 466 467 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, 468 reg, (u8 *) be_buf, len, NULL, 0); 469 } 470 EXPORT_SYMBOL(mma9551_write_config_words); 471 472 /** 473 * mma9551_update_config_bits() - update bits in register 474 * @client: I2C client 475 * @app_id: Application ID 476 * @reg: Application register 477 * @mask: Mask for the bits to update 478 * @val: Value of the bits to update 479 * 480 * Update bits in the given register using a bit mask. 481 * 482 * Locking note: This function must be called with the device lock held. 483 * Locking is not handled inside the function. Callers should ensure they 484 * serialize access to the HW. 485 * 486 * Returns: 0 on success, negative value on failure. 487 */ 488 int mma9551_update_config_bits(struct i2c_client *client, u8 app_id, 489 u16 reg, u8 mask, u8 val) 490 { 491 int ret; 492 u8 tmp, orig; 493 494 ret = mma9551_read_config_byte(client, app_id, reg, &orig); 495 if (ret < 0) 496 return ret; 497 498 tmp = orig & ~mask; 499 tmp |= val & mask; 500 501 if (tmp == orig) 502 return 0; 503 504 return mma9551_write_config_byte(client, app_id, reg, tmp); 505 } 506 EXPORT_SYMBOL(mma9551_update_config_bits); 507 508 /** 509 * mma9551_gpio_config() - configure gpio 510 * @client: I2C client 511 * @pin: GPIO pin to configure 512 * @app_id: Application ID 513 * @bitnum: Bit number of status register being assigned to the GPIO pin. 514 * @polarity: The polarity parameter is described in section 6.2.2, page 66, 515 * of the Software Reference Manual. Basically, polarity=0 means 516 * the interrupt line has the same value as the selected bit, 517 * while polarity=1 means the line is inverted. 518 * 519 * Assign a bit from an application’s status register to a specific GPIO pin. 520 * 521 * Locking note: This function must be called with the device lock held. 522 * Locking is not handled inside the function. Callers should ensure they 523 * serialize access to the HW. 524 * 525 * Returns: 0 on success, negative value on failure. 526 */ 527 int mma9551_gpio_config(struct i2c_client *client, enum mma9551_gpio_pin pin, 528 u8 app_id, u8 bitnum, int polarity) 529 { 530 u8 reg, pol_mask, pol_val; 531 int ret; 532 533 if (pin > mma9551_gpio_max) { 534 dev_err(&client->dev, "bad GPIO pin\n"); 535 return -EINVAL; 536 } 537 538 /* 539 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and 540 * 0x03, and so on. 541 */ 542 reg = pin * 2; 543 544 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO, 545 reg, app_id); 546 if (ret < 0) { 547 dev_err(&client->dev, "error setting GPIO app_id\n"); 548 return ret; 549 } 550 551 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO, 552 reg + 1, bitnum); 553 if (ret < 0) { 554 dev_err(&client->dev, "error setting GPIO bit number\n"); 555 return ret; 556 } 557 558 switch (pin) { 559 case mma9551_gpio6: 560 reg = MMA9551_GPIO_POL_LSB; 561 pol_mask = 1 << 6; 562 break; 563 case mma9551_gpio7: 564 reg = MMA9551_GPIO_POL_LSB; 565 pol_mask = 1 << 7; 566 break; 567 case mma9551_gpio8: 568 reg = MMA9551_GPIO_POL_MSB; 569 pol_mask = 1 << 0; 570 break; 571 case mma9551_gpio9: 572 reg = MMA9551_GPIO_POL_MSB; 573 pol_mask = 1 << 1; 574 break; 575 } 576 pol_val = polarity ? pol_mask : 0; 577 578 ret = mma9551_update_config_bits(client, MMA9551_APPID_GPIO, reg, 579 pol_mask, pol_val); 580 if (ret < 0) 581 dev_err(&client->dev, "error setting GPIO polarity\n"); 582 583 return ret; 584 } 585 EXPORT_SYMBOL(mma9551_gpio_config); 586 587 /** 588 * mma9551_read_version() - read device version information 589 * @client: I2C client 590 * 591 * Read version information and print device id and firmware version. 592 * 593 * Locking note: This function must be called with the device lock held. 594 * Locking is not handled inside the function. Callers should ensure they 595 * serialize access to the HW. 596 * 597 * Returns: 0 on success, negative value on failure. 598 */ 599 int mma9551_read_version(struct i2c_client *client) 600 { 601 struct mma9551_version_info info; 602 int ret; 603 604 ret = mma9551_transfer(client, MMA9551_APPID_VERSION, 0x00, 0x00, 605 NULL, 0, (u8 *)&info, sizeof(info)); 606 if (ret < 0) 607 return ret; 608 609 dev_info(&client->dev, "device ID 0x%x, firmware version %02x.%02x\n", 610 be32_to_cpu(info.device_id), info.fw_version[0], 611 info.fw_version[1]); 612 613 return 0; 614 } 615 EXPORT_SYMBOL(mma9551_read_version); 616 617 /** 618 * mma9551_set_device_state() - sets HW power mode 619 * @client: I2C client 620 * @enable: Use true to power on device, false to cause the device 621 * to enter sleep. 622 * 623 * Set power on/off for device using the Sleep/Wake Application. 624 * When enable is true, power on chip and enable doze mode. 625 * When enable is false, enter sleep mode (device remains in the 626 * lowest-power mode). 627 * 628 * Locking note: This function must be called with the device lock held. 629 * Locking is not handled inside the function. Callers should ensure they 630 * serialize access to the HW. 631 * 632 * Returns: 0 on success, negative value on failure. 633 */ 634 int mma9551_set_device_state(struct i2c_client *client, bool enable) 635 { 636 return mma9551_update_config_bits(client, MMA9551_APPID_SLEEP_WAKE, 637 MMA9551_SLEEP_CFG, 638 MMA9551_SLEEP_CFG_SNCEN | 639 MMA9551_SLEEP_CFG_FLEEN | 640 MMA9551_SLEEP_CFG_SCHEN, 641 enable ? MMA9551_SLEEP_CFG_SCHEN | 642 MMA9551_SLEEP_CFG_FLEEN : 643 MMA9551_SLEEP_CFG_SNCEN); 644 } 645 EXPORT_SYMBOL(mma9551_set_device_state); 646 647 /** 648 * mma9551_set_power_state() - sets runtime PM state 649 * @client: I2C client 650 * @on: Use true to power on device, false to power off 651 * 652 * Resume or suspend the device using Runtime PM. 653 * The device will suspend after the autosuspend delay. 654 * 655 * Returns: 0 on success, negative value on failure. 656 */ 657 int mma9551_set_power_state(struct i2c_client *client, bool on) 658 { 659 #ifdef CONFIG_PM 660 int ret; 661 662 if (on) 663 ret = pm_runtime_get_sync(&client->dev); 664 else { 665 pm_runtime_mark_last_busy(&client->dev); 666 ret = pm_runtime_put_autosuspend(&client->dev); 667 } 668 669 if (ret < 0) { 670 dev_err(&client->dev, 671 "failed to change power state to %d\n", on); 672 if (on) 673 pm_runtime_put_noidle(&client->dev); 674 675 return ret; 676 } 677 #endif 678 679 return 0; 680 } 681 EXPORT_SYMBOL(mma9551_set_power_state); 682 683 /** 684 * mma9551_sleep() - sleep 685 * @freq: Application frequency 686 * 687 * Firmware applications run at a certain frequency on the 688 * device. Sleep for one application cycle to make sure the 689 * application had time to run once and initialize set values. 690 */ 691 void mma9551_sleep(int freq) 692 { 693 int sleep_val = 1000 / freq; 694 695 if (sleep_val < 20) 696 usleep_range(sleep_val * 1000, 20000); 697 else 698 msleep_interruptible(sleep_val); 699 } 700 EXPORT_SYMBOL(mma9551_sleep); 701 702 /** 703 * mma9551_read_accel_chan() - read accelerometer channel 704 * @client: I2C client 705 * @chan: IIO channel 706 * @val: Pointer to the accelerometer value read 707 * @val2: Unused 708 * 709 * Read accelerometer value for the specified channel. 710 * 711 * Locking note: This function must be called with the device lock held. 712 * Locking is not handled inside the function. Callers should ensure they 713 * serialize access to the HW. 714 * 715 * Returns: IIO_VAL_INT on success, negative value on failure. 716 */ 717 int mma9551_read_accel_chan(struct i2c_client *client, 718 const struct iio_chan_spec *chan, 719 int *val, int *val2) 720 { 721 u16 reg_addr; 722 s16 raw_accel; 723 int ret; 724 725 switch (chan->channel2) { 726 case IIO_MOD_X: 727 reg_addr = MMA9551_AFE_X_ACCEL_REG; 728 break; 729 case IIO_MOD_Y: 730 reg_addr = MMA9551_AFE_Y_ACCEL_REG; 731 break; 732 case IIO_MOD_Z: 733 reg_addr = MMA9551_AFE_Z_ACCEL_REG; 734 break; 735 default: 736 return -EINVAL; 737 } 738 739 ret = mma9551_set_power_state(client, true); 740 if (ret < 0) 741 return ret; 742 743 ret = mma9551_read_status_word(client, MMA9551_APPID_AFE, 744 reg_addr, &raw_accel); 745 if (ret < 0) 746 goto out_poweroff; 747 748 *val = raw_accel; 749 750 ret = IIO_VAL_INT; 751 752 out_poweroff: 753 mma9551_set_power_state(client, false); 754 return ret; 755 } 756 EXPORT_SYMBOL(mma9551_read_accel_chan); 757 758 /** 759 * mma9551_read_accel_scale() - read accelerometer scale 760 * @val: Pointer to the accelerometer scale (int value) 761 * @val2: Pointer to the accelerometer scale (micro value) 762 * 763 * Read accelerometer scale. 764 * 765 * Returns: IIO_VAL_INT_PLUS_MICRO. 766 */ 767 int mma9551_read_accel_scale(int *val, int *val2) 768 { 769 *val = 0; 770 *val2 = 2440; 771 772 return IIO_VAL_INT_PLUS_MICRO; 773 } 774 EXPORT_SYMBOL(mma9551_read_accel_scale); 775 776 /** 777 * mma9551_app_reset() - reset application 778 * @client: I2C client 779 * @app_mask: Application to reset 780 * 781 * Reset the given application (using the Reset/Suspend/Clear 782 * Control Application) 783 * 784 * Returns: 0 on success, negative value on failure. 785 */ 786 int mma9551_app_reset(struct i2c_client *client, u32 app_mask) 787 { 788 return mma9551_write_config_byte(client, MMA9551_APPID_RCS, 789 MMA9551_RSC_RESET + 790 MMA9551_RSC_OFFSET(app_mask), 791 MMA9551_RSC_VAL(app_mask)); 792 } 793 EXPORT_SYMBOL(mma9551_app_reset); 794 795 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 796 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 797 MODULE_LICENSE("GPL v2"); 798 MODULE_DESCRIPTION("MMA955xL sensors core"); 799