Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * KXCJK-1013 3-axis accelerometer 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/delay.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/acpi.h> |
| 23 | #include <linux/gpio/consumer.h> |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 24 | #include <linux/pm.h> |
| 25 | #include <linux/pm_runtime.h> |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 26 | #include <linux/iio/iio.h> |
| 27 | #include <linux/iio/sysfs.h> |
| 28 | #include <linux/iio/buffer.h> |
| 29 | #include <linux/iio/trigger.h> |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 30 | #include <linux/iio/events.h> |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 31 | #include <linux/iio/trigger_consumer.h> |
| 32 | #include <linux/iio/triggered_buffer.h> |
| 33 | #include <linux/iio/accel/kxcjk_1013.h> |
| 34 | |
| 35 | #define KXCJK1013_DRV_NAME "kxcjk1013" |
| 36 | #define KXCJK1013_IRQ_NAME "kxcjk1013_event" |
| 37 | |
| 38 | #define KXCJK1013_REG_XOUT_L 0x06 |
| 39 | /* |
| 40 | * From low byte X axis register, all the other addresses of Y and Z can be |
| 41 | * obtained by just applying axis offset. The following axis defines are just |
| 42 | * provide clarity, but not used. |
| 43 | */ |
| 44 | #define KXCJK1013_REG_XOUT_H 0x07 |
| 45 | #define KXCJK1013_REG_YOUT_L 0x08 |
| 46 | #define KXCJK1013_REG_YOUT_H 0x09 |
| 47 | #define KXCJK1013_REG_ZOUT_L 0x0A |
| 48 | #define KXCJK1013_REG_ZOUT_H 0x0B |
| 49 | |
| 50 | #define KXCJK1013_REG_DCST_RESP 0x0C |
| 51 | #define KXCJK1013_REG_WHO_AM_I 0x0F |
| 52 | #define KXCJK1013_REG_INT_SRC1 0x16 |
| 53 | #define KXCJK1013_REG_INT_SRC2 0x17 |
| 54 | #define KXCJK1013_REG_STATUS_REG 0x18 |
| 55 | #define KXCJK1013_REG_INT_REL 0x1A |
| 56 | #define KXCJK1013_REG_CTRL1 0x1B |
| 57 | #define KXCJK1013_REG_CTRL2 0x1D |
| 58 | #define KXCJK1013_REG_INT_CTRL1 0x1E |
| 59 | #define KXCJK1013_REG_INT_CTRL2 0x1F |
| 60 | #define KXCJK1013_REG_DATA_CTRL 0x21 |
| 61 | #define KXCJK1013_REG_WAKE_TIMER 0x29 |
| 62 | #define KXCJK1013_REG_SELF_TEST 0x3A |
| 63 | #define KXCJK1013_REG_WAKE_THRES 0x6A |
| 64 | |
| 65 | #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7) |
| 66 | #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6) |
| 67 | #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5) |
| 68 | #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4) |
| 69 | #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3) |
| 70 | #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1) |
| 71 | #define KXCJK1013_REG_INT_REG1_BIT_IEA BIT(4) |
| 72 | #define KXCJK1013_REG_INT_REG1_BIT_IEN BIT(5) |
| 73 | |
| 74 | #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF |
| 75 | #define KXCJK1013_MAX_STARTUP_TIME_US 100000 |
| 76 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 77 | #define KXCJK1013_SLEEP_DELAY_MS 2000 |
| 78 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 79 | #define KXCJK1013_REG_INT_SRC2_BIT_ZP BIT(0) |
| 80 | #define KXCJK1013_REG_INT_SRC2_BIT_ZN BIT(1) |
| 81 | #define KXCJK1013_REG_INT_SRC2_BIT_YP BIT(2) |
| 82 | #define KXCJK1013_REG_INT_SRC2_BIT_YN BIT(3) |
| 83 | #define KXCJK1013_REG_INT_SRC2_BIT_XP BIT(4) |
| 84 | #define KXCJK1013_REG_INT_SRC2_BIT_XN BIT(5) |
| 85 | |
| 86 | #define KXCJK1013_DEFAULT_WAKE_THRES 1 |
| 87 | |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 88 | enum kx_chipset { |
| 89 | KXCJK1013, |
| 90 | KXCJ91008, |
| 91 | KXTJ21009, |
| 92 | KX_MAX_CHIPS /* this must be last */ |
| 93 | }; |
| 94 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 95 | struct kxcjk1013_data { |
| 96 | struct i2c_client *client; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 97 | struct iio_trigger *dready_trig; |
| 98 | struct iio_trigger *motion_trig; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 99 | struct mutex mutex; |
| 100 | s16 buffer[8]; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 101 | u8 odr_bits; |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 102 | u8 range; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 103 | int wake_thres; |
| 104 | int wake_dur; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 105 | bool active_high_intr; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 106 | bool dready_trigger_on; |
| 107 | int ev_enable_state; |
| 108 | bool motion_trigger_on; |
| 109 | int64_t timestamp; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 110 | enum kx_chipset chipset; |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 111 | bool is_smo8500_device; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | enum kxcjk1013_axis { |
| 115 | AXIS_X, |
| 116 | AXIS_Y, |
| 117 | AXIS_Z, |
| 118 | }; |
| 119 | |
| 120 | enum kxcjk1013_mode { |
| 121 | STANDBY, |
| 122 | OPERATION, |
| 123 | }; |
| 124 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 125 | enum kxcjk1013_range { |
| 126 | KXCJK1013_RANGE_2G, |
| 127 | KXCJK1013_RANGE_4G, |
| 128 | KXCJK1013_RANGE_8G, |
| 129 | }; |
| 130 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 131 | static const struct { |
| 132 | int val; |
| 133 | int val2; |
| 134 | int odr_bits; |
| 135 | } samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09}, |
Srinivas Pandruvada | f0ca974 | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 136 | {3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0}, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 137 | {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03}, |
| 138 | {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06}, |
| 139 | {1600, 0, 0x07} }; |
| 140 | |
| 141 | /* Refer to section 4 of the specification */ |
| 142 | static const struct { |
| 143 | int odr_bits; |
| 144 | int usec; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 145 | } odr_start_up_times[KX_MAX_CHIPS][12] = { |
| 146 | /* KXCJK-1013 */ |
| 147 | { |
| 148 | {0x08, 100000}, |
| 149 | {0x09, 100000}, |
| 150 | {0x0A, 100000}, |
| 151 | {0x0B, 100000}, |
| 152 | {0, 80000}, |
| 153 | {0x01, 41000}, |
| 154 | {0x02, 21000}, |
| 155 | {0x03, 11000}, |
| 156 | {0x04, 6400}, |
| 157 | {0x05, 3900}, |
| 158 | {0x06, 2700}, |
| 159 | {0x07, 2100}, |
| 160 | }, |
| 161 | /* KXCJ9-1008 */ |
| 162 | { |
| 163 | {0x08, 100000}, |
| 164 | {0x09, 100000}, |
| 165 | {0x0A, 100000}, |
| 166 | {0x0B, 100000}, |
| 167 | {0, 80000}, |
| 168 | {0x01, 41000}, |
| 169 | {0x02, 21000}, |
| 170 | {0x03, 11000}, |
| 171 | {0x04, 6400}, |
| 172 | {0x05, 3900}, |
| 173 | {0x06, 2700}, |
| 174 | {0x07, 2100}, |
| 175 | }, |
| 176 | /* KXCTJ2-1009 */ |
| 177 | { |
| 178 | {0x08, 1240000}, |
| 179 | {0x09, 621000}, |
| 180 | {0x0A, 309000}, |
| 181 | {0x0B, 151000}, |
| 182 | {0, 80000}, |
| 183 | {0x01, 41000}, |
| 184 | {0x02, 21000}, |
| 185 | {0x03, 11000}, |
| 186 | {0x04, 6000}, |
| 187 | {0x05, 4000}, |
| 188 | {0x06, 3000}, |
| 189 | {0x07, 2000}, |
| 190 | }, |
| 191 | }; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 192 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 193 | static const struct { |
| 194 | u16 scale; |
| 195 | u8 gsel_0; |
| 196 | u8 gsel_1; |
| 197 | } KXCJK1013_scale_table[] = { {9582, 0, 0}, |
| 198 | {19163, 1, 0}, |
| 199 | {38326, 0, 1} }; |
| 200 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 201 | static const struct { |
| 202 | int val; |
| 203 | int val2; |
| 204 | int odr_bits; |
| 205 | } wake_odr_data_rate_table[] = { {0, 781000, 0x00}, |
| 206 | {1, 563000, 0x01}, |
| 207 | {3, 125000, 0x02}, |
| 208 | {6, 250000, 0x03}, |
| 209 | {12, 500000, 0x04}, |
| 210 | {25, 0, 0x05}, |
| 211 | {50, 0, 0x06}, |
| 212 | {100, 0, 0x06}, |
| 213 | {200, 0, 0x06}, |
| 214 | {400, 0, 0x06}, |
| 215 | {800, 0, 0x06}, |
| 216 | {1600, 0, 0x06} }; |
| 217 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 218 | static int kxcjk1013_set_mode(struct kxcjk1013_data *data, |
| 219 | enum kxcjk1013_mode mode) |
| 220 | { |
| 221 | int ret; |
| 222 | |
| 223 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 224 | if (ret < 0) { |
| 225 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | if (mode == STANDBY) |
| 230 | ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1; |
| 231 | else |
| 232 | ret |= KXCJK1013_REG_CTRL1_BIT_PC1; |
| 233 | |
| 234 | ret = i2c_smbus_write_byte_data(data->client, |
| 235 | KXCJK1013_REG_CTRL1, ret); |
| 236 | if (ret < 0) { |
| 237 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 244 | static int kxcjk1013_get_mode(struct kxcjk1013_data *data, |
| 245 | enum kxcjk1013_mode *mode) |
| 246 | { |
| 247 | int ret; |
| 248 | |
| 249 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 250 | if (ret < 0) { |
| 251 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | if (ret & KXCJK1013_REG_CTRL1_BIT_PC1) |
| 256 | *mode = OPERATION; |
| 257 | else |
| 258 | *mode = STANDBY; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 263 | static int kxcjk1013_set_range(struct kxcjk1013_data *data, int range_index) |
| 264 | { |
| 265 | int ret; |
| 266 | |
| 267 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 268 | if (ret < 0) { |
| 269 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | ret |= (KXCJK1013_scale_table[range_index].gsel_0 << 3); |
| 274 | ret |= (KXCJK1013_scale_table[range_index].gsel_1 << 4); |
| 275 | |
| 276 | ret = i2c_smbus_write_byte_data(data->client, |
| 277 | KXCJK1013_REG_CTRL1, |
| 278 | ret); |
| 279 | if (ret < 0) { |
| 280 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | data->range = range_index; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 289 | static int kxcjk1013_chip_init(struct kxcjk1013_data *data) |
| 290 | { |
| 291 | int ret; |
| 292 | |
| 293 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I); |
| 294 | if (ret < 0) { |
| 295 | dev_err(&data->client->dev, "Error reading who_am_i\n"); |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret); |
| 300 | |
| 301 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 302 | if (ret < 0) |
| 303 | return ret; |
| 304 | |
| 305 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 306 | if (ret < 0) { |
| 307 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 308 | return ret; |
| 309 | } |
| 310 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 311 | /* Set 12 bit mode */ |
| 312 | ret |= KXCJK1013_REG_CTRL1_BIT_RES; |
| 313 | |
| 314 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1, |
| 315 | ret); |
| 316 | if (ret < 0) { |
| 317 | dev_err(&data->client->dev, "Error reading reg_ctrl\n"); |
| 318 | return ret; |
| 319 | } |
| 320 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 321 | /* Setting range to 4G */ |
| 322 | ret = kxcjk1013_set_range(data, KXCJK1013_RANGE_4G); |
| 323 | if (ret < 0) |
| 324 | return ret; |
| 325 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 326 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL); |
| 327 | if (ret < 0) { |
| 328 | dev_err(&data->client->dev, "Error reading reg_data_ctrl\n"); |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | data->odr_bits = ret; |
| 333 | |
| 334 | /* Set up INT polarity */ |
| 335 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); |
| 336 | if (ret < 0) { |
| 337 | dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | if (data->active_high_intr) |
| 342 | ret |= KXCJK1013_REG_INT_REG1_BIT_IEA; |
| 343 | else |
| 344 | ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA; |
| 345 | |
| 346 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, |
| 347 | ret); |
| 348 | if (ret < 0) { |
| 349 | dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); |
| 350 | return ret; |
| 351 | } |
| 352 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 353 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 357 | data->wake_thres = KXCJK1013_DEFAULT_WAKE_THRES; |
| 358 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Daniel Baluta | c9bf237 | 2014-03-09 16:13:00 +0000 | [diff] [blame] | 362 | #ifdef CONFIG_PM_RUNTIME |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 363 | static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) |
| 364 | { |
| 365 | int i; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 366 | int idx = data->chipset; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 367 | |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 368 | for (i = 0; i < ARRAY_SIZE(odr_start_up_times[idx]); ++i) { |
| 369 | if (odr_start_up_times[idx][i].odr_bits == data->odr_bits) |
| 370 | return odr_start_up_times[idx][i].usec; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return KXCJK1013_MAX_STARTUP_TIME_US; |
| 374 | } |
Daniel Baluta | c9bf237 | 2014-03-09 16:13:00 +0000 | [diff] [blame] | 375 | #endif |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 376 | |
| 377 | static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on) |
| 378 | { |
Irina Tirdea | c75b8dc | 2014-12-06 00:18:08 +0200 | [diff] [blame] | 379 | #ifdef CONFIG_PM |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 380 | int ret; |
| 381 | |
| 382 | if (on) |
| 383 | ret = pm_runtime_get_sync(&data->client->dev); |
| 384 | else { |
| 385 | pm_runtime_mark_last_busy(&data->client->dev); |
| 386 | ret = pm_runtime_put_autosuspend(&data->client->dev); |
| 387 | } |
| 388 | if (ret < 0) { |
| 389 | dev_err(&data->client->dev, |
| 390 | "Failed: kxcjk1013_set_power_state for %d\n", on); |
Irina Tirdea | fbd123e | 2014-12-10 18:23:53 +0200 | [diff] [blame] | 391 | if (on) |
| 392 | pm_runtime_put_noidle(&data->client->dev); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 393 | return ret; |
| 394 | } |
Irina Tirdea | c75b8dc | 2014-12-06 00:18:08 +0200 | [diff] [blame] | 395 | #endif |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 396 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 400 | static int kxcjk1013_chip_update_thresholds(struct kxcjk1013_data *data) |
| 401 | { |
| 402 | int ret; |
| 403 | |
| 404 | ret = i2c_smbus_write_byte_data(data->client, |
| 405 | KXCJK1013_REG_WAKE_TIMER, |
| 406 | data->wake_dur); |
| 407 | if (ret < 0) { |
| 408 | dev_err(&data->client->dev, |
| 409 | "Error writing reg_wake_timer\n"); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | ret = i2c_smbus_write_byte_data(data->client, |
| 414 | KXCJK1013_REG_WAKE_THRES, |
| 415 | data->wake_thres); |
| 416 | if (ret < 0) { |
| 417 | dev_err(&data->client->dev, "Error writing reg_wake_thres\n"); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int kxcjk1013_setup_any_motion_interrupt(struct kxcjk1013_data *data, |
| 425 | bool status) |
| 426 | { |
| 427 | int ret; |
| 428 | enum kxcjk1013_mode store_mode; |
| 429 | |
| 430 | ret = kxcjk1013_get_mode(data, &store_mode); |
| 431 | if (ret < 0) |
| 432 | return ret; |
| 433 | |
| 434 | /* This is requirement by spec to change state to STANDBY */ |
| 435 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 436 | if (ret < 0) |
| 437 | return ret; |
| 438 | |
| 439 | ret = kxcjk1013_chip_update_thresholds(data); |
| 440 | if (ret < 0) |
| 441 | return ret; |
| 442 | |
| 443 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); |
| 444 | if (ret < 0) { |
| 445 | dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | if (status) |
| 450 | ret |= KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 451 | else |
| 452 | ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 453 | |
| 454 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, |
| 455 | ret); |
| 456 | if (ret < 0) { |
| 457 | dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 462 | if (ret < 0) { |
| 463 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | if (status) |
| 468 | ret |= KXCJK1013_REG_CTRL1_BIT_WUFE; |
| 469 | else |
| 470 | ret &= ~KXCJK1013_REG_CTRL1_BIT_WUFE; |
| 471 | |
| 472 | ret = i2c_smbus_write_byte_data(data->client, |
| 473 | KXCJK1013_REG_CTRL1, ret); |
| 474 | if (ret < 0) { |
| 475 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | if (store_mode == OPERATION) { |
| 480 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static int kxcjk1013_setup_new_data_interrupt(struct kxcjk1013_data *data, |
| 489 | bool status) |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 490 | { |
| 491 | int ret; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 492 | enum kxcjk1013_mode store_mode; |
| 493 | |
| 494 | ret = kxcjk1013_get_mode(data, &store_mode); |
| 495 | if (ret < 0) |
| 496 | return ret; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 497 | |
| 498 | /* This is requirement by spec to change state to STANDBY */ |
| 499 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | |
| 503 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); |
| 504 | if (ret < 0) { |
| 505 | dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | if (status) |
| 510 | ret |= KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 511 | else |
| 512 | ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 513 | |
| 514 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, |
| 515 | ret); |
| 516 | if (ret < 0) { |
| 517 | dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 522 | if (ret < 0) { |
| 523 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 524 | return ret; |
| 525 | } |
| 526 | |
| 527 | if (status) |
| 528 | ret |= KXCJK1013_REG_CTRL1_BIT_DRDY; |
| 529 | else |
| 530 | ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY; |
| 531 | |
| 532 | ret = i2c_smbus_write_byte_data(data->client, |
| 533 | KXCJK1013_REG_CTRL1, ret); |
| 534 | if (ret < 0) { |
| 535 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 536 | return ret; |
| 537 | } |
| 538 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 539 | if (store_mode == OPERATION) { |
| 540 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 541 | if (ret < 0) |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | return 0; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | static int kxcjk1013_convert_freq_to_bit(int val, int val2) |
| 549 | { |
| 550 | int i; |
| 551 | |
| 552 | for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) { |
| 553 | if (samp_freq_table[i].val == val && |
| 554 | samp_freq_table[i].val2 == val2) { |
| 555 | return samp_freq_table[i].odr_bits; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 562 | static int kxcjk1013_convert_wake_odr_to_bit(int val, int val2) |
| 563 | { |
| 564 | int i; |
| 565 | |
| 566 | for (i = 0; i < ARRAY_SIZE(wake_odr_data_rate_table); ++i) { |
| 567 | if (wake_odr_data_rate_table[i].val == val && |
| 568 | wake_odr_data_rate_table[i].val2 == val2) { |
| 569 | return wake_odr_data_rate_table[i].odr_bits; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 576 | static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2) |
| 577 | { |
| 578 | int ret; |
| 579 | int odr_bits; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 580 | enum kxcjk1013_mode store_mode; |
| 581 | |
| 582 | ret = kxcjk1013_get_mode(data, &store_mode); |
| 583 | if (ret < 0) |
| 584 | return ret; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 585 | |
| 586 | odr_bits = kxcjk1013_convert_freq_to_bit(val, val2); |
| 587 | if (odr_bits < 0) |
| 588 | return odr_bits; |
| 589 | |
| 590 | /* To change ODR, the chip must be set to STANDBY as per spec */ |
| 591 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 592 | if (ret < 0) |
| 593 | return ret; |
| 594 | |
| 595 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL, |
| 596 | odr_bits); |
| 597 | if (ret < 0) { |
| 598 | dev_err(&data->client->dev, "Error writing data_ctrl\n"); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | data->odr_bits = odr_bits; |
| 603 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 604 | odr_bits = kxcjk1013_convert_wake_odr_to_bit(val, val2); |
| 605 | if (odr_bits < 0) |
| 606 | return odr_bits; |
| 607 | |
| 608 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL2, |
| 609 | odr_bits); |
| 610 | if (ret < 0) { |
| 611 | dev_err(&data->client->dev, "Error writing reg_ctrl2\n"); |
| 612 | return ret; |
| 613 | } |
| 614 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 615 | if (store_mode == OPERATION) { |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 616 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2) |
| 625 | { |
| 626 | int i; |
| 627 | |
| 628 | for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) { |
| 629 | if (samp_freq_table[i].odr_bits == data->odr_bits) { |
| 630 | *val = samp_freq_table[i].val; |
| 631 | *val2 = samp_freq_table[i].val2; |
| 632 | return IIO_VAL_INT_PLUS_MICRO; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis) |
| 640 | { |
| 641 | u8 reg = KXCJK1013_REG_XOUT_L + axis * 2; |
| 642 | int ret; |
| 643 | |
| 644 | ret = i2c_smbus_read_word_data(data->client, reg); |
| 645 | if (ret < 0) { |
| 646 | dev_err(&data->client->dev, |
| 647 | "failed to read accel_%c registers\n", 'x' + axis); |
| 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | return ret; |
| 652 | } |
| 653 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 654 | static int kxcjk1013_set_scale(struct kxcjk1013_data *data, int val) |
| 655 | { |
| 656 | int ret, i; |
| 657 | enum kxcjk1013_mode store_mode; |
| 658 | |
| 659 | |
| 660 | for (i = 0; i < ARRAY_SIZE(KXCJK1013_scale_table); ++i) { |
| 661 | if (KXCJK1013_scale_table[i].scale == val) { |
| 662 | |
| 663 | ret = kxcjk1013_get_mode(data, &store_mode); |
| 664 | if (ret < 0) |
| 665 | return ret; |
| 666 | |
| 667 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 668 | if (ret < 0) |
| 669 | return ret; |
| 670 | |
| 671 | ret = kxcjk1013_set_range(data, i); |
| 672 | if (ret < 0) |
| 673 | return ret; |
| 674 | |
| 675 | if (store_mode == OPERATION) { |
| 676 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | return -EINVAL; |
| 686 | } |
| 687 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 688 | static int kxcjk1013_read_raw(struct iio_dev *indio_dev, |
| 689 | struct iio_chan_spec const *chan, int *val, |
| 690 | int *val2, long mask) |
| 691 | { |
| 692 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 693 | int ret; |
| 694 | |
| 695 | switch (mask) { |
| 696 | case IIO_CHAN_INFO_RAW: |
| 697 | mutex_lock(&data->mutex); |
| 698 | if (iio_buffer_enabled(indio_dev)) |
| 699 | ret = -EBUSY; |
| 700 | else { |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 701 | ret = kxcjk1013_set_power_state(data, true); |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 702 | if (ret < 0) { |
| 703 | mutex_unlock(&data->mutex); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 704 | return ret; |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 705 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 706 | ret = kxcjk1013_get_acc_reg(data, chan->scan_index); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 707 | if (ret < 0) { |
| 708 | kxcjk1013_set_power_state(data, false); |
| 709 | mutex_unlock(&data->mutex); |
| 710 | return ret; |
| 711 | } |
| 712 | *val = sign_extend32(ret >> 4, 11); |
| 713 | ret = kxcjk1013_set_power_state(data, false); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 714 | } |
| 715 | mutex_unlock(&data->mutex); |
| 716 | |
| 717 | if (ret < 0) |
| 718 | return ret; |
| 719 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 720 | return IIO_VAL_INT; |
| 721 | |
| 722 | case IIO_CHAN_INFO_SCALE: |
| 723 | *val = 0; |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 724 | *val2 = KXCJK1013_scale_table[data->range].scale; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 725 | return IIO_VAL_INT_PLUS_MICRO; |
| 726 | |
| 727 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 728 | mutex_lock(&data->mutex); |
| 729 | ret = kxcjk1013_get_odr(data, val, val2); |
| 730 | mutex_unlock(&data->mutex); |
| 731 | return ret; |
| 732 | |
| 733 | default: |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static int kxcjk1013_write_raw(struct iio_dev *indio_dev, |
| 739 | struct iio_chan_spec const *chan, int val, |
| 740 | int val2, long mask) |
| 741 | { |
| 742 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 743 | int ret; |
| 744 | |
| 745 | switch (mask) { |
| 746 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 747 | mutex_lock(&data->mutex); |
| 748 | ret = kxcjk1013_set_odr(data, val, val2); |
| 749 | mutex_unlock(&data->mutex); |
| 750 | break; |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 751 | case IIO_CHAN_INFO_SCALE: |
| 752 | if (val) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | mutex_lock(&data->mutex); |
| 756 | ret = kxcjk1013_set_scale(data, val2); |
| 757 | mutex_unlock(&data->mutex); |
| 758 | break; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 759 | default: |
| 760 | ret = -EINVAL; |
| 761 | } |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 766 | static int kxcjk1013_read_event(struct iio_dev *indio_dev, |
| 767 | const struct iio_chan_spec *chan, |
| 768 | enum iio_event_type type, |
| 769 | enum iio_event_direction dir, |
| 770 | enum iio_event_info info, |
| 771 | int *val, int *val2) |
| 772 | { |
| 773 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 774 | |
| 775 | *val2 = 0; |
| 776 | switch (info) { |
| 777 | case IIO_EV_INFO_VALUE: |
| 778 | *val = data->wake_thres; |
| 779 | break; |
| 780 | case IIO_EV_INFO_PERIOD: |
| 781 | *val = data->wake_dur; |
| 782 | break; |
| 783 | default: |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | return IIO_VAL_INT; |
| 788 | } |
| 789 | |
| 790 | static int kxcjk1013_write_event(struct iio_dev *indio_dev, |
| 791 | const struct iio_chan_spec *chan, |
| 792 | enum iio_event_type type, |
| 793 | enum iio_event_direction dir, |
| 794 | enum iio_event_info info, |
| 795 | int val, int val2) |
| 796 | { |
| 797 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 798 | |
| 799 | if (data->ev_enable_state) |
| 800 | return -EBUSY; |
| 801 | |
| 802 | switch (info) { |
| 803 | case IIO_EV_INFO_VALUE: |
| 804 | data->wake_thres = val; |
| 805 | break; |
| 806 | case IIO_EV_INFO_PERIOD: |
| 807 | data->wake_dur = val; |
| 808 | break; |
| 809 | default: |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int kxcjk1013_read_event_config(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 | { |
| 821 | |
| 822 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 823 | |
| 824 | return data->ev_enable_state; |
| 825 | } |
| 826 | |
| 827 | static int kxcjk1013_write_event_config(struct iio_dev *indio_dev, |
| 828 | const struct iio_chan_spec *chan, |
| 829 | enum iio_event_type type, |
| 830 | enum iio_event_direction dir, |
| 831 | int state) |
| 832 | { |
| 833 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 834 | int ret; |
| 835 | |
| 836 | if (state && data->ev_enable_state) |
| 837 | return 0; |
| 838 | |
| 839 | mutex_lock(&data->mutex); |
| 840 | |
| 841 | if (!state && data->motion_trigger_on) { |
| 842 | data->ev_enable_state = 0; |
| 843 | mutex_unlock(&data->mutex); |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * We will expect the enable and disable to do operation in |
| 849 | * in reverse order. This will happen here anyway as our |
| 850 | * resume operation uses sync mode runtime pm calls, the |
| 851 | * suspend operation will be delayed by autosuspend delay |
| 852 | * So the disable operation will still happen in reverse of |
| 853 | * enable operation. When runtime pm is disabled the mode |
| 854 | * is always on so sequence doesn't matter |
| 855 | */ |
| 856 | ret = kxcjk1013_set_power_state(data, state); |
| 857 | if (ret < 0) { |
| 858 | mutex_unlock(&data->mutex); |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | ret = kxcjk1013_setup_any_motion_interrupt(data, state); |
| 863 | if (ret < 0) { |
Irina Tirdea | fbd123e | 2014-12-10 18:23:53 +0200 | [diff] [blame] | 864 | kxcjk1013_set_power_state(data, false); |
| 865 | data->ev_enable_state = 0; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 866 | mutex_unlock(&data->mutex); |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | data->ev_enable_state = state; |
| 871 | mutex_unlock(&data->mutex); |
| 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 876 | static int kxcjk1013_validate_trigger(struct iio_dev *indio_dev, |
| 877 | struct iio_trigger *trig) |
| 878 | { |
| 879 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 880 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 881 | if (data->dready_trig != trig && data->motion_trig != trig) |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( |
| 888 | "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600"); |
| 889 | |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 890 | static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019163 0.038326"); |
| 891 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 892 | static struct attribute *kxcjk1013_attributes[] = { |
| 893 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
Srinivas Pandruvada | a735e3d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 894 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 895 | NULL, |
| 896 | }; |
| 897 | |
| 898 | static const struct attribute_group kxcjk1013_attrs_group = { |
| 899 | .attrs = kxcjk1013_attributes, |
| 900 | }; |
| 901 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 902 | static const struct iio_event_spec kxcjk1013_event = { |
| 903 | .type = IIO_EV_TYPE_THRESH, |
| 904 | .dir = IIO_EV_DIR_RISING | IIO_EV_DIR_FALLING, |
| 905 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 906 | BIT(IIO_EV_INFO_ENABLE) | |
| 907 | BIT(IIO_EV_INFO_PERIOD) |
| 908 | }; |
| 909 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 910 | #define KXCJK1013_CHANNEL(_axis) { \ |
| 911 | .type = IIO_ACCEL, \ |
| 912 | .modified = 1, \ |
| 913 | .channel2 = IIO_MOD_##_axis, \ |
| 914 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 915 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 916 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 917 | .scan_index = AXIS_##_axis, \ |
| 918 | .scan_type = { \ |
| 919 | .sign = 's', \ |
| 920 | .realbits = 12, \ |
| 921 | .storagebits = 16, \ |
| 922 | .shift = 4, \ |
Peter Meerwald | f4e2f94d | 2014-07-14 21:38:00 +0100 | [diff] [blame] | 923 | .endianness = IIO_CPU, \ |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 924 | }, \ |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 925 | .event_spec = &kxcjk1013_event, \ |
| 926 | .num_event_specs = 1 \ |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static const struct iio_chan_spec kxcjk1013_channels[] = { |
| 930 | KXCJK1013_CHANNEL(X), |
| 931 | KXCJK1013_CHANNEL(Y), |
| 932 | KXCJK1013_CHANNEL(Z), |
| 933 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 934 | }; |
| 935 | |
| 936 | static const struct iio_info kxcjk1013_info = { |
| 937 | .attrs = &kxcjk1013_attrs_group, |
| 938 | .read_raw = kxcjk1013_read_raw, |
| 939 | .write_raw = kxcjk1013_write_raw, |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 940 | .read_event_value = kxcjk1013_read_event, |
| 941 | .write_event_value = kxcjk1013_write_event, |
| 942 | .write_event_config = kxcjk1013_write_event_config, |
| 943 | .read_event_config = kxcjk1013_read_event_config, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 944 | .validate_trigger = kxcjk1013_validate_trigger, |
| 945 | .driver_module = THIS_MODULE, |
| 946 | }; |
| 947 | |
| 948 | static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p) |
| 949 | { |
| 950 | struct iio_poll_func *pf = p; |
| 951 | struct iio_dev *indio_dev = pf->indio_dev; |
| 952 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 953 | int bit, ret, i = 0; |
| 954 | |
| 955 | mutex_lock(&data->mutex); |
| 956 | |
| 957 | for_each_set_bit(bit, indio_dev->buffer->scan_mask, |
| 958 | indio_dev->masklength) { |
| 959 | ret = kxcjk1013_get_acc_reg(data, bit); |
| 960 | if (ret < 0) { |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 961 | mutex_unlock(&data->mutex); |
| 962 | goto err; |
| 963 | } |
| 964 | data->buffer[i++] = ret; |
| 965 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 966 | mutex_unlock(&data->mutex); |
| 967 | |
| 968 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 969 | data->timestamp); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 970 | err: |
| 971 | iio_trigger_notify_done(indio_dev->trig); |
| 972 | |
| 973 | return IRQ_HANDLED; |
| 974 | } |
| 975 | |
Srinivas Pandruvada | 59bfeab | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 976 | static int kxcjk1013_trig_try_reen(struct iio_trigger *trig) |
| 977 | { |
| 978 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 979 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 980 | int ret; |
| 981 | |
| 982 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL); |
| 983 | if (ret < 0) { |
| 984 | dev_err(&data->client->dev, "Error reading reg_int_rel\n"); |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 991 | static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 992 | bool state) |
| 993 | { |
| 994 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 995 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 996 | int ret; |
| 997 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 998 | mutex_lock(&data->mutex); |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 999 | |
| 1000 | if (!state && data->ev_enable_state && data->motion_trigger_on) { |
| 1001 | data->motion_trigger_on = false; |
| 1002 | mutex_unlock(&data->mutex); |
| 1003 | return 0; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1004 | } |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1005 | |
| 1006 | ret = kxcjk1013_set_power_state(data, state); |
| 1007 | if (ret < 0) { |
| 1008 | mutex_unlock(&data->mutex); |
| 1009 | return ret; |
| 1010 | } |
| 1011 | if (data->motion_trig == trig) |
| 1012 | ret = kxcjk1013_setup_any_motion_interrupt(data, state); |
| 1013 | else |
| 1014 | ret = kxcjk1013_setup_new_data_interrupt(data, state); |
| 1015 | if (ret < 0) { |
Irina Tirdea | fbd123e | 2014-12-10 18:23:53 +0200 | [diff] [blame] | 1016 | kxcjk1013_set_power_state(data, false); |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1017 | mutex_unlock(&data->mutex); |
| 1018 | return ret; |
| 1019 | } |
| 1020 | if (data->motion_trig == trig) |
| 1021 | data->motion_trigger_on = state; |
| 1022 | else |
| 1023 | data->dready_trigger_on = state; |
| 1024 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1025 | mutex_unlock(&data->mutex); |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static const struct iio_trigger_ops kxcjk1013_trigger_ops = { |
| 1031 | .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state, |
Srinivas Pandruvada | 59bfeab | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 1032 | .try_reenable = kxcjk1013_trig_try_reen, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1033 | .owner = THIS_MODULE, |
| 1034 | }; |
| 1035 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1036 | static irqreturn_t kxcjk1013_event_handler(int irq, void *private) |
| 1037 | { |
| 1038 | struct iio_dev *indio_dev = private; |
| 1039 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 1040 | int ret; |
| 1041 | |
| 1042 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_SRC1); |
| 1043 | if (ret < 0) { |
| 1044 | dev_err(&data->client->dev, "Error reading reg_int_src1\n"); |
| 1045 | goto ack_intr; |
| 1046 | } |
| 1047 | |
| 1048 | if (ret & 0x02) { |
| 1049 | ret = i2c_smbus_read_byte_data(data->client, |
| 1050 | KXCJK1013_REG_INT_SRC2); |
| 1051 | if (ret < 0) { |
| 1052 | dev_err(&data->client->dev, |
| 1053 | "Error reading reg_int_src2\n"); |
| 1054 | goto ack_intr; |
| 1055 | } |
| 1056 | |
| 1057 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_XN) |
| 1058 | iio_push_event(indio_dev, |
| 1059 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1060 | 0, |
| 1061 | IIO_MOD_X, |
| 1062 | IIO_EV_TYPE_THRESH, |
| 1063 | IIO_EV_DIR_FALLING), |
| 1064 | data->timestamp); |
| 1065 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_XP) |
| 1066 | iio_push_event(indio_dev, |
| 1067 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1068 | 0, |
| 1069 | IIO_MOD_X, |
| 1070 | IIO_EV_TYPE_THRESH, |
| 1071 | IIO_EV_DIR_RISING), |
| 1072 | data->timestamp); |
| 1073 | |
| 1074 | |
| 1075 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_YN) |
| 1076 | iio_push_event(indio_dev, |
| 1077 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1078 | 0, |
| 1079 | IIO_MOD_Y, |
| 1080 | IIO_EV_TYPE_THRESH, |
| 1081 | IIO_EV_DIR_FALLING), |
| 1082 | data->timestamp); |
| 1083 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_YP) |
| 1084 | iio_push_event(indio_dev, |
| 1085 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1086 | 0, |
| 1087 | IIO_MOD_Y, |
| 1088 | IIO_EV_TYPE_THRESH, |
| 1089 | IIO_EV_DIR_RISING), |
| 1090 | data->timestamp); |
| 1091 | |
| 1092 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZN) |
| 1093 | iio_push_event(indio_dev, |
| 1094 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1095 | 0, |
| 1096 | IIO_MOD_Z, |
| 1097 | IIO_EV_TYPE_THRESH, |
| 1098 | IIO_EV_DIR_FALLING), |
| 1099 | data->timestamp); |
| 1100 | if (ret & KXCJK1013_REG_INT_SRC2_BIT_ZP) |
| 1101 | iio_push_event(indio_dev, |
| 1102 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1103 | 0, |
| 1104 | IIO_MOD_Z, |
| 1105 | IIO_EV_TYPE_THRESH, |
| 1106 | IIO_EV_DIR_RISING), |
| 1107 | data->timestamp); |
| 1108 | } |
| 1109 | |
| 1110 | ack_intr: |
| 1111 | if (data->dready_trigger_on) |
| 1112 | return IRQ_HANDLED; |
| 1113 | |
| 1114 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL); |
| 1115 | if (ret < 0) |
| 1116 | dev_err(&data->client->dev, "Error reading reg_int_rel\n"); |
| 1117 | |
| 1118 | return IRQ_HANDLED; |
| 1119 | } |
| 1120 | |
| 1121 | static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private) |
| 1122 | { |
| 1123 | struct iio_dev *indio_dev = private; |
| 1124 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 1125 | |
| 1126 | data->timestamp = iio_get_time_ns(); |
| 1127 | |
| 1128 | if (data->dready_trigger_on) |
| 1129 | iio_trigger_poll(data->dready_trig); |
| 1130 | else if (data->motion_trigger_on) |
| 1131 | iio_trigger_poll(data->motion_trig); |
| 1132 | |
| 1133 | if (data->ev_enable_state) |
| 1134 | return IRQ_WAKE_THREAD; |
| 1135 | else |
| 1136 | return IRQ_HANDLED; |
| 1137 | } |
| 1138 | |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1139 | static const char *kxcjk1013_match_acpi_device(struct device *dev, |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1140 | enum kx_chipset *chipset, |
| 1141 | bool *is_smo8500_device) |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1142 | { |
| 1143 | const struct acpi_device_id *id; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1144 | id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 1145 | if (!id) |
| 1146 | return NULL; |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1147 | if (strcmp(id->id, "SMO8500") == 0) |
| 1148 | *is_smo8500_device = true; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1149 | *chipset = (enum kx_chipset)id->driver_data; |
| 1150 | |
| 1151 | return dev_name(dev); |
| 1152 | } |
| 1153 | |
| 1154 | static int kxcjk1013_gpio_probe(struct i2c_client *client, |
| 1155 | struct kxcjk1013_data *data) |
| 1156 | { |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1157 | struct device *dev; |
| 1158 | struct gpio_desc *gpio; |
| 1159 | int ret; |
| 1160 | |
| 1161 | if (!client) |
| 1162 | return -EINVAL; |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1163 | if (data->is_smo8500_device) |
| 1164 | return -ENOTSUPP; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1165 | |
| 1166 | dev = &client->dev; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* data ready gpio interrupt pin */ |
| 1169 | gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0); |
| 1170 | if (IS_ERR(gpio)) { |
| 1171 | dev_err(dev, "acpi gpio get index failed\n"); |
| 1172 | return PTR_ERR(gpio); |
| 1173 | } |
| 1174 | |
| 1175 | ret = gpiod_direction_input(gpio); |
| 1176 | if (ret) |
| 1177 | return ret; |
| 1178 | |
| 1179 | ret = gpiod_to_irq(gpio); |
| 1180 | |
| 1181 | dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); |
| 1182 | |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | static int kxcjk1013_probe(struct i2c_client *client, |
| 1187 | const struct i2c_device_id *id) |
| 1188 | { |
| 1189 | struct kxcjk1013_data *data; |
| 1190 | struct iio_dev *indio_dev; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1191 | struct kxcjk_1013_platform_data *pdata; |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1192 | const char *name; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1193 | int ret; |
| 1194 | |
| 1195 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1196 | if (!indio_dev) |
| 1197 | return -ENOMEM; |
| 1198 | |
| 1199 | data = iio_priv(indio_dev); |
| 1200 | i2c_set_clientdata(client, indio_dev); |
| 1201 | data->client = client; |
| 1202 | |
| 1203 | pdata = dev_get_platdata(&client->dev); |
| 1204 | if (pdata) |
| 1205 | data->active_high_intr = pdata->active_high_intr; |
| 1206 | else |
| 1207 | data->active_high_intr = true; /* default polarity */ |
| 1208 | |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1209 | if (id) { |
| 1210 | data->chipset = (enum kx_chipset)(id->driver_data); |
| 1211 | name = id->name; |
| 1212 | } else if (ACPI_HANDLE(&client->dev)) { |
| 1213 | name = kxcjk1013_match_acpi_device(&client->dev, |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1214 | &data->chipset, |
| 1215 | &data->is_smo8500_device); |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1216 | } else |
| 1217 | return -ENODEV; |
| 1218 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1219 | ret = kxcjk1013_chip_init(data); |
| 1220 | if (ret < 0) |
| 1221 | return ret; |
| 1222 | |
| 1223 | mutex_init(&data->mutex); |
| 1224 | |
| 1225 | indio_dev->dev.parent = &client->dev; |
| 1226 | indio_dev->channels = kxcjk1013_channels; |
| 1227 | indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels); |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1228 | indio_dev->name = name; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1229 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 1230 | indio_dev->info = &kxcjk1013_info; |
| 1231 | |
| 1232 | if (client->irq < 0) |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1233 | client->irq = kxcjk1013_gpio_probe(client, data); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1234 | |
| 1235 | if (client->irq >= 0) { |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1236 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 1237 | kxcjk1013_data_rdy_trig_poll, |
| 1238 | kxcjk1013_event_handler, |
| 1239 | IRQF_TRIGGER_RISING, |
| 1240 | KXCJK1013_IRQ_NAME, |
| 1241 | indio_dev); |
| 1242 | if (ret) |
Irina Tirdea | 9d02daf73 | 2014-12-10 18:23:54 +0200 | [diff] [blame^] | 1243 | goto err_poweroff; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1244 | |
| 1245 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, |
| 1246 | "%s-dev%d", |
| 1247 | indio_dev->name, |
| 1248 | indio_dev->id); |
Irina Tirdea | 9d02daf73 | 2014-12-10 18:23:54 +0200 | [diff] [blame^] | 1249 | if (!data->dready_trig) { |
| 1250 | ret = -ENOMEM; |
| 1251 | goto err_poweroff; |
| 1252 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1253 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1254 | data->motion_trig = devm_iio_trigger_alloc(&client->dev, |
| 1255 | "%s-any-motion-dev%d", |
| 1256 | indio_dev->name, |
| 1257 | indio_dev->id); |
Irina Tirdea | 9d02daf73 | 2014-12-10 18:23:54 +0200 | [diff] [blame^] | 1258 | if (!data->motion_trig) { |
| 1259 | ret = -ENOMEM; |
| 1260 | goto err_poweroff; |
| 1261 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1262 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1263 | data->dready_trig->dev.parent = &client->dev; |
| 1264 | data->dready_trig->ops = &kxcjk1013_trigger_ops; |
| 1265 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
| 1266 | indio_dev->trig = data->dready_trig; |
Srinivas Pandruvada | c1288b8 | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 1267 | iio_trigger_get(indio_dev->trig); |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1268 | ret = iio_trigger_register(data->dready_trig); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1269 | if (ret) |
Irina Tirdea | 9d02daf73 | 2014-12-10 18:23:54 +0200 | [diff] [blame^] | 1270 | goto err_poweroff; |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1271 | |
| 1272 | data->motion_trig->dev.parent = &client->dev; |
| 1273 | data->motion_trig->ops = &kxcjk1013_trigger_ops; |
| 1274 | iio_trigger_set_drvdata(data->motion_trig, indio_dev); |
| 1275 | ret = iio_trigger_register(data->motion_trig); |
| 1276 | if (ret) { |
| 1277 | data->motion_trig = NULL; |
| 1278 | goto err_trigger_unregister; |
| 1279 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1280 | |
| 1281 | ret = iio_triggered_buffer_setup(indio_dev, |
| 1282 | &iio_pollfunc_store_time, |
| 1283 | kxcjk1013_trigger_handler, |
| 1284 | NULL); |
| 1285 | if (ret < 0) { |
| 1286 | dev_err(&client->dev, |
| 1287 | "iio triggered buffer setup failed\n"); |
| 1288 | goto err_trigger_unregister; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1292 | ret = iio_device_register(indio_dev); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1293 | if (ret < 0) { |
| 1294 | dev_err(&client->dev, "unable to register iio device\n"); |
| 1295 | goto err_buffer_cleanup; |
| 1296 | } |
| 1297 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1298 | ret = pm_runtime_set_active(&client->dev); |
| 1299 | if (ret) |
| 1300 | goto err_iio_unregister; |
| 1301 | |
| 1302 | pm_runtime_enable(&client->dev); |
| 1303 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 1304 | KXCJK1013_SLEEP_DELAY_MS); |
| 1305 | pm_runtime_use_autosuspend(&client->dev); |
| 1306 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1307 | return 0; |
| 1308 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1309 | err_iio_unregister: |
| 1310 | iio_device_unregister(indio_dev); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1311 | err_buffer_cleanup: |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1312 | if (data->dready_trig) |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1313 | iio_triggered_buffer_cleanup(indio_dev); |
| 1314 | err_trigger_unregister: |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1315 | if (data->dready_trig) |
| 1316 | iio_trigger_unregister(data->dready_trig); |
| 1317 | if (data->motion_trig) |
| 1318 | iio_trigger_unregister(data->motion_trig); |
Irina Tirdea | 9d02daf73 | 2014-12-10 18:23:54 +0200 | [diff] [blame^] | 1319 | err_poweroff: |
| 1320 | kxcjk1013_set_mode(data, STANDBY); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1321 | |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
| 1325 | static int kxcjk1013_remove(struct i2c_client *client) |
| 1326 | { |
| 1327 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1328 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 1329 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1330 | pm_runtime_disable(&client->dev); |
| 1331 | pm_runtime_set_suspended(&client->dev); |
| 1332 | pm_runtime_put_noidle(&client->dev); |
| 1333 | |
| 1334 | iio_device_unregister(indio_dev); |
| 1335 | |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1336 | if (data->dready_trig) { |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1337 | iio_triggered_buffer_cleanup(indio_dev); |
Srinivas Pandruvada | b4b491c | 2014-08-22 20:01:00 +0100 | [diff] [blame] | 1338 | iio_trigger_unregister(data->dready_trig); |
| 1339 | iio_trigger_unregister(data->motion_trig); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | mutex_lock(&data->mutex); |
| 1343 | kxcjk1013_set_mode(data, STANDBY); |
| 1344 | mutex_unlock(&data->mutex); |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | #ifdef CONFIG_PM_SLEEP |
| 1350 | static int kxcjk1013_suspend(struct device *dev) |
| 1351 | { |
| 1352 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1353 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1354 | int ret; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1355 | |
| 1356 | mutex_lock(&data->mutex); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1357 | ret = kxcjk1013_set_mode(data, STANDBY); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1358 | mutex_unlock(&data->mutex); |
| 1359 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1360 | return ret; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static int kxcjk1013_resume(struct device *dev) |
| 1364 | { |
| 1365 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1366 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1367 | int ret = 0; |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1368 | |
| 1369 | mutex_lock(&data->mutex); |
Irina Tirdea | d3653d0 | 2014-12-06 00:18:07 +0200 | [diff] [blame] | 1370 | ret = kxcjk1013_set_mode(data, OPERATION); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1371 | mutex_unlock(&data->mutex); |
| 1372 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1373 | return ret; |
| 1374 | } |
| 1375 | #endif |
| 1376 | |
| 1377 | #ifdef CONFIG_PM_RUNTIME |
| 1378 | static int kxcjk1013_runtime_suspend(struct device *dev) |
| 1379 | { |
| 1380 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1381 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
Irina Tirdea | fbd123e | 2014-12-10 18:23:53 +0200 | [diff] [blame] | 1382 | int ret; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1383 | |
Irina Tirdea | fbd123e | 2014-12-10 18:23:53 +0200 | [diff] [blame] | 1384 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 1385 | if (ret < 0) { |
| 1386 | dev_err(&data->client->dev, "powering off device failed\n"); |
| 1387 | return -EAGAIN; |
| 1388 | } |
| 1389 | return 0; |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | static int kxcjk1013_runtime_resume(struct device *dev) |
| 1393 | { |
| 1394 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1395 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 1396 | int ret; |
| 1397 | int sleep_val; |
| 1398 | |
| 1399 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 1400 | if (ret < 0) |
| 1401 | return ret; |
| 1402 | |
| 1403 | sleep_val = kxcjk1013_get_startup_times(data); |
| 1404 | if (sleep_val < 20000) |
| 1405 | usleep_range(sleep_val, 20000); |
| 1406 | else |
| 1407 | msleep_interruptible(sleep_val/1000); |
| 1408 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1409 | return 0; |
| 1410 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1411 | #endif |
| 1412 | |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1413 | static const struct dev_pm_ops kxcjk1013_pm_ops = { |
| 1414 | SET_SYSTEM_SLEEP_PM_OPS(kxcjk1013_suspend, kxcjk1013_resume) |
| 1415 | SET_RUNTIME_PM_OPS(kxcjk1013_runtime_suspend, |
| 1416 | kxcjk1013_runtime_resume, NULL) |
| 1417 | }; |
| 1418 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1419 | static const struct acpi_device_id kx_acpi_match[] = { |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1420 | {"KXCJ1013", KXCJK1013}, |
| 1421 | {"KXCJ1008", KXCJ91008}, |
| 1422 | {"KXTJ1009", KXTJ21009}, |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1423 | {"SMO8500", KXCJ91008}, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1424 | { }, |
| 1425 | }; |
| 1426 | MODULE_DEVICE_TABLE(acpi, kx_acpi_match); |
| 1427 | |
| 1428 | static const struct i2c_device_id kxcjk1013_id[] = { |
Daniel Baluta | c686137 | 2014-03-09 08:33:00 +0000 | [diff] [blame] | 1429 | {"kxcjk1013", KXCJK1013}, |
| 1430 | {"kxcj91008", KXCJ91008}, |
| 1431 | {"kxtj21009", KXTJ21009}, |
Bastien Nocera | 3bfa74f | 2014-05-11 22:09:00 +0100 | [diff] [blame] | 1432 | {"SMO8500", KXCJ91008}, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1433 | {} |
| 1434 | }; |
| 1435 | |
| 1436 | MODULE_DEVICE_TABLE(i2c, kxcjk1013_id); |
| 1437 | |
| 1438 | static struct i2c_driver kxcjk1013_driver = { |
| 1439 | .driver = { |
| 1440 | .name = KXCJK1013_DRV_NAME, |
| 1441 | .acpi_match_table = ACPI_PTR(kx_acpi_match), |
Srinivas Pandruvada | 124e1b1d | 2014-05-08 22:58:00 +0100 | [diff] [blame] | 1442 | .pm = &kxcjk1013_pm_ops, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 1443 | }, |
| 1444 | .probe = kxcjk1013_probe, |
| 1445 | .remove = kxcjk1013_remove, |
| 1446 | .id_table = kxcjk1013_id, |
| 1447 | }; |
| 1448 | module_i2c_driver(kxcjk1013_driver); |
| 1449 | |
| 1450 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 1451 | MODULE_LICENSE("GPL v2"); |
| 1452 | MODULE_DESCRIPTION("KXCJK1013 accelerometer driver"); |