Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Freescale MMA9553L Intelligent Pedometer 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/gpio/consumer.h> |
| 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
| 23 | #include <linux/iio/events.h> |
| 24 | #include <linux/pm_runtime.h> |
| 25 | #include "mma9551_core.h" |
| 26 | |
| 27 | #define MMA9553_DRV_NAME "mma9553" |
| 28 | #define MMA9553_IRQ_NAME "mma9553_event" |
| 29 | #define MMA9553_GPIO_NAME "mma9553_int" |
| 30 | |
| 31 | /* Pedometer configuration registers (R/W) */ |
| 32 | #define MMA9553_REG_CONF_SLEEPMIN 0x00 |
| 33 | #define MMA9553_REG_CONF_SLEEPMAX 0x02 |
| 34 | #define MMA9553_REG_CONF_SLEEPTHD 0x04 |
| 35 | #define MMA9553_MASK_CONF_WORD GENMASK(15, 0) |
| 36 | |
| 37 | #define MMA9553_REG_CONF_CONF_STEPLEN 0x06 |
| 38 | #define MMA9553_MASK_CONF_CONFIG BIT(15) |
| 39 | #define MMA9553_MASK_CONF_ACT_DBCNTM BIT(14) |
| 40 | #define MMA9553_MASK_CONF_SLP_DBCNTM BIT(13) |
| 41 | #define MMA9553_MASK_CONF_STEPLEN GENMASK(7, 0) |
| 42 | |
| 43 | #define MMA9553_REG_CONF_HEIGHT_WEIGHT 0x08 |
| 44 | #define MMA9553_MASK_CONF_HEIGHT GENMASK(15, 8) |
| 45 | #define MMA9553_MASK_CONF_WEIGHT GENMASK(7, 0) |
| 46 | |
| 47 | #define MMA9553_REG_CONF_FILTER 0x0A |
| 48 | #define MMA9553_MASK_CONF_FILTSTEP GENMASK(15, 8) |
| 49 | #define MMA9553_MASK_CONF_MALE BIT(7) |
| 50 | #define MMA9553_MASK_CONF_FILTTIME GENMASK(6, 0) |
| 51 | |
| 52 | #define MMA9553_REG_CONF_SPEED_STEP 0x0C |
| 53 | #define MMA9553_MASK_CONF_SPDPRD GENMASK(15, 8) |
| 54 | #define MMA9553_MASK_CONF_STEPCOALESCE GENMASK(7, 0) |
| 55 | |
| 56 | #define MMA9553_REG_CONF_ACTTHD 0x0E |
| 57 | |
| 58 | /* Pedometer status registers (R-only) */ |
| 59 | #define MMA9553_REG_STATUS 0x00 |
| 60 | #define MMA9553_MASK_STATUS_MRGFL BIT(15) |
| 61 | #define MMA9553_MASK_STATUS_SUSPCHG BIT(14) |
| 62 | #define MMA9553_MASK_STATUS_STEPCHG BIT(13) |
| 63 | #define MMA9553_MASK_STATUS_ACTCHG BIT(12) |
| 64 | #define MMA9553_MASK_STATUS_SUSP BIT(11) |
| 65 | #define MMA9553_MASK_STATUS_ACTIVITY (BIT(10) | BIT(9) | BIT(8)) |
| 66 | #define MMA9553_MASK_STATUS_VERSION 0x00FF |
| 67 | |
| 68 | #define MMA9553_REG_STEPCNT 0x02 |
| 69 | #define MMA9553_REG_DISTANCE 0x04 |
| 70 | #define MMA9553_REG_SPEED 0x06 |
| 71 | #define MMA9553_REG_CALORIES 0x08 |
| 72 | #define MMA9553_REG_SLEEPCNT 0x0A |
| 73 | |
| 74 | /* Pedometer events are always mapped to this pin. */ |
| 75 | #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6 |
| 76 | #define MMA9553_DEFAULT_GPIO_POLARITY 0 |
| 77 | |
| 78 | /* Bitnum used for gpio configuration = bit number in high status byte */ |
| 79 | #define STATUS_TO_BITNUM(bit) (ffs(bit) - 9) |
| 80 | |
| 81 | #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */ |
| 82 | |
| 83 | /* |
| 84 | * The internal activity level must be stable for ACTTHD samples before |
| 85 | * ACTIVITY is updated.The ACTIVITY variable contains the current activity |
| 86 | * level and is updated every time a step is detected or once a second |
| 87 | * if there are no steps. |
| 88 | */ |
| 89 | #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE) |
| 90 | #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE) |
| 91 | |
| 92 | /* |
| 93 | * Autonomously suspend pedometer if acceleration vector magnitude |
| 94 | * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds. |
| 95 | */ |
| 96 | #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */ |
| 97 | #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */ |
| 98 | #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30) |
| 99 | |
| 100 | #define MMA9553_CONFIG_RETRIES 2 |
| 101 | |
| 102 | /* Status register - activity field */ |
| 103 | enum activity_level { |
| 104 | ACTIVITY_UNKNOWN, |
| 105 | ACTIVITY_REST, |
| 106 | ACTIVITY_WALKING, |
| 107 | ACTIVITY_JOGGING, |
| 108 | ACTIVITY_RUNNING, |
| 109 | }; |
| 110 | |
| 111 | static struct mma9553_event_info { |
| 112 | enum iio_chan_type type; |
| 113 | enum iio_modifier mod; |
| 114 | enum iio_event_direction dir; |
| 115 | } mma9553_events_info[] = { |
| 116 | { |
| 117 | .type = IIO_STEPS, |
| 118 | .mod = IIO_NO_MOD, |
| 119 | .dir = IIO_EV_DIR_NONE, |
| 120 | }, |
| 121 | { |
| 122 | .type = IIO_ACTIVITY, |
| 123 | .mod = IIO_MOD_STILL, |
| 124 | .dir = IIO_EV_DIR_RISING, |
| 125 | }, |
| 126 | { |
| 127 | .type = IIO_ACTIVITY, |
| 128 | .mod = IIO_MOD_STILL, |
| 129 | .dir = IIO_EV_DIR_FALLING, |
| 130 | }, |
| 131 | { |
| 132 | .type = IIO_ACTIVITY, |
| 133 | .mod = IIO_MOD_WALKING, |
| 134 | .dir = IIO_EV_DIR_RISING, |
| 135 | }, |
| 136 | { |
| 137 | .type = IIO_ACTIVITY, |
| 138 | .mod = IIO_MOD_WALKING, |
| 139 | .dir = IIO_EV_DIR_FALLING, |
| 140 | }, |
| 141 | { |
| 142 | .type = IIO_ACTIVITY, |
| 143 | .mod = IIO_MOD_JOGGING, |
| 144 | .dir = IIO_EV_DIR_RISING, |
| 145 | }, |
| 146 | { |
| 147 | .type = IIO_ACTIVITY, |
| 148 | .mod = IIO_MOD_JOGGING, |
| 149 | .dir = IIO_EV_DIR_FALLING, |
| 150 | }, |
| 151 | { |
| 152 | .type = IIO_ACTIVITY, |
| 153 | .mod = IIO_MOD_RUNNING, |
| 154 | .dir = IIO_EV_DIR_RISING, |
| 155 | }, |
| 156 | { |
| 157 | .type = IIO_ACTIVITY, |
| 158 | .mod = IIO_MOD_RUNNING, |
| 159 | .dir = IIO_EV_DIR_FALLING, |
| 160 | }, |
| 161 | }; |
| 162 | |
| 163 | #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info) |
| 164 | |
| 165 | struct mma9553_event { |
| 166 | struct mma9553_event_info *info; |
| 167 | bool enabled; |
| 168 | }; |
| 169 | |
| 170 | struct mma9553_conf_regs { |
| 171 | u16 sleepmin; |
| 172 | u16 sleepmax; |
| 173 | u16 sleepthd; |
| 174 | u16 config; |
| 175 | u16 height_weight; |
| 176 | u16 filter; |
| 177 | u16 speed_step; |
| 178 | u16 actthd; |
| 179 | } __packed; |
| 180 | |
| 181 | struct mma9553_data { |
| 182 | struct i2c_client *client; |
| 183 | struct mutex mutex; |
| 184 | struct mma9553_conf_regs conf; |
| 185 | struct mma9553_event events[MMA9553_EVENTS_INFO_SIZE]; |
| 186 | int num_events; |
| 187 | u8 gpio_bitnum; |
| 188 | /* |
| 189 | * This is used for all features that depend on step count: |
| 190 | * step count, distance, speed, calories. |
| 191 | */ |
| 192 | bool stepcnt_enabled; |
| 193 | u16 stepcnt; |
| 194 | u8 activity; |
| 195 | s64 timestamp; |
| 196 | }; |
| 197 | |
| 198 | static u8 mma9553_get_bits(u16 val, u16 mask) |
| 199 | { |
| 200 | return (val & mask) >> (ffs(mask) - 1); |
| 201 | } |
| 202 | |
| 203 | static u16 mma9553_set_bits(u16 current_val, u16 val, u16 mask) |
| 204 | { |
| 205 | return (current_val & ~mask) | (val << (ffs(mask) - 1)); |
| 206 | } |
| 207 | |
| 208 | static enum iio_modifier mma9553_activity_to_mod(enum activity_level activity) |
| 209 | { |
| 210 | switch (activity) { |
| 211 | case ACTIVITY_RUNNING: |
| 212 | return IIO_MOD_RUNNING; |
| 213 | case ACTIVITY_JOGGING: |
| 214 | return IIO_MOD_JOGGING; |
| 215 | case ACTIVITY_WALKING: |
| 216 | return IIO_MOD_WALKING; |
| 217 | case ACTIVITY_REST: |
| 218 | return IIO_MOD_STILL; |
| 219 | case ACTIVITY_UNKNOWN: |
| 220 | default: |
| 221 | return IIO_NO_MOD; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static void mma9553_init_events(struct mma9553_data *data) |
| 226 | { |
| 227 | int i; |
| 228 | |
| 229 | data->num_events = MMA9553_EVENTS_INFO_SIZE; |
| 230 | for (i = 0; i < data->num_events; i++) { |
| 231 | data->events[i].info = &mma9553_events_info[i]; |
| 232 | data->events[i].enabled = false; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static struct mma9553_event *mma9553_get_event(struct mma9553_data *data, |
| 237 | enum iio_chan_type type, |
| 238 | enum iio_modifier mod, |
| 239 | enum iio_event_direction dir) |
| 240 | { |
| 241 | int i; |
| 242 | |
| 243 | for (i = 0; i < data->num_events; i++) |
| 244 | if (data->events[i].info->type == type && |
| 245 | data->events[i].info->mod == mod && |
| 246 | data->events[i].info->dir == dir) |
| 247 | return &data->events[i]; |
| 248 | |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | static bool mma9553_is_any_event_enabled(struct mma9553_data *data, |
| 253 | bool check_type, |
| 254 | enum iio_chan_type type) |
| 255 | { |
| 256 | int i; |
| 257 | |
| 258 | for (i = 0; i < data->num_events; i++) |
| 259 | if ((check_type && data->events[i].info->type == type && |
| 260 | data->events[i].enabled) || |
| 261 | (!check_type && data->events[i].enabled)) |
| 262 | return true; |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | static int mma9553_set_config(struct mma9553_data *data, u16 reg, |
| 268 | u16 *p_reg_val, u16 val, u16 mask) |
| 269 | { |
| 270 | int ret, retries; |
| 271 | u16 reg_val, config; |
| 272 | |
| 273 | reg_val = *p_reg_val; |
| 274 | if (val == mma9553_get_bits(reg_val, mask)) |
| 275 | return 0; |
| 276 | |
| 277 | reg_val = mma9553_set_bits(reg_val, val, mask); |
| 278 | ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER, |
| 279 | reg, reg_val); |
| 280 | if (ret < 0) { |
| 281 | dev_err(&data->client->dev, |
| 282 | "error writing config register 0x%x\n", reg); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | *p_reg_val = reg_val; |
| 287 | |
| 288 | /* Reinitializes the pedometer with current configuration values */ |
| 289 | config = mma9553_set_bits(data->conf.config, 1, |
| 290 | MMA9553_MASK_CONF_CONFIG); |
| 291 | |
| 292 | ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER, |
| 293 | MMA9553_REG_CONF_CONF_STEPLEN, config); |
| 294 | if (ret < 0) { |
| 295 | dev_err(&data->client->dev, |
| 296 | "error writing config register 0x%x\n", |
| 297 | MMA9553_REG_CONF_CONF_STEPLEN); |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | retries = MMA9553_CONFIG_RETRIES; |
| 302 | do { |
| 303 | mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE); |
| 304 | ret = mma9551_read_config_word(data->client, |
| 305 | MMA9551_APPID_PEDOMETER, |
| 306 | MMA9553_REG_CONF_CONF_STEPLEN, |
| 307 | &config); |
| 308 | if (ret < 0) |
| 309 | return ret; |
| 310 | } while (mma9553_get_bits(config, MMA9553_MASK_CONF_CONFIG) && |
| 311 | --retries > 0); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int mma9553_read_activity_stepcnt(struct mma9553_data *data, |
| 317 | u8 *activity, u16 *stepcnt) |
| 318 | { |
Irina Tirdea | cd62322 | 2015-04-13 18:40:48 +0300 | [diff] [blame] | 319 | u16 buf[2]; |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 320 | int ret; |
| 321 | |
| 322 | ret = mma9551_read_status_words(data->client, MMA9551_APPID_PEDOMETER, |
Irina Tirdea | cd62322 | 2015-04-13 18:40:48 +0300 | [diff] [blame] | 323 | MMA9553_REG_STATUS, sizeof(u32), buf); |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 324 | if (ret < 0) { |
| 325 | dev_err(&data->client->dev, |
| 326 | "error reading status and stepcnt\n"); |
| 327 | return ret; |
| 328 | } |
| 329 | |
Irina Tirdea | cd62322 | 2015-04-13 18:40:48 +0300 | [diff] [blame] | 330 | *activity = mma9553_get_bits(buf[0], MMA9553_MASK_STATUS_ACTIVITY); |
| 331 | *stepcnt = buf[1]; |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int mma9553_conf_gpio(struct mma9553_data *data) |
| 337 | { |
| 338 | u8 bitnum = 0, appid = MMA9551_APPID_PEDOMETER; |
| 339 | int ret; |
| 340 | struct mma9553_event *ev_step_detect; |
| 341 | bool activity_enabled; |
| 342 | |
| 343 | activity_enabled = |
| 344 | mma9553_is_any_event_enabled(data, true, IIO_ACTIVITY); |
| 345 | ev_step_detect = |
| 346 | mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD, IIO_EV_DIR_NONE); |
| 347 | |
| 348 | /* |
| 349 | * If both step detector and activity are enabled, use the MRGFL bit. |
| 350 | * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags. |
| 351 | */ |
| 352 | if (activity_enabled && ev_step_detect->enabled) |
| 353 | bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL); |
| 354 | else if (ev_step_detect->enabled) |
| 355 | bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG); |
| 356 | else if (activity_enabled) |
| 357 | bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG); |
| 358 | else /* Reset */ |
| 359 | appid = MMA9551_APPID_NONE; |
| 360 | |
| 361 | if (data->gpio_bitnum == bitnum) |
| 362 | return 0; |
| 363 | |
| 364 | /* Save initial values for activity and stepcnt */ |
| 365 | if (activity_enabled || ev_step_detect->enabled) |
| 366 | mma9553_read_activity_stepcnt(data, &data->activity, |
| 367 | &data->stepcnt); |
| 368 | |
| 369 | ret = mma9551_gpio_config(data->client, |
| 370 | MMA9553_DEFAULT_GPIO_PIN, |
| 371 | appid, bitnum, MMA9553_DEFAULT_GPIO_POLARITY); |
| 372 | if (ret < 0) |
| 373 | return ret; |
| 374 | data->gpio_bitnum = bitnum; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int mma9553_init(struct mma9553_data *data) |
| 380 | { |
| 381 | int ret; |
| 382 | |
| 383 | ret = mma9551_read_version(data->client); |
| 384 | if (ret) |
| 385 | return ret; |
| 386 | |
| 387 | /* |
| 388 | * Read all the pedometer configuration registers. This is used as |
| 389 | * a device identification command to differentiate the MMA9553L |
| 390 | * from the MMA9550L. |
| 391 | */ |
| 392 | ret = |
| 393 | mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER, |
| 394 | MMA9553_REG_CONF_SLEEPMIN, |
| 395 | sizeof(data->conf), (u16 *) &data->conf); |
| 396 | if (ret < 0) { |
| 397 | dev_err(&data->client->dev, |
| 398 | "device is not MMA9553L: failed to read cfg regs\n"); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /* Reset gpio */ |
| 404 | data->gpio_bitnum = -1; |
| 405 | ret = mma9553_conf_gpio(data); |
| 406 | if (ret < 0) |
| 407 | return ret; |
| 408 | |
| 409 | ret = mma9551_app_reset(data->client, MMA9551_RSC_PED); |
| 410 | if (ret < 0) |
| 411 | return ret; |
| 412 | |
| 413 | /* Init config registers */ |
| 414 | data->conf.sleepmin = MMA9553_DEFAULT_SLEEPMIN; |
| 415 | data->conf.sleepmax = MMA9553_DEFAULT_SLEEPMAX; |
| 416 | data->conf.sleepthd = MMA9553_DEFAULT_SLEEPTHD; |
| 417 | data->conf.config = |
| 418 | mma9553_set_bits(data->conf.config, 1, MMA9553_MASK_CONF_CONFIG); |
| 419 | /* |
| 420 | * Clear the activity debounce counter when the activity level changes, |
| 421 | * so that the confidence level applies for any activity level. |
| 422 | */ |
| 423 | data->conf.config = mma9553_set_bits(data->conf.config, 1, |
| 424 | MMA9553_MASK_CONF_ACT_DBCNTM); |
| 425 | ret = |
| 426 | mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER, |
| 427 | MMA9553_REG_CONF_SLEEPMIN, |
| 428 | sizeof(data->conf), (u16 *) &data->conf); |
| 429 | if (ret < 0) { |
| 430 | dev_err(&data->client->dev, |
| 431 | "failed to write configuration registers\n"); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | return mma9551_set_device_state(data->client, true); |
| 436 | } |
| 437 | |
| 438 | static int mma9553_read_raw(struct iio_dev *indio_dev, |
| 439 | struct iio_chan_spec const *chan, |
| 440 | int *val, int *val2, long mask) |
| 441 | { |
| 442 | struct mma9553_data *data = iio_priv(indio_dev); |
| 443 | int ret; |
| 444 | u16 tmp; |
| 445 | u8 activity; |
| 446 | bool powered_on; |
| 447 | |
| 448 | switch (mask) { |
| 449 | case IIO_CHAN_INFO_PROCESSED: |
| 450 | switch (chan->type) { |
| 451 | case IIO_STEPS: |
| 452 | /* |
| 453 | * The HW only counts steps and other dependent |
| 454 | * parameters (speed, distance, calories, activity) |
| 455 | * if power is on (from enabling an event or the |
| 456 | * step counter */ |
| 457 | powered_on = |
| 458 | mma9553_is_any_event_enabled(data, false, 0) || |
| 459 | data->stepcnt_enabled; |
| 460 | if (!powered_on) { |
| 461 | dev_err(&data->client->dev, |
| 462 | "No channels enabled\n"); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | mutex_lock(&data->mutex); |
| 466 | ret = mma9551_read_status_word(data->client, |
| 467 | MMA9551_APPID_PEDOMETER, |
| 468 | MMA9553_REG_STEPCNT, |
| 469 | &tmp); |
| 470 | mutex_unlock(&data->mutex); |
| 471 | if (ret < 0) |
| 472 | return ret; |
| 473 | *val = tmp; |
| 474 | return IIO_VAL_INT; |
| 475 | case IIO_DISTANCE: |
| 476 | powered_on = |
| 477 | mma9553_is_any_event_enabled(data, false, 0) || |
| 478 | data->stepcnt_enabled; |
| 479 | if (!powered_on) { |
| 480 | dev_err(&data->client->dev, |
| 481 | "No channels enabled\n"); |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | mutex_lock(&data->mutex); |
| 485 | ret = mma9551_read_status_word(data->client, |
| 486 | MMA9551_APPID_PEDOMETER, |
| 487 | MMA9553_REG_DISTANCE, |
| 488 | &tmp); |
| 489 | mutex_unlock(&data->mutex); |
| 490 | if (ret < 0) |
| 491 | return ret; |
| 492 | *val = tmp; |
| 493 | return IIO_VAL_INT; |
| 494 | case IIO_ACTIVITY: |
| 495 | powered_on = |
| 496 | mma9553_is_any_event_enabled(data, false, 0) || |
| 497 | data->stepcnt_enabled; |
| 498 | if (!powered_on) { |
| 499 | dev_err(&data->client->dev, |
| 500 | "No channels enabled\n"); |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | mutex_lock(&data->mutex); |
| 504 | ret = mma9551_read_status_word(data->client, |
| 505 | MMA9551_APPID_PEDOMETER, |
| 506 | MMA9553_REG_STATUS, |
| 507 | &tmp); |
| 508 | mutex_unlock(&data->mutex); |
| 509 | if (ret < 0) |
| 510 | return ret; |
| 511 | |
| 512 | activity = |
| 513 | mma9553_get_bits(tmp, MMA9553_MASK_STATUS_ACTIVITY); |
| 514 | |
| 515 | /* |
| 516 | * The device does not support confidence value levels, |
| 517 | * so we will always have 100% for current activity and |
| 518 | * 0% for the others. |
| 519 | */ |
| 520 | if (chan->channel2 == mma9553_activity_to_mod(activity)) |
| 521 | *val = 100; |
| 522 | else |
| 523 | *val = 0; |
| 524 | return IIO_VAL_INT; |
| 525 | default: |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | case IIO_CHAN_INFO_RAW: |
| 529 | switch (chan->type) { |
| 530 | case IIO_VELOCITY: /* m/h */ |
| 531 | if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) |
| 532 | return -EINVAL; |
| 533 | powered_on = |
| 534 | mma9553_is_any_event_enabled(data, false, 0) || |
| 535 | data->stepcnt_enabled; |
| 536 | if (!powered_on) { |
| 537 | dev_err(&data->client->dev, |
| 538 | "No channels enabled\n"); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | mutex_lock(&data->mutex); |
| 542 | ret = mma9551_read_status_word(data->client, |
| 543 | MMA9551_APPID_PEDOMETER, |
| 544 | MMA9553_REG_SPEED, &tmp); |
| 545 | mutex_unlock(&data->mutex); |
| 546 | if (ret < 0) |
| 547 | return ret; |
| 548 | *val = tmp; |
| 549 | return IIO_VAL_INT; |
| 550 | case IIO_ENERGY: /* Cal or kcal */ |
| 551 | powered_on = |
| 552 | mma9553_is_any_event_enabled(data, false, 0) || |
| 553 | data->stepcnt_enabled; |
| 554 | if (!powered_on) { |
| 555 | dev_err(&data->client->dev, |
| 556 | "No channels enabled\n"); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | mutex_lock(&data->mutex); |
| 560 | ret = mma9551_read_status_word(data->client, |
| 561 | MMA9551_APPID_PEDOMETER, |
| 562 | MMA9553_REG_CALORIES, |
| 563 | &tmp); |
| 564 | mutex_unlock(&data->mutex); |
| 565 | if (ret < 0) |
| 566 | return ret; |
| 567 | *val = tmp; |
| 568 | return IIO_VAL_INT; |
| 569 | case IIO_ACCEL: |
| 570 | mutex_lock(&data->mutex); |
| 571 | ret = mma9551_read_accel_chan(data->client, |
| 572 | chan, val, val2); |
| 573 | mutex_unlock(&data->mutex); |
| 574 | return ret; |
| 575 | default: |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | case IIO_CHAN_INFO_SCALE: |
| 579 | switch (chan->type) { |
| 580 | case IIO_VELOCITY: /* m/h to m/s */ |
| 581 | if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) |
| 582 | return -EINVAL; |
| 583 | *val = 0; |
| 584 | *val2 = 277; /* 0.000277 */ |
| 585 | return IIO_VAL_INT_PLUS_MICRO; |
| 586 | case IIO_ENERGY: /* Cal or kcal to J */ |
| 587 | *val = 4184; |
| 588 | return IIO_VAL_INT; |
| 589 | case IIO_ACCEL: |
| 590 | return mma9551_read_accel_scale(val, val2); |
| 591 | default: |
| 592 | return -EINVAL; |
| 593 | } |
| 594 | case IIO_CHAN_INFO_ENABLE: |
| 595 | *val = data->stepcnt_enabled; |
| 596 | return IIO_VAL_INT; |
| 597 | case IIO_CHAN_INFO_CALIBHEIGHT: |
| 598 | tmp = mma9553_get_bits(data->conf.height_weight, |
| 599 | MMA9553_MASK_CONF_HEIGHT); |
| 600 | *val = tmp / 100; /* cm to m */ |
| 601 | *val2 = (tmp % 100) * 10000; |
| 602 | return IIO_VAL_INT_PLUS_MICRO; |
| 603 | case IIO_CHAN_INFO_CALIBWEIGHT: |
| 604 | *val = mma9553_get_bits(data->conf.height_weight, |
| 605 | MMA9553_MASK_CONF_WEIGHT); |
| 606 | return IIO_VAL_INT; |
| 607 | case IIO_CHAN_INFO_DEBOUNCE_COUNT: |
| 608 | switch (chan->type) { |
| 609 | case IIO_STEPS: |
| 610 | *val = mma9553_get_bits(data->conf.filter, |
| 611 | MMA9553_MASK_CONF_FILTSTEP); |
| 612 | return IIO_VAL_INT; |
| 613 | default: |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | case IIO_CHAN_INFO_DEBOUNCE_TIME: |
| 617 | switch (chan->type) { |
| 618 | case IIO_STEPS: |
| 619 | *val = mma9553_get_bits(data->conf.filter, |
| 620 | MMA9553_MASK_CONF_FILTTIME); |
| 621 | return IIO_VAL_INT; |
| 622 | default: |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | case IIO_CHAN_INFO_INT_TIME: |
| 626 | switch (chan->type) { |
| 627 | case IIO_VELOCITY: |
| 628 | if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) |
| 629 | return -EINVAL; |
| 630 | *val = mma9553_get_bits(data->conf.speed_step, |
| 631 | MMA9553_MASK_CONF_SPDPRD); |
| 632 | return IIO_VAL_INT; |
| 633 | default: |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | default: |
| 637 | return -EINVAL; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static int mma9553_write_raw(struct iio_dev *indio_dev, |
| 642 | struct iio_chan_spec const *chan, |
| 643 | int val, int val2, long mask) |
| 644 | { |
| 645 | struct mma9553_data *data = iio_priv(indio_dev); |
| 646 | int ret, tmp; |
| 647 | |
| 648 | switch (mask) { |
| 649 | case IIO_CHAN_INFO_ENABLE: |
| 650 | if (data->stepcnt_enabled == !!val) |
| 651 | return 0; |
| 652 | mutex_lock(&data->mutex); |
| 653 | ret = mma9551_set_power_state(data->client, val); |
| 654 | if (ret < 0) { |
| 655 | mutex_unlock(&data->mutex); |
| 656 | return ret; |
| 657 | } |
| 658 | data->stepcnt_enabled = val; |
| 659 | mutex_unlock(&data->mutex); |
| 660 | return 0; |
| 661 | case IIO_CHAN_INFO_CALIBHEIGHT: |
| 662 | /* m to cm */ |
| 663 | tmp = val * 100 + val2 / 10000; |
| 664 | if (tmp < 0 || tmp > 255) |
| 665 | return -EINVAL; |
| 666 | mutex_lock(&data->mutex); |
| 667 | ret = mma9553_set_config(data, |
| 668 | MMA9553_REG_CONF_HEIGHT_WEIGHT, |
| 669 | &data->conf.height_weight, |
| 670 | tmp, MMA9553_MASK_CONF_HEIGHT); |
| 671 | mutex_unlock(&data->mutex); |
| 672 | return ret; |
| 673 | case IIO_CHAN_INFO_CALIBWEIGHT: |
| 674 | if (val < 0 || val > 255) |
| 675 | return -EINVAL; |
| 676 | mutex_lock(&data->mutex); |
| 677 | ret = mma9553_set_config(data, |
| 678 | MMA9553_REG_CONF_HEIGHT_WEIGHT, |
| 679 | &data->conf.height_weight, |
| 680 | val, MMA9553_MASK_CONF_WEIGHT); |
| 681 | mutex_unlock(&data->mutex); |
| 682 | return ret; |
| 683 | case IIO_CHAN_INFO_DEBOUNCE_COUNT: |
| 684 | switch (chan->type) { |
| 685 | case IIO_STEPS: |
| 686 | /* |
| 687 | * Set to 0 to disable step filtering. If the value |
| 688 | * specified is greater than 6, then 6 will be used. |
| 689 | */ |
| 690 | if (val < 0) |
| 691 | return -EINVAL; |
| 692 | if (val > 6) |
| 693 | val = 6; |
| 694 | mutex_lock(&data->mutex); |
| 695 | ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER, |
| 696 | &data->conf.filter, val, |
| 697 | MMA9553_MASK_CONF_FILTSTEP); |
| 698 | mutex_unlock(&data->mutex); |
| 699 | return ret; |
| 700 | default: |
| 701 | return -EINVAL; |
| 702 | } |
| 703 | case IIO_CHAN_INFO_DEBOUNCE_TIME: |
| 704 | switch (chan->type) { |
| 705 | case IIO_STEPS: |
| 706 | if (val < 0 || val > 127) |
| 707 | return -EINVAL; |
| 708 | mutex_lock(&data->mutex); |
| 709 | ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER, |
| 710 | &data->conf.filter, val, |
| 711 | MMA9553_MASK_CONF_FILTTIME); |
| 712 | mutex_unlock(&data->mutex); |
| 713 | return ret; |
| 714 | default: |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | case IIO_CHAN_INFO_INT_TIME: |
| 718 | switch (chan->type) { |
| 719 | case IIO_VELOCITY: |
| 720 | if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) |
| 721 | return -EINVAL; |
| 722 | /* |
| 723 | * If set to a value greater than 5, then 5 will be |
| 724 | * used. Warning: Do not set SPDPRD to 0 or 1 as |
| 725 | * this may cause undesirable behavior. |
| 726 | */ |
| 727 | if (val < 2) |
| 728 | return -EINVAL; |
| 729 | if (val > 5) |
| 730 | val = 5; |
| 731 | mutex_lock(&data->mutex); |
| 732 | ret = mma9553_set_config(data, |
| 733 | MMA9553_REG_CONF_SPEED_STEP, |
| 734 | &data->conf.speed_step, val, |
| 735 | MMA9553_MASK_CONF_SPDPRD); |
| 736 | mutex_unlock(&data->mutex); |
| 737 | return ret; |
| 738 | default: |
| 739 | return -EINVAL; |
| 740 | } |
| 741 | default: |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static int mma9553_read_event_config(struct iio_dev *indio_dev, |
| 747 | const struct iio_chan_spec *chan, |
| 748 | enum iio_event_type type, |
| 749 | enum iio_event_direction dir) |
| 750 | { |
| 751 | |
| 752 | struct mma9553_data *data = iio_priv(indio_dev); |
| 753 | struct mma9553_event *event; |
| 754 | |
| 755 | event = mma9553_get_event(data, chan->type, chan->channel2, dir); |
| 756 | if (!event) |
| 757 | return -EINVAL; |
| 758 | |
| 759 | return event->enabled; |
| 760 | } |
| 761 | |
| 762 | static int mma9553_write_event_config(struct iio_dev *indio_dev, |
| 763 | const struct iio_chan_spec *chan, |
| 764 | enum iio_event_type type, |
| 765 | enum iio_event_direction dir, int state) |
| 766 | { |
| 767 | struct mma9553_data *data = iio_priv(indio_dev); |
| 768 | struct mma9553_event *event; |
| 769 | int ret; |
| 770 | |
| 771 | event = mma9553_get_event(data, chan->type, chan->channel2, dir); |
| 772 | if (!event) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | if (event->enabled == state) |
| 776 | return 0; |
| 777 | |
| 778 | mutex_lock(&data->mutex); |
| 779 | |
| 780 | ret = mma9551_set_power_state(data->client, state); |
| 781 | if (ret < 0) |
| 782 | goto err_out; |
| 783 | event->enabled = state; |
| 784 | |
| 785 | ret = mma9553_conf_gpio(data); |
| 786 | if (ret < 0) |
| 787 | goto err_conf_gpio; |
| 788 | |
| 789 | mutex_unlock(&data->mutex); |
| 790 | |
| 791 | return ret; |
| 792 | |
| 793 | err_conf_gpio: |
| 794 | if (state) { |
| 795 | event->enabled = false; |
| 796 | mma9551_set_power_state(data->client, false); |
| 797 | } |
| 798 | err_out: |
| 799 | mutex_unlock(&data->mutex); |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | static int mma9553_read_event_value(struct iio_dev *indio_dev, |
| 804 | const struct iio_chan_spec *chan, |
| 805 | enum iio_event_type type, |
| 806 | enum iio_event_direction dir, |
| 807 | enum iio_event_info info, |
| 808 | int *val, int *val2) |
| 809 | { |
| 810 | struct mma9553_data *data = iio_priv(indio_dev); |
| 811 | |
| 812 | *val2 = 0; |
| 813 | switch (info) { |
| 814 | case IIO_EV_INFO_VALUE: |
| 815 | switch (chan->type) { |
| 816 | case IIO_STEPS: |
| 817 | *val = mma9553_get_bits(data->conf.speed_step, |
| 818 | MMA9553_MASK_CONF_STEPCOALESCE); |
| 819 | return IIO_VAL_INT; |
| 820 | case IIO_ACTIVITY: |
| 821 | /* |
| 822 | * The device does not support confidence value levels. |
| 823 | * We set an average of 50%. |
| 824 | */ |
| 825 | *val = 50; |
| 826 | return IIO_VAL_INT; |
| 827 | default: |
| 828 | return -EINVAL; |
| 829 | } |
| 830 | case IIO_EV_INFO_PERIOD: |
| 831 | switch (chan->type) { |
| 832 | case IIO_ACTIVITY: |
| 833 | *val = MMA9553_ACTIVITY_THD_TO_SEC(data->conf.actthd); |
| 834 | return IIO_VAL_INT; |
| 835 | default: |
| 836 | return -EINVAL; |
| 837 | } |
| 838 | default: |
| 839 | return -EINVAL; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | static int mma9553_write_event_value(struct iio_dev *indio_dev, |
| 844 | const struct iio_chan_spec *chan, |
| 845 | enum iio_event_type type, |
| 846 | enum iio_event_direction dir, |
| 847 | enum iio_event_info info, |
| 848 | int val, int val2) |
| 849 | { |
| 850 | struct mma9553_data *data = iio_priv(indio_dev); |
| 851 | int ret; |
| 852 | |
| 853 | switch (info) { |
| 854 | case IIO_EV_INFO_VALUE: |
| 855 | switch (chan->type) { |
| 856 | case IIO_STEPS: |
| 857 | if (val < 0 || val > 255) |
| 858 | return -EINVAL; |
| 859 | mutex_lock(&data->mutex); |
| 860 | ret = mma9553_set_config(data, |
| 861 | MMA9553_REG_CONF_SPEED_STEP, |
| 862 | &data->conf.speed_step, val, |
| 863 | MMA9553_MASK_CONF_STEPCOALESCE); |
| 864 | mutex_unlock(&data->mutex); |
| 865 | return ret; |
| 866 | default: |
| 867 | return -EINVAL; |
| 868 | } |
| 869 | case IIO_EV_INFO_PERIOD: |
| 870 | switch (chan->type) { |
| 871 | case IIO_ACTIVITY: |
| 872 | mutex_lock(&data->mutex); |
| 873 | ret = mma9553_set_config(data, MMA9553_REG_CONF_ACTTHD, |
| 874 | &data->conf.actthd, |
| 875 | MMA9553_ACTIVITY_SEC_TO_THD |
| 876 | (val), MMA9553_MASK_CONF_WORD); |
| 877 | mutex_unlock(&data->mutex); |
| 878 | return ret; |
| 879 | default: |
| 880 | return -EINVAL; |
| 881 | } |
| 882 | default: |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | static int mma9553_get_calibgender_mode(struct iio_dev *indio_dev, |
| 888 | const struct iio_chan_spec *chan) |
| 889 | { |
| 890 | struct mma9553_data *data = iio_priv(indio_dev); |
| 891 | u8 gender; |
| 892 | |
| 893 | gender = mma9553_get_bits(data->conf.filter, MMA9553_MASK_CONF_MALE); |
| 894 | /* |
| 895 | * HW expects 0 for female and 1 for male, |
| 896 | * while iio index is 0 for male and 1 for female |
| 897 | */ |
| 898 | return !gender; |
| 899 | } |
| 900 | |
| 901 | static int mma9553_set_calibgender_mode(struct iio_dev *indio_dev, |
| 902 | const struct iio_chan_spec *chan, |
| 903 | unsigned int mode) |
| 904 | { |
| 905 | struct mma9553_data *data = iio_priv(indio_dev); |
| 906 | u8 gender = !mode; |
| 907 | int ret; |
| 908 | |
| 909 | if ((mode != 0) && (mode != 1)) |
| 910 | return -EINVAL; |
| 911 | mutex_lock(&data->mutex); |
| 912 | ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER, |
| 913 | &data->conf.filter, gender, |
| 914 | MMA9553_MASK_CONF_MALE); |
| 915 | mutex_unlock(&data->mutex); |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | static const struct iio_event_spec mma9553_step_event = { |
| 921 | .type = IIO_EV_TYPE_CHANGE, |
| 922 | .dir = IIO_EV_DIR_NONE, |
| 923 | .mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE), |
| 924 | }; |
| 925 | |
| 926 | static const struct iio_event_spec mma9553_activity_events[] = { |
| 927 | { |
| 928 | .type = IIO_EV_TYPE_THRESH, |
| 929 | .dir = IIO_EV_DIR_RISING, |
| 930 | .mask_separate = BIT(IIO_EV_INFO_ENABLE) | |
| 931 | BIT(IIO_EV_INFO_VALUE) | |
| 932 | BIT(IIO_EV_INFO_PERIOD), |
| 933 | }, |
| 934 | { |
| 935 | .type = IIO_EV_TYPE_THRESH, |
| 936 | .dir = IIO_EV_DIR_FALLING, |
| 937 | .mask_separate = BIT(IIO_EV_INFO_ENABLE) | |
| 938 | BIT(IIO_EV_INFO_VALUE) | |
| 939 | BIT(IIO_EV_INFO_PERIOD), |
| 940 | }, |
| 941 | }; |
| 942 | |
| 943 | static const char * const calibgender_modes[] = { "male", "female" }; |
| 944 | |
| 945 | static const struct iio_enum mma9553_calibgender_enum = { |
| 946 | .items = calibgender_modes, |
| 947 | .num_items = ARRAY_SIZE(calibgender_modes), |
| 948 | .get = mma9553_get_calibgender_mode, |
| 949 | .set = mma9553_set_calibgender_mode, |
| 950 | }; |
| 951 | |
| 952 | static const struct iio_chan_spec_ext_info mma9553_ext_info[] = { |
| 953 | IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE, &mma9553_calibgender_enum), |
| 954 | IIO_ENUM_AVAILABLE("calibgender", &mma9553_calibgender_enum), |
| 955 | {}, |
| 956 | }; |
| 957 | |
| 958 | #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \ |
| 959 | .type = _type, \ |
| 960 | .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \ |
| 961 | BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \ |
| 962 | _mask, \ |
| 963 | .ext_info = mma9553_ext_info, \ |
| 964 | } |
| 965 | |
| 966 | #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \ |
| 967 | .type = IIO_ACTIVITY, \ |
| 968 | .modified = 1, \ |
| 969 | .channel2 = _chan2, \ |
| 970 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ |
Irina Tirdea | ae2ec95 | 2015-04-13 18:40:51 +0300 | [diff] [blame^] | 971 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \ |
| 972 | BIT(IIO_CHAN_INFO_ENABLE), \ |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 973 | .event_spec = mma9553_activity_events, \ |
| 974 | .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \ |
| 975 | .ext_info = mma9553_ext_info, \ |
| 976 | } |
| 977 | |
| 978 | static const struct iio_chan_spec mma9553_channels[] = { |
| 979 | MMA9551_ACCEL_CHANNEL(IIO_MOD_X), |
| 980 | MMA9551_ACCEL_CHANNEL(IIO_MOD_Y), |
| 981 | MMA9551_ACCEL_CHANNEL(IIO_MOD_Z), |
| 982 | |
| 983 | { |
| 984 | .type = IIO_STEPS, |
| 985 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 986 | BIT(IIO_CHAN_INFO_ENABLE) | |
| 987 | BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT) | |
| 988 | BIT(IIO_CHAN_INFO_DEBOUNCE_TIME), |
| 989 | .event_spec = &mma9553_step_event, |
| 990 | .num_event_specs = 1, |
| 991 | }, |
| 992 | |
| 993 | MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)), |
| 994 | { |
| 995 | .type = IIO_VELOCITY, |
| 996 | .modified = 1, |
| 997 | .channel2 = IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z, |
| 998 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 999 | BIT(IIO_CHAN_INFO_SCALE) | |
| 1000 | BIT(IIO_CHAN_INFO_INT_TIME) | |
| 1001 | BIT(IIO_CHAN_INFO_ENABLE), |
| 1002 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT), |
| 1003 | .ext_info = mma9553_ext_info, |
| 1004 | }, |
| 1005 | MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY, BIT(IIO_CHAN_INFO_RAW) | |
| 1006 | BIT(IIO_CHAN_INFO_SCALE) | |
| 1007 | BIT(IIO_CHAN_INFO_CALIBWEIGHT)), |
| 1008 | |
| 1009 | MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING), |
| 1010 | MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING), |
| 1011 | MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING), |
| 1012 | MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL), |
| 1013 | }; |
| 1014 | |
| 1015 | static const struct iio_info mma9553_info = { |
| 1016 | .driver_module = THIS_MODULE, |
| 1017 | .read_raw = mma9553_read_raw, |
| 1018 | .write_raw = mma9553_write_raw, |
| 1019 | .read_event_config = mma9553_read_event_config, |
| 1020 | .write_event_config = mma9553_write_event_config, |
| 1021 | .read_event_value = mma9553_read_event_value, |
| 1022 | .write_event_value = mma9553_write_event_value, |
| 1023 | }; |
| 1024 | |
| 1025 | static irqreturn_t mma9553_irq_handler(int irq, void *private) |
| 1026 | { |
| 1027 | struct iio_dev *indio_dev = private; |
| 1028 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1029 | |
| 1030 | data->timestamp = iio_get_time_ns(); |
| 1031 | /* |
| 1032 | * Since we only configure the interrupt pin when an |
| 1033 | * event is enabled, we are sure we have at least |
| 1034 | * one event enabled at this point. |
| 1035 | */ |
| 1036 | return IRQ_WAKE_THREAD; |
| 1037 | } |
| 1038 | |
| 1039 | static irqreturn_t mma9553_event_handler(int irq, void *private) |
| 1040 | { |
| 1041 | struct iio_dev *indio_dev = private; |
| 1042 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1043 | u16 stepcnt; |
| 1044 | u8 activity; |
| 1045 | struct mma9553_event *ev_activity, *ev_prev_activity, *ev_step_detect; |
| 1046 | int ret; |
| 1047 | |
| 1048 | mutex_lock(&data->mutex); |
| 1049 | ret = mma9553_read_activity_stepcnt(data, &activity, &stepcnt); |
| 1050 | if (ret < 0) { |
| 1051 | mutex_unlock(&data->mutex); |
| 1052 | return IRQ_HANDLED; |
| 1053 | } |
| 1054 | |
| 1055 | ev_prev_activity = |
| 1056 | mma9553_get_event(data, IIO_ACTIVITY, |
| 1057 | mma9553_activity_to_mod(data->activity), |
| 1058 | IIO_EV_DIR_FALLING); |
| 1059 | ev_activity = |
| 1060 | mma9553_get_event(data, IIO_ACTIVITY, |
| 1061 | mma9553_activity_to_mod(activity), |
| 1062 | IIO_EV_DIR_RISING); |
| 1063 | ev_step_detect = |
| 1064 | mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD, IIO_EV_DIR_NONE); |
| 1065 | |
| 1066 | if (ev_step_detect->enabled && (stepcnt != data->stepcnt)) { |
| 1067 | data->stepcnt = stepcnt; |
| 1068 | iio_push_event(indio_dev, |
| 1069 | IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD, |
| 1070 | IIO_EV_DIR_NONE, IIO_EV_TYPE_CHANGE, 0, 0, 0), |
| 1071 | data->timestamp); |
| 1072 | } |
| 1073 | |
| 1074 | if (activity != data->activity) { |
| 1075 | data->activity = activity; |
| 1076 | /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */ |
| 1077 | if (ev_prev_activity && ev_prev_activity->enabled) |
| 1078 | iio_push_event(indio_dev, |
| 1079 | IIO_EVENT_CODE(IIO_ACTIVITY, 0, |
| 1080 | ev_prev_activity->info->mod, |
| 1081 | IIO_EV_DIR_FALLING, |
| 1082 | IIO_EV_TYPE_THRESH, 0, 0, 0), |
| 1083 | data->timestamp); |
| 1084 | |
| 1085 | if (ev_activity && ev_activity->enabled) |
| 1086 | iio_push_event(indio_dev, |
| 1087 | IIO_EVENT_CODE(IIO_ACTIVITY, 0, |
| 1088 | ev_activity->info->mod, |
| 1089 | IIO_EV_DIR_RISING, |
| 1090 | IIO_EV_TYPE_THRESH, 0, 0, 0), |
| 1091 | data->timestamp); |
| 1092 | } |
| 1093 | mutex_unlock(&data->mutex); |
| 1094 | |
| 1095 | return IRQ_HANDLED; |
| 1096 | } |
| 1097 | |
| 1098 | static int mma9553_gpio_probe(struct i2c_client *client) |
| 1099 | { |
| 1100 | struct device *dev; |
| 1101 | struct gpio_desc *gpio; |
| 1102 | int ret; |
| 1103 | |
| 1104 | if (!client) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | dev = &client->dev; |
| 1108 | |
| 1109 | /* data ready gpio interrupt pin */ |
Uwe Kleine-König | b457f53 | 2015-02-18 13:47:11 +0100 | [diff] [blame] | 1110 | gpio = devm_gpiod_get_index(dev, MMA9553_GPIO_NAME, 0, GPIOD_IN); |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 1111 | if (IS_ERR(gpio)) { |
| 1112 | dev_err(dev, "acpi gpio get index failed\n"); |
| 1113 | return PTR_ERR(gpio); |
| 1114 | } |
| 1115 | |
Irina Tirdea | 40cb761 | 2015-01-29 18:45:10 +0000 | [diff] [blame] | 1116 | ret = gpiod_to_irq(gpio); |
| 1117 | |
| 1118 | dev_dbg(dev, "gpio resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); |
| 1119 | |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | static const char *mma9553_match_acpi_device(struct device *dev) |
| 1124 | { |
| 1125 | const struct acpi_device_id *id; |
| 1126 | |
| 1127 | id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 1128 | if (!id) |
| 1129 | return NULL; |
| 1130 | |
| 1131 | return dev_name(dev); |
| 1132 | } |
| 1133 | |
| 1134 | static int mma9553_probe(struct i2c_client *client, |
| 1135 | const struct i2c_device_id *id) |
| 1136 | { |
| 1137 | struct mma9553_data *data; |
| 1138 | struct iio_dev *indio_dev; |
| 1139 | const char *name = NULL; |
| 1140 | int ret; |
| 1141 | |
| 1142 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1143 | if (!indio_dev) |
| 1144 | return -ENOMEM; |
| 1145 | |
| 1146 | data = iio_priv(indio_dev); |
| 1147 | i2c_set_clientdata(client, indio_dev); |
| 1148 | data->client = client; |
| 1149 | |
| 1150 | if (id) |
| 1151 | name = id->name; |
| 1152 | else if (ACPI_HANDLE(&client->dev)) |
| 1153 | name = mma9553_match_acpi_device(&client->dev); |
| 1154 | else |
| 1155 | return -ENOSYS; |
| 1156 | |
| 1157 | mutex_init(&data->mutex); |
| 1158 | mma9553_init_events(data); |
| 1159 | |
| 1160 | ret = mma9553_init(data); |
| 1161 | if (ret < 0) |
| 1162 | return ret; |
| 1163 | |
| 1164 | indio_dev->dev.parent = &client->dev; |
| 1165 | indio_dev->channels = mma9553_channels; |
| 1166 | indio_dev->num_channels = ARRAY_SIZE(mma9553_channels); |
| 1167 | indio_dev->name = name; |
| 1168 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 1169 | indio_dev->info = &mma9553_info; |
| 1170 | |
| 1171 | if (client->irq < 0) |
| 1172 | client->irq = mma9553_gpio_probe(client); |
| 1173 | |
| 1174 | if (client->irq >= 0) { |
| 1175 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 1176 | mma9553_irq_handler, |
| 1177 | mma9553_event_handler, |
| 1178 | IRQF_TRIGGER_RISING, |
| 1179 | MMA9553_IRQ_NAME, indio_dev); |
| 1180 | if (ret < 0) { |
| 1181 | dev_err(&client->dev, "request irq %d failed\n", |
| 1182 | client->irq); |
| 1183 | goto out_poweroff; |
| 1184 | } |
| 1185 | |
| 1186 | } |
| 1187 | |
| 1188 | ret = iio_device_register(indio_dev); |
| 1189 | if (ret < 0) { |
| 1190 | dev_err(&client->dev, "unable to register iio device\n"); |
| 1191 | goto out_poweroff; |
| 1192 | } |
| 1193 | |
| 1194 | ret = pm_runtime_set_active(&client->dev); |
| 1195 | if (ret < 0) |
| 1196 | goto out_iio_unregister; |
| 1197 | |
| 1198 | pm_runtime_enable(&client->dev); |
| 1199 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 1200 | MMA9551_AUTO_SUSPEND_DELAY_MS); |
| 1201 | pm_runtime_use_autosuspend(&client->dev); |
| 1202 | |
| 1203 | dev_dbg(&indio_dev->dev, "Registered device %s\n", name); |
| 1204 | |
| 1205 | return 0; |
| 1206 | |
| 1207 | out_iio_unregister: |
| 1208 | iio_device_unregister(indio_dev); |
| 1209 | out_poweroff: |
| 1210 | mma9551_set_device_state(client, false); |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | static int mma9553_remove(struct i2c_client *client) |
| 1215 | { |
| 1216 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1217 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1218 | |
| 1219 | pm_runtime_disable(&client->dev); |
| 1220 | pm_runtime_set_suspended(&client->dev); |
| 1221 | pm_runtime_put_noidle(&client->dev); |
| 1222 | |
| 1223 | iio_device_unregister(indio_dev); |
| 1224 | mutex_lock(&data->mutex); |
| 1225 | mma9551_set_device_state(data->client, false); |
| 1226 | mutex_unlock(&data->mutex); |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | #ifdef CONFIG_PM |
| 1232 | static int mma9553_runtime_suspend(struct device *dev) |
| 1233 | { |
| 1234 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1235 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1236 | int ret; |
| 1237 | |
| 1238 | mutex_lock(&data->mutex); |
| 1239 | ret = mma9551_set_device_state(data->client, false); |
| 1240 | mutex_unlock(&data->mutex); |
| 1241 | if (ret < 0) { |
| 1242 | dev_err(&data->client->dev, "powering off device failed\n"); |
| 1243 | return -EAGAIN; |
| 1244 | } |
| 1245 | |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | static int mma9553_runtime_resume(struct device *dev) |
| 1250 | { |
| 1251 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1252 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1253 | int ret; |
| 1254 | |
| 1255 | ret = mma9551_set_device_state(data->client, true); |
| 1256 | if (ret < 0) |
| 1257 | return ret; |
| 1258 | |
| 1259 | mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE); |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | #endif |
| 1264 | |
| 1265 | #ifdef CONFIG_PM_SLEEP |
| 1266 | static int mma9553_suspend(struct device *dev) |
| 1267 | { |
| 1268 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1269 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1270 | int ret; |
| 1271 | |
| 1272 | mutex_lock(&data->mutex); |
| 1273 | ret = mma9551_set_device_state(data->client, false); |
| 1274 | mutex_unlock(&data->mutex); |
| 1275 | |
| 1276 | return ret; |
| 1277 | } |
| 1278 | |
| 1279 | static int mma9553_resume(struct device *dev) |
| 1280 | { |
| 1281 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1282 | struct mma9553_data *data = iio_priv(indio_dev); |
| 1283 | int ret; |
| 1284 | |
| 1285 | mutex_lock(&data->mutex); |
| 1286 | ret = mma9551_set_device_state(data->client, true); |
| 1287 | mutex_unlock(&data->mutex); |
| 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | #endif |
| 1292 | |
| 1293 | static const struct dev_pm_ops mma9553_pm_ops = { |
| 1294 | SET_SYSTEM_SLEEP_PM_OPS(mma9553_suspend, mma9553_resume) |
| 1295 | SET_RUNTIME_PM_OPS(mma9553_runtime_suspend, |
| 1296 | mma9553_runtime_resume, NULL) |
| 1297 | }; |
| 1298 | |
| 1299 | static const struct acpi_device_id mma9553_acpi_match[] = { |
| 1300 | {"MMA9553", 0}, |
| 1301 | {}, |
| 1302 | }; |
| 1303 | |
| 1304 | MODULE_DEVICE_TABLE(acpi, mma9553_acpi_match); |
| 1305 | |
| 1306 | static const struct i2c_device_id mma9553_id[] = { |
| 1307 | {"mma9553", 0}, |
| 1308 | {}, |
| 1309 | }; |
| 1310 | |
| 1311 | MODULE_DEVICE_TABLE(i2c, mma9553_id); |
| 1312 | |
| 1313 | static struct i2c_driver mma9553_driver = { |
| 1314 | .driver = { |
| 1315 | .name = MMA9553_DRV_NAME, |
| 1316 | .acpi_match_table = ACPI_PTR(mma9553_acpi_match), |
| 1317 | .pm = &mma9553_pm_ops, |
| 1318 | }, |
| 1319 | .probe = mma9553_probe, |
| 1320 | .remove = mma9553_remove, |
| 1321 | .id_table = mma9553_id, |
| 1322 | }; |
| 1323 | |
| 1324 | module_i2c_driver(mma9553_driver); |
| 1325 | |
| 1326 | MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); |
| 1327 | MODULE_LICENSE("GPL v2"); |
| 1328 | MODULE_DESCRIPTION("MMA9553L pedometer platform driver"); |