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> |
| 24 | #include <linux/iio/iio.h> |
| 25 | #include <linux/iio/sysfs.h> |
| 26 | #include <linux/iio/buffer.h> |
| 27 | #include <linux/iio/trigger.h> |
| 28 | #include <linux/iio/trigger_consumer.h> |
| 29 | #include <linux/iio/triggered_buffer.h> |
| 30 | #include <linux/iio/accel/kxcjk_1013.h> |
| 31 | |
| 32 | #define KXCJK1013_DRV_NAME "kxcjk1013" |
| 33 | #define KXCJK1013_IRQ_NAME "kxcjk1013_event" |
| 34 | |
| 35 | #define KXCJK1013_REG_XOUT_L 0x06 |
| 36 | /* |
| 37 | * From low byte X axis register, all the other addresses of Y and Z can be |
| 38 | * obtained by just applying axis offset. The following axis defines are just |
| 39 | * provide clarity, but not used. |
| 40 | */ |
| 41 | #define KXCJK1013_REG_XOUT_H 0x07 |
| 42 | #define KXCJK1013_REG_YOUT_L 0x08 |
| 43 | #define KXCJK1013_REG_YOUT_H 0x09 |
| 44 | #define KXCJK1013_REG_ZOUT_L 0x0A |
| 45 | #define KXCJK1013_REG_ZOUT_H 0x0B |
| 46 | |
| 47 | #define KXCJK1013_REG_DCST_RESP 0x0C |
| 48 | #define KXCJK1013_REG_WHO_AM_I 0x0F |
| 49 | #define KXCJK1013_REG_INT_SRC1 0x16 |
| 50 | #define KXCJK1013_REG_INT_SRC2 0x17 |
| 51 | #define KXCJK1013_REG_STATUS_REG 0x18 |
| 52 | #define KXCJK1013_REG_INT_REL 0x1A |
| 53 | #define KXCJK1013_REG_CTRL1 0x1B |
| 54 | #define KXCJK1013_REG_CTRL2 0x1D |
| 55 | #define KXCJK1013_REG_INT_CTRL1 0x1E |
| 56 | #define KXCJK1013_REG_INT_CTRL2 0x1F |
| 57 | #define KXCJK1013_REG_DATA_CTRL 0x21 |
| 58 | #define KXCJK1013_REG_WAKE_TIMER 0x29 |
| 59 | #define KXCJK1013_REG_SELF_TEST 0x3A |
| 60 | #define KXCJK1013_REG_WAKE_THRES 0x6A |
| 61 | |
| 62 | #define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7) |
| 63 | #define KXCJK1013_REG_CTRL1_BIT_RES BIT(6) |
| 64 | #define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5) |
| 65 | #define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4) |
| 66 | #define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3) |
| 67 | #define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1) |
| 68 | #define KXCJK1013_REG_INT_REG1_BIT_IEA BIT(4) |
| 69 | #define KXCJK1013_REG_INT_REG1_BIT_IEN BIT(5) |
| 70 | |
| 71 | #define KXCJK1013_DATA_MASK_12_BIT 0x0FFF |
| 72 | #define KXCJK1013_MAX_STARTUP_TIME_US 100000 |
| 73 | |
| 74 | struct kxcjk1013_data { |
| 75 | struct i2c_client *client; |
| 76 | struct iio_trigger *trig; |
| 77 | bool trig_mode; |
| 78 | struct mutex mutex; |
| 79 | s16 buffer[8]; |
| 80 | int power_state; |
| 81 | u8 odr_bits; |
| 82 | bool active_high_intr; |
| 83 | }; |
| 84 | |
| 85 | enum kxcjk1013_axis { |
| 86 | AXIS_X, |
| 87 | AXIS_Y, |
| 88 | AXIS_Z, |
| 89 | }; |
| 90 | |
| 91 | enum kxcjk1013_mode { |
| 92 | STANDBY, |
| 93 | OPERATION, |
| 94 | }; |
| 95 | |
| 96 | static const struct { |
| 97 | int val; |
| 98 | int val2; |
| 99 | int odr_bits; |
| 100 | } samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09}, |
Srinivas Pandruvada | f0ca974 | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 101 | {3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0}, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 102 | {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03}, |
| 103 | {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06}, |
| 104 | {1600, 0, 0x07} }; |
| 105 | |
| 106 | /* Refer to section 4 of the specification */ |
| 107 | static const struct { |
| 108 | int odr_bits; |
| 109 | int usec; |
| 110 | } odr_start_up_times[] = { {0x08, 100000}, {0x09, 100000}, {0x0A, 100000}, |
| 111 | {0x0B, 100000}, { 0, 80000}, {0x01, 41000}, |
| 112 | {0x02, 21000}, {0x03, 11000}, {0x04, 6400}, |
| 113 | {0x05, 3900}, {0x06, 2700}, {0x07, 2100} }; |
| 114 | |
| 115 | static int kxcjk1013_set_mode(struct kxcjk1013_data *data, |
| 116 | enum kxcjk1013_mode mode) |
| 117 | { |
| 118 | int ret; |
| 119 | |
| 120 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 121 | if (ret < 0) { |
| 122 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | if (mode == STANDBY) |
| 127 | ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1; |
| 128 | else |
| 129 | ret |= KXCJK1013_REG_CTRL1_BIT_PC1; |
| 130 | |
| 131 | ret = i2c_smbus_write_byte_data(data->client, |
| 132 | KXCJK1013_REG_CTRL1, ret); |
| 133 | if (ret < 0) { |
| 134 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 141 | static int kxcjk1013_chip_init(struct kxcjk1013_data *data) |
| 142 | { |
| 143 | int ret; |
| 144 | |
| 145 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I); |
| 146 | if (ret < 0) { |
| 147 | dev_err(&data->client->dev, "Error reading who_am_i\n"); |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret); |
| 152 | |
| 153 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 154 | if (ret < 0) |
| 155 | return ret; |
| 156 | |
| 157 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 158 | if (ret < 0) { |
| 159 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | /* Setting range to 4G */ |
| 164 | ret |= KXCJK1013_REG_CTRL1_BIT_GSEL0; |
| 165 | ret &= ~KXCJK1013_REG_CTRL1_BIT_GSEL1; |
| 166 | |
| 167 | /* Set 12 bit mode */ |
| 168 | ret |= KXCJK1013_REG_CTRL1_BIT_RES; |
| 169 | |
| 170 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1, |
| 171 | ret); |
| 172 | if (ret < 0) { |
| 173 | dev_err(&data->client->dev, "Error reading reg_ctrl\n"); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL); |
| 178 | if (ret < 0) { |
| 179 | dev_err(&data->client->dev, "Error reading reg_data_ctrl\n"); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | data->odr_bits = ret; |
| 184 | |
| 185 | /* Set up INT polarity */ |
| 186 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); |
| 187 | if (ret < 0) { |
| 188 | dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | if (data->active_high_intr) |
| 193 | ret |= KXCJK1013_REG_INT_REG1_BIT_IEA; |
| 194 | else |
| 195 | ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA; |
| 196 | |
| 197 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, |
| 198 | ret); |
| 199 | if (ret < 0) { |
| 200 | dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int kxcjk1013_chip_setup_interrupt(struct kxcjk1013_data *data, |
| 208 | bool status) |
| 209 | { |
| 210 | int ret; |
| 211 | |
| 212 | /* This is requirement by spec to change state to STANDBY */ |
| 213 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 214 | if (ret < 0) |
| 215 | return ret; |
| 216 | |
| 217 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1); |
| 218 | if (ret < 0) { |
| 219 | dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n"); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | if (status) |
| 224 | ret |= KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 225 | else |
| 226 | ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN; |
| 227 | |
| 228 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1, |
| 229 | ret); |
| 230 | if (ret < 0) { |
| 231 | dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n"); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1); |
| 236 | if (ret < 0) { |
| 237 | dev_err(&data->client->dev, "Error reading reg_ctrl1\n"); |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | if (status) |
| 242 | ret |= KXCJK1013_REG_CTRL1_BIT_DRDY; |
| 243 | else |
| 244 | ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY; |
| 245 | |
| 246 | ret = i2c_smbus_write_byte_data(data->client, |
| 247 | KXCJK1013_REG_CTRL1, ret); |
| 248 | if (ret < 0) { |
| 249 | dev_err(&data->client->dev, "Error writing reg_ctrl1\n"); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static int kxcjk1013_convert_freq_to_bit(int val, int val2) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) { |
| 261 | if (samp_freq_table[i].val == val && |
| 262 | samp_freq_table[i].val2 == val2) { |
| 263 | return samp_freq_table[i].odr_bits; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2) |
| 271 | { |
| 272 | int ret; |
| 273 | int odr_bits; |
| 274 | |
| 275 | odr_bits = kxcjk1013_convert_freq_to_bit(val, val2); |
| 276 | if (odr_bits < 0) |
| 277 | return odr_bits; |
| 278 | |
| 279 | /* To change ODR, the chip must be set to STANDBY as per spec */ |
| 280 | ret = kxcjk1013_set_mode(data, STANDBY); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | |
| 284 | ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL, |
| 285 | odr_bits); |
| 286 | if (ret < 0) { |
| 287 | dev_err(&data->client->dev, "Error writing data_ctrl\n"); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | data->odr_bits = odr_bits; |
| 292 | |
| 293 | /* Check, if the ODR is changed after data enable */ |
| 294 | if (data->power_state) { |
| 295 | /* Set the state back to operation */ |
| 296 | ret = kxcjk1013_set_mode(data, OPERATION); |
| 297 | if (ret < 0) |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2) |
| 305 | { |
| 306 | int i; |
| 307 | |
| 308 | for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) { |
| 309 | if (samp_freq_table[i].odr_bits == data->odr_bits) { |
| 310 | *val = samp_freq_table[i].val; |
| 311 | *val2 = samp_freq_table[i].val2; |
| 312 | return IIO_VAL_INT_PLUS_MICRO; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis) |
| 320 | { |
| 321 | u8 reg = KXCJK1013_REG_XOUT_L + axis * 2; |
| 322 | int ret; |
| 323 | |
| 324 | ret = i2c_smbus_read_word_data(data->client, reg); |
| 325 | if (ret < 0) { |
| 326 | dev_err(&data->client->dev, |
| 327 | "failed to read accel_%c registers\n", 'x' + axis); |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data) |
| 335 | { |
| 336 | int i; |
| 337 | |
| 338 | for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) { |
| 339 | if (odr_start_up_times[i].odr_bits == data->odr_bits) |
| 340 | return odr_start_up_times[i].usec; |
| 341 | } |
| 342 | |
| 343 | return KXCJK1013_MAX_STARTUP_TIME_US; |
| 344 | } |
| 345 | |
| 346 | static int kxcjk1013_read_raw(struct iio_dev *indio_dev, |
| 347 | struct iio_chan_spec const *chan, int *val, |
| 348 | int *val2, long mask) |
| 349 | { |
| 350 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 351 | int ret; |
| 352 | |
| 353 | switch (mask) { |
| 354 | case IIO_CHAN_INFO_RAW: |
| 355 | mutex_lock(&data->mutex); |
| 356 | if (iio_buffer_enabled(indio_dev)) |
| 357 | ret = -EBUSY; |
| 358 | else { |
| 359 | int sleep_val; |
| 360 | |
| 361 | ret = kxcjk1013_set_mode(data, OPERATION); |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 362 | if (ret < 0) { |
| 363 | mutex_unlock(&data->mutex); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 364 | return ret; |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 365 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 366 | ++data->power_state; |
| 367 | sleep_val = kxcjk1013_get_startup_times(data); |
| 368 | if (sleep_val < 20000) |
| 369 | usleep_range(sleep_val, 20000); |
| 370 | else |
| 371 | msleep_interruptible(sleep_val/1000); |
| 372 | ret = kxcjk1013_get_acc_reg(data, chan->scan_index); |
| 373 | if (--data->power_state == 0) |
| 374 | kxcjk1013_set_mode(data, STANDBY); |
| 375 | } |
| 376 | mutex_unlock(&data->mutex); |
| 377 | |
| 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
| 381 | *val = sign_extend32(ret >> 4, 11); |
| 382 | return IIO_VAL_INT; |
| 383 | |
| 384 | case IIO_CHAN_INFO_SCALE: |
| 385 | *val = 0; |
| 386 | *val2 = 19163; /* range +-4g (4/2047*9.806650) */ |
| 387 | return IIO_VAL_INT_PLUS_MICRO; |
| 388 | |
| 389 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 390 | mutex_lock(&data->mutex); |
| 391 | ret = kxcjk1013_get_odr(data, val, val2); |
| 392 | mutex_unlock(&data->mutex); |
| 393 | return ret; |
| 394 | |
| 395 | default: |
| 396 | return -EINVAL; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static int kxcjk1013_write_raw(struct iio_dev *indio_dev, |
| 401 | struct iio_chan_spec const *chan, int val, |
| 402 | int val2, long mask) |
| 403 | { |
| 404 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 405 | int ret; |
| 406 | |
| 407 | switch (mask) { |
| 408 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 409 | mutex_lock(&data->mutex); |
| 410 | ret = kxcjk1013_set_odr(data, val, val2); |
| 411 | mutex_unlock(&data->mutex); |
| 412 | break; |
| 413 | default: |
| 414 | ret = -EINVAL; |
| 415 | } |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | static int kxcjk1013_validate_trigger(struct iio_dev *indio_dev, |
| 421 | struct iio_trigger *trig) |
| 422 | { |
| 423 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 424 | |
| 425 | if (data->trig != trig) |
| 426 | return -EINVAL; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( |
| 432 | "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600"); |
| 433 | |
| 434 | static struct attribute *kxcjk1013_attributes[] = { |
| 435 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 436 | NULL, |
| 437 | }; |
| 438 | |
| 439 | static const struct attribute_group kxcjk1013_attrs_group = { |
| 440 | .attrs = kxcjk1013_attributes, |
| 441 | }; |
| 442 | |
| 443 | #define KXCJK1013_CHANNEL(_axis) { \ |
| 444 | .type = IIO_ACCEL, \ |
| 445 | .modified = 1, \ |
| 446 | .channel2 = IIO_MOD_##_axis, \ |
| 447 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 448 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 449 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 450 | .scan_index = AXIS_##_axis, \ |
| 451 | .scan_type = { \ |
| 452 | .sign = 's', \ |
| 453 | .realbits = 12, \ |
| 454 | .storagebits = 16, \ |
| 455 | .shift = 4, \ |
Peter Meerwald | f4e2f94d | 2014-07-14 21:38:00 +0100 | [diff] [blame] | 456 | .endianness = IIO_CPU, \ |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 457 | }, \ |
| 458 | } |
| 459 | |
| 460 | static const struct iio_chan_spec kxcjk1013_channels[] = { |
| 461 | KXCJK1013_CHANNEL(X), |
| 462 | KXCJK1013_CHANNEL(Y), |
| 463 | KXCJK1013_CHANNEL(Z), |
| 464 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 465 | }; |
| 466 | |
| 467 | static const struct iio_info kxcjk1013_info = { |
| 468 | .attrs = &kxcjk1013_attrs_group, |
| 469 | .read_raw = kxcjk1013_read_raw, |
| 470 | .write_raw = kxcjk1013_write_raw, |
| 471 | .validate_trigger = kxcjk1013_validate_trigger, |
| 472 | .driver_module = THIS_MODULE, |
| 473 | }; |
| 474 | |
| 475 | static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p) |
| 476 | { |
| 477 | struct iio_poll_func *pf = p; |
| 478 | struct iio_dev *indio_dev = pf->indio_dev; |
| 479 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 480 | int bit, ret, i = 0; |
| 481 | |
| 482 | mutex_lock(&data->mutex); |
| 483 | |
| 484 | for_each_set_bit(bit, indio_dev->buffer->scan_mask, |
| 485 | indio_dev->masklength) { |
| 486 | ret = kxcjk1013_get_acc_reg(data, bit); |
| 487 | if (ret < 0) { |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 488 | mutex_unlock(&data->mutex); |
| 489 | goto err; |
| 490 | } |
| 491 | data->buffer[i++] = ret; |
| 492 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 493 | mutex_unlock(&data->mutex); |
| 494 | |
| 495 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 496 | pf->timestamp); |
| 497 | err: |
| 498 | iio_trigger_notify_done(indio_dev->trig); |
| 499 | |
| 500 | return IRQ_HANDLED; |
| 501 | } |
| 502 | |
Srinivas Pandruvada | 59bfeab | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 503 | static int kxcjk1013_trig_try_reen(struct iio_trigger *trig) |
| 504 | { |
| 505 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 506 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 507 | int ret; |
| 508 | |
| 509 | ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL); |
| 510 | if (ret < 0) { |
| 511 | dev_err(&data->client->dev, "Error reading reg_int_rel\n"); |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 518 | static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 519 | bool state) |
| 520 | { |
| 521 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 522 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 523 | |
| 524 | mutex_lock(&data->mutex); |
| 525 | if (state) { |
| 526 | kxcjk1013_chip_setup_interrupt(data, true); |
| 527 | kxcjk1013_set_mode(data, OPERATION); |
| 528 | ++data->power_state; |
| 529 | } else { |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 530 | if (--data->power_state) { |
| 531 | mutex_unlock(&data->mutex); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 532 | return 0; |
Srinivas Pandruvada | 88f6da7 | 2014-06-16 20:00:00 +0100 | [diff] [blame] | 533 | } |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 534 | kxcjk1013_chip_setup_interrupt(data, false); |
| 535 | kxcjk1013_set_mode(data, STANDBY); |
| 536 | } |
| 537 | mutex_unlock(&data->mutex); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static const struct iio_trigger_ops kxcjk1013_trigger_ops = { |
| 543 | .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state, |
Srinivas Pandruvada | 59bfeab | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 544 | .try_reenable = kxcjk1013_trig_try_reen, |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 545 | .owner = THIS_MODULE, |
| 546 | }; |
| 547 | |
| 548 | static int kxcjk1013_acpi_gpio_probe(struct i2c_client *client, |
| 549 | struct kxcjk1013_data *data) |
| 550 | { |
| 551 | const struct acpi_device_id *id; |
| 552 | struct device *dev; |
| 553 | struct gpio_desc *gpio; |
| 554 | int ret; |
| 555 | |
| 556 | if (!client) |
| 557 | return -EINVAL; |
| 558 | |
| 559 | dev = &client->dev; |
| 560 | if (!ACPI_HANDLE(dev)) |
| 561 | return -ENODEV; |
| 562 | |
| 563 | id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 564 | if (!id) |
| 565 | return -ENODEV; |
| 566 | |
| 567 | /* data ready gpio interrupt pin */ |
| 568 | gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0); |
| 569 | if (IS_ERR(gpio)) { |
| 570 | dev_err(dev, "acpi gpio get index failed\n"); |
| 571 | return PTR_ERR(gpio); |
| 572 | } |
| 573 | |
| 574 | ret = gpiod_direction_input(gpio); |
| 575 | if (ret) |
| 576 | return ret; |
| 577 | |
| 578 | ret = gpiod_to_irq(gpio); |
| 579 | |
| 580 | dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int kxcjk1013_probe(struct i2c_client *client, |
| 586 | const struct i2c_device_id *id) |
| 587 | { |
| 588 | struct kxcjk1013_data *data; |
| 589 | struct iio_dev *indio_dev; |
| 590 | struct iio_trigger *trig = NULL; |
| 591 | struct kxcjk_1013_platform_data *pdata; |
| 592 | int ret; |
| 593 | |
| 594 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 595 | if (!indio_dev) |
| 596 | return -ENOMEM; |
| 597 | |
| 598 | data = iio_priv(indio_dev); |
| 599 | i2c_set_clientdata(client, indio_dev); |
| 600 | data->client = client; |
| 601 | |
| 602 | pdata = dev_get_platdata(&client->dev); |
| 603 | if (pdata) |
| 604 | data->active_high_intr = pdata->active_high_intr; |
| 605 | else |
| 606 | data->active_high_intr = true; /* default polarity */ |
| 607 | |
| 608 | ret = kxcjk1013_chip_init(data); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | |
| 612 | mutex_init(&data->mutex); |
| 613 | |
| 614 | indio_dev->dev.parent = &client->dev; |
| 615 | indio_dev->channels = kxcjk1013_channels; |
| 616 | indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels); |
| 617 | indio_dev->name = KXCJK1013_DRV_NAME; |
| 618 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 619 | indio_dev->info = &kxcjk1013_info; |
| 620 | |
| 621 | if (client->irq < 0) |
| 622 | client->irq = kxcjk1013_acpi_gpio_probe(client, data); |
| 623 | |
| 624 | if (client->irq >= 0) { |
| 625 | trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, |
| 626 | indio_dev->id); |
| 627 | if (!trig) |
| 628 | return -ENOMEM; |
| 629 | |
| 630 | data->trig_mode = true; |
| 631 | |
| 632 | ret = devm_request_irq(&client->dev, client->irq, |
| 633 | iio_trigger_generic_data_rdy_poll, |
| 634 | IRQF_TRIGGER_RISING, |
| 635 | KXCJK1013_IRQ_NAME, |
| 636 | trig); |
| 637 | if (ret) { |
| 638 | dev_err(&client->dev, "unable to request IRQ\n"); |
| 639 | goto err_trigger_free; |
| 640 | } |
| 641 | |
| 642 | trig->dev.parent = &client->dev; |
| 643 | trig->ops = &kxcjk1013_trigger_ops; |
| 644 | iio_trigger_set_drvdata(trig, indio_dev); |
| 645 | data->trig = trig; |
| 646 | indio_dev->trig = trig; |
Srinivas Pandruvada | c1288b8 | 2014-07-17 01:42:00 +0100 | [diff] [blame] | 647 | iio_trigger_get(indio_dev->trig); |
Srinivas Pandruvada | 1a4fbf6 | 2014-11-06 23:07:00 +0000 | [diff] [blame] | 648 | |
| 649 | ret = iio_trigger_register(trig); |
| 650 | if (ret) |
| 651 | goto err_trigger_free; |
| 652 | |
| 653 | ret = iio_triggered_buffer_setup(indio_dev, |
| 654 | &iio_pollfunc_store_time, |
| 655 | kxcjk1013_trigger_handler, |
| 656 | NULL); |
| 657 | if (ret < 0) { |
| 658 | dev_err(&client->dev, |
| 659 | "iio triggered buffer setup failed\n"); |
| 660 | goto err_trigger_unregister; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | ret = devm_iio_device_register(&client->dev, indio_dev); |
| 665 | if (ret < 0) { |
| 666 | dev_err(&client->dev, "unable to register iio device\n"); |
| 667 | goto err_buffer_cleanup; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | |
| 672 | err_buffer_cleanup: |
| 673 | if (data->trig_mode) |
| 674 | iio_triggered_buffer_cleanup(indio_dev); |
| 675 | err_trigger_unregister: |
| 676 | if (data->trig_mode) |
| 677 | iio_trigger_unregister(trig); |
| 678 | err_trigger_free: |
| 679 | if (data->trig_mode) |
| 680 | iio_trigger_free(trig); |
| 681 | |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | static int kxcjk1013_remove(struct i2c_client *client) |
| 686 | { |
| 687 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 688 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 689 | |
| 690 | if (data->trig_mode) { |
| 691 | iio_triggered_buffer_cleanup(indio_dev); |
| 692 | iio_trigger_unregister(data->trig); |
| 693 | iio_trigger_free(data->trig); |
| 694 | } |
| 695 | |
| 696 | mutex_lock(&data->mutex); |
| 697 | kxcjk1013_set_mode(data, STANDBY); |
| 698 | mutex_unlock(&data->mutex); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | #ifdef CONFIG_PM_SLEEP |
| 704 | static int kxcjk1013_suspend(struct device *dev) |
| 705 | { |
| 706 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 707 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 708 | |
| 709 | mutex_lock(&data->mutex); |
| 710 | kxcjk1013_set_mode(data, STANDBY); |
| 711 | mutex_unlock(&data->mutex); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | static int kxcjk1013_resume(struct device *dev) |
| 717 | { |
| 718 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 719 | struct kxcjk1013_data *data = iio_priv(indio_dev); |
| 720 | |
| 721 | mutex_lock(&data->mutex); |
| 722 | |
| 723 | if (data->power_state) |
| 724 | kxcjk1013_set_mode(data, OPERATION); |
| 725 | |
| 726 | mutex_unlock(&data->mutex); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static SIMPLE_DEV_PM_OPS(kxcjk1013_pm_ops, kxcjk1013_suspend, kxcjk1013_resume); |
| 732 | #define KXCJK1013_PM_OPS (&kxcjk1013_pm_ops) |
| 733 | #else |
| 734 | #define KXCJK1013_PM_OPS NULL |
| 735 | #endif |
| 736 | |
| 737 | static const struct acpi_device_id kx_acpi_match[] = { |
| 738 | {"KXCJ1013", 0}, |
| 739 | { }, |
| 740 | }; |
| 741 | MODULE_DEVICE_TABLE(acpi, kx_acpi_match); |
| 742 | |
| 743 | static const struct i2c_device_id kxcjk1013_id[] = { |
| 744 | {"kxcjk1013", 0}, |
| 745 | {} |
| 746 | }; |
| 747 | |
| 748 | MODULE_DEVICE_TABLE(i2c, kxcjk1013_id); |
| 749 | |
| 750 | static struct i2c_driver kxcjk1013_driver = { |
| 751 | .driver = { |
| 752 | .name = KXCJK1013_DRV_NAME, |
| 753 | .acpi_match_table = ACPI_PTR(kx_acpi_match), |
| 754 | .pm = KXCJK1013_PM_OPS, |
| 755 | }, |
| 756 | .probe = kxcjk1013_probe, |
| 757 | .remove = kxcjk1013_remove, |
| 758 | .id_table = kxcjk1013_id, |
| 759 | }; |
| 760 | module_i2c_driver(kxcjk1013_driver); |
| 761 | |
| 762 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 763 | MODULE_LICENSE("GPL v2"); |
| 764 | MODULE_DESCRIPTION("KXCJK1013 accelerometer driver"); |