aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-04-13 20:37:33 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2015-04-13 20:37:33 -0400
commitb79013b2449c23f1f505bdf39c5a6c330338b244 (patch)
tree67908ffb1705a595cda8de7224d121fa40b8f36d /drivers/iio
parentc4be50eee2bd4d50e0f0ca58776f685c08de69c3 (diff)
parentc610f7f772aa06ae2bd8e5ace87cde4d90f70198 (diff)
Merge tag 'staging-4.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging driver updates from Greg KH: "Here's the big staging driver patchset for 4.1-rc1. There's a lot of patches here, the Outreachy application period happened during this development cycle, so that means that there was a lot of cleanup patches accepted. Other than the normal coding style and sparse fixes here, there are some driver updates and work toward making some of the drivers into "mergable" shape (like the Unisys drivers.) All of these have been in linux-next for a while" * tag 'staging-4.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (1214 commits) staging: lustre: orthography & coding style staging: lustre: lnet: lnet: fix error return code staging: lustre: fix sparse warning Revert "Staging: sm750fb: Fix C99 Comments" Staging: rtl8192u: use correct array for debug output staging: rtl8192e: Remove dead code staging: rtl8192e: Comment cleanup (style/format) staging: rtl8192e: Fix indentation in rtllib_rx_auth_resp() staging: rtl8192e: Decrease nesting of rtllib_rx_auth_resp() staging: rtl8192e: Divide rtllib_rx_auth() staging: rtl8192e: Fix PRINTK_WITHOUT_KERN_LEVEL warnings staging: rtl8192e: Fix DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON warning staging: rtl8192e: Fix BRACES warning staging: rtl8192e: Fix LINE_CONTINUATIONS warning staging: rtl8192e: Fix UNNECESSARY_PARENTHESES warnings staging: rtl8192e: remove unused EXPORT_SYMBOL_RSL macro staging: rtl8192e: Fix RETURN_VOID warnings staging: rtl8192e: Fix UNNECESSARY_ELSE warning staging: rtl8723au: Remove unneeded comments staging: rtl8723au: Use __func__ in trace logs ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/bmc150-accel.c993
-rw-r--r--drivers/iio/accel/kxcjk-1013.c6
-rw-r--r--drivers/iio/accel/mma9551.c9
-rw-r--r--drivers/iio/accel/mma9553.c6
-rw-r--r--drivers/iio/accel/st_accel.h1
-rw-r--r--drivers/iio/accel/st_accel_core.c81
-rw-r--r--drivers/iio/accel/st_accel_i2c.c4
-rw-r--r--drivers/iio/adc/Kconfig5
-rw-r--r--drivers/iio/adc/ad7793.c2
-rw-r--r--drivers/iio/adc/vf610_adc.c5
-rw-r--r--drivers/iio/common/ssp_sensors/ssp_dev.c2
-rw-r--r--drivers/iio/dac/Kconfig11
-rw-r--r--drivers/iio/dac/max517.c45
-rw-r--r--drivers/iio/gyro/bmg160.c6
-rw-r--r--drivers/iio/gyro/itg3200_core.c29
-rw-r--r--drivers/iio/gyro/st_gyro_core.c83
-rw-r--r--drivers/iio/imu/inv_mpu6050/Makefile2
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c211
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c7
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h3
-rw-r--r--drivers/iio/imu/kmx61.c70
-rw-r--r--drivers/iio/industrialio-buffer.c197
-rw-r--r--drivers/iio/kfifo_buf.c11
-rw-r--r--drivers/iio/light/Kconfig10
-rw-r--r--drivers/iio/light/Makefile1
-rw-r--r--drivers/iio/light/cm3232.c36
-rw-r--r--drivers/iio/light/cm3323.c286
-rw-r--r--drivers/iio/light/gp2ap020a00f.c6
-rw-r--r--drivers/iio/light/jsa1212.c2
-rw-r--r--drivers/iio/light/ltr501.c18
-rw-r--r--drivers/iio/magnetometer/mag3110.c18
-rw-r--r--drivers/iio/pressure/Kconfig27
-rw-r--r--drivers/iio/pressure/Makefile3
-rw-r--r--drivers/iio/pressure/ms5611.h44
-rw-r--r--drivers/iio/pressure/ms5611_core.c215
-rw-r--r--drivers/iio/pressure/ms5611_i2c.c128
-rw-r--r--drivers/iio/pressure/ms5611_spi.c127
-rw-r--r--drivers/iio/proximity/sx9500.c6
-rw-r--r--drivers/iio/temperature/mlx90614.c97
39 files changed, 2346 insertions, 467 deletions
diff --git a/drivers/iio/accel/bmc150-accel.c b/drivers/iio/accel/bmc150-accel.c
index 75567fd457dc..73e87739d219 100644
--- a/drivers/iio/accel/bmc150-accel.c
+++ b/drivers/iio/accel/bmc150-accel.c
@@ -70,7 +70,9 @@
70#define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2) 70#define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2)
71 71
72#define BMC150_ACCEL_REG_INT_MAP_1 0x1A 72#define BMC150_ACCEL_REG_INT_MAP_1 0x1A
73#define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0) 73#define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0)
74#define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1)
75#define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2)
74 76
75#define BMC150_ACCEL_REG_INT_RST_LATCH 0x21 77#define BMC150_ACCEL_REG_INT_RST_LATCH 0x21
76#define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80 78#define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80
@@ -83,7 +85,9 @@
83#define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2) 85#define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2)
84 86
85#define BMC150_ACCEL_REG_INT_EN_1 0x17 87#define BMC150_ACCEL_REG_INT_EN_1 0x17
86#define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4) 88#define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4)
89#define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5)
90#define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6)
87 91
88#define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20 92#define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20
89#define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0) 93#define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0)
@@ -122,6 +126,12 @@
122#define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2)) 126#define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2))
123#define BMC150_AUTO_SUSPEND_DELAY_MS 2000 127#define BMC150_AUTO_SUSPEND_DELAY_MS 2000
124 128
129#define BMC150_ACCEL_REG_FIFO_STATUS 0x0E
130#define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30
131#define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E
132#define BMC150_ACCEL_REG_FIFO_DATA 0x3F
133#define BMC150_ACCEL_FIFO_LENGTH 32
134
125enum bmc150_accel_axis { 135enum bmc150_accel_axis {
126 AXIS_X, 136 AXIS_X,
127 AXIS_Y, 137 AXIS_Y,
@@ -147,20 +157,46 @@ struct bmc150_accel_chip_info {
147 const struct bmc150_scale_info scale_table[4]; 157 const struct bmc150_scale_info scale_table[4];
148}; 158};
149 159
160struct bmc150_accel_interrupt {
161 const struct bmc150_accel_interrupt_info *info;
162 atomic_t users;
163};
164
165struct bmc150_accel_trigger {
166 struct bmc150_accel_data *data;
167 struct iio_trigger *indio_trig;
168 int (*setup)(struct bmc150_accel_trigger *t, bool state);
169 int intr;
170 bool enabled;
171};
172
173enum bmc150_accel_interrupt_id {
174 BMC150_ACCEL_INT_DATA_READY,
175 BMC150_ACCEL_INT_ANY_MOTION,
176 BMC150_ACCEL_INT_WATERMARK,
177 BMC150_ACCEL_INTERRUPTS,
178};
179
180enum bmc150_accel_trigger_id {
181 BMC150_ACCEL_TRIGGER_DATA_READY,
182 BMC150_ACCEL_TRIGGER_ANY_MOTION,
183 BMC150_ACCEL_TRIGGERS,
184};
185
150struct bmc150_accel_data { 186struct bmc150_accel_data {
151 struct i2c_client *client; 187 struct i2c_client *client;
152 struct iio_trigger *dready_trig; 188 struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS];
153 struct iio_trigger *motion_trig; 189 atomic_t active_intr;
190 struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS];
154 struct mutex mutex; 191 struct mutex mutex;
192 u8 fifo_mode, watermark;
155 s16 buffer[8]; 193 s16 buffer[8];
156 u8 bw_bits; 194 u8 bw_bits;
157 u32 slope_dur; 195 u32 slope_dur;
158 u32 slope_thres; 196 u32 slope_thres;
159 u32 range; 197 u32 range;
160 int ev_enable_state; 198 int ev_enable_state;
161 bool dready_trigger_on; 199 int64_t timestamp, old_timestamp;
162 bool motion_trigger_on;
163 int64_t timestamp;
164 const struct bmc150_accel_chip_info *chip_info; 200 const struct bmc150_accel_chip_info *chip_info;
165}; 201};
166 202
@@ -269,6 +305,46 @@ static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val,
269 return -EINVAL; 305 return -EINVAL;
270} 306}
271 307
308static int bmc150_accel_update_slope(struct bmc150_accel_data *data)
309{
310 int ret, val;
311
312 ret = i2c_smbus_write_byte_data(data->client, BMC150_ACCEL_REG_INT_6,
313 data->slope_thres);
314 if (ret < 0) {
315 dev_err(&data->client->dev, "Error writing reg_int_6\n");
316 return ret;
317 }
318
319 ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5);
320 if (ret < 0) {
321 dev_err(&data->client->dev, "Error reading reg_int_5\n");
322 return ret;
323 }
324
325 val = (ret & ~BMC150_ACCEL_SLOPE_DUR_MASK) | data->slope_dur;
326 ret = i2c_smbus_write_byte_data(data->client, BMC150_ACCEL_REG_INT_5,
327 val);
328 if (ret < 0) {
329 dev_err(&data->client->dev, "Error write reg_int_5\n");
330 return ret;
331 }
332
333 dev_dbg(&data->client->dev, "%s: %x %x\n", __func__, data->slope_thres,
334 data->slope_dur);
335
336 return ret;
337}
338
339static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t,
340 bool state)
341{
342 if (state)
343 return bmc150_accel_update_slope(t->data);
344
345 return 0;
346}
347
272static int bmc150_accel_chip_init(struct bmc150_accel_data *data) 348static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
273{ 349{
274 int ret; 350 int ret;
@@ -307,32 +383,12 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
307 383
308 data->range = BMC150_ACCEL_DEF_RANGE_4G; 384 data->range = BMC150_ACCEL_DEF_RANGE_4G;
309 385
310 /* Set default slope duration */ 386 /* Set default slope duration and thresholds */
311 ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5);
312 if (ret < 0) {
313 dev_err(&data->client->dev, "Error reading reg_int_5\n");
314 return ret;
315 }
316 data->slope_dur |= BMC150_ACCEL_DEF_SLOPE_DURATION;
317 ret = i2c_smbus_write_byte_data(data->client,
318 BMC150_ACCEL_REG_INT_5,
319 data->slope_dur);
320 if (ret < 0) {
321 dev_err(&data->client->dev, "Error writing reg_int_5\n");
322 return ret;
323 }
324 dev_dbg(&data->client->dev, "slope_dur %x\n", data->slope_dur);
325
326 /* Set default slope thresholds */
327 ret = i2c_smbus_write_byte_data(data->client,
328 BMC150_ACCEL_REG_INT_6,
329 BMC150_ACCEL_DEF_SLOPE_THRESHOLD);
330 if (ret < 0) {
331 dev_err(&data->client->dev, "Error writing reg_int_6\n");
332 return ret;
333 }
334 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD; 387 data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD;
335 dev_dbg(&data->client->dev, "slope_thres %x\n", data->slope_thres); 388 data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION;
389 ret = bmc150_accel_update_slope(data);
390 if (ret < 0)
391 return ret;
336 392
337 /* Set default as latched interrupts */ 393 /* Set default as latched interrupts */
338 ret = i2c_smbus_write_byte_data(data->client, 394 ret = i2c_smbus_write_byte_data(data->client,
@@ -348,155 +404,6 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data)
348 return 0; 404 return 0;
349} 405}
350 406
351static int bmc150_accel_setup_any_motion_interrupt(
352 struct bmc150_accel_data *data,
353 bool status)
354{
355 int ret;
356
357 /* Enable/Disable INT1 mapping */
358 ret = i2c_smbus_read_byte_data(data->client,
359 BMC150_ACCEL_REG_INT_MAP_0);
360 if (ret < 0) {
361 dev_err(&data->client->dev, "Error reading reg_int_map_0\n");
362 return ret;
363 }
364 if (status)
365 ret |= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
366 else
367 ret &= ~BMC150_ACCEL_INT_MAP_0_BIT_SLOPE;
368
369 ret = i2c_smbus_write_byte_data(data->client,
370 BMC150_ACCEL_REG_INT_MAP_0,
371 ret);
372 if (ret < 0) {
373 dev_err(&data->client->dev, "Error writing reg_int_map_0\n");
374 return ret;
375 }
376
377 if (status) {
378 /* Set slope duration (no of samples) */
379 ret = i2c_smbus_write_byte_data(data->client,
380 BMC150_ACCEL_REG_INT_5,
381 data->slope_dur);
382 if (ret < 0) {
383 dev_err(&data->client->dev, "Error write reg_int_5\n");
384 return ret;
385 }
386
387 /* Set slope thresholds */
388 ret = i2c_smbus_write_byte_data(data->client,
389 BMC150_ACCEL_REG_INT_6,
390 data->slope_thres);
391 if (ret < 0) {
392 dev_err(&data->client->dev, "Error write reg_int_6\n");
393 return ret;
394 }
395
396 /*
397 * New data interrupt is always non-latched,
398 * which will have higher priority, so no need
399 * to set latched mode, we will be flooded anyway with INTR
400 */
401 if (!data->dready_trigger_on) {
402 ret = i2c_smbus_write_byte_data(data->client,
403 BMC150_ACCEL_REG_INT_RST_LATCH,
404 BMC150_ACCEL_INT_MODE_LATCH_INT |
405 BMC150_ACCEL_INT_MODE_LATCH_RESET);
406 if (ret < 0) {
407 dev_err(&data->client->dev,
408 "Error writing reg_int_rst_latch\n");
409 return ret;
410 }
411 }
412
413 ret = i2c_smbus_write_byte_data(data->client,
414 BMC150_ACCEL_REG_INT_EN_0,
415 BMC150_ACCEL_INT_EN_BIT_SLP_X |
416 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
417 BMC150_ACCEL_INT_EN_BIT_SLP_Z);
418 } else
419 ret = i2c_smbus_write_byte_data(data->client,
420 BMC150_ACCEL_REG_INT_EN_0,
421 0);
422
423 if (ret < 0) {
424 dev_err(&data->client->dev, "Error writing reg_int_en_0\n");
425 return ret;
426 }
427
428 return 0;
429}
430
431static int bmc150_accel_setup_new_data_interrupt(struct bmc150_accel_data *data,
432 bool status)
433{
434 int ret;
435
436 /* Enable/Disable INT1 mapping */
437 ret = i2c_smbus_read_byte_data(data->client,
438 BMC150_ACCEL_REG_INT_MAP_1);
439 if (ret < 0) {
440 dev_err(&data->client->dev, "Error reading reg_int_map_1\n");
441 return ret;
442 }
443 if (status)
444 ret |= BMC150_ACCEL_INT_MAP_1_BIT_DATA;
445 else
446 ret &= ~BMC150_ACCEL_INT_MAP_1_BIT_DATA;
447
448 ret = i2c_smbus_write_byte_data(data->client,
449 BMC150_ACCEL_REG_INT_MAP_1,
450 ret);
451 if (ret < 0) {
452 dev_err(&data->client->dev, "Error writing reg_int_map_1\n");
453 return ret;
454 }
455
456 if (status) {
457 /*
458 * Set non latched mode interrupt and clear any latched
459 * interrupt
460 */
461 ret = i2c_smbus_write_byte_data(data->client,
462 BMC150_ACCEL_REG_INT_RST_LATCH,
463 BMC150_ACCEL_INT_MODE_NON_LATCH_INT |
464 BMC150_ACCEL_INT_MODE_LATCH_RESET);
465 if (ret < 0) {
466 dev_err(&data->client->dev,
467 "Error writing reg_int_rst_latch\n");
468 return ret;
469 }
470
471 ret = i2c_smbus_write_byte_data(data->client,
472 BMC150_ACCEL_REG_INT_EN_1,
473 BMC150_ACCEL_INT_EN_BIT_DATA_EN);
474
475 } else {
476 /* Restore default interrupt mode */
477 ret = i2c_smbus_write_byte_data(data->client,
478 BMC150_ACCEL_REG_INT_RST_LATCH,
479 BMC150_ACCEL_INT_MODE_LATCH_INT |
480 BMC150_ACCEL_INT_MODE_LATCH_RESET);
481 if (ret < 0) {
482 dev_err(&data->client->dev,
483 "Error writing reg_int_rst_latch\n");
484 return ret;
485 }
486
487 ret = i2c_smbus_write_byte_data(data->client,
488 BMC150_ACCEL_REG_INT_EN_1,
489 0);
490 }
491
492 if (ret < 0) {
493 dev_err(&data->client->dev, "Error writing reg_int_en_1\n");
494 return ret;
495 }
496
497 return 0;
498}
499
500static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val, 407static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val,
501 int *val2) 408 int *val2)
502{ 409{
@@ -554,6 +461,120 @@ static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on)
554} 461}
555#endif 462#endif
556 463
464static const struct bmc150_accel_interrupt_info {
465 u8 map_reg;
466 u8 map_bitmask;
467 u8 en_reg;
468 u8 en_bitmask;
469} bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = {
470 { /* data ready interrupt */
471 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
472 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA,
473 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
474 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN,
475 },
476 { /* motion interrupt */
477 .map_reg = BMC150_ACCEL_REG_INT_MAP_0,
478 .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE,
479 .en_reg = BMC150_ACCEL_REG_INT_EN_0,
480 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X |
481 BMC150_ACCEL_INT_EN_BIT_SLP_Y |
482 BMC150_ACCEL_INT_EN_BIT_SLP_Z
483 },
484 { /* fifo watermark interrupt */
485 .map_reg = BMC150_ACCEL_REG_INT_MAP_1,
486 .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM,
487 .en_reg = BMC150_ACCEL_REG_INT_EN_1,
488 .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN,
489 },
490};
491
492static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev,
493 struct bmc150_accel_data *data)
494{
495 int i;
496
497 for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++)
498 data->interrupts[i].info = &bmc150_accel_interrupts[i];
499}
500
501static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i,
502 bool state)
503{
504 struct bmc150_accel_interrupt *intr = &data->interrupts[i];
505 const struct bmc150_accel_interrupt_info *info = intr->info;
506 int ret;
507
508 if (state) {
509 if (atomic_inc_return(&intr->users) > 1)
510 return 0;
511 } else {
512 if (atomic_dec_return(&intr->users) > 0)
513 return 0;
514 }
515
516 /*
517 * We will expect the enable and disable to do operation in
518 * in reverse order. This will happen here anyway as our
519 * resume operation uses sync mode runtime pm calls, the
520 * suspend operation will be delayed by autosuspend delay
521 * So the disable operation will still happen in reverse of
522 * enable operation. When runtime pm is disabled the mode
523 * is always on so sequence doesn't matter
524 */
525 ret = bmc150_accel_set_power_state(data, state);
526 if (ret < 0)
527 return ret;
528
529 /* map the interrupt to the appropriate pins */
530 ret = i2c_smbus_read_byte_data(data->client, info->map_reg);
531 if (ret < 0) {
532 dev_err(&data->client->dev, "Error reading reg_int_map\n");
533 goto out_fix_power_state;
534 }
535 if (state)
536 ret |= info->map_bitmask;
537 else
538 ret &= ~info->map_bitmask;
539
540 ret = i2c_smbus_write_byte_data(data->client, info->map_reg,
541 ret);
542 if (ret < 0) {
543 dev_err(&data->client->dev, "Error writing reg_int_map\n");
544 goto out_fix_power_state;
545 }
546
547 /* enable/disable the interrupt */
548 ret = i2c_smbus_read_byte_data(data->client, info->en_reg);
549 if (ret < 0) {
550 dev_err(&data->client->dev, "Error reading reg_int_en\n");
551 goto out_fix_power_state;
552 }
553
554 if (state)
555 ret |= info->en_bitmask;
556 else
557 ret &= ~info->en_bitmask;
558
559 ret = i2c_smbus_write_byte_data(data->client, info->en_reg, ret);
560 if (ret < 0) {
561 dev_err(&data->client->dev, "Error writing reg_int_en\n");
562 goto out_fix_power_state;
563 }
564
565 if (state)
566 atomic_inc(&data->active_intr);
567 else
568 atomic_dec(&data->active_intr);
569
570 return 0;
571
572out_fix_power_state:
573 bmc150_accel_set_power_state(data, false);
574 return ret;
575}
576
577
557static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) 578static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val)
558{ 579{
559 int ret, i; 580 int ret, i;
@@ -732,7 +753,7 @@ static int bmc150_accel_read_event(struct iio_dev *indio_dev,
732 *val = data->slope_thres; 753 *val = data->slope_thres;
733 break; 754 break;
734 case IIO_EV_INFO_PERIOD: 755 case IIO_EV_INFO_PERIOD:
735 *val = data->slope_dur & BMC150_ACCEL_SLOPE_DUR_MASK; 756 *val = data->slope_dur;
736 break; 757 break;
737 default: 758 default:
738 return -EINVAL; 759 return -EINVAL;
@@ -755,11 +776,10 @@ static int bmc150_accel_write_event(struct iio_dev *indio_dev,
755 776
756 switch (info) { 777 switch (info) {
757 case IIO_EV_INFO_VALUE: 778 case IIO_EV_INFO_VALUE:
758 data->slope_thres = val; 779 data->slope_thres = val & 0xFF;
759 break; 780 break;
760 case IIO_EV_INFO_PERIOD: 781 case IIO_EV_INFO_PERIOD:
761 data->slope_dur &= ~BMC150_ACCEL_SLOPE_DUR_MASK; 782 data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK;
762 data->slope_dur |= val & BMC150_ACCEL_SLOPE_DUR_MASK;
763 break; 783 break;
764 default: 784 default:
765 return -EINVAL; 785 return -EINVAL;
@@ -788,36 +808,14 @@ static int bmc150_accel_write_event_config(struct iio_dev *indio_dev,
788 struct bmc150_accel_data *data = iio_priv(indio_dev); 808 struct bmc150_accel_data *data = iio_priv(indio_dev);
789 int ret; 809 int ret;
790 810
791 if (state && data->ev_enable_state) 811 if (state == data->ev_enable_state)
792 return 0; 812 return 0;
793 813
794 mutex_lock(&data->mutex); 814 mutex_lock(&data->mutex);
795 815
796 if (!state && data->motion_trigger_on) { 816 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION,
797 data->ev_enable_state = 0; 817 state);
798 mutex_unlock(&data->mutex);
799 return 0;
800 }
801
802 /*
803 * We will expect the enable and disable to do operation in
804 * in reverse order. This will happen here anyway as our
805 * resume operation uses sync mode runtime pm calls, the
806 * suspend operation will be delayed by autosuspend delay
807 * So the disable operation will still happen in reverse of
808 * enable operation. When runtime pm is disabled the mode
809 * is always on so sequence doesn't matter
810 */
811
812 ret = bmc150_accel_set_power_state(data, state);
813 if (ret < 0) {
814 mutex_unlock(&data->mutex);
815 return ret;
816 }
817
818 ret = bmc150_accel_setup_any_motion_interrupt(data, state);
819 if (ret < 0) { 818 if (ret < 0) {
820 bmc150_accel_set_power_state(data, false);
821 mutex_unlock(&data->mutex); 819 mutex_unlock(&data->mutex);
822 return ret; 820 return ret;
823 } 821 }
@@ -832,13 +830,224 @@ static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev,
832 struct iio_trigger *trig) 830 struct iio_trigger *trig)
833{ 831{
834 struct bmc150_accel_data *data = iio_priv(indio_dev); 832 struct bmc150_accel_data *data = iio_priv(indio_dev);
833 int i;
835 834
836 if (data->dready_trig != trig && data->motion_trig != trig) 835 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
837 return -EINVAL; 836 if (data->triggers[i].indio_trig == trig)
837 return 0;
838 }
839
840 return -EINVAL;
841}
842
843static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev,
844 struct device_attribute *attr,
845 char *buf)
846{
847 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
848 struct bmc150_accel_data *data = iio_priv(indio_dev);
849 int wm;
850
851 mutex_lock(&data->mutex);
852 wm = data->watermark;
853 mutex_unlock(&data->mutex);
854
855 return sprintf(buf, "%d\n", wm);
856}
857
858static ssize_t bmc150_accel_get_fifo_state(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
861{
862 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
863 struct bmc150_accel_data *data = iio_priv(indio_dev);
864 bool state;
865
866 mutex_lock(&data->mutex);
867 state = data->fifo_mode;
868 mutex_unlock(&data->mutex);
869
870 return sprintf(buf, "%d\n", state);
871}
872
873static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
874static IIO_CONST_ATTR(hwfifo_watermark_max,
875 __stringify(BMC150_ACCEL_FIFO_LENGTH));
876static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO,
877 bmc150_accel_get_fifo_state, NULL, 0);
878static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO,
879 bmc150_accel_get_fifo_watermark, NULL, 0);
880
881static const struct attribute *bmc150_accel_fifo_attributes[] = {
882 &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
883 &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
884 &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
885 &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
886 NULL,
887};
888
889static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val)
890{
891 struct bmc150_accel_data *data = iio_priv(indio_dev);
892
893 if (val > BMC150_ACCEL_FIFO_LENGTH)
894 val = BMC150_ACCEL_FIFO_LENGTH;
895
896 mutex_lock(&data->mutex);
897 data->watermark = val;
898 mutex_unlock(&data->mutex);
838 899
839 return 0; 900 return 0;
840} 901}
841 902
903/*
904 * We must read at least one full frame in one burst, otherwise the rest of the
905 * frame data is discarded.
906 */
907static int bmc150_accel_fifo_transfer(const struct i2c_client *client,
908 char *buffer, int samples)
909{
910 int sample_length = 3 * 2;
911 u8 reg_fifo_data = BMC150_ACCEL_REG_FIFO_DATA;
912 int ret = -EIO;
913
914 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
915 struct i2c_msg msg[2] = {
916 {
917 .addr = client->addr,
918 .flags = 0,
919 .buf = &reg_fifo_data,
920 .len = sizeof(reg_fifo_data),
921 },
922 {
923 .addr = client->addr,
924 .flags = I2C_M_RD,
925 .buf = (u8 *)buffer,
926 .len = samples * sample_length,
927 }
928 };
929
930 ret = i2c_transfer(client->adapter, msg, 2);
931 if (ret != 2)
932 ret = -EIO;
933 else
934 ret = 0;
935 } else {
936 int i, step = I2C_SMBUS_BLOCK_MAX / sample_length;
937
938 for (i = 0; i < samples * sample_length; i += step) {
939 ret = i2c_smbus_read_i2c_block_data(client,
940 reg_fifo_data, step,
941 &buffer[i]);
942 if (ret != step) {
943 ret = -EIO;
944 break;
945 }
946
947 ret = 0;
948 }
949 }
950
951 if (ret)
952 dev_err(&client->dev, "Error transferring data from fifo\n");
953
954 return ret;
955}
956
957static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
958 unsigned samples, bool irq)
959{
960 struct bmc150_accel_data *data = iio_priv(indio_dev);
961 int ret, i;
962 u8 count;
963 u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3];
964 int64_t tstamp;
965 uint64_t sample_period;
966 ret = i2c_smbus_read_byte_data(data->client,
967 BMC150_ACCEL_REG_FIFO_STATUS);
968 if (ret < 0) {
969 dev_err(&data->client->dev, "Error reading reg_fifo_status\n");
970 return ret;
971 }
972
973 count = ret & 0x7F;
974
975 if (!count)
976 return 0;
977
978 /*
979 * If we getting called from IRQ handler we know the stored timestamp is
980 * fairly accurate for the last stored sample. Otherwise, if we are
981 * called as a result of a read operation from userspace and hence
982 * before the watermark interrupt was triggered, take a timestamp
983 * now. We can fall anywhere in between two samples so the error in this
984 * case is at most one sample period.
985 */
986 if (!irq) {
987 data->old_timestamp = data->timestamp;
988 data->timestamp = iio_get_time_ns();
989 }
990
991 /*
992 * Approximate timestamps for each of the sample based on the sampling
993 * frequency, timestamp for last sample and number of samples.
994 *
995 * Note that we can't use the current bandwidth settings to compute the
996 * sample period because the sample rate varies with the device
997 * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That
998 * small variation adds when we store a large number of samples and
999 * creates significant jitter between the last and first samples in
1000 * different batches (e.g. 32ms vs 21ms).
1001 *
1002 * To avoid this issue we compute the actual sample period ourselves
1003 * based on the timestamp delta between the last two flush operations.
1004 */
1005 sample_period = (data->timestamp - data->old_timestamp);
1006 do_div(sample_period, count);
1007 tstamp = data->timestamp - (count - 1) * sample_period;
1008
1009 if (samples && count > samples)
1010 count = samples;
1011
1012 ret = bmc150_accel_fifo_transfer(data->client, (u8 *)buffer, count);
1013 if (ret)
1014 return ret;
1015
1016 /*
1017 * Ideally we want the IIO core to handle the demux when running in fifo
1018 * mode but not when running in triggered buffer mode. Unfortunately
1019 * this does not seem to be possible, so stick with driver demux for
1020 * now.
1021 */
1022 for (i = 0; i < count; i++) {
1023 u16 sample[8];
1024 int j, bit;
1025
1026 j = 0;
1027 for_each_set_bit(bit, indio_dev->active_scan_mask,
1028 indio_dev->masklength)
1029 memcpy(&sample[j++], &buffer[i * 3 + bit], 2);
1030
1031 iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp);
1032
1033 tstamp += sample_period;
1034 }
1035
1036 return count;
1037}
1038
1039static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples)
1040{
1041 struct bmc150_accel_data *data = iio_priv(indio_dev);
1042 int ret;
1043
1044 mutex_lock(&data->mutex);
1045 ret = __bmc150_accel_fifo_flush(indio_dev, samples, false);
1046 mutex_unlock(&data->mutex);
1047
1048 return ret;
1049}
1050
842static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 1051static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
843 "15.620000 31.260000 62.50000 125 250 500 1000 2000"); 1052 "15.620000 31.260000 62.50000 125 250 500 1000 2000");
844 1053
@@ -978,6 +1187,20 @@ static const struct iio_info bmc150_accel_info = {
978 .driver_module = THIS_MODULE, 1187 .driver_module = THIS_MODULE,
979}; 1188};
980 1189
1190static const struct iio_info bmc150_accel_info_fifo = {
1191 .attrs = &bmc150_accel_attrs_group,
1192 .read_raw = bmc150_accel_read_raw,
1193 .write_raw = bmc150_accel_write_raw,
1194 .read_event_value = bmc150_accel_read_event,
1195 .write_event_value = bmc150_accel_write_event,
1196 .write_event_config = bmc150_accel_write_event_config,
1197 .read_event_config = bmc150_accel_read_event_config,
1198 .validate_trigger = bmc150_accel_validate_trigger,
1199 .hwfifo_set_watermark = bmc150_accel_set_watermark,
1200 .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush,
1201 .driver_module = THIS_MODULE,
1202};
1203
981static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) 1204static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p)
982{ 1205{
983 struct iio_poll_func *pf = p; 1206 struct iio_poll_func *pf = p;
@@ -1008,12 +1231,12 @@ err_read:
1008 1231
1009static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) 1232static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1010{ 1233{
1011 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1234 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1012 struct bmc150_accel_data *data = iio_priv(indio_dev); 1235 struct bmc150_accel_data *data = t->data;
1013 int ret; 1236 int ret;
1014 1237
1015 /* new data interrupts don't need ack */ 1238 /* new data interrupts don't need ack */
1016 if (data->dready_trigger_on) 1239 if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY])
1017 return 0; 1240 return 0;
1018 1241
1019 mutex_lock(&data->mutex); 1242 mutex_lock(&data->mutex);
@@ -1032,43 +1255,35 @@ static int bmc150_accel_trig_try_reen(struct iio_trigger *trig)
1032 return 0; 1255 return 0;
1033} 1256}
1034 1257
1035static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig, 1258static int bmc150_accel_trigger_set_state(struct iio_trigger *trig,
1036 bool state) 1259 bool state)
1037{ 1260{
1038 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1261 struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig);
1039 struct bmc150_accel_data *data = iio_priv(indio_dev); 1262 struct bmc150_accel_data *data = t->data;
1040 int ret; 1263 int ret;
1041 1264
1042 mutex_lock(&data->mutex); 1265 mutex_lock(&data->mutex);
1043 1266
1044 if (!state && data->ev_enable_state && data->motion_trigger_on) { 1267 if (t->enabled == state) {
1045 data->motion_trigger_on = false;
1046 mutex_unlock(&data->mutex); 1268 mutex_unlock(&data->mutex);
1047 return 0; 1269 return 0;
1048 } 1270 }
1049 1271
1050 /* 1272 if (t->setup) {
1051 * Refer to comment in bmc150_accel_write_event_config for 1273 ret = t->setup(t, state);
1052 * enable/disable operation order 1274 if (ret < 0) {
1053 */ 1275 mutex_unlock(&data->mutex);
1054 ret = bmc150_accel_set_power_state(data, state); 1276 return ret;
1055 if (ret < 0) { 1277 }
1056 mutex_unlock(&data->mutex);
1057 return ret;
1058 } 1278 }
1059 if (data->motion_trig == trig) 1279
1060 ret = bmc150_accel_setup_any_motion_interrupt(data, state); 1280 ret = bmc150_accel_set_interrupt(data, t->intr, state);
1061 else
1062 ret = bmc150_accel_setup_new_data_interrupt(data, state);
1063 if (ret < 0) { 1281 if (ret < 0) {
1064 bmc150_accel_set_power_state(data, false);
1065 mutex_unlock(&data->mutex); 1282 mutex_unlock(&data->mutex);
1066 return ret; 1283 return ret;
1067 } 1284 }
1068 if (data->motion_trig == trig) 1285
1069 data->motion_trigger_on = state; 1286 t->enabled = state;
1070 else
1071 data->dready_trigger_on = state;
1072 1287
1073 mutex_unlock(&data->mutex); 1288 mutex_unlock(&data->mutex);
1074 1289
@@ -1076,23 +1291,22 @@ static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig,
1076} 1291}
1077 1292
1078static const struct iio_trigger_ops bmc150_accel_trigger_ops = { 1293static const struct iio_trigger_ops bmc150_accel_trigger_ops = {
1079 .set_trigger_state = bmc150_accel_data_rdy_trigger_set_state, 1294 .set_trigger_state = bmc150_accel_trigger_set_state,
1080 .try_reenable = bmc150_accel_trig_try_reen, 1295 .try_reenable = bmc150_accel_trig_try_reen,
1081 .owner = THIS_MODULE, 1296 .owner = THIS_MODULE,
1082}; 1297};
1083 1298
1084static irqreturn_t bmc150_accel_event_handler(int irq, void *private) 1299static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev)
1085{ 1300{
1086 struct iio_dev *indio_dev = private;
1087 struct bmc150_accel_data *data = iio_priv(indio_dev); 1301 struct bmc150_accel_data *data = iio_priv(indio_dev);
1088 int ret;
1089 int dir; 1302 int dir;
1303 int ret;
1090 1304
1091 ret = i2c_smbus_read_byte_data(data->client, 1305 ret = i2c_smbus_read_byte_data(data->client,
1092 BMC150_ACCEL_REG_INT_STATUS_2); 1306 BMC150_ACCEL_REG_INT_STATUS_2);
1093 if (ret < 0) { 1307 if (ret < 0) {
1094 dev_err(&data->client->dev, "Error reading reg_int_status_2\n"); 1308 dev_err(&data->client->dev, "Error reading reg_int_status_2\n");
1095 goto ack_intr_status; 1309 return ret;
1096 } 1310 }
1097 1311
1098 if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN) 1312 if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN)
@@ -1121,32 +1335,73 @@ static irqreturn_t bmc150_accel_event_handler(int irq, void *private)
1121 IIO_EV_TYPE_ROC, 1335 IIO_EV_TYPE_ROC,
1122 dir), 1336 dir),
1123 data->timestamp); 1337 data->timestamp);
1124ack_intr_status: 1338 return ret;
1125 if (!data->dready_trigger_on) 1339}
1340
1341static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private)
1342{
1343 struct iio_dev *indio_dev = private;
1344 struct bmc150_accel_data *data = iio_priv(indio_dev);
1345 bool ack = false;
1346 int ret;
1347
1348 mutex_lock(&data->mutex);
1349
1350 if (data->fifo_mode) {
1351 ret = __bmc150_accel_fifo_flush(indio_dev,
1352 BMC150_ACCEL_FIFO_LENGTH, true);
1353 if (ret > 0)
1354 ack = true;
1355 }
1356
1357 if (data->ev_enable_state) {
1358 ret = bmc150_accel_handle_roc_event(indio_dev);
1359 if (ret > 0)
1360 ack = true;
1361 }
1362
1363 if (ack) {
1126 ret = i2c_smbus_write_byte_data(data->client, 1364 ret = i2c_smbus_write_byte_data(data->client,
1127 BMC150_ACCEL_REG_INT_RST_LATCH, 1365 BMC150_ACCEL_REG_INT_RST_LATCH,
1128 BMC150_ACCEL_INT_MODE_LATCH_INT | 1366 BMC150_ACCEL_INT_MODE_LATCH_INT |
1129 BMC150_ACCEL_INT_MODE_LATCH_RESET); 1367 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1368 if (ret)
1369 dev_err(&data->client->dev, "Error writing reg_int_rst_latch\n");
1370 ret = IRQ_HANDLED;
1371 } else {
1372 ret = IRQ_NONE;
1373 }
1130 1374
1131 return IRQ_HANDLED; 1375 mutex_unlock(&data->mutex);
1376
1377 return ret;
1132} 1378}
1133 1379
1134static irqreturn_t bmc150_accel_data_rdy_trig_poll(int irq, void *private) 1380static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
1135{ 1381{
1136 struct iio_dev *indio_dev = private; 1382 struct iio_dev *indio_dev = private;
1137 struct bmc150_accel_data *data = iio_priv(indio_dev); 1383 struct bmc150_accel_data *data = iio_priv(indio_dev);
1384 bool ack = false;
1385 int i;
1138 1386
1387 data->old_timestamp = data->timestamp;
1139 data->timestamp = iio_get_time_ns(); 1388 data->timestamp = iio_get_time_ns();
1140 1389
1141 if (data->dready_trigger_on) 1390 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1142 iio_trigger_poll(data->dready_trig); 1391 if (data->triggers[i].enabled) {
1143 else if (data->motion_trigger_on) 1392 iio_trigger_poll(data->triggers[i].indio_trig);
1144 iio_trigger_poll(data->motion_trig); 1393 ack = true;
1394 break;
1395 }
1396 }
1145 1397
1146 if (data->ev_enable_state) 1398 if (data->ev_enable_state || data->fifo_mode)
1147 return IRQ_WAKE_THREAD; 1399 return IRQ_WAKE_THREAD;
1148 else 1400
1401 if (ack)
1149 return IRQ_HANDLED; 1402 return IRQ_HANDLED;
1403
1404 return IRQ_NONE;
1150} 1405}
1151 1406
1152static const char *bmc150_accel_match_acpi_device(struct device *dev, int *data) 1407static const char *bmc150_accel_match_acpi_device(struct device *dev, int *data)
@@ -1176,23 +1431,171 @@ static int bmc150_accel_gpio_probe(struct i2c_client *client,
1176 dev = &client->dev; 1431 dev = &client->dev;
1177 1432
1178 /* data ready gpio interrupt pin */ 1433 /* data ready gpio interrupt pin */
1179 gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0); 1434 gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0, GPIOD_IN);
1180 if (IS_ERR(gpio)) { 1435 if (IS_ERR(gpio)) {
1181 dev_err(dev, "Failed: gpio get index\n"); 1436 dev_err(dev, "Failed: gpio get index\n");
1182 return PTR_ERR(gpio); 1437 return PTR_ERR(gpio);
1183 } 1438 }
1184 1439
1185 ret = gpiod_direction_input(gpio); 1440 ret = gpiod_to_irq(gpio);
1441
1442 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
1443
1444 return ret;
1445}
1446
1447static const struct {
1448 int intr;
1449 const char *name;
1450 int (*setup)(struct bmc150_accel_trigger *t, bool state);
1451} bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = {
1452 {
1453 .intr = 0,
1454 .name = "%s-dev%d",
1455 },
1456 {
1457 .intr = 1,
1458 .name = "%s-any-motion-dev%d",
1459 .setup = bmc150_accel_any_motion_setup,
1460 },
1461};
1462
1463static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data,
1464 int from)
1465{
1466 int i;
1467
1468 for (i = from; i >= 0; i++) {
1469 if (data->triggers[i].indio_trig) {
1470 iio_trigger_unregister(data->triggers[i].indio_trig);
1471 data->triggers[i].indio_trig = NULL;
1472 }
1473 }
1474}
1475
1476static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev,
1477 struct bmc150_accel_data *data)
1478{
1479 int i, ret;
1480
1481 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1482 struct bmc150_accel_trigger *t = &data->triggers[i];
1483
1484 t->indio_trig = devm_iio_trigger_alloc(&data->client->dev,
1485 bmc150_accel_triggers[i].name,
1486 indio_dev->name,
1487 indio_dev->id);
1488 if (!t->indio_trig) {
1489 ret = -ENOMEM;
1490 break;
1491 }
1492
1493 t->indio_trig->dev.parent = &data->client->dev;
1494 t->indio_trig->ops = &bmc150_accel_trigger_ops;
1495 t->intr = bmc150_accel_triggers[i].intr;
1496 t->data = data;
1497 t->setup = bmc150_accel_triggers[i].setup;
1498 iio_trigger_set_drvdata(t->indio_trig, t);
1499
1500 ret = iio_trigger_register(t->indio_trig);
1501 if (ret)
1502 break;
1503 }
1504
1186 if (ret) 1505 if (ret)
1506 bmc150_accel_unregister_triggers(data, i - 1);
1507
1508 return ret;
1509}
1510
1511#define BMC150_ACCEL_FIFO_MODE_STREAM 0x80
1512#define BMC150_ACCEL_FIFO_MODE_FIFO 0x40
1513#define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00
1514
1515static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data)
1516{
1517 u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1;
1518 int ret;
1519
1520 ret = i2c_smbus_write_byte_data(data->client, reg, data->fifo_mode);
1521 if (ret < 0) {
1522 dev_err(&data->client->dev, "Error writing reg_fifo_config1\n");
1187 return ret; 1523 return ret;
1524 }
1188 1525
1189 ret = gpiod_to_irq(gpio); 1526 if (!data->fifo_mode)
1527 return 0;
1190 1528
1191 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1529 ret = i2c_smbus_write_byte_data(data->client,
1530 BMC150_ACCEL_REG_FIFO_CONFIG0,
1531 data->watermark);
1532 if (ret < 0)
1533 dev_err(&data->client->dev, "Error writing reg_fifo_config0\n");
1192 1534
1193 return ret; 1535 return ret;
1194} 1536}
1195 1537
1538static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev)
1539{
1540 struct bmc150_accel_data *data = iio_priv(indio_dev);
1541 int ret = 0;
1542
1543 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1544 return iio_triggered_buffer_postenable(indio_dev);
1545
1546 mutex_lock(&data->mutex);
1547
1548 if (!data->watermark)
1549 goto out;
1550
1551 ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1552 true);
1553 if (ret)
1554 goto out;
1555
1556 data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO;
1557
1558 ret = bmc150_accel_fifo_set_mode(data);
1559 if (ret) {
1560 data->fifo_mode = 0;
1561 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK,
1562 false);
1563 }
1564
1565out:
1566 mutex_unlock(&data->mutex);
1567
1568 return ret;
1569}
1570
1571static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev)
1572{
1573 struct bmc150_accel_data *data = iio_priv(indio_dev);
1574
1575 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1576 return iio_triggered_buffer_predisable(indio_dev);
1577
1578 mutex_lock(&data->mutex);
1579
1580 if (!data->fifo_mode)
1581 goto out;
1582
1583 bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false);
1584 __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false);
1585 data->fifo_mode = 0;
1586 bmc150_accel_fifo_set_mode(data);
1587
1588out:
1589 mutex_unlock(&data->mutex);
1590
1591 return 0;
1592}
1593
1594static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = {
1595 .postenable = bmc150_accel_buffer_postenable,
1596 .predisable = bmc150_accel_buffer_predisable,
1597};
1598
1196static int bmc150_accel_probe(struct i2c_client *client, 1599static int bmc150_accel_probe(struct i2c_client *client,
1197 const struct i2c_device_id *id) 1600 const struct i2c_device_id *id)
1198{ 1601{
@@ -1239,53 +1642,51 @@ static int bmc150_accel_probe(struct i2c_client *client,
1239 if (client->irq >= 0) { 1642 if (client->irq >= 0) {
1240 ret = devm_request_threaded_irq( 1643 ret = devm_request_threaded_irq(
1241 &client->dev, client->irq, 1644 &client->dev, client->irq,
1242 bmc150_accel_data_rdy_trig_poll, 1645 bmc150_accel_irq_handler,
1243 bmc150_accel_event_handler, 1646 bmc150_accel_irq_thread_handler,
1244 IRQF_TRIGGER_RISING, 1647 IRQF_TRIGGER_RISING,
1245 BMC150_ACCEL_IRQ_NAME, 1648 BMC150_ACCEL_IRQ_NAME,
1246 indio_dev); 1649 indio_dev);
1247 if (ret) 1650 if (ret)
1248 return ret; 1651 return ret;
1249 1652
1250 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 1653 /*
1251 "%s-dev%d", 1654 * Set latched mode interrupt. While certain interrupts are
1252 indio_dev->name, 1655 * non-latched regardless of this settings (e.g. new data) we
1253 indio_dev->id); 1656 * want to use latch mode when we can to prevent interrupt
1254 if (!data->dready_trig) 1657 * flooding.
1255 return -ENOMEM; 1658 */
1256 1659 ret = i2c_smbus_write_byte_data(data->client,
1257 data->motion_trig = devm_iio_trigger_alloc(&client->dev, 1660 BMC150_ACCEL_REG_INT_RST_LATCH,
1258 "%s-any-motion-dev%d", 1661 BMC150_ACCEL_INT_MODE_LATCH_RESET);
1259 indio_dev->name, 1662 if (ret < 0) {
1260 indio_dev->id); 1663 dev_err(&data->client->dev, "Error writing reg_int_rst_latch\n");
1261 if (!data->motion_trig)
1262 return -ENOMEM;
1263
1264 data->dready_trig->dev.parent = &client->dev;
1265 data->dready_trig->ops = &bmc150_accel_trigger_ops;
1266 iio_trigger_set_drvdata(data->dready_trig, indio_dev);
1267 ret = iio_trigger_register(data->dready_trig);
1268 if (ret)
1269 return ret; 1664 return ret;
1270
1271 data->motion_trig->dev.parent = &client->dev;
1272 data->motion_trig->ops = &bmc150_accel_trigger_ops;
1273 iio_trigger_set_drvdata(data->motion_trig, indio_dev);
1274 ret = iio_trigger_register(data->motion_trig);
1275 if (ret) {
1276 data->motion_trig = NULL;
1277 goto err_trigger_unregister;
1278 } 1665 }
1279 1666
1667 bmc150_accel_interrupts_setup(indio_dev, data);
1668
1669 ret = bmc150_accel_triggers_setup(indio_dev, data);
1670 if (ret)
1671 return ret;
1672
1280 ret = iio_triggered_buffer_setup(indio_dev, 1673 ret = iio_triggered_buffer_setup(indio_dev,
1281 &iio_pollfunc_store_time, 1674 &iio_pollfunc_store_time,
1282 bmc150_accel_trigger_handler, 1675 bmc150_accel_trigger_handler,
1283 NULL); 1676 &bmc150_accel_buffer_ops);
1284 if (ret < 0) { 1677 if (ret < 0) {
1285 dev_err(&client->dev, 1678 dev_err(&client->dev,
1286 "Failed: iio triggered buffer setup\n"); 1679 "Failed: iio triggered buffer setup\n");
1287 goto err_trigger_unregister; 1680 goto err_trigger_unregister;
1288 } 1681 }
1682
1683 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) ||
1684 i2c_check_functionality(client->adapter,
1685 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
1686 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1687 indio_dev->info = &bmc150_accel_info_fifo;
1688 indio_dev->buffer->attrs = bmc150_accel_fifo_attributes;
1689 }
1289 } 1690 }
1290 1691
1291 ret = iio_device_register(indio_dev); 1692 ret = iio_device_register(indio_dev);
@@ -1308,13 +1709,10 @@ static int bmc150_accel_probe(struct i2c_client *client,
1308err_iio_unregister: 1709err_iio_unregister:
1309 iio_device_unregister(indio_dev); 1710 iio_device_unregister(indio_dev);
1310err_buffer_cleanup: 1711err_buffer_cleanup:
1311 if (data->dready_trig) 1712 if (indio_dev->pollfunc)
1312 iio_triggered_buffer_cleanup(indio_dev); 1713 iio_triggered_buffer_cleanup(indio_dev);
1313err_trigger_unregister: 1714err_trigger_unregister:
1314 if (data->dready_trig) 1715 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1315 iio_trigger_unregister(data->dready_trig);
1316 if (data->motion_trig)
1317 iio_trigger_unregister(data->motion_trig);
1318 1716
1319 return ret; 1717 return ret;
1320} 1718}
@@ -1330,11 +1728,7 @@ static int bmc150_accel_remove(struct i2c_client *client)
1330 1728
1331 iio_device_unregister(indio_dev); 1729 iio_device_unregister(indio_dev);
1332 1730
1333 if (data->dready_trig) { 1731 bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1);
1334 iio_triggered_buffer_cleanup(indio_dev);
1335 iio_trigger_unregister(data->dready_trig);
1336 iio_trigger_unregister(data->motion_trig);
1337 }
1338 1732
1339 mutex_lock(&data->mutex); 1733 mutex_lock(&data->mutex);
1340 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0); 1734 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0);
@@ -1362,9 +1756,9 @@ static int bmc150_accel_resume(struct device *dev)
1362 struct bmc150_accel_data *data = iio_priv(indio_dev); 1756 struct bmc150_accel_data *data = iio_priv(indio_dev);
1363 1757
1364 mutex_lock(&data->mutex); 1758 mutex_lock(&data->mutex);
1365 if (data->dready_trigger_on || data->motion_trigger_on || 1759 if (atomic_read(&data->active_intr))
1366 data->ev_enable_state)
1367 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 1760 bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1761 bmc150_accel_fifo_set_mode(data);
1368 mutex_unlock(&data->mutex); 1762 mutex_unlock(&data->mutex);
1369 1763
1370 return 0; 1764 return 0;
@@ -1398,6 +1792,9 @@ static int bmc150_accel_runtime_resume(struct device *dev)
1398 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); 1792 ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0);
1399 if (ret < 0) 1793 if (ret < 0)
1400 return ret; 1794 return ret;
1795 ret = bmc150_accel_fifo_set_mode(data);
1796 if (ret < 0)
1797 return ret;
1401 1798
1402 sleep_val = bmc150_accel_get_startup_times(data); 1799 sleep_val = bmc150_accel_get_startup_times(data);
1403 if (sleep_val < 20) 1800 if (sleep_val < 20)
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index 1a6379525fa4..51da3692d561 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -1169,16 +1169,12 @@ static int kxcjk1013_gpio_probe(struct i2c_client *client,
1169 dev = &client->dev; 1169 dev = &client->dev;
1170 1170
1171 /* data ready gpio interrupt pin */ 1171 /* data ready gpio interrupt pin */
1172 gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0); 1172 gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0, GPIOD_IN);
1173 if (IS_ERR(gpio)) { 1173 if (IS_ERR(gpio)) {
1174 dev_err(dev, "acpi gpio get index failed\n"); 1174 dev_err(dev, "acpi gpio get index failed\n");
1175 return PTR_ERR(gpio); 1175 return PTR_ERR(gpio);
1176 } 1176 }
1177 1177
1178 ret = gpiod_direction_input(gpio);
1179 if (ret)
1180 return ret;
1181
1182 ret = gpiod_to_irq(gpio); 1178 ret = gpiod_to_irq(gpio);
1183 1179
1184 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1180 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
diff --git a/drivers/iio/accel/mma9551.c b/drivers/iio/accel/mma9551.c
index 46c38351c6a3..7db7cc0bf362 100644
--- a/drivers/iio/accel/mma9551.c
+++ b/drivers/iio/accel/mma9551.c
@@ -418,17 +418,18 @@ static int mma9551_gpio_probe(struct iio_dev *indio_dev)
418 struct device *dev = &data->client->dev; 418 struct device *dev = &data->client->dev;
419 419
420 for (i = 0; i < MMA9551_GPIO_COUNT; i++) { 420 for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
421 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i); 421 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i,
422 GPIOD_IN);
422 if (IS_ERR(gpio)) { 423 if (IS_ERR(gpio)) {
423 dev_err(dev, "acpi gpio get index failed\n"); 424 dev_err(dev, "acpi gpio get index failed\n");
424 return PTR_ERR(gpio); 425 return PTR_ERR(gpio);
425 } 426 }
426 427
427 ret = gpiod_direction_input(gpio); 428 ret = gpiod_to_irq(gpio);
428 if (ret) 429 if (ret < 0)
429 return ret; 430 return ret;
430 431
431 data->irqs[i] = gpiod_to_irq(gpio); 432 data->irqs[i] = ret;
432 ret = devm_request_threaded_irq(dev, data->irqs[i], 433 ret = devm_request_threaded_irq(dev, data->irqs[i],
433 NULL, mma9551_event_handler, 434 NULL, mma9551_event_handler,
434 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 435 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
diff --git a/drivers/iio/accel/mma9553.c b/drivers/iio/accel/mma9553.c
index d23ebf192f63..2df1af7d43fc 100644
--- a/drivers/iio/accel/mma9553.c
+++ b/drivers/iio/accel/mma9553.c
@@ -1109,16 +1109,12 @@ static int mma9553_gpio_probe(struct i2c_client *client)
1109 dev = &client->dev; 1109 dev = &client->dev;
1110 1110
1111 /* data ready gpio interrupt pin */ 1111 /* data ready gpio interrupt pin */
1112 gpio = devm_gpiod_get_index(dev, MMA9553_GPIO_NAME, 0); 1112 gpio = devm_gpiod_get_index(dev, MMA9553_GPIO_NAME, 0, GPIOD_IN);
1113 if (IS_ERR(gpio)) { 1113 if (IS_ERR(gpio)) {
1114 dev_err(dev, "acpi gpio get index failed\n"); 1114 dev_err(dev, "acpi gpio get index failed\n");
1115 return PTR_ERR(gpio); 1115 return PTR_ERR(gpio);
1116 } 1116 }
1117 1117
1118 ret = gpiod_direction_input(gpio);
1119 if (ret)
1120 return ret;
1121
1122 ret = gpiod_to_irq(gpio); 1118 ret = gpiod_to_irq(gpio);
1123 1119
1124 dev_dbg(dev, "gpio resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1120 dev_dbg(dev, "gpio resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
diff --git a/drivers/iio/accel/st_accel.h b/drivers/iio/accel/st_accel.h
index fa9646034305..7ee9724b1428 100644
--- a/drivers/iio/accel/st_accel.h
+++ b/drivers/iio/accel/st_accel.h
@@ -14,6 +14,7 @@
14#include <linux/types.h> 14#include <linux/types.h>
15#include <linux/iio/common/st_sensors.h> 15#include <linux/iio/common/st_sensors.h>
16 16
17#define LIS3LV02DL_ACCEL_DEV_NAME "lis3lv02dl_accel"
17#define LSM303DLHC_ACCEL_DEV_NAME "lsm303dlhc_accel" 18#define LSM303DLHC_ACCEL_DEV_NAME "lsm303dlhc_accel"
18#define LIS3DH_ACCEL_DEV_NAME "lis3dh" 19#define LIS3DH_ACCEL_DEV_NAME "lis3dh"
19#define LSM330D_ACCEL_DEV_NAME "lsm330d_accel" 20#define LSM330D_ACCEL_DEV_NAME "lsm330d_accel"
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index 53f32629283a..58d1d13d552a 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -129,6 +129,30 @@
129#define ST_ACCEL_3_IG1_EN_MASK 0x08 129#define ST_ACCEL_3_IG1_EN_MASK 0x08
130#define ST_ACCEL_3_MULTIREAD_BIT false 130#define ST_ACCEL_3_MULTIREAD_BIT false
131 131
132/* CUSTOM VALUES FOR SENSOR 4 */
133#define ST_ACCEL_4_WAI_EXP 0x3a
134#define ST_ACCEL_4_ODR_ADDR 0x20
135#define ST_ACCEL_4_ODR_MASK 0x30 /* DF1 and DF0 */
136#define ST_ACCEL_4_ODR_AVL_40HZ_VAL 0x00
137#define ST_ACCEL_4_ODR_AVL_160HZ_VAL 0x01
138#define ST_ACCEL_4_ODR_AVL_640HZ_VAL 0x02
139#define ST_ACCEL_4_ODR_AVL_2560HZ_VAL 0x03
140#define ST_ACCEL_4_PW_ADDR 0x20
141#define ST_ACCEL_4_PW_MASK 0xc0
142#define ST_ACCEL_4_FS_ADDR 0x21
143#define ST_ACCEL_4_FS_MASK 0x80
144#define ST_ACCEL_4_FS_AVL_2_VAL 0X00
145#define ST_ACCEL_4_FS_AVL_6_VAL 0X01
146#define ST_ACCEL_4_FS_AVL_2_GAIN IIO_G_TO_M_S_2(1024)
147#define ST_ACCEL_4_FS_AVL_6_GAIN IIO_G_TO_M_S_2(340)
148#define ST_ACCEL_4_BDU_ADDR 0x21
149#define ST_ACCEL_4_BDU_MASK 0x40
150#define ST_ACCEL_4_DRDY_IRQ_ADDR 0x21
151#define ST_ACCEL_4_DRDY_IRQ_INT1_MASK 0x04
152#define ST_ACCEL_4_IG1_EN_ADDR 0x21
153#define ST_ACCEL_4_IG1_EN_MASK 0x08
154#define ST_ACCEL_4_MULTIREAD_BIT true
155
132static const struct iio_chan_spec st_accel_12bit_channels[] = { 156static const struct iio_chan_spec st_accel_12bit_channels[] = {
133 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 157 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
134 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 158 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
@@ -373,6 +397,63 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
373 .multi_read_bit = ST_ACCEL_3_MULTIREAD_BIT, 397 .multi_read_bit = ST_ACCEL_3_MULTIREAD_BIT,
374 .bootime = 2, 398 .bootime = 2,
375 }, 399 },
400 {
401 .wai = ST_ACCEL_4_WAI_EXP,
402 .sensors_supported = {
403 [0] = LIS3LV02DL_ACCEL_DEV_NAME,
404 },
405 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
406 .odr = {
407 .addr = ST_ACCEL_4_ODR_ADDR,
408 .mask = ST_ACCEL_4_ODR_MASK,
409 .odr_avl = {
410 { 40, ST_ACCEL_4_ODR_AVL_40HZ_VAL },
411 { 160, ST_ACCEL_4_ODR_AVL_160HZ_VAL, },
412 { 640, ST_ACCEL_4_ODR_AVL_640HZ_VAL, },
413 { 2560, ST_ACCEL_4_ODR_AVL_2560HZ_VAL, },
414 },
415 },
416 .pw = {
417 .addr = ST_ACCEL_4_PW_ADDR,
418 .mask = ST_ACCEL_4_PW_MASK,
419 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
420 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
421 },
422 .enable_axis = {
423 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
424 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
425 },
426 .fs = {
427 .addr = ST_ACCEL_4_FS_ADDR,
428 .mask = ST_ACCEL_4_FS_MASK,
429 .fs_avl = {
430 [0] = {
431 .num = ST_ACCEL_FS_AVL_2G,
432 .value = ST_ACCEL_4_FS_AVL_2_VAL,
433 .gain = ST_ACCEL_4_FS_AVL_2_GAIN,
434 },
435 [1] = {
436 .num = ST_ACCEL_FS_AVL_6G,
437 .value = ST_ACCEL_4_FS_AVL_6_VAL,
438 .gain = ST_ACCEL_4_FS_AVL_6_GAIN,
439 },
440 },
441 },
442 .bdu = {
443 .addr = ST_ACCEL_4_BDU_ADDR,
444 .mask = ST_ACCEL_4_BDU_MASK,
445 },
446 .drdy_irq = {
447 .addr = ST_ACCEL_4_DRDY_IRQ_ADDR,
448 .mask_int1 = ST_ACCEL_4_DRDY_IRQ_INT1_MASK,
449 .ig1 = {
450 .en_addr = ST_ACCEL_4_IG1_EN_ADDR,
451 .en_mask = ST_ACCEL_4_IG1_EN_MASK,
452 },
453 },
454 .multi_read_bit = ST_ACCEL_4_MULTIREAD_BIT,
455 .bootime = 2, /* guess */
456 },
376}; 457};
377 458
378static int st_accel_read_raw(struct iio_dev *indio_dev, 459static int st_accel_read_raw(struct iio_dev *indio_dev,
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index c7246bdd30b9..6b720c190b2d 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -21,6 +21,10 @@
21#ifdef CONFIG_OF 21#ifdef CONFIG_OF
22static const struct of_device_id st_accel_of_match[] = { 22static const struct of_device_id st_accel_of_match[] = {
23 { 23 {
24 .compatible = "st,lis3lv02dl-accel",
25 .data = LIS3LV02DL_ACCEL_DEV_NAME,
26 },
27 {
24 .compatible = "st,lsm303dlh-accel", 28 .compatible = "st,lsm303dlh-accel",
25 .data = LSM303DLH_ACCEL_DEV_NAME, 29 .data = LSM303DLH_ACCEL_DEV_NAME,
26 }, 30 },
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index d82af14986ba..e36a73e7c3a8 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -196,10 +196,11 @@ config MAX1363
196 data via the iio dev interface. 196 data via the iio dev interface.
197 197
198config MCP320X 198config MCP320X
199 tristate "Microchip Technology MCP3204/08" 199 tristate "Microchip Technology MCP3x01/02/04/08"
200 depends on SPI 200 depends on SPI
201 help 201 help
202 Say yes here to build support for Microchip Technology's MCP3204 or 202 Say yes here to build support for Microchip Technology's
203 MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204 or
203 MCP3208 analog to digital converter. 204 MCP3208 analog to digital converter.
204 205
205 This driver can also be built as a module. If so, the module will be 206 This driver can also be built as a module. If so, the module will be
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
index 4dddeabdfbb0..b84922a4b32e 100644
--- a/drivers/iio/adc/ad7793.c
+++ b/drivers/iio/adc/ad7793.c
@@ -861,5 +861,5 @@ static struct spi_driver ad7793_driver = {
861module_spi_driver(ad7793_driver); 861module_spi_driver(ad7793_driver);
862 862
863MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 863MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
864MODULE_DESCRIPTION("Analog Devices AD7793 and simialr ADCs"); 864MODULE_DESCRIPTION("Analog Devices AD7793 and similar ADCs");
865MODULE_LICENSE("GPL v2"); 865MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
index e63b8e76d4c3..56292ae4538d 100644
--- a/drivers/iio/adc/vf610_adc.c
+++ b/drivers/iio/adc/vf610_adc.c
@@ -275,7 +275,6 @@ static void vf610_adc_cfg_post_set(struct vf610_adc *info)
275static void vf610_adc_calibration(struct vf610_adc *info) 275static void vf610_adc_calibration(struct vf610_adc *info)
276{ 276{
277 int adc_gc, hc_cfg; 277 int adc_gc, hc_cfg;
278 int timeout;
279 278
280 if (!info->adc_feature.calibration) 279 if (!info->adc_feature.calibration)
281 return; 280 return;
@@ -287,9 +286,7 @@ static void vf610_adc_calibration(struct vf610_adc *info)
287 adc_gc = readl(info->regs + VF610_REG_ADC_GC); 286 adc_gc = readl(info->regs + VF610_REG_ADC_GC);
288 writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC); 287 writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
289 288
290 timeout = wait_for_completion_timeout 289 if (!wait_for_completion_timeout(&info->completion, VF610_ADC_TIMEOUT))
291 (&info->completion, VF610_ADC_TIMEOUT);
292 if (timeout == 0)
293 dev_err(info->dev, "Timeout for adc calibration\n"); 290 dev_err(info->dev, "Timeout for adc calibration\n");
294 291
295 adc_gc = readl(info->regs + VF610_REG_ADC_GS); 292 adc_gc = readl(info->regs + VF610_REG_ADC_GS);
diff --git a/drivers/iio/common/ssp_sensors/ssp_dev.c b/drivers/iio/common/ssp_sensors/ssp_dev.c
index 55a90082a29b..9a40097e7cf8 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -437,7 +437,7 @@ int ssp_queue_ssp_refresh_task(struct ssp_data *data, unsigned int delay)
437} 437}
438 438
439#ifdef CONFIG_OF 439#ifdef CONFIG_OF
440static struct of_device_id ssp_of_match[] = { 440static const struct of_device_id ssp_of_match[] = {
441 { 441 {
442 .compatible = "samsung,sensorhub-rinato", 442 .compatible = "samsung,sensorhub-rinato",
443 .data = &ssp_rinato_info, 443 .data = &ssp_rinato_info,
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index 2236ea22f98a..13471a76e5bf 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -143,11 +143,16 @@ config AD7303
143 ad7303. 143 ad7303.
144 144
145config MAX517 145config MAX517
146 tristate "Maxim MAX517/518/519 DAC driver" 146 tristate "Maxim MAX517/518/519/520/521 DAC driver"
147 depends on I2C 147 depends on I2C
148 help 148 help
149 If you say yes here you get support for the Maxim chips MAX517, 149 If you say yes here you get support for the following Maxim chips
150 MAX518 and MAX519 (I2C 8-Bit DACs with rail-to-rail outputs). 150 (I2C 8-Bit DACs with rail-to-rail outputs):
151 MAX517 - Single channel, single reference
152 MAX518 - Dual channel, ref=Vdd
153 MAX519 - Dual channel, dual reference
154 MAX520 - Quad channel, quad reference
155 MAX521 - Octal channel, independent ref for ch0-3, shared ref for ch4-7
151 156
152 This driver can also be built as a module. If so, the module 157 This driver can also be built as a module. If so, the module
153 will be called max517. 158 will be called max517.
diff --git a/drivers/iio/dac/max517.c b/drivers/iio/dac/max517.c
index 9a82a7255ebb..5507b3970b4b 100644
--- a/drivers/iio/dac/max517.c
+++ b/drivers/iio/dac/max517.c
@@ -39,11 +39,13 @@ enum max517_device_ids {
39 ID_MAX517, 39 ID_MAX517,
40 ID_MAX518, 40 ID_MAX518,
41 ID_MAX519, 41 ID_MAX519,
42 ID_MAX520,
43 ID_MAX521,
42}; 44};
43 45
44struct max517_data { 46struct max517_data {
45 struct i2c_client *client; 47 struct i2c_client *client;
46 unsigned short vref_mv[2]; 48 unsigned short vref_mv[8];
47}; 49};
48 50
49/* 51/*
@@ -149,7 +151,13 @@ static const struct iio_info max517_info = {
149 151
150static const struct iio_chan_spec max517_channels[] = { 152static const struct iio_chan_spec max517_channels[] = {
151 MAX517_CHANNEL(0), 153 MAX517_CHANNEL(0),
152 MAX517_CHANNEL(1) 154 MAX517_CHANNEL(1),
155 MAX517_CHANNEL(2),
156 MAX517_CHANNEL(3),
157 MAX517_CHANNEL(4),
158 MAX517_CHANNEL(5),
159 MAX517_CHANNEL(6),
160 MAX517_CHANNEL(7),
153}; 161};
154 162
155static int max517_probe(struct i2c_client *client, 163static int max517_probe(struct i2c_client *client,
@@ -158,6 +166,7 @@ static int max517_probe(struct i2c_client *client,
158 struct max517_data *data; 166 struct max517_data *data;
159 struct iio_dev *indio_dev; 167 struct iio_dev *indio_dev;
160 struct max517_platform_data *platform_data = client->dev.platform_data; 168 struct max517_platform_data *platform_data = client->dev.platform_data;
169 int chan;
161 170
162 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 171 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
163 if (!indio_dev) 172 if (!indio_dev)
@@ -169,11 +178,21 @@ static int max517_probe(struct i2c_client *client,
169 /* establish that the iio_dev is a child of the i2c device */ 178 /* establish that the iio_dev is a child of the i2c device */
170 indio_dev->dev.parent = &client->dev; 179 indio_dev->dev.parent = &client->dev;
171 180
172 /* reduced channel set for MAX517 */ 181 switch (id->driver_data) {
173 if (id->driver_data == ID_MAX517) 182 case ID_MAX521:
174 indio_dev->num_channels = 1; 183 indio_dev->num_channels = 8;
175 else 184 break;
185 case ID_MAX520:
186 indio_dev->num_channels = 4;
187 break;
188 case ID_MAX519:
189 case ID_MAX518:
176 indio_dev->num_channels = 2; 190 indio_dev->num_channels = 2;
191 break;
192 default: /* single channel for MAX517 */
193 indio_dev->num_channels = 1;
194 break;
195 }
177 indio_dev->channels = max517_channels; 196 indio_dev->channels = max517_channels;
178 indio_dev->modes = INDIO_DIRECT_MODE; 197 indio_dev->modes = INDIO_DIRECT_MODE;
179 indio_dev->info = &max517_info; 198 indio_dev->info = &max517_info;
@@ -182,11 +201,11 @@ static int max517_probe(struct i2c_client *client,
182 * Reference voltage on MAX518 and default is 5V, else take vref_mv 201 * Reference voltage on MAX518 and default is 5V, else take vref_mv
183 * from platform_data 202 * from platform_data
184 */ 203 */
185 if (id->driver_data == ID_MAX518 || !platform_data) { 204 for (chan = 0; chan < indio_dev->num_channels; chan++) {
186 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */ 205 if (id->driver_data == ID_MAX518 || !platform_data)
187 } else { 206 data->vref_mv[chan] = 5000; /* mV */
188 data->vref_mv[0] = platform_data->vref_mv[0]; 207 else
189 data->vref_mv[1] = platform_data->vref_mv[1]; 208 data->vref_mv[chan] = platform_data->vref_mv[chan];
190 } 209 }
191 210
192 return iio_device_register(indio_dev); 211 return iio_device_register(indio_dev);
@@ -202,6 +221,8 @@ static const struct i2c_device_id max517_id[] = {
202 { "max517", ID_MAX517 }, 221 { "max517", ID_MAX517 },
203 { "max518", ID_MAX518 }, 222 { "max518", ID_MAX518 },
204 { "max519", ID_MAX519 }, 223 { "max519", ID_MAX519 },
224 { "max520", ID_MAX520 },
225 { "max521", ID_MAX521 },
205 { } 226 { }
206}; 227};
207MODULE_DEVICE_TABLE(i2c, max517_id); 228MODULE_DEVICE_TABLE(i2c, max517_id);
@@ -218,5 +239,5 @@ static struct i2c_driver max517_driver = {
218module_i2c_driver(max517_driver); 239module_i2c_driver(max517_driver);
219 240
220MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 241MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
221MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC"); 242MODULE_DESCRIPTION("MAX517/518/519/520/521 8-bit DAC");
222MODULE_LICENSE("GPL"); 243MODULE_LICENSE("GPL");
diff --git a/drivers/iio/gyro/bmg160.c b/drivers/iio/gyro/bmg160.c
index ccf3ea7e1afa..4415f55d26b6 100644
--- a/drivers/iio/gyro/bmg160.c
+++ b/drivers/iio/gyro/bmg160.c
@@ -1001,16 +1001,12 @@ static int bmg160_gpio_probe(struct i2c_client *client,
1001 dev = &client->dev; 1001 dev = &client->dev;
1002 1002
1003 /* data ready gpio interrupt pin */ 1003 /* data ready gpio interrupt pin */
1004 gpio = devm_gpiod_get_index(dev, BMG160_GPIO_NAME, 0); 1004 gpio = devm_gpiod_get_index(dev, BMG160_GPIO_NAME, 0, GPIOD_IN);
1005 if (IS_ERR(gpio)) { 1005 if (IS_ERR(gpio)) {
1006 dev_err(dev, "acpi gpio get index failed\n"); 1006 dev_err(dev, "acpi gpio get index failed\n");
1007 return PTR_ERR(gpio); 1007 return PTR_ERR(gpio);
1008 } 1008 }
1009 1009
1010 ret = gpiod_direction_input(gpio);
1011 if (ret)
1012 return ret;
1013
1014 ret = gpiod_to_irq(gpio); 1010 ret = gpiod_to_irq(gpio);
1015 1011
1016 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1012 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
index 6a8020d48140..f0fd94055d88 100644
--- a/drivers/iio/gyro/itg3200_core.c
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -223,6 +223,10 @@ static int itg3200_initial_setup(struct iio_dev *indio_dev)
223 int ret; 223 int ret;
224 u8 val; 224 u8 val;
225 225
226 ret = itg3200_reset(indio_dev);
227 if (ret)
228 goto err_ret;
229
226 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val); 230 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_ADDRESS, &val);
227 if (ret) 231 if (ret)
228 goto err_ret; 232 goto err_ret;
@@ -233,10 +237,6 @@ static int itg3200_initial_setup(struct iio_dev *indio_dev)
233 goto err_ret; 237 goto err_ret;
234 } 238 }
235 239
236 ret = itg3200_reset(indio_dev);
237 if (ret)
238 goto err_ret;
239
240 ret = itg3200_enable_full_scale(indio_dev); 240 ret = itg3200_enable_full_scale(indio_dev);
241err_ret: 241err_ret:
242 return ret; 242 return ret;
@@ -351,6 +351,26 @@ static int itg3200_remove(struct i2c_client *client)
351 return 0; 351 return 0;
352} 352}
353 353
354static int __maybe_unused itg3200_suspend(struct device *dev)
355{
356 struct iio_dev *indio_dev = dev_get_drvdata(dev);
357 struct itg3200 *st = iio_priv(indio_dev);
358
359 dev_dbg(&st->i2c->dev, "suspend device");
360
361 return itg3200_write_reg_8(indio_dev, ITG3200_REG_POWER_MANAGEMENT,
362 ITG3200_SLEEP);
363}
364
365static int __maybe_unused itg3200_resume(struct device *dev)
366{
367 struct iio_dev *indio_dev = dev_get_drvdata(dev);
368
369 return itg3200_initial_setup(indio_dev);
370}
371
372static SIMPLE_DEV_PM_OPS(itg3200_pm_ops, itg3200_suspend, itg3200_resume);
373
354static const struct i2c_device_id itg3200_id[] = { 374static const struct i2c_device_id itg3200_id[] = {
355 { "itg3200", 0 }, 375 { "itg3200", 0 },
356 { } 376 { }
@@ -361,6 +381,7 @@ static struct i2c_driver itg3200_driver = {
361 .driver = { 381 .driver = {
362 .owner = THIS_MODULE, 382 .owner = THIS_MODULE,
363 .name = "itg3200", 383 .name = "itg3200",
384 .pm = &itg3200_pm_ops,
364 }, 385 },
365 .id_table = itg3200_id, 386 .id_table = itg3200_id,
366 .probe = itg3200_probe, 387 .probe = itg3200_probe,
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index f07a2336f7dc..21395f26d227 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -87,6 +87,31 @@
87#define ST_GYRO_2_DRDY_IRQ_INT2_MASK 0x08 87#define ST_GYRO_2_DRDY_IRQ_INT2_MASK 0x08
88#define ST_GYRO_2_MULTIREAD_BIT true 88#define ST_GYRO_2_MULTIREAD_BIT true
89 89
90/* CUSTOM VALUES FOR SENSOR 3 */
91#define ST_GYRO_3_WAI_EXP 0xd7
92#define ST_GYRO_3_ODR_ADDR 0x20
93#define ST_GYRO_3_ODR_MASK 0xc0
94#define ST_GYRO_3_ODR_AVL_95HZ_VAL 0x00
95#define ST_GYRO_3_ODR_AVL_190HZ_VAL 0x01
96#define ST_GYRO_3_ODR_AVL_380HZ_VAL 0x02
97#define ST_GYRO_3_ODR_AVL_760HZ_VAL 0x03
98#define ST_GYRO_3_PW_ADDR 0x20
99#define ST_GYRO_3_PW_MASK 0x08
100#define ST_GYRO_3_FS_ADDR 0x23
101#define ST_GYRO_3_FS_MASK 0x30
102#define ST_GYRO_3_FS_AVL_250_VAL 0x00
103#define ST_GYRO_3_FS_AVL_500_VAL 0x01
104#define ST_GYRO_3_FS_AVL_2000_VAL 0x02
105#define ST_GYRO_3_FS_AVL_250_GAIN IIO_DEGREE_TO_RAD(8750)
106#define ST_GYRO_3_FS_AVL_500_GAIN IIO_DEGREE_TO_RAD(17500)
107#define ST_GYRO_3_FS_AVL_2000_GAIN IIO_DEGREE_TO_RAD(70000)
108#define ST_GYRO_3_BDU_ADDR 0x23
109#define ST_GYRO_3_BDU_MASK 0x80
110#define ST_GYRO_3_DRDY_IRQ_ADDR 0x22
111#define ST_GYRO_3_DRDY_IRQ_INT2_MASK 0x08
112#define ST_GYRO_3_MULTIREAD_BIT true
113
114
90static const struct iio_chan_spec st_gyro_16bit_channels[] = { 115static const struct iio_chan_spec st_gyro_16bit_channels[] = {
91 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL, 116 ST_SENSORS_LSM_CHANNELS(IIO_ANGL_VEL,
92 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 117 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
@@ -225,6 +250,64 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = {
225 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, 250 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
226 .bootime = 2, 251 .bootime = 2,
227 }, 252 },
253 {
254 .wai = ST_GYRO_3_WAI_EXP,
255 .sensors_supported = {
256 [0] = L3GD20_GYRO_DEV_NAME,
257 },
258 .ch = (struct iio_chan_spec *)st_gyro_16bit_channels,
259 .odr = {
260 .addr = ST_GYRO_3_ODR_ADDR,
261 .mask = ST_GYRO_3_ODR_MASK,
262 .odr_avl = {
263 { 95, ST_GYRO_3_ODR_AVL_95HZ_VAL, },
264 { 190, ST_GYRO_3_ODR_AVL_190HZ_VAL, },
265 { 380, ST_GYRO_3_ODR_AVL_380HZ_VAL, },
266 { 760, ST_GYRO_3_ODR_AVL_760HZ_VAL, },
267 },
268 },
269 .pw = {
270 .addr = ST_GYRO_3_PW_ADDR,
271 .mask = ST_GYRO_3_PW_MASK,
272 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
273 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
274 },
275 .enable_axis = {
276 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
277 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
278 },
279 .fs = {
280 .addr = ST_GYRO_3_FS_ADDR,
281 .mask = ST_GYRO_3_FS_MASK,
282 .fs_avl = {
283 [0] = {
284 .num = ST_GYRO_FS_AVL_250DPS,
285 .value = ST_GYRO_3_FS_AVL_250_VAL,
286 .gain = ST_GYRO_3_FS_AVL_250_GAIN,
287 },
288 [1] = {
289 .num = ST_GYRO_FS_AVL_500DPS,
290 .value = ST_GYRO_3_FS_AVL_500_VAL,
291 .gain = ST_GYRO_3_FS_AVL_500_GAIN,
292 },
293 [2] = {
294 .num = ST_GYRO_FS_AVL_2000DPS,
295 .value = ST_GYRO_3_FS_AVL_2000_VAL,
296 .gain = ST_GYRO_3_FS_AVL_2000_GAIN,
297 },
298 },
299 },
300 .bdu = {
301 .addr = ST_GYRO_3_BDU_ADDR,
302 .mask = ST_GYRO_3_BDU_MASK,
303 },
304 .drdy_irq = {
305 .addr = ST_GYRO_3_DRDY_IRQ_ADDR,
306 .mask_int2 = ST_GYRO_3_DRDY_IRQ_INT2_MASK,
307 },
308 .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT,
309 .bootime = 2,
310 },
228}; 311};
229 312
230static int st_gyro_read_raw(struct iio_dev *indio_dev, 313static int st_gyro_read_raw(struct iio_dev *indio_dev,
diff --git a/drivers/iio/imu/inv_mpu6050/Makefile b/drivers/iio/imu/inv_mpu6050/Makefile
index 3a677c778afb..f566f6a7b3a9 100644
--- a/drivers/iio/imu/inv_mpu6050/Makefile
+++ b/drivers/iio/imu/inv_mpu6050/Makefile
@@ -3,4 +3,4 @@
3# 3#
4 4
5obj-$(CONFIG_INV_MPU6050_IIO) += inv-mpu6050.o 5obj-$(CONFIG_INV_MPU6050_IIO) += inv-mpu6050.o
6inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o 6inv-mpu6050-objs := inv_mpu_core.o inv_mpu_ring.o inv_mpu_trigger.o inv_mpu_acpi.o
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c
new file mode 100644
index 000000000000..1c982a56acd5
--- /dev/null
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_acpi.c
@@ -0,0 +1,211 @@
1/*
2 * inv_mpu_acpi: ACPI processing for creating client devices
3 * Copyright (c) 2015, 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#ifdef CONFIG_ACPI
16
17#include <linux/kernel.h>
18#include <linux/i2c.h>
19#include <linux/dmi.h>
20#include <linux/acpi.h>
21#include "inv_mpu_iio.h"
22
23enum inv_mpu_product_name {
24 INV_MPU_NOT_MATCHED,
25 INV_MPU_ASUS_T100TA,
26};
27
28static enum inv_mpu_product_name matched_product_name;
29
30static int __init asus_t100_matched(const struct dmi_system_id *d)
31{
32 matched_product_name = INV_MPU_ASUS_T100TA;
33
34 return 0;
35}
36
37static const struct dmi_system_id inv_mpu_dev_list[] = {
38 {
39 .callback = asus_t100_matched,
40 .ident = "Asus Transformer Book T100",
41 .matches = {
42 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC"),
43 DMI_MATCH(DMI_PRODUCT_NAME, "T100TA"),
44 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
45 },
46 },
47 /* Add more matching tables here..*/
48 {}
49};
50
51static int asus_acpi_get_sensor_info(struct acpi_device *adev,
52 struct i2c_client *client,
53 struct i2c_board_info *info)
54{
55 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
56 int i;
57 acpi_status status;
58 union acpi_object *cpm;
59
60 status = acpi_evaluate_object(adev->handle, "CNF0", NULL, &buffer);
61 if (ACPI_FAILURE(status))
62 return -ENODEV;
63
64 cpm = buffer.pointer;
65 for (i = 0; i < cpm->package.count; ++i) {
66 union acpi_object *elem;
67 int j;
68
69 elem = &(cpm->package.elements[i]);
70 for (j = 0; j < elem->package.count; ++j) {
71 union acpi_object *sub_elem;
72
73 sub_elem = &(elem->package.elements[j]);
74 if (sub_elem->type == ACPI_TYPE_STRING)
75 strlcpy(info->type, sub_elem->string.pointer,
76 sizeof(info->type));
77 else if (sub_elem->type == ACPI_TYPE_INTEGER) {
78 if (sub_elem->integer.value != client->addr) {
79 info->addr = sub_elem->integer.value;
80 break; /* Not a MPU6500 primary */
81 }
82 }
83 }
84 }
85
86 kfree(buffer.pointer);
87
88 return cpm->package.count;
89}
90
91static int acpi_i2c_check_resource(struct acpi_resource *ares, void *data)
92{
93 u32 *addr = data;
94
95 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
96 struct acpi_resource_i2c_serialbus *sb;
97
98 sb = &ares->data.i2c_serial_bus;
99 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
100 if (*addr)
101 *addr |= (sb->slave_address << 16);
102 else
103 *addr = sb->slave_address;
104 }
105 }
106
107 /* Tell the ACPI core that we already copied this address */
108 return 1;
109}
110
111static int inv_mpu_process_acpi_config(struct i2c_client *client,
112 unsigned short *primary_addr,
113 unsigned short *secondary_addr)
114{
115 const struct acpi_device_id *id;
116 struct acpi_device *adev;
117 u32 i2c_addr = 0;
118 LIST_HEAD(resources);
119 int ret;
120
121 id = acpi_match_device(client->dev.driver->acpi_match_table,
122 &client->dev);
123 if (!id)
124 return -ENODEV;
125
126 adev = ACPI_COMPANION(&client->dev);
127 if (!adev)
128 return -ENODEV;
129
130 ret = acpi_dev_get_resources(adev, &resources,
131 acpi_i2c_check_resource, &i2c_addr);
132 if (ret < 0)
133 return ret;
134
135 acpi_dev_free_resource_list(&resources);
136 *primary_addr = i2c_addr & 0x0000ffff;
137 *secondary_addr = (i2c_addr & 0xffff0000) >> 16;
138
139 return 0;
140}
141
142int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
143{
144
145 st->mux_client = NULL;
146 if (ACPI_HANDLE(&st->client->dev)) {
147 struct i2c_board_info info;
148 struct acpi_device *adev;
149 int ret = -1;
150
151 adev = ACPI_COMPANION(&st->client->dev);
152 memset(&info, 0, sizeof(info));
153
154 dmi_check_system(inv_mpu_dev_list);
155 switch (matched_product_name) {
156 case INV_MPU_ASUS_T100TA:
157 ret = asus_acpi_get_sensor_info(adev, st->client,
158 &info);
159 break;
160 /* Add more matched product processing here */
161 default:
162 break;
163 }
164
165 if (ret < 0) {
166 /* No matching DMI, so create device on INV6XX type */
167 unsigned short primary, secondary;
168
169 ret = inv_mpu_process_acpi_config(st->client, &primary,
170 &secondary);
171 if (!ret && secondary) {
172 char *name;
173
174 info.addr = secondary;
175 strlcpy(info.type, dev_name(&adev->dev),
176 sizeof(info.type));
177 name = strchr(info.type, ':');
178 if (name)
179 *name = '\0';
180 strlcat(info.type, "-client",
181 sizeof(info.type));
182 } else
183 return 0; /* no secondary addr, which is OK */
184 }
185 st->mux_client = i2c_new_device(st->mux_adapter, &info);
186 if (!st->mux_client)
187 return -ENODEV;
188
189 }
190
191 return 0;
192}
193
194void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st)
195{
196 if (st->mux_client)
197 i2c_unregister_device(st->mux_client);
198}
199#else
200
201#include "inv_mpu_iio.h"
202
203int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st)
204{
205 return 0;
206}
207
208void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st)
209{
210}
211#endif
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index ef76afe2643c..17d4bb15be4d 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -829,8 +829,14 @@ static int inv_mpu_probe(struct i2c_client *client,
829 goto out_unreg_device; 829 goto out_unreg_device;
830 } 830 }
831 831
832 result = inv_mpu_acpi_create_mux_client(st);
833 if (result)
834 goto out_del_mux;
835
832 return 0; 836 return 0;
833 837
838out_del_mux:
839 i2c_del_mux_adapter(st->mux_adapter);
834out_unreg_device: 840out_unreg_device:
835 iio_device_unregister(indio_dev); 841 iio_device_unregister(indio_dev);
836out_remove_trigger: 842out_remove_trigger:
@@ -845,6 +851,7 @@ static int inv_mpu_remove(struct i2c_client *client)
845 struct iio_dev *indio_dev = i2c_get_clientdata(client); 851 struct iio_dev *indio_dev = i2c_get_clientdata(client);
846 struct inv_mpu6050_state *st = iio_priv(indio_dev); 852 struct inv_mpu6050_state *st = iio_priv(indio_dev);
847 853
854 inv_mpu_acpi_delete_mux_client(st);
848 i2c_del_mux_adapter(st->mux_adapter); 855 i2c_del_mux_adapter(st->mux_adapter);
849 iio_device_unregister(indio_dev); 856 iio_device_unregister(indio_dev);
850 inv_mpu6050_remove_trigger(st); 857 inv_mpu6050_remove_trigger(st);
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
index aa837de57079..db0a4a2758ab 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
@@ -121,6 +121,7 @@ struct inv_mpu6050_state {
121 spinlock_t time_stamp_lock; 121 spinlock_t time_stamp_lock;
122 struct i2c_client *client; 122 struct i2c_client *client;
123 struct i2c_adapter *mux_adapter; 123 struct i2c_adapter *mux_adapter;
124 struct i2c_client *mux_client;
124 unsigned int powerup_count; 125 unsigned int powerup_count;
125 struct inv_mpu6050_platform_data plat_data; 126 struct inv_mpu6050_platform_data plat_data;
126 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE); 127 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
@@ -251,3 +252,5 @@ int inv_reset_fifo(struct iio_dev *indio_dev);
251int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask); 252int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask);
252int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val); 253int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 val);
253int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on); 254int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on);
255int inv_mpu_acpi_create_mux_client(struct inv_mpu6050_state *st);
256void inv_mpu_acpi_delete_mux_client(struct inv_mpu6050_state *st);
diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
index b3a36376c719..462a010628cd 100644
--- a/drivers/iio/imu/kmx61.c
+++ b/drivers/iio/imu/kmx61.c
@@ -169,19 +169,18 @@ static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
169static const struct { 169static const struct {
170 int val; 170 int val;
171 int val2; 171 int val2;
172 u8 odr_bits; 172} kmx61_samp_freq_table[] = { {12, 500000},
173} kmx61_samp_freq_table[] = { {12, 500000, 0x00}, 173 {25, 0},
174 {25, 0, 0x01}, 174 {50, 0},
175 {50, 0, 0x02}, 175 {100, 0},
176 {100, 0, 0x03}, 176 {200, 0},
177 {200, 0, 0x04}, 177 {400, 0},
178 {400, 0, 0x05}, 178 {800, 0},
179 {800, 0, 0x06}, 179 {1600, 0},
180 {1600, 0, 0x07}, 180 {0, 781000},
181 {0, 781000, 0x08}, 181 {1, 563000},
182 {1, 563000, 0x09}, 182 {3, 125000},
183 {3, 125000, 0x0A}, 183 {6, 250000} };
184 {6, 250000, 0x0B} };
185 184
186static const struct { 185static const struct {
187 int val; 186 int val;
@@ -302,24 +301,10 @@ static int kmx61_convert_freq_to_bit(int val, int val2)
302 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++) 301 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
303 if (val == kmx61_samp_freq_table[i].val && 302 if (val == kmx61_samp_freq_table[i].val &&
304 val2 == kmx61_samp_freq_table[i].val2) 303 val2 == kmx61_samp_freq_table[i].val2)
305 return kmx61_samp_freq_table[i].odr_bits; 304 return i;
306 return -EINVAL; 305 return -EINVAL;
307} 306}
308 307
309static int kmx61_convert_bit_to_freq(u8 odr_bits, int *val, int *val2)
310{
311 int i;
312
313 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
314 if (odr_bits == kmx61_samp_freq_table[i].odr_bits) {
315 *val = kmx61_samp_freq_table[i].val;
316 *val2 = kmx61_samp_freq_table[i].val2;
317 return 0;
318 }
319 return -EINVAL;
320}
321
322
323static int kmx61_convert_wake_up_odr_to_bit(int val, int val2) 308static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
324{ 309{
325 int i; 310 int i;
@@ -478,7 +463,7 @@ static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
478 463
479static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2, 464static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
480 u8 device) 465 u8 device)
481{ int i; 466{
482 u8 lodr_bits; 467 u8 lodr_bits;
483 468
484 if (device & KMX61_ACC) 469 if (device & KMX61_ACC)
@@ -490,13 +475,13 @@ static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
490 else 475 else
491 return -EINVAL; 476 return -EINVAL;
492 477
493 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++) 478 if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
494 if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) { 479 return -EINVAL;
495 *val = kmx61_samp_freq_table[i].val; 480
496 *val2 = kmx61_samp_freq_table[i].val2; 481 *val = kmx61_samp_freq_table[lodr_bits].val;
497 return 0; 482 *val2 = kmx61_samp_freq_table[lodr_bits].val2;
498 } 483
499 return -EINVAL; 484 return 0;
500} 485}
501 486
502static int kmx61_set_range(struct kmx61_data *data, u8 range) 487static int kmx61_set_range(struct kmx61_data *data, u8 range)
@@ -580,8 +565,11 @@ static int kmx61_chip_init(struct kmx61_data *data)
580 } 565 }
581 data->odr_bits = ret; 566 data->odr_bits = ret;
582 567
583 /* set output data rate for wake up (motion detection) function */ 568 /*
584 ret = kmx61_convert_bit_to_freq(data->odr_bits, &val, &val2); 569 * set output data rate for wake up (motion detection) function
570 * to match data rate for accelerometer sampling
571 */
572 ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
585 if (ret < 0) 573 if (ret < 0)
586 return ret; 574 return ret;
587 575
@@ -1267,16 +1255,12 @@ static int kmx61_gpio_probe(struct i2c_client *client, struct kmx61_data *data)
1267 dev = &client->dev; 1255 dev = &client->dev;
1268 1256
1269 /* data ready gpio interrupt pin */ 1257 /* data ready gpio interrupt pin */
1270 gpio = devm_gpiod_get_index(dev, KMX61_GPIO_NAME, 0); 1258 gpio = devm_gpiod_get_index(dev, KMX61_GPIO_NAME, 0, GPIOD_IN);
1271 if (IS_ERR(gpio)) { 1259 if (IS_ERR(gpio)) {
1272 dev_err(dev, "acpi gpio get index failed\n"); 1260 dev_err(dev, "acpi gpio get index failed\n");
1273 return PTR_ERR(gpio); 1261 return PTR_ERR(gpio);
1274 } 1262 }
1275 1263
1276 ret = gpiod_direction_input(gpio);
1277 if (ret)
1278 return ret;
1279
1280 ret = gpiod_to_irq(gpio); 1264 ret = gpiod_to_irq(gpio);
1281 1265
1282 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 1266 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 71333140d42c..df919f44d513 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -37,11 +37,57 @@ static bool iio_buffer_is_active(struct iio_buffer *buf)
37 return !list_empty(&buf->buffer_list); 37 return !list_empty(&buf->buffer_list);
38} 38}
39 39
40static bool iio_buffer_data_available(struct iio_buffer *buf) 40static size_t iio_buffer_data_available(struct iio_buffer *buf)
41{ 41{
42 return buf->access->data_available(buf); 42 return buf->access->data_available(buf);
43} 43}
44 44
45static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
46 struct iio_buffer *buf, size_t required)
47{
48 if (!indio_dev->info->hwfifo_flush_to_buffer)
49 return -ENODEV;
50
51 return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
52}
53
54static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
55 size_t to_wait, int to_flush)
56{
57 size_t avail;
58 int flushed = 0;
59
60 /* wakeup if the device was unregistered */
61 if (!indio_dev->info)
62 return true;
63
64 /* drain the buffer if it was disabled */
65 if (!iio_buffer_is_active(buf)) {
66 to_wait = min_t(size_t, to_wait, 1);
67 to_flush = 0;
68 }
69
70 avail = iio_buffer_data_available(buf);
71
72 if (avail >= to_wait) {
73 /* force a flush for non-blocking reads */
74 if (!to_wait && !avail && to_flush)
75 iio_buffer_flush_hwfifo(indio_dev, buf, to_flush);
76 return true;
77 }
78
79 if (to_flush)
80 flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
81 to_wait - avail);
82 if (flushed <= 0)
83 return false;
84
85 if (avail + flushed >= to_wait)
86 return true;
87
88 return false;
89}
90
45/** 91/**
46 * iio_buffer_read_first_n_outer() - chrdev read for buffer access 92 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
47 * 93 *
@@ -53,6 +99,9 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
53{ 99{
54 struct iio_dev *indio_dev = filp->private_data; 100 struct iio_dev *indio_dev = filp->private_data;
55 struct iio_buffer *rb = indio_dev->buffer; 101 struct iio_buffer *rb = indio_dev->buffer;
102 size_t datum_size;
103 size_t to_wait = 0;
104 size_t to_read;
56 int ret; 105 int ret;
57 106
58 if (!indio_dev->info) 107 if (!indio_dev->info)
@@ -61,19 +110,28 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
61 if (!rb || !rb->access->read_first_n) 110 if (!rb || !rb->access->read_first_n)
62 return -EINVAL; 111 return -EINVAL;
63 112
113 datum_size = rb->bytes_per_datum;
114
115 /*
116 * If datum_size is 0 there will never be anything to read from the
117 * buffer, so signal end of file now.
118 */
119 if (!datum_size)
120 return 0;
121
122 to_read = min_t(size_t, n / datum_size, rb->watermark);
123
124 if (!(filp->f_flags & O_NONBLOCK))
125 to_wait = to_read;
126
64 do { 127 do {
65 if (!iio_buffer_data_available(rb)) { 128 ret = wait_event_interruptible(rb->pollq,
66 if (filp->f_flags & O_NONBLOCK) 129 iio_buffer_ready(indio_dev, rb, to_wait, to_read));
67 return -EAGAIN; 130 if (ret)
131 return ret;
68 132
69 ret = wait_event_interruptible(rb->pollq, 133 if (!indio_dev->info)
70 iio_buffer_data_available(rb) || 134 return -ENODEV;
71 indio_dev->info == NULL);
72 if (ret)
73 return ret;
74 if (indio_dev->info == NULL)
75 return -ENODEV;
76 }
77 135
78 ret = rb->access->read_first_n(rb, n, buf); 136 ret = rb->access->read_first_n(rb, n, buf);
79 if (ret == 0 && (filp->f_flags & O_NONBLOCK)) 137 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
@@ -96,9 +154,8 @@ unsigned int iio_buffer_poll(struct file *filp,
96 return -ENODEV; 154 return -ENODEV;
97 155
98 poll_wait(filp, &rb->pollq, wait); 156 poll_wait(filp, &rb->pollq, wait);
99 if (iio_buffer_data_available(rb)) 157 if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
100 return POLLIN | POLLRDNORM; 158 return POLLIN | POLLRDNORM;
101 /* need a way of knowing if there may be enough data... */
102 return 0; 159 return 0;
103} 160}
104 161
@@ -123,6 +180,7 @@ void iio_buffer_init(struct iio_buffer *buffer)
123 INIT_LIST_HEAD(&buffer->buffer_list); 180 INIT_LIST_HEAD(&buffer->buffer_list);
124 init_waitqueue_head(&buffer->pollq); 181 init_waitqueue_head(&buffer->pollq);
125 kref_init(&buffer->ref); 182 kref_init(&buffer->ref);
183 buffer->watermark = 1;
126} 184}
127EXPORT_SYMBOL(iio_buffer_init); 185EXPORT_SYMBOL(iio_buffer_init);
128 186
@@ -416,6 +474,11 @@ static ssize_t iio_buffer_write_length(struct device *dev,
416 buffer->access->set_length(buffer, val); 474 buffer->access->set_length(buffer, val);
417 ret = 0; 475 ret = 0;
418 } 476 }
477 if (ret)
478 goto out;
479 if (buffer->length && buffer->length < buffer->watermark)
480 buffer->watermark = buffer->length;
481out:
419 mutex_unlock(&indio_dev->mlock); 482 mutex_unlock(&indio_dev->mlock);
420 483
421 return ret ? ret : len; 484 return ret ? ret : len;
@@ -472,6 +535,7 @@ static void iio_buffer_activate(struct iio_dev *indio_dev,
472static void iio_buffer_deactivate(struct iio_buffer *buffer) 535static void iio_buffer_deactivate(struct iio_buffer *buffer)
473{ 536{
474 list_del_init(&buffer->buffer_list); 537 list_del_init(&buffer->buffer_list);
538 wake_up_interruptible(&buffer->pollq);
475 iio_buffer_put(buffer); 539 iio_buffer_put(buffer);
476} 540}
477 541
@@ -629,19 +693,16 @@ static int __iio_update_buffers(struct iio_dev *indio_dev,
629 } 693 }
630 } 694 }
631 /* Definitely possible for devices to support both of these. */ 695 /* Definitely possible for devices to support both of these. */
632 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { 696 if ((indio_dev->modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
633 if (!indio_dev->trig) {
634 printk(KERN_INFO "Buffer not started: no trigger\n");
635 ret = -EINVAL;
636 /* Can only occur on first buffer */
637 goto error_run_postdisable;
638 }
639 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; 697 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
640 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 698 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
641 indio_dev->currentmode = INDIO_BUFFER_HARDWARE; 699 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
642 } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) { 700 } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) {
643 indio_dev->currentmode = INDIO_BUFFER_SOFTWARE; 701 indio_dev->currentmode = INDIO_BUFFER_SOFTWARE;
644 } else { /* Should never be reached */ 702 } else { /* Should never be reached */
703 /* Can only occur on first buffer */
704 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
705 pr_info("Buffer not started: no trigger\n");
645 ret = -EINVAL; 706 ret = -EINVAL;
646 goto error_run_postdisable; 707 goto error_run_postdisable;
647 } 708 }
@@ -754,12 +815,68 @@ done:
754 815
755static const char * const iio_scan_elements_group_name = "scan_elements"; 816static const char * const iio_scan_elements_group_name = "scan_elements";
756 817
818static ssize_t iio_buffer_show_watermark(struct device *dev,
819 struct device_attribute *attr,
820 char *buf)
821{
822 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
823 struct iio_buffer *buffer = indio_dev->buffer;
824
825 return sprintf(buf, "%u\n", buffer->watermark);
826}
827
828static ssize_t iio_buffer_store_watermark(struct device *dev,
829 struct device_attribute *attr,
830 const char *buf,
831 size_t len)
832{
833 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
834 struct iio_buffer *buffer = indio_dev->buffer;
835 unsigned int val;
836 int ret;
837
838 ret = kstrtouint(buf, 10, &val);
839 if (ret)
840 return ret;
841 if (!val)
842 return -EINVAL;
843
844 mutex_lock(&indio_dev->mlock);
845
846 if (val > buffer->length) {
847 ret = -EINVAL;
848 goto out;
849 }
850
851 if (iio_buffer_is_active(indio_dev->buffer)) {
852 ret = -EBUSY;
853 goto out;
854 }
855
856 buffer->watermark = val;
857
858 if (indio_dev->info->hwfifo_set_watermark)
859 indio_dev->info->hwfifo_set_watermark(indio_dev, val);
860out:
861 mutex_unlock(&indio_dev->mlock);
862
863 return ret ? ret : len;
864}
865
757static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 866static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
758 iio_buffer_write_length); 867 iio_buffer_write_length);
759static struct device_attribute dev_attr_length_ro = __ATTR(length, 868static struct device_attribute dev_attr_length_ro = __ATTR(length,
760 S_IRUGO, iio_buffer_read_length, NULL); 869 S_IRUGO, iio_buffer_read_length, NULL);
761static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 870static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
762 iio_buffer_show_enable, iio_buffer_store_enable); 871 iio_buffer_show_enable, iio_buffer_store_enable);
872static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
873 iio_buffer_show_watermark, iio_buffer_store_watermark);
874
875static struct attribute *iio_buffer_attrs[] = {
876 &dev_attr_length.attr,
877 &dev_attr_enable.attr,
878 &dev_attr_watermark.attr,
879};
763 880
764int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 881int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
765{ 882{
@@ -778,21 +895,23 @@ int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
778 attrcount++; 895 attrcount++;
779 } 896 }
780 897
781 buffer->buffer_group.name = "buffer"; 898 attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
782 buffer->buffer_group.attrs = kcalloc(attrcount + 3, 899 sizeof(struct attribute *), GFP_KERNEL);
783 sizeof(*buffer->buffer_group.attrs), GFP_KERNEL); 900 if (!attr)
784 if (!buffer->buffer_group.attrs)
785 return -ENOMEM; 901 return -ENOMEM;
786 902
787 if (buffer->access->set_length) 903 memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
788 buffer->buffer_group.attrs[0] = &dev_attr_length.attr; 904 if (!buffer->access->set_length)
789 else 905 attr[0] = &dev_attr_length_ro.attr;
790 buffer->buffer_group.attrs[0] = &dev_attr_length_ro.attr; 906
791 buffer->buffer_group.attrs[1] = &dev_attr_enable.attr;
792 if (buffer->attrs) 907 if (buffer->attrs)
793 memcpy(&buffer->buffer_group.attrs[2], buffer->attrs, 908 memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
794 sizeof(*&buffer->buffer_group.attrs) * attrcount); 909 sizeof(struct attribute *) * attrcount);
795 buffer->buffer_group.attrs[attrcount+2] = NULL; 910
911 attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
912
913 buffer->buffer_group.name = "buffer";
914 buffer->buffer_group.attrs = attr;
796 915
797 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 916 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
798 917
@@ -937,8 +1056,18 @@ static const void *iio_demux(struct iio_buffer *buffer,
937static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 1056static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
938{ 1057{
939 const void *dataout = iio_demux(buffer, data); 1058 const void *dataout = iio_demux(buffer, data);
1059 int ret;
940 1060
941 return buffer->access->store_to(buffer, dataout); 1061 ret = buffer->access->store_to(buffer, dataout);
1062 if (ret)
1063 return ret;
1064
1065 /*
1066 * We can't just test for watermark to decide if we wake the poll queue
1067 * because read may request less samples than the watermark.
1068 */
1069 wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
1070 return 0;
942} 1071}
943 1072
944static void iio_buffer_demux_free(struct iio_buffer *buffer) 1073static void iio_buffer_demux_free(struct iio_buffer *buffer)
diff --git a/drivers/iio/kfifo_buf.c b/drivers/iio/kfifo_buf.c
index b2beea01c49b..847ca561afe0 100644
--- a/drivers/iio/kfifo_buf.c
+++ b/drivers/iio/kfifo_buf.c
@@ -83,9 +83,6 @@ static int iio_store_to_kfifo(struct iio_buffer *r,
83 ret = kfifo_in(&kf->kf, data, 1); 83 ret = kfifo_in(&kf->kf, data, 1);
84 if (ret != 1) 84 if (ret != 1)
85 return -EBUSY; 85 return -EBUSY;
86
87 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
88
89 return 0; 86 return 0;
90} 87}
91 88
@@ -109,16 +106,16 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
109 return copied; 106 return copied;
110} 107}
111 108
112static bool iio_kfifo_buf_data_available(struct iio_buffer *r) 109static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
113{ 110{
114 struct iio_kfifo *kf = iio_to_kfifo(r); 111 struct iio_kfifo *kf = iio_to_kfifo(r);
115 bool empty; 112 size_t samples;
116 113
117 mutex_lock(&kf->user_lock); 114 mutex_lock(&kf->user_lock);
118 empty = kfifo_is_empty(&kf->kf); 115 samples = kfifo_len(&kf->kf);
119 mutex_unlock(&kf->user_lock); 116 mutex_unlock(&kf->user_lock);
120 117
121 return !empty; 118 return samples;
122} 119}
123 120
124static void iio_kfifo_buffer_release(struct iio_buffer *buffer) 121static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index a224afd6380c..01a1a16ab7be 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -59,6 +59,16 @@ config CM3232
59 To compile this driver as a module, choose M here: 59 To compile this driver as a module, choose M here:
60 the module will be called cm3232. 60 the module will be called cm3232.
61 61
62config CM3323
63 depends on I2C
64 tristate "Capella CM3323 color light sensor"
65 help
66 Say Y here if you want to build a driver for Capela CM3323
67 color sensor.
68
69 To compile this driver as a module, choose M here: the module will
70 be called cm3323.
71
62config CM36651 72config CM36651
63 depends on I2C 73 depends on I2C
64 tristate "CM36651 driver" 74 tristate "CM36651 driver"
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index b12a5160d9e0..ad7c30fe443b 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_AL3320A) += al3320a.o
8obj-$(CONFIG_APDS9300) += apds9300.o 8obj-$(CONFIG_APDS9300) += apds9300.o
9obj-$(CONFIG_CM32181) += cm32181.o 9obj-$(CONFIG_CM32181) += cm32181.o
10obj-$(CONFIG_CM3232) += cm3232.o 10obj-$(CONFIG_CM3232) += cm3232.o
11obj-$(CONFIG_CM3323) += cm3323.o
11obj-$(CONFIG_CM36651) += cm36651.o 12obj-$(CONFIG_CM36651) += cm36651.o
12obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o 13obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
13obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o 14obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
diff --git a/drivers/iio/light/cm3232.c b/drivers/iio/light/cm3232.c
index 90e3519a91de..39c8d99cc48e 100644
--- a/drivers/iio/light/cm3232.c
+++ b/drivers/iio/light/cm3232.c
@@ -378,6 +378,39 @@ static const struct i2c_device_id cm3232_id[] = {
378 {} 378 {}
379}; 379};
380 380
381#ifdef CONFIG_PM_SLEEP
382static int cm3232_suspend(struct device *dev)
383{
384 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
385 struct cm3232_chip *chip = iio_priv(indio_dev);
386 struct i2c_client *client = chip->client;
387 int ret;
388
389 chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
390 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
391 chip->regs_cmd);
392
393 return ret;
394}
395
396static int cm3232_resume(struct device *dev)
397{
398 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
399 struct cm3232_chip *chip = iio_priv(indio_dev);
400 struct i2c_client *client = chip->client;
401 int ret;
402
403 chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
404 ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
405 chip->regs_cmd | CM3232_CMD_ALS_RESET);
406
407 return ret;
408}
409
410static const struct dev_pm_ops cm3232_pm_ops = {
411 SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
412#endif
413
381MODULE_DEVICE_TABLE(i2c, cm3232_id); 414MODULE_DEVICE_TABLE(i2c, cm3232_id);
382 415
383static const struct of_device_id cm3232_of_match[] = { 416static const struct of_device_id cm3232_of_match[] = {
@@ -390,6 +423,9 @@ static struct i2c_driver cm3232_driver = {
390 .name = "cm3232", 423 .name = "cm3232",
391 .owner = THIS_MODULE, 424 .owner = THIS_MODULE,
392 .of_match_table = of_match_ptr(cm3232_of_match), 425 .of_match_table = of_match_ptr(cm3232_of_match),
426#ifdef CONFIG_PM_SLEEP
427 .pm = &cm3232_pm_ops,
428#endif
393 }, 429 },
394 .id_table = cm3232_id, 430 .id_table = cm3232_id,
395 .probe = cm3232_probe, 431 .probe = cm3232_probe,
diff --git a/drivers/iio/light/cm3323.c b/drivers/iio/light/cm3323.c
new file mode 100644
index 000000000000..869033e48a1f
--- /dev/null
+++ b/drivers/iio/light/cm3323.c
@@ -0,0 +1,286 @@
1/*
2 * CM3323 - Capella Color Light Sensor
3 *
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * IIO driver for CM3323 (7-bit I2C slave address 0x10)
11 *
12 * TODO: calibscale to correct the lens factor
13 */
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/i2c.h>
17#include <linux/mutex.h>
18
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21
22#define CM3323_DRV_NAME "cm3323"
23
24#define CM3323_CMD_CONF 0x00
25#define CM3323_CMD_RED_DATA 0x08
26#define CM3323_CMD_GREEN_DATA 0x09
27#define CM3323_CMD_BLUE_DATA 0x0A
28#define CM3323_CMD_CLEAR_DATA 0x0B
29
30#define CM3323_CONF_SD_BIT BIT(0) /* sensor disable */
31#define CM3323_CONF_AF_BIT BIT(1) /* auto/manual force mode */
32#define CM3323_CONF_IT_MASK (BIT(4) | BIT(5) | BIT(6))
33#define CM3323_CONF_IT_SHIFT 4
34
35#define CM3323_INT_TIME_AVAILABLE "0.04 0.08 0.16 0.32 0.64 1.28"
36
37static const struct {
38 int val;
39 int val2;
40} cm3323_int_time[] = {
41 {0, 40000}, /* 40 ms */
42 {0, 80000}, /* 80 ms */
43 {0, 160000}, /* 160 ms */
44 {0, 320000}, /* 320 ms */
45 {0, 640000}, /* 640 ms */
46 {1, 280000}, /* 1280 ms */
47};
48
49struct cm3323_data {
50 struct i2c_client *client;
51 u16 reg_conf;
52 struct mutex mutex;
53};
54
55#define CM3323_COLOR_CHANNEL(_color, _addr) { \
56 .type = IIO_INTENSITY, \
57 .modified = 1, \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
59 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
60 .channel2 = IIO_MOD_LIGHT_##_color, \
61 .address = _addr, \
62}
63
64static const struct iio_chan_spec cm3323_channels[] = {
65 CM3323_COLOR_CHANNEL(RED, CM3323_CMD_RED_DATA),
66 CM3323_COLOR_CHANNEL(GREEN, CM3323_CMD_GREEN_DATA),
67 CM3323_COLOR_CHANNEL(BLUE, CM3323_CMD_BLUE_DATA),
68 CM3323_COLOR_CHANNEL(CLEAR, CM3323_CMD_CLEAR_DATA),
69};
70
71static IIO_CONST_ATTR_INT_TIME_AVAIL(CM3323_INT_TIME_AVAILABLE);
72
73static struct attribute *cm3323_attributes[] = {
74 &iio_const_attr_integration_time_available.dev_attr.attr,
75 NULL
76};
77
78static const struct attribute_group cm3323_attribute_group = {
79 .attrs = cm3323_attributes,
80};
81
82static int cm3323_init(struct iio_dev *indio_dev)
83{
84 int ret;
85 struct cm3323_data *data = iio_priv(indio_dev);
86
87 ret = i2c_smbus_read_word_data(data->client, CM3323_CMD_CONF);
88 if (ret < 0) {
89 dev_err(&data->client->dev, "Error reading reg_conf\n");
90 return ret;
91 }
92
93 /* enable sensor and set auto force mode */
94 ret &= ~(CM3323_CONF_SD_BIT | CM3323_CONF_AF_BIT);
95
96 ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF, ret);
97 if (ret < 0) {
98 dev_err(&data->client->dev, "Error writing reg_conf\n");
99 return ret;
100 }
101
102 data->reg_conf = ret;
103
104 return 0;
105}
106
107static void cm3323_disable(struct iio_dev *indio_dev)
108{
109 int ret;
110 struct cm3323_data *data = iio_priv(indio_dev);
111
112 ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF,
113 CM3323_CONF_SD_BIT);
114 if (ret < 0)
115 dev_err(&data->client->dev, "Error writing reg_conf\n");
116}
117
118static int cm3323_set_it_bits(struct cm3323_data *data, int val, int val2)
119{
120 int i, ret;
121 u16 reg_conf;
122
123 for (i = 0; i < ARRAY_SIZE(cm3323_int_time); i++) {
124 if (val == cm3323_int_time[i].val &&
125 val2 == cm3323_int_time[i].val2) {
126 reg_conf = data->reg_conf;
127 reg_conf |= i << CM3323_CONF_IT_SHIFT;
128
129 ret = i2c_smbus_write_word_data(data->client,
130 CM3323_CMD_CONF,
131 reg_conf);
132 if (ret < 0)
133 return ret;
134
135 data->reg_conf = reg_conf;
136 return 0;
137 }
138 }
139 return -EINVAL;
140}
141
142static int cm3323_get_it_bits(struct cm3323_data *data)
143{
144 int bits;
145
146 bits = (data->reg_conf & CM3323_CONF_IT_MASK) >>
147 CM3323_CONF_IT_SHIFT;
148
149 if (bits >= ARRAY_SIZE(cm3323_int_time))
150 return -EINVAL;
151 return bits;
152}
153
154static int cm3323_read_raw(struct iio_dev *indio_dev,
155 struct iio_chan_spec const *chan, int *val,
156 int *val2, long mask)
157{
158 int i, ret;
159 struct cm3323_data *data = iio_priv(indio_dev);
160
161 switch (mask) {
162 case IIO_CHAN_INFO_RAW:
163 mutex_lock(&data->mutex);
164 ret = i2c_smbus_read_word_data(data->client, chan->address);
165 if (ret < 0) {
166 mutex_unlock(&data->mutex);
167 return ret;
168 }
169 *val = ret;
170 mutex_unlock(&data->mutex);
171
172 return IIO_VAL_INT;
173 case IIO_CHAN_INFO_INT_TIME:
174 mutex_lock(&data->mutex);
175 i = cm3323_get_it_bits(data);
176 if (i < 0) {
177 mutex_unlock(&data->mutex);
178 return -EINVAL;
179 }
180
181 *val = cm3323_int_time[i].val;
182 *val2 = cm3323_int_time[i].val2;
183 mutex_unlock(&data->mutex);
184
185 return IIO_VAL_INT_PLUS_MICRO;
186 default:
187 return -EINVAL;
188 }
189}
190
191static int cm3323_write_raw(struct iio_dev *indio_dev,
192 struct iio_chan_spec const *chan, int val,
193 int val2, long mask)
194{
195 struct cm3323_data *data = iio_priv(indio_dev);
196 int ret;
197
198 switch (mask) {
199 case IIO_CHAN_INFO_INT_TIME:
200 mutex_lock(&data->mutex);
201 ret = cm3323_set_it_bits(data, val, val2);
202 mutex_unlock(&data->mutex);
203
204 return ret;
205 default:
206 return -EINVAL;
207 }
208}
209
210static const struct iio_info cm3323_info = {
211 .driver_module = THIS_MODULE,
212 .read_raw = cm3323_read_raw,
213 .write_raw = cm3323_write_raw,
214 .attrs = &cm3323_attribute_group,
215};
216
217static int cm3323_probe(struct i2c_client *client,
218 const struct i2c_device_id *id)
219{
220 struct cm3323_data *data;
221 struct iio_dev *indio_dev;
222 int ret;
223
224 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
225 if (!indio_dev)
226 return -ENOMEM;
227
228 data = iio_priv(indio_dev);
229 i2c_set_clientdata(client, indio_dev);
230 data->client = client;
231
232 mutex_init(&data->mutex);
233
234 indio_dev->dev.parent = &client->dev;
235 indio_dev->info = &cm3323_info;
236 indio_dev->name = CM3323_DRV_NAME;
237 indio_dev->channels = cm3323_channels;
238 indio_dev->num_channels = ARRAY_SIZE(cm3323_channels);
239 indio_dev->modes = INDIO_DIRECT_MODE;
240
241 ret = cm3323_init(indio_dev);
242 if (ret < 0) {
243 dev_err(&client->dev, "cm3323 chip init failed\n");
244 return ret;
245 }
246 ret = iio_device_register(indio_dev);
247 if (ret < 0) {
248 dev_err(&client->dev, "failed to register iio dev\n");
249 goto err_init;
250 }
251 return 0;
252err_init:
253 cm3323_disable(indio_dev);
254 return ret;
255}
256
257static int cm3323_remove(struct i2c_client *client)
258{
259 struct iio_dev *indio_dev = i2c_get_clientdata(client);
260
261 iio_device_unregister(indio_dev);
262 cm3323_disable(indio_dev);
263
264 return 0;
265}
266
267static const struct i2c_device_id cm3323_id[] = {
268 {"cm3323", 0},
269 {}
270};
271MODULE_DEVICE_TABLE(i2c, cm3323_id);
272
273static struct i2c_driver cm3323_driver = {
274 .driver = {
275 .name = CM3323_DRV_NAME,
276 },
277 .probe = cm3323_probe,
278 .remove = cm3323_remove,
279 .id_table = cm3323_id,
280};
281
282module_i2c_driver(cm3323_driver);
283
284MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
285MODULE_DESCRIPTION("Capella CM3323 Color Light Sensor driver");
286MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/light/gp2ap020a00f.c b/drivers/iio/light/gp2ap020a00f.c
index 221ed16de1f7..32b6449833fa 100644
--- a/drivers/iio/light/gp2ap020a00f.c
+++ b/drivers/iio/light/gp2ap020a00f.c
@@ -46,6 +46,7 @@
46#include <linux/regmap.h> 46#include <linux/regmap.h>
47#include <linux/regulator/consumer.h> 47#include <linux/regulator/consumer.h>
48#include <linux/slab.h> 48#include <linux/slab.h>
49#include <asm/unaligned.h>
49#include <linux/iio/buffer.h> 50#include <linux/iio/buffer.h>
50#include <linux/iio/events.h> 51#include <linux/iio/events.h>
51#include <linux/iio/iio.h> 52#include <linux/iio/iio.h>
@@ -966,7 +967,6 @@ static irqreturn_t gp2ap020a00f_trigger_handler(int irq, void *data)
966 struct iio_dev *indio_dev = pf->indio_dev; 967 struct iio_dev *indio_dev = pf->indio_dev;
967 struct gp2ap020a00f_data *priv = iio_priv(indio_dev); 968 struct gp2ap020a00f_data *priv = iio_priv(indio_dev);
968 size_t d_size = 0; 969 size_t d_size = 0;
969 __le32 light_lux;
970 int i, out_val, ret; 970 int i, out_val, ret;
971 971
972 for_each_set_bit(i, indio_dev->active_scan_mask, 972 for_each_set_bit(i, indio_dev->active_scan_mask,
@@ -981,8 +981,8 @@ static irqreturn_t gp2ap020a00f_trigger_handler(int irq, void *data)
981 i == GP2AP020A00F_SCAN_MODE_LIGHT_IR) { 981 i == GP2AP020A00F_SCAN_MODE_LIGHT_IR) {
982 out_val = le16_to_cpup((__le16 *)&priv->buffer[d_size]); 982 out_val = le16_to_cpup((__le16 *)&priv->buffer[d_size]);
983 gp2ap020a00f_output_to_lux(priv, &out_val); 983 gp2ap020a00f_output_to_lux(priv, &out_val);
984 light_lux = cpu_to_le32(out_val); 984
985 memcpy(&priv->buffer[d_size], (u8 *)&light_lux, 4); 985 put_unaligned_le32(out_val, &priv->buffer[d_size]);
986 d_size += 4; 986 d_size += 4;
987 } else { 987 } else {
988 d_size += 2; 988 d_size += 2;
diff --git a/drivers/iio/light/jsa1212.c b/drivers/iio/light/jsa1212.c
index 29de7e7d9562..3a3af89beaf9 100644
--- a/drivers/iio/light/jsa1212.c
+++ b/drivers/iio/light/jsa1212.c
@@ -308,7 +308,7 @@ static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
308 } 308 }
309} 309}
310 310
311static struct regmap_config jsa1212_regmap_config = { 311static const struct regmap_config jsa1212_regmap_config = {
312 .name = JSA1212_REGMAP_NAME, 312 .name = JSA1212_REGMAP_NAME,
313 .reg_bits = 8, 313 .reg_bits = 8,
314 .val_bits = 8, 314 .val_bits = 8,
diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 62b7072af4de..78b87839c4b9 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -333,6 +333,13 @@ static int ltr501_init(struct ltr501_data *data)
333 data->ps_contr); 333 data->ps_contr);
334} 334}
335 335
336static int ltr501_powerdown(struct ltr501_data *data)
337{
338 return ltr501_write_contr(data->client,
339 data->als_contr & ~LTR501_CONTR_ACTIVE,
340 data->ps_contr & ~LTR501_CONTR_ACTIVE);
341}
342
336static int ltr501_probe(struct i2c_client *client, 343static int ltr501_probe(struct i2c_client *client,
337 const struct i2c_device_id *id) 344 const struct i2c_device_id *id)
338{ 345{
@@ -370,7 +377,7 @@ static int ltr501_probe(struct i2c_client *client,
370 ret = iio_triggered_buffer_setup(indio_dev, NULL, 377 ret = iio_triggered_buffer_setup(indio_dev, NULL,
371 ltr501_trigger_handler, NULL); 378 ltr501_trigger_handler, NULL);
372 if (ret) 379 if (ret)
373 return ret; 380 goto powerdown_on_error;
374 381
375 ret = iio_device_register(indio_dev); 382 ret = iio_device_register(indio_dev);
376 if (ret) 383 if (ret)
@@ -380,16 +387,11 @@ static int ltr501_probe(struct i2c_client *client,
380 387
381error_unreg_buffer: 388error_unreg_buffer:
382 iio_triggered_buffer_cleanup(indio_dev); 389 iio_triggered_buffer_cleanup(indio_dev);
390powerdown_on_error:
391 ltr501_powerdown(data);
383 return ret; 392 return ret;
384} 393}
385 394
386static int ltr501_powerdown(struct ltr501_data *data)
387{
388 return ltr501_write_contr(data->client,
389 data->als_contr & ~LTR501_CONTR_ACTIVE,
390 data->ps_contr & ~LTR501_CONTR_ACTIVE);
391}
392
393static int ltr501_remove(struct i2c_client *client) 395static int ltr501_remove(struct i2c_client *client)
394{ 396{
395 struct iio_dev *indio_dev = i2c_get_clientdata(client); 397 struct iio_dev *indio_dev = i2c_get_clientdata(client);
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index e3106b43ef48..261d517428e4 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -321,6 +321,12 @@ static const struct iio_info mag3110_info = {
321 321
322static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0}; 322static const unsigned long mag3110_scan_masks[] = {0x7, 0xf, 0};
323 323
324static int mag3110_standby(struct mag3110_data *data)
325{
326 return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
327 data->ctrl_reg1 & ~MAG3110_CTRL_AC);
328}
329
324static int mag3110_probe(struct i2c_client *client, 330static int mag3110_probe(struct i2c_client *client,
325 const struct i2c_device_id *id) 331 const struct i2c_device_id *id)
326{ 332{
@@ -360,12 +366,12 @@ static int mag3110_probe(struct i2c_client *client,
360 ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2, 366 ret = i2c_smbus_write_byte_data(client, MAG3110_CTRL_REG2,
361 MAG3110_CTRL_AUTO_MRST_EN); 367 MAG3110_CTRL_AUTO_MRST_EN);
362 if (ret < 0) 368 if (ret < 0)
363 return ret; 369 goto standby_on_error;
364 370
365 ret = iio_triggered_buffer_setup(indio_dev, NULL, 371 ret = iio_triggered_buffer_setup(indio_dev, NULL,
366 mag3110_trigger_handler, NULL); 372 mag3110_trigger_handler, NULL);
367 if (ret < 0) 373 if (ret < 0)
368 return ret; 374 goto standby_on_error;
369 375
370 ret = iio_device_register(indio_dev); 376 ret = iio_device_register(indio_dev);
371 if (ret < 0) 377 if (ret < 0)
@@ -374,15 +380,11 @@ static int mag3110_probe(struct i2c_client *client,
374 380
375buffer_cleanup: 381buffer_cleanup:
376 iio_triggered_buffer_cleanup(indio_dev); 382 iio_triggered_buffer_cleanup(indio_dev);
383standby_on_error:
384 mag3110_standby(iio_priv(indio_dev));
377 return ret; 385 return ret;
378} 386}
379 387
380static int mag3110_standby(struct mag3110_data *data)
381{
382 return i2c_smbus_write_byte_data(data->client, MAG3110_CTRL_REG1,
383 data->ctrl_reg1 & ~MAG3110_CTRL_AC);
384}
385
386static int mag3110_remove(struct i2c_client *client) 388static int mag3110_remove(struct i2c_client *client)
387{ 389{
388 struct iio_dev *indio_dev = i2c_get_clientdata(client); 390 struct iio_dev *indio_dev = i2c_get_clientdata(client);
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index a3be53792072..fa6295041947 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -52,6 +52,33 @@ config MPL3115
52 To compile this driver as a module, choose M here: the module 52 To compile this driver as a module, choose M here: the module
53 will be called mpl3115. 53 will be called mpl3115.
54 54
55config MS5611
56 tristate "Measurement Specialities MS5611 pressure sensor driver"
57 help
58 Say Y here to build support for the Measurement Specialities
59 MS5611 pressure and temperature sensor.
60
61 To compile this driver as a module, choose M here: the module will
62 be called ms5611_core.
63
64config MS5611_I2C
65 tristate "support I2C bus connection"
66 depends on I2C && MS5611
67 help
68 Say Y here to build I2C bus support for MS5611.
69
70 To compile this driver as a module, choose M here: the module will
71 be called ms5611_i2c.
72
73config MS5611_SPI
74 tristate "support SPI bus connection"
75 depends on SPI_MASTER && MS5611
76 help
77 Say Y here to build SPI bus support for MS5611.
78
79 To compile this driver as a module, choose M here: the module will
80 be called ms5611_spi.
81
55config IIO_ST_PRESS 82config IIO_ST_PRESS
56 tristate "STMicroelectronics pressure sensor Driver" 83 tristate "STMicroelectronics pressure sensor Driver"
57 depends on (I2C || SPI_MASTER) && SYSFS 84 depends on (I2C || SPI_MASTER) && SYSFS
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index 88011f2ae00e..a4f98f8d90ed 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -7,6 +7,9 @@ obj-$(CONFIG_BMP280) += bmp280.o
7obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o 7obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o
8obj-$(CONFIG_MPL115) += mpl115.o 8obj-$(CONFIG_MPL115) += mpl115.o
9obj-$(CONFIG_MPL3115) += mpl3115.o 9obj-$(CONFIG_MPL3115) += mpl3115.o
10obj-$(CONFIG_MS5611) += ms5611_core.o
11obj-$(CONFIG_MS5611_I2C) += ms5611_i2c.o
12obj-$(CONFIG_MS5611_SPI) += ms5611_spi.o
10obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o 13obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o
11st_pressure-y := st_pressure_core.o 14st_pressure-y := st_pressure_core.o
12st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o 15st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
diff --git a/drivers/iio/pressure/ms5611.h b/drivers/iio/pressure/ms5611.h
new file mode 100644
index 000000000000..099c6cdea43f
--- /dev/null
+++ b/drivers/iio/pressure/ms5611.h
@@ -0,0 +1,44 @@
1/*
2 * MS5611 pressure and temperature sensor driver
3 *
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#ifndef _MS5611_H
13#define _MS5611_H
14
15#include <linux/device.h>
16#include <linux/iio/iio.h>
17#include <linux/mutex.h>
18
19#define MS5611_RESET 0x1e
20#define MS5611_READ_ADC 0x00
21#define MS5611_READ_PROM_WORD 0xA0
22#define MS5611_START_TEMP_CONV 0x58
23#define MS5611_START_PRESSURE_CONV 0x48
24
25#define MS5611_CONV_TIME_MIN 9040
26#define MS5611_CONV_TIME_MAX 10000
27
28#define MS5611_PROM_WORDS_NB 8
29
30struct ms5611_state {
31 void *client;
32 struct mutex lock;
33
34 int (*reset)(struct device *dev);
35 int (*read_prom_word)(struct device *dev, int index, u16 *word);
36 int (*read_adc_temp_and_pressure)(struct device *dev,
37 s32 *temp, s32 *pressure);
38
39 u16 prom[MS5611_PROM_WORDS_NB];
40};
41
42int ms5611_probe(struct iio_dev *indio_dev, struct device *dev);
43
44#endif /* _MS5611_H */
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c
new file mode 100644
index 000000000000..e42c8531d9b3
--- /dev/null
+++ b/drivers/iio/pressure/ms5611_core.c
@@ -0,0 +1,215 @@
1/*
2 * MS5611 pressure and temperature sensor driver
3 *
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Data sheet:
11 * http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/iio/iio.h>
17#include <linux/delay.h>
18
19#include "ms5611.h"
20
21static bool ms5611_prom_is_valid(u16 *prom, size_t len)
22{
23 int i, j;
24 uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
25
26 prom[7] &= 0xFF00;
27
28 for (i = 0; i < len * 2; i++) {
29 if (i % 2 == 1)
30 crc ^= prom[i >> 1] & 0x00FF;
31 else
32 crc ^= prom[i >> 1] >> 8;
33
34 for (j = 0; j < 8; j++) {
35 if (crc & 0x8000)
36 crc = (crc << 1) ^ 0x3000;
37 else
38 crc <<= 1;
39 }
40 }
41
42 crc = (crc >> 12) & 0x000F;
43
44 return crc_orig != 0x0000 && crc == crc_orig;
45}
46
47static int ms5611_read_prom(struct iio_dev *indio_dev)
48{
49 int ret, i;
50 struct ms5611_state *st = iio_priv(indio_dev);
51
52 for (i = 0; i < MS5611_PROM_WORDS_NB; i++) {
53 ret = st->read_prom_word(&indio_dev->dev, i, &st->prom[i]);
54 if (ret < 0) {
55 dev_err(&indio_dev->dev,
56 "failed to read prom at %d\n", i);
57 return ret;
58 }
59 }
60
61 if (!ms5611_prom_is_valid(st->prom, MS5611_PROM_WORDS_NB)) {
62 dev_err(&indio_dev->dev, "PROM integrity check failed\n");
63 return -ENODEV;
64 }
65
66 return 0;
67}
68
69static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
70 s32 *temp, s32 *pressure)
71{
72 int ret;
73 s32 t, p;
74 s64 off, sens, dt;
75 struct ms5611_state *st = iio_priv(indio_dev);
76
77 ret = st->read_adc_temp_and_pressure(&indio_dev->dev, &t, &p);
78 if (ret < 0) {
79 dev_err(&indio_dev->dev,
80 "failed to read temperature and pressure\n");
81 return ret;
82 }
83
84 dt = t - (st->prom[5] << 8);
85 off = ((s64)st->prom[2] << 16) + ((st->prom[4] * dt) >> 7);
86 sens = ((s64)st->prom[1] << 15) + ((st->prom[3] * dt) >> 8);
87
88 t = 2000 + ((st->prom[6] * dt) >> 23);
89 if (t < 2000) {
90 s64 off2, sens2, t2;
91
92 t2 = (dt * dt) >> 31;
93 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
94 sens2 = off2 >> 1;
95
96 if (t < -1500) {
97 s64 tmp = (t + 1500) * (t + 1500);
98
99 off2 += 7 * tmp;
100 sens2 += (11 * tmp) >> 1;
101 }
102
103 t -= t2;
104 off -= off2;
105 sens -= sens2;
106 }
107
108 *temp = t;
109 *pressure = (((p * sens) >> 21) - off) >> 15;
110
111 return 0;
112}
113
114static int ms5611_reset(struct iio_dev *indio_dev)
115{
116 int ret;
117 struct ms5611_state *st = iio_priv(indio_dev);
118
119 ret = st->reset(&indio_dev->dev);
120 if (ret < 0) {
121 dev_err(&indio_dev->dev, "failed to reset device\n");
122 return ret;
123 }
124
125 usleep_range(3000, 4000);
126
127 return 0;
128}
129
130static int ms5611_read_raw(struct iio_dev *indio_dev,
131 struct iio_chan_spec const *chan,
132 int *val, int *val2, long mask)
133{
134 int ret;
135 s32 temp, pressure;
136 struct ms5611_state *st = iio_priv(indio_dev);
137
138 switch (mask) {
139 case IIO_CHAN_INFO_PROCESSED:
140 mutex_lock(&st->lock);
141 ret = ms5611_read_temp_and_pressure(indio_dev,
142 &temp, &pressure);
143 mutex_unlock(&st->lock);
144 if (ret < 0)
145 return ret;
146
147 switch (chan->type) {
148 case IIO_TEMP:
149 *val = temp * 10;
150 return IIO_VAL_INT;
151 case IIO_PRESSURE:
152 *val = pressure / 1000;
153 *val2 = (pressure % 1000) * 1000;
154 return IIO_VAL_INT_PLUS_MICRO;
155 default:
156 return -EINVAL;
157 }
158 }
159
160 return -EINVAL;
161}
162
163static const struct iio_chan_spec ms5611_channels[] = {
164 {
165 .type = IIO_PRESSURE,
166 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
167 BIT(IIO_CHAN_INFO_SCALE)
168 },
169 {
170 .type = IIO_TEMP,
171 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
172 BIT(IIO_CHAN_INFO_SCALE)
173 }
174};
175
176static const struct iio_info ms5611_info = {
177 .read_raw = &ms5611_read_raw,
178 .driver_module = THIS_MODULE,
179};
180
181static int ms5611_init(struct iio_dev *indio_dev)
182{
183 int ret;
184
185 ret = ms5611_reset(indio_dev);
186 if (ret < 0)
187 return ret;
188
189 return ms5611_read_prom(indio_dev);
190}
191
192int ms5611_probe(struct iio_dev *indio_dev, struct device *dev)
193{
194 int ret;
195 struct ms5611_state *st = iio_priv(indio_dev);
196
197 mutex_init(&st->lock);
198 indio_dev->dev.parent = dev;
199 indio_dev->name = dev->driver->name;
200 indio_dev->info = &ms5611_info;
201 indio_dev->channels = ms5611_channels;
202 indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
203 indio_dev->modes = INDIO_DIRECT_MODE;
204
205 ret = ms5611_init(indio_dev);
206 if (ret < 0)
207 return ret;
208
209 return devm_iio_device_register(dev, indio_dev);
210}
211EXPORT_SYMBOL(ms5611_probe);
212
213MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
214MODULE_DESCRIPTION("MS5611 core driver");
215MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/ms5611_i2c.c b/drivers/iio/pressure/ms5611_i2c.c
new file mode 100644
index 000000000000..748fd9acaad8
--- /dev/null
+++ b/drivers/iio/pressure/ms5611_i2c.c
@@ -0,0 +1,128 @@
1/*
2 * MS5611 pressure and temperature sensor driver (I2C bus)
3 *
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * 7-bit I2C slave addresses:
11 *
12 * 0x77 (CSB pin low)
13 * 0x76 (CSB pin high)
14 *
15 */
16
17#include <linux/delay.h>
18#include <linux/i2c.h>
19#include <linux/module.h>
20
21#include "ms5611.h"
22
23static int ms5611_i2c_reset(struct device *dev)
24{
25 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
26
27 return i2c_smbus_write_byte(st->client, MS5611_RESET);
28}
29
30static int ms5611_i2c_read_prom_word(struct device *dev, int index, u16 *word)
31{
32 int ret;
33 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
34
35 ret = i2c_smbus_read_word_swapped(st->client,
36 MS5611_READ_PROM_WORD + (index << 1));
37 if (ret < 0)
38 return ret;
39
40 *word = ret;
41
42 return 0;
43}
44
45static int ms5611_i2c_read_adc(struct ms5611_state *st, s32 *val)
46{
47 int ret;
48 u8 buf[3];
49
50 ret = i2c_smbus_read_i2c_block_data(st->client, MS5611_READ_ADC,
51 3, buf);
52 if (ret < 0)
53 return ret;
54
55 *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
56
57 return 0;
58}
59
60static int ms5611_i2c_read_adc_temp_and_pressure(struct device *dev,
61 s32 *temp, s32 *pressure)
62{
63 int ret;
64 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
65
66 ret = i2c_smbus_write_byte(st->client, MS5611_START_TEMP_CONV);
67 if (ret < 0)
68 return ret;
69
70 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
71
72 ret = ms5611_i2c_read_adc(st, temp);
73 if (ret < 0)
74 return ret;
75
76 ret = i2c_smbus_write_byte(st->client, MS5611_START_PRESSURE_CONV);
77 if (ret < 0)
78 return ret;
79
80 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
81
82 return ms5611_i2c_read_adc(st, pressure);
83}
84
85static int ms5611_i2c_probe(struct i2c_client *client,
86 const struct i2c_device_id *id)
87{
88 struct ms5611_state *st;
89 struct iio_dev *indio_dev;
90
91 if (!i2c_check_functionality(client->adapter,
92 I2C_FUNC_SMBUS_WRITE_BYTE |
93 I2C_FUNC_SMBUS_READ_WORD_DATA |
94 I2C_FUNC_SMBUS_READ_I2C_BLOCK))
95 return -ENODEV;
96
97 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
98 if (!indio_dev)
99 return -ENOMEM;
100
101 st = iio_priv(indio_dev);
102 st->reset = ms5611_i2c_reset;
103 st->read_prom_word = ms5611_i2c_read_prom_word;
104 st->read_adc_temp_and_pressure = ms5611_i2c_read_adc_temp_and_pressure;
105 st->client = client;
106
107 return ms5611_probe(indio_dev, &client->dev);
108}
109
110static const struct i2c_device_id ms5611_id[] = {
111 { "ms5611", 0 },
112 { }
113};
114MODULE_DEVICE_TABLE(i2c, ms5611_id);
115
116static struct i2c_driver ms5611_driver = {
117 .driver = {
118 .name = "ms5611",
119 .owner = THIS_MODULE,
120 },
121 .id_table = ms5611_id,
122 .probe = ms5611_i2c_probe,
123};
124module_i2c_driver(ms5611_driver);
125
126MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
127MODULE_DESCRIPTION("MS5611 i2c driver");
128MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/ms5611_spi.c b/drivers/iio/pressure/ms5611_spi.c
new file mode 100644
index 000000000000..976726fd4e6c
--- /dev/null
+++ b/drivers/iio/pressure/ms5611_spi.c
@@ -0,0 +1,127 @@
1/*
2 * MS5611 pressure and temperature sensor driver (SPI bus)
3 *
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/delay.h>
13#include <linux/module.h>
14#include <linux/spi/spi.h>
15
16#include "ms5611.h"
17
18static int ms5611_spi_reset(struct device *dev)
19{
20 u8 cmd = MS5611_RESET;
21 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
22
23 return spi_write_then_read(st->client, &cmd, 1, NULL, 0);
24}
25
26static int ms5611_spi_read_prom_word(struct device *dev, int index, u16 *word)
27{
28 int ret;
29 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
30
31 ret = spi_w8r16be(st->client, MS5611_READ_PROM_WORD + (index << 1));
32 if (ret < 0)
33 return ret;
34
35 *word = ret;
36
37 return 0;
38}
39
40static int ms5611_spi_read_adc(struct device *dev, s32 *val)
41{
42 int ret;
43 u8 buf[3] = { MS5611_READ_ADC };
44 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
45
46 ret = spi_write_then_read(st->client, buf, 1, buf, 3);
47 if (ret < 0)
48 return ret;
49
50 *val = (buf[0] << 16) | (buf[1] << 8) | buf[2];
51
52 return 0;
53}
54
55static int ms5611_spi_read_adc_temp_and_pressure(struct device *dev,
56 s32 *temp, s32 *pressure)
57{
58 u8 cmd;
59 int ret;
60 struct ms5611_state *st = iio_priv(dev_to_iio_dev(dev));
61
62 cmd = MS5611_START_TEMP_CONV;
63 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
64 if (ret < 0)
65 return ret;
66
67 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
68
69 ret = ms5611_spi_read_adc(dev, temp);
70 if (ret < 0)
71 return ret;
72
73 cmd = MS5611_START_PRESSURE_CONV;
74 ret = spi_write_then_read(st->client, &cmd, 1, NULL, 0);
75 if (ret < 0)
76 return ret;
77
78 usleep_range(MS5611_CONV_TIME_MIN, MS5611_CONV_TIME_MAX);
79
80 return ms5611_spi_read_adc(dev, pressure);
81}
82
83static int ms5611_spi_probe(struct spi_device *spi)
84{
85 int ret;
86 struct ms5611_state *st;
87 struct iio_dev *indio_dev;
88
89 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
90 if (!indio_dev)
91 return -ENOMEM;
92
93 spi->mode = SPI_MODE_0;
94 spi->max_speed_hz = 20000000;
95 spi->bits_per_word = 8;
96 ret = spi_setup(spi);
97 if (ret < 0)
98 return ret;
99
100 st = iio_priv(indio_dev);
101 st->reset = ms5611_spi_reset;
102 st->read_prom_word = ms5611_spi_read_prom_word;
103 st->read_adc_temp_and_pressure = ms5611_spi_read_adc_temp_and_pressure;
104 st->client = spi;
105
106 return ms5611_probe(indio_dev, &spi->dev);
107}
108
109static const struct spi_device_id ms5611_id[] = {
110 { "ms5611", 0 },
111 { }
112};
113MODULE_DEVICE_TABLE(spi, ms5611_id);
114
115static struct spi_driver ms5611_driver = {
116 .driver = {
117 .name = "ms5611",
118 .owner = THIS_MODULE,
119 },
120 .id_table = ms5611_id,
121 .probe = ms5611_spi_probe,
122};
123module_spi_driver(ms5611_driver);
124
125MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
126MODULE_DESCRIPTION("MS5611 spi driver");
127MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/proximity/sx9500.c b/drivers/iio/proximity/sx9500.c
index 89fca3a70750..fa40f6d0ca39 100644
--- a/drivers/iio/proximity/sx9500.c
+++ b/drivers/iio/proximity/sx9500.c
@@ -618,16 +618,12 @@ static int sx9500_gpio_probe(struct i2c_client *client,
618 dev = &client->dev; 618 dev = &client->dev;
619 619
620 /* data ready gpio interrupt pin */ 620 /* data ready gpio interrupt pin */
621 gpio = devm_gpiod_get_index(dev, SX9500_GPIO_NAME, 0); 621 gpio = devm_gpiod_get_index(dev, SX9500_GPIO_NAME, 0, GPIOD_IN);
622 if (IS_ERR(gpio)) { 622 if (IS_ERR(gpio)) {
623 dev_err(dev, "acpi gpio get index failed\n"); 623 dev_err(dev, "acpi gpio get index failed\n");
624 return PTR_ERR(gpio); 624 return PTR_ERR(gpio);
625 } 625 }
626 626
627 ret = gpiod_direction_input(gpio);
628 if (ret)
629 return ret;
630
631 ret = gpiod_to_irq(gpio); 627 ret = gpiod_to_irq(gpio);
632 628
633 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 629 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
diff --git a/drivers/iio/temperature/mlx90614.c b/drivers/iio/temperature/mlx90614.c
index c8b6ac8b2d69..a112fc9abf43 100644
--- a/drivers/iio/temperature/mlx90614.c
+++ b/drivers/iio/temperature/mlx90614.c
@@ -2,6 +2,7 @@
2 * mlx90614.c - Support for Melexis MLX90614 contactless IR temperature sensor 2 * mlx90614.c - Support for Melexis MLX90614 contactless IR temperature sensor
3 * 3 *
4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net> 4 * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
5 * Copyright (c) 2015 Essensium NV
5 * 6 *
6 * This file is subject to the terms and conditions of version 2 of 7 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main 8 * the GNU General Public License. See the file COPYING in the main
@@ -20,11 +21,35 @@
20 21
21#include <linux/iio/iio.h> 22#include <linux/iio/iio.h>
22 23
23#define MLX90614_OP_RAM 0x00 24#define MLX90614_OP_RAM 0x00
25#define MLX90614_OP_EEPROM 0x20
26#define MLX90614_OP_SLEEP 0xff
24 27
25/* RAM offsets with 16-bit data, MSB first */ 28/* RAM offsets with 16-bit data, MSB first */
26#define MLX90614_TA 0x06 /* ambient temperature */ 29#define MLX90614_RAW1 (MLX90614_OP_RAM | 0x04) /* raw data IR channel 1 */
27#define MLX90614_TOBJ1 0x07 /* object temperature */ 30#define MLX90614_RAW2 (MLX90614_OP_RAM | 0x05) /* raw data IR channel 2 */
31#define MLX90614_TA (MLX90614_OP_RAM | 0x06) /* ambient temperature */
32#define MLX90614_TOBJ1 (MLX90614_OP_RAM | 0x07) /* object 1 temperature */
33#define MLX90614_TOBJ2 (MLX90614_OP_RAM | 0x08) /* object 2 temperature */
34
35/* EEPROM offsets with 16-bit data, MSB first */
36#define MLX90614_EMISSIVITY (MLX90614_OP_EEPROM | 0x04) /* emissivity correction coefficient */
37#define MLX90614_CONFIG (MLX90614_OP_EEPROM | 0x05) /* configuration register */
38
39/* Control bits in configuration register */
40#define MLX90614_CONFIG_IIR_SHIFT 0 /* IIR coefficient */
41#define MLX90614_CONFIG_IIR_MASK (0x7 << MLX90614_CONFIG_IIR_SHIFT)
42#define MLX90614_CONFIG_DUAL_SHIFT 6 /* single (0) or dual (1) IR sensor */
43#define MLX90614_CONFIG_DUAL_MASK (1 << MLX90614_CONFIG_DUAL_SHIFT)
44#define MLX90614_CONFIG_FIR_SHIFT 8 /* FIR coefficient */
45#define MLX90614_CONFIG_FIR_MASK (0x7 << MLX90614_CONFIG_FIR_SHIFT)
46#define MLX90614_CONFIG_GAIN_SHIFT 11 /* gain */
47#define MLX90614_CONFIG_GAIN_MASK (0x7 << MLX90614_CONFIG_GAIN_SHIFT)
48
49/* Timings (in ms) */
50#define MLX90614_TIMING_EEPROM 20 /* time for EEPROM write/erase to complete */
51#define MLX90614_TIMING_WAKEUP 34 /* time to hold SDA low for wake-up */
52#define MLX90614_TIMING_STARTUP 250 /* time before first data after wake-up */
28 53
29struct mlx90614_data { 54struct mlx90614_data {
30 struct i2c_client *client; 55 struct i2c_client *client;
@@ -35,26 +60,34 @@ static int mlx90614_read_raw(struct iio_dev *indio_dev,
35 int *val2, long mask) 60 int *val2, long mask)
36{ 61{
37 struct mlx90614_data *data = iio_priv(indio_dev); 62 struct mlx90614_data *data = iio_priv(indio_dev);
63 u8 cmd;
38 s32 ret; 64 s32 ret;
39 65
40 switch (mask) { 66 switch (mask) {
41 case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */ 67 case IIO_CHAN_INFO_RAW: /* 0.02K / LSB */
42 switch (channel->channel2) { 68 switch (channel->channel2) {
43 case IIO_MOD_TEMP_AMBIENT: 69 case IIO_MOD_TEMP_AMBIENT:
44 ret = i2c_smbus_read_word_data(data->client, 70 cmd = MLX90614_TA;
45 MLX90614_OP_RAM | MLX90614_TA);
46 if (ret < 0)
47 return ret;
48 break; 71 break;
49 case IIO_MOD_TEMP_OBJECT: 72 case IIO_MOD_TEMP_OBJECT:
50 ret = i2c_smbus_read_word_data(data->client, 73 switch (channel->channel) {
51 MLX90614_OP_RAM | MLX90614_TOBJ1); 74 case 0:
52 if (ret < 0) 75 cmd = MLX90614_TOBJ1;
53 return ret; 76 break;
77 case 1:
78 cmd = MLX90614_TOBJ2;
79 break;
80 default:
81 return -EINVAL;
82 }
54 break; 83 break;
55 default: 84 default:
56 return -EINVAL; 85 return -EINVAL;
57 } 86 }
87
88 ret = i2c_smbus_read_word_data(data->client, cmd);
89 if (ret < 0)
90 return ret;
58 *val = ret; 91 *val = ret;
59 return IIO_VAL_INT; 92 return IIO_VAL_INT;
60 case IIO_CHAN_INFO_OFFSET: 93 case IIO_CHAN_INFO_OFFSET:
@@ -86,6 +119,16 @@ static const struct iio_chan_spec mlx90614_channels[] = {
86 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 119 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
87 BIT(IIO_CHAN_INFO_SCALE), 120 BIT(IIO_CHAN_INFO_SCALE),
88 }, 121 },
122 {
123 .type = IIO_TEMP,
124 .indexed = 1,
125 .modified = 1,
126 .channel = 1,
127 .channel2 = IIO_MOD_TEMP_OBJECT,
128 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
129 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
130 BIT(IIO_CHAN_INFO_SCALE),
131 },
89}; 132};
90 133
91static const struct iio_info mlx90614_info = { 134static const struct iio_info mlx90614_info = {
@@ -93,11 +136,25 @@ static const struct iio_info mlx90614_info = {
93 .driver_module = THIS_MODULE, 136 .driver_module = THIS_MODULE,
94}; 137};
95 138
139/* Return 0 for single sensor, 1 for dual sensor, <0 on error. */
140static int mlx90614_probe_num_ir_sensors(struct i2c_client *client)
141{
142 s32 ret;
143
144 ret = i2c_smbus_read_word_data(client, MLX90614_CONFIG);
145
146 if (ret < 0)
147 return ret;
148
149 return (ret & MLX90614_CONFIG_DUAL_MASK) ? 1 : 0;
150}
151
96static int mlx90614_probe(struct i2c_client *client, 152static int mlx90614_probe(struct i2c_client *client,
97 const struct i2c_device_id *id) 153 const struct i2c_device_id *id)
98{ 154{
99 struct iio_dev *indio_dev; 155 struct iio_dev *indio_dev;
100 struct mlx90614_data *data; 156 struct mlx90614_data *data;
157 int ret;
101 158
102 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 159 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
103 return -ENODEV; 160 return -ENODEV;
@@ -115,8 +172,21 @@ static int mlx90614_probe(struct i2c_client *client,
115 indio_dev->modes = INDIO_DIRECT_MODE; 172 indio_dev->modes = INDIO_DIRECT_MODE;
116 indio_dev->info = &mlx90614_info; 173 indio_dev->info = &mlx90614_info;
117 174
118 indio_dev->channels = mlx90614_channels; 175 ret = mlx90614_probe_num_ir_sensors(client);
119 indio_dev->num_channels = ARRAY_SIZE(mlx90614_channels); 176 switch (ret) {
177 case 0:
178 dev_dbg(&client->dev, "Found single sensor");
179 indio_dev->channels = mlx90614_channels;
180 indio_dev->num_channels = 2;
181 break;
182 case 1:
183 dev_dbg(&client->dev, "Found dual sensor");
184 indio_dev->channels = mlx90614_channels;
185 indio_dev->num_channels = 3;
186 break;
187 default:
188 return ret;
189 }
120 190
121 return iio_device_register(indio_dev); 191 return iio_device_register(indio_dev);
122} 192}
@@ -146,5 +216,6 @@ static struct i2c_driver mlx90614_driver = {
146module_i2c_driver(mlx90614_driver); 216module_i2c_driver(mlx90614_driver);
147 217
148MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 218MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
219MODULE_AUTHOR("Vianney le Clément de Saint-Marcq <vianney.leclement@essensium.com>");
149MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver"); 220MODULE_DESCRIPTION("Melexis MLX90614 contactless IR temperature sensor driver");
150MODULE_LICENSE("GPL"); 221MODULE_LICENSE("GPL");