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