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 #include "mma9551_core.h" 27 28 #define MMA9551_DRV_NAME "mma9551" 29 #define MMA9551_IRQ_NAME "mma9551_event" 30 #define MMA9551_GPIO_COUNT 4 31 32 /* Tilt application (inclination in IIO terms). */ 33 #define MMA9551_TILT_XZ_ANG_REG 0x00 34 #define MMA9551_TILT_YZ_ANG_REG 0x01 35 #define MMA9551_TILT_XY_ANG_REG 0x02 36 #define MMA9551_TILT_ANGFLG BIT(7) 37 #define MMA9551_TILT_QUAD_REG 0x03 38 #define MMA9551_TILT_XY_QUAD_SHIFT 0 39 #define MMA9551_TILT_YZ_QUAD_SHIFT 2 40 #define MMA9551_TILT_XZ_QUAD_SHIFT 4 41 #define MMA9551_TILT_CFG_REG 0x01 42 #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0) 43 44 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */ 45 46 /* Tilt events are mapped to the first three GPIO pins. */ 47 enum mma9551_tilt_axis { 48 mma9551_x = 0, 49 mma9551_y, 50 mma9551_z, 51 }; 52 53 struct mma9551_data { 54 struct i2c_client *client; 55 struct mutex mutex; 56 int event_enabled[3]; 57 int irqs[MMA9551_GPIO_COUNT]; 58 }; 59 60 static int mma9551_read_incli_chan(struct i2c_client *client, 61 const struct iio_chan_spec *chan, 62 int *val) 63 { 64 u8 quad_shift, angle, quadrant; 65 u16 reg_addr; 66 int ret; 67 68 switch (chan->channel2) { 69 case IIO_MOD_X: 70 reg_addr = MMA9551_TILT_YZ_ANG_REG; 71 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT; 72 break; 73 case IIO_MOD_Y: 74 reg_addr = MMA9551_TILT_XZ_ANG_REG; 75 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT; 76 break; 77 case IIO_MOD_Z: 78 reg_addr = MMA9551_TILT_XY_ANG_REG; 79 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT; 80 break; 81 default: 82 return -EINVAL; 83 } 84 85 ret = mma9551_set_power_state(client, true); 86 if (ret < 0) 87 return ret; 88 89 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT, 90 reg_addr, &angle); 91 if (ret < 0) 92 goto out_poweroff; 93 94 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT, 95 MMA9551_TILT_QUAD_REG, &quadrant); 96 if (ret < 0) 97 goto out_poweroff; 98 99 angle &= ~MMA9551_TILT_ANGFLG; 100 quadrant = (quadrant >> quad_shift) & 0x03; 101 102 if (quadrant == 1 || quadrant == 3) 103 *val = 90 * (quadrant + 1) - angle; 104 else 105 *val = angle + 90 * quadrant; 106 107 ret = IIO_VAL_INT; 108 109 out_poweroff: 110 mma9551_set_power_state(client, false); 111 return ret; 112 } 113 114 static int mma9551_read_raw(struct iio_dev *indio_dev, 115 struct iio_chan_spec const *chan, 116 int *val, int *val2, long mask) 117 { 118 struct mma9551_data *data = iio_priv(indio_dev); 119 int ret; 120 121 switch (mask) { 122 case IIO_CHAN_INFO_PROCESSED: 123 switch (chan->type) { 124 case IIO_INCLI: 125 mutex_lock(&data->mutex); 126 ret = mma9551_read_incli_chan(data->client, chan, val); 127 mutex_unlock(&data->mutex); 128 return ret; 129 default: 130 return -EINVAL; 131 } 132 case IIO_CHAN_INFO_RAW: 133 switch (chan->type) { 134 case IIO_ACCEL: 135 mutex_lock(&data->mutex); 136 ret = mma9551_read_accel_chan(data->client, 137 chan, val, val2); 138 mutex_unlock(&data->mutex); 139 return ret; 140 default: 141 return -EINVAL; 142 } 143 case IIO_CHAN_INFO_SCALE: 144 switch (chan->type) { 145 case IIO_ACCEL: 146 return mma9551_read_accel_scale(val, val2); 147 default: 148 return -EINVAL; 149 } 150 default: 151 return -EINVAL; 152 } 153 } 154 155 static int mma9551_read_event_config(struct iio_dev *indio_dev, 156 const struct iio_chan_spec *chan, 157 enum iio_event_type type, 158 enum iio_event_direction dir) 159 { 160 struct mma9551_data *data = iio_priv(indio_dev); 161 162 switch (chan->type) { 163 case IIO_INCLI: 164 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */ 165 return data->event_enabled[chan->channel2 - 1]; 166 default: 167 return -EINVAL; 168 } 169 } 170 171 static int mma9551_config_incli_event(struct iio_dev *indio_dev, 172 enum iio_modifier axis, 173 int state) 174 { 175 struct mma9551_data *data = iio_priv(indio_dev); 176 enum mma9551_tilt_axis mma_axis; 177 int ret; 178 179 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */ 180 mma_axis = axis - 1; 181 182 if (data->event_enabled[mma_axis] == state) 183 return 0; 184 185 if (state == 0) { 186 ret = mma9551_gpio_config(data->client, 187 (enum mma9551_gpio_pin)mma_axis, 188 MMA9551_APPID_NONE, 0, 0); 189 if (ret < 0) 190 return ret; 191 192 ret = mma9551_set_power_state(data->client, false); 193 if (ret < 0) 194 return ret; 195 } else { 196 int bitnum; 197 198 /* Bit 7 of each angle register holds the angle flag. */ 199 switch (axis) { 200 case IIO_MOD_X: 201 bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG; 202 break; 203 case IIO_MOD_Y: 204 bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG; 205 break; 206 case IIO_MOD_Z: 207 bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG; 208 break; 209 default: 210 return -EINVAL; 211 } 212 213 214 ret = mma9551_set_power_state(data->client, true); 215 if (ret < 0) 216 return ret; 217 218 ret = mma9551_gpio_config(data->client, 219 (enum mma9551_gpio_pin)mma_axis, 220 MMA9551_APPID_TILT, bitnum, 0); 221 if (ret < 0) { 222 mma9551_set_power_state(data->client, false); 223 return ret; 224 } 225 } 226 227 data->event_enabled[mma_axis] = state; 228 229 return ret; 230 } 231 232 static int mma9551_write_event_config(struct iio_dev *indio_dev, 233 const struct iio_chan_spec *chan, 234 enum iio_event_type type, 235 enum iio_event_direction dir, 236 int state) 237 { 238 struct mma9551_data *data = iio_priv(indio_dev); 239 int ret; 240 241 switch (chan->type) { 242 case IIO_INCLI: 243 mutex_lock(&data->mutex); 244 ret = mma9551_config_incli_event(indio_dev, 245 chan->channel2, state); 246 mutex_unlock(&data->mutex); 247 return ret; 248 default: 249 return -EINVAL; 250 } 251 } 252 253 static int mma9551_write_event_value(struct iio_dev *indio_dev, 254 const struct iio_chan_spec *chan, 255 enum iio_event_type type, 256 enum iio_event_direction dir, 257 enum iio_event_info info, 258 int val, int val2) 259 { 260 struct mma9551_data *data = iio_priv(indio_dev); 261 int ret; 262 263 switch (chan->type) { 264 case IIO_INCLI: 265 if (val2 != 0 || val < 1 || val > 10) 266 return -EINVAL; 267 mutex_lock(&data->mutex); 268 ret = mma9551_update_config_bits(data->client, 269 MMA9551_APPID_TILT, 270 MMA9551_TILT_CFG_REG, 271 MMA9551_TILT_ANG_THRESH_MASK, 272 val); 273 mutex_unlock(&data->mutex); 274 return ret; 275 default: 276 return -EINVAL; 277 } 278 } 279 280 static int mma9551_read_event_value(struct iio_dev *indio_dev, 281 const struct iio_chan_spec *chan, 282 enum iio_event_type type, 283 enum iio_event_direction dir, 284 enum iio_event_info info, 285 int *val, int *val2) 286 { 287 struct mma9551_data *data = iio_priv(indio_dev); 288 int ret; 289 u8 tmp; 290 291 switch (chan->type) { 292 case IIO_INCLI: 293 mutex_lock(&data->mutex); 294 ret = mma9551_read_config_byte(data->client, 295 MMA9551_APPID_TILT, 296 MMA9551_TILT_CFG_REG, &tmp); 297 mutex_unlock(&data->mutex); 298 if (ret < 0) 299 return ret; 300 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK; 301 *val2 = 0; 302 return IIO_VAL_INT; 303 default: 304 return -EINVAL; 305 } 306 } 307 308 static const struct iio_event_spec mma9551_incli_event = { 309 .type = IIO_EV_TYPE_ROC, 310 .dir = IIO_EV_DIR_RISING, 311 .mask_separate = BIT(IIO_EV_INFO_ENABLE), 312 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE), 313 }; 314 315 #define MMA9551_INCLI_CHANNEL(axis) { \ 316 .type = IIO_INCLI, \ 317 .modified = 1, \ 318 .channel2 = axis, \ 319 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 320 .event_spec = &mma9551_incli_event, \ 321 .num_event_specs = 1, \ 322 } 323 324 static const struct iio_chan_spec mma9551_channels[] = { 325 MMA9551_ACCEL_CHANNEL(IIO_MOD_X), 326 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y), 327 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z), 328 329 MMA9551_INCLI_CHANNEL(IIO_MOD_X), 330 MMA9551_INCLI_CHANNEL(IIO_MOD_Y), 331 MMA9551_INCLI_CHANNEL(IIO_MOD_Z), 332 }; 333 334 static const struct iio_info mma9551_info = { 335 .driver_module = THIS_MODULE, 336 .read_raw = mma9551_read_raw, 337 .read_event_config = mma9551_read_event_config, 338 .write_event_config = mma9551_write_event_config, 339 .read_event_value = mma9551_read_event_value, 340 .write_event_value = mma9551_write_event_value, 341 }; 342 343 static irqreturn_t mma9551_event_handler(int irq, void *private) 344 { 345 struct iio_dev *indio_dev = private; 346 struct mma9551_data *data = iio_priv(indio_dev); 347 int i, ret, mma_axis = -1; 348 u16 reg; 349 u8 val; 350 351 mutex_lock(&data->mutex); 352 353 for (i = 0; i < 3; i++) 354 if (irq == data->irqs[i]) { 355 mma_axis = i; 356 break; 357 } 358 359 if (mma_axis == -1) { 360 /* IRQ was triggered on 4th line, which we don't use. */ 361 dev_warn(&data->client->dev, 362 "irq triggered on unused line %d\n", data->irqs[3]); 363 goto out; 364 } 365 366 switch (mma_axis) { 367 case mma9551_x: 368 reg = MMA9551_TILT_YZ_ANG_REG; 369 break; 370 case mma9551_y: 371 reg = MMA9551_TILT_XZ_ANG_REG; 372 break; 373 case mma9551_z: 374 reg = MMA9551_TILT_XY_ANG_REG; 375 break; 376 } 377 378 /* 379 * Read the angle even though we don't use it, otherwise we 380 * won't get any further interrupts. 381 */ 382 ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT, 383 reg, &val); 384 if (ret < 0) { 385 dev_err(&data->client->dev, 386 "error %d reading tilt register in IRQ\n", ret); 387 goto out; 388 } 389 390 iio_push_event(indio_dev, 391 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1), 392 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING), 393 iio_get_time_ns(indio_dev)); 394 395 out: 396 mutex_unlock(&data->mutex); 397 398 return IRQ_HANDLED; 399 } 400 401 static int mma9551_init(struct mma9551_data *data) 402 { 403 int ret; 404 405 ret = mma9551_read_version(data->client); 406 if (ret) 407 return ret; 408 409 return mma9551_set_device_state(data->client, true); 410 } 411 412 static int mma9551_gpio_probe(struct iio_dev *indio_dev) 413 { 414 struct gpio_desc *gpio; 415 int i, ret; 416 struct mma9551_data *data = iio_priv(indio_dev); 417 struct device *dev = &data->client->dev; 418 419 for (i = 0; i < MMA9551_GPIO_COUNT; i++) { 420 gpio = devm_gpiod_get_index(dev, NULL, i, GPIOD_IN); 421 if (IS_ERR(gpio)) { 422 dev_err(dev, "acpi gpio get index failed\n"); 423 return PTR_ERR(gpio); 424 } 425 426 ret = gpiod_to_irq(gpio); 427 if (ret < 0) 428 return ret; 429 430 data->irqs[i] = ret; 431 ret = devm_request_threaded_irq(dev, data->irqs[i], 432 NULL, mma9551_event_handler, 433 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 434 MMA9551_IRQ_NAME, indio_dev); 435 if (ret < 0) { 436 dev_err(dev, "request irq %d failed\n", data->irqs[i]); 437 return ret; 438 } 439 440 dev_dbg(dev, "gpio resource, no:%d irq:%d\n", 441 desc_to_gpio(gpio), data->irqs[i]); 442 } 443 444 return 0; 445 } 446 447 static const char *mma9551_match_acpi_device(struct device *dev) 448 { 449 const struct acpi_device_id *id; 450 451 id = acpi_match_device(dev->driver->acpi_match_table, dev); 452 if (!id) 453 return NULL; 454 455 return dev_name(dev); 456 } 457 458 static int mma9551_probe(struct i2c_client *client, 459 const struct i2c_device_id *id) 460 { 461 struct mma9551_data *data; 462 struct iio_dev *indio_dev; 463 const char *name = NULL; 464 int ret; 465 466 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 467 if (!indio_dev) 468 return -ENOMEM; 469 470 data = iio_priv(indio_dev); 471 i2c_set_clientdata(client, indio_dev); 472 data->client = client; 473 474 if (id) 475 name = id->name; 476 else if (ACPI_HANDLE(&client->dev)) 477 name = mma9551_match_acpi_device(&client->dev); 478 479 ret = mma9551_init(data); 480 if (ret < 0) 481 return ret; 482 483 mutex_init(&data->mutex); 484 485 indio_dev->dev.parent = &client->dev; 486 indio_dev->channels = mma9551_channels; 487 indio_dev->num_channels = ARRAY_SIZE(mma9551_channels); 488 indio_dev->name = name; 489 indio_dev->modes = INDIO_DIRECT_MODE; 490 indio_dev->info = &mma9551_info; 491 492 ret = mma9551_gpio_probe(indio_dev); 493 if (ret < 0) 494 goto out_poweroff; 495 496 ret = pm_runtime_set_active(&client->dev); 497 if (ret < 0) 498 goto out_poweroff; 499 500 pm_runtime_enable(&client->dev); 501 pm_runtime_set_autosuspend_delay(&client->dev, 502 MMA9551_AUTO_SUSPEND_DELAY_MS); 503 pm_runtime_use_autosuspend(&client->dev); 504 505 ret = iio_device_register(indio_dev); 506 if (ret < 0) { 507 dev_err(&client->dev, "unable to register iio device\n"); 508 goto out_poweroff; 509 } 510 511 return 0; 512 513 out_poweroff: 514 mma9551_set_device_state(client, false); 515 516 return ret; 517 } 518 519 static int mma9551_remove(struct i2c_client *client) 520 { 521 struct iio_dev *indio_dev = i2c_get_clientdata(client); 522 struct mma9551_data *data = iio_priv(indio_dev); 523 524 iio_device_unregister(indio_dev); 525 526 pm_runtime_disable(&client->dev); 527 pm_runtime_set_suspended(&client->dev); 528 pm_runtime_put_noidle(&client->dev); 529 530 mutex_lock(&data->mutex); 531 mma9551_set_device_state(data->client, false); 532 mutex_unlock(&data->mutex); 533 534 return 0; 535 } 536 537 #ifdef CONFIG_PM 538 static int mma9551_runtime_suspend(struct device *dev) 539 { 540 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 541 struct mma9551_data *data = iio_priv(indio_dev); 542 int ret; 543 544 mutex_lock(&data->mutex); 545 ret = mma9551_set_device_state(data->client, false); 546 mutex_unlock(&data->mutex); 547 if (ret < 0) { 548 dev_err(&data->client->dev, "powering off device failed\n"); 549 return -EAGAIN; 550 } 551 552 return 0; 553 } 554 555 static int mma9551_runtime_resume(struct device *dev) 556 { 557 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 558 struct mma9551_data *data = iio_priv(indio_dev); 559 int ret; 560 561 ret = mma9551_set_device_state(data->client, true); 562 if (ret < 0) 563 return ret; 564 565 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE); 566 567 return 0; 568 } 569 #endif 570 571 #ifdef CONFIG_PM_SLEEP 572 static int mma9551_suspend(struct device *dev) 573 { 574 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 575 struct mma9551_data *data = iio_priv(indio_dev); 576 int ret; 577 578 mutex_lock(&data->mutex); 579 ret = mma9551_set_device_state(data->client, false); 580 mutex_unlock(&data->mutex); 581 582 return ret; 583 } 584 585 static int mma9551_resume(struct device *dev) 586 { 587 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 588 struct mma9551_data *data = iio_priv(indio_dev); 589 int ret; 590 591 mutex_lock(&data->mutex); 592 ret = mma9551_set_device_state(data->client, true); 593 mutex_unlock(&data->mutex); 594 595 return ret; 596 } 597 #endif 598 599 static const struct dev_pm_ops mma9551_pm_ops = { 600 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume) 601 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend, 602 mma9551_runtime_resume, NULL) 603 }; 604 605 static const struct acpi_device_id mma9551_acpi_match[] = { 606 {"MMA9551", 0}, 607 {}, 608 }; 609 610 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match); 611 612 static const struct i2c_device_id mma9551_id[] = { 613 {"mma9551", 0}, 614 {} 615 }; 616 617 MODULE_DEVICE_TABLE(i2c, mma9551_id); 618 619 static struct i2c_driver mma9551_driver = { 620 .driver = { 621 .name = MMA9551_DRV_NAME, 622 .acpi_match_table = ACPI_PTR(mma9551_acpi_match), 623 .pm = &mma9551_pm_ops, 624 }, 625 .probe = mma9551_probe, 626 .remove = mma9551_remove, 627 .id_table = mma9551_id, 628 }; 629 630 module_i2c_driver(mma9551_driver); 631 632 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 633 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 634 MODULE_LICENSE("GPL v2"); 635 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver"); 636