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