aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/trigger/stm32-timer-trigger.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-08-27 20:03:33 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-08-27 20:03:33 -0400
commitc3c162635f4f064af5c1d531be6355d6b23e3d0c (patch)
treef33464b15c6baaf55e2c330ee46a88828526c158 /drivers/iio/trigger/stm32-timer-trigger.c
parentfff4e7a0e680b29bb8006cc37921562ef4b8e8df (diff)
parent2c68888f1d76c668024a9efa50af382df4f3cf8e (diff)
Merge tag 'staging-4.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging/iio fixes from Greg KH: "Here are few small staging driver fixes, and some more IIO driver fixes for 4.13-rc7. Nothing major, just resolutions for some reported problems. All of these have been in linux-next with no reported problems" * tag 'staging-4.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: iio: magnetometer: st_magn: remove ihl property for LSM303AGR iio: magnetometer: st_magn: fix status register address for LSM303AGR iio: hid-sensor-trigger: Fix the race with user space powering up sensors iio: trigger: stm32-timer: fix get trigger mode iio: imu: adis16480: Fix acceleration scale factor for adis16480 PATCH] iio: Fix some documentation warnings staging: rtl8188eu: add RNX-N150NUB support Revert "staging: fsl-mc: be consistent when checking strcmp() return" iio: adc: stm32: fix common clock rate iio: adc: ina219: Avoid underflow for sleeping time iio: trigger: stm32-timer: add enable attribute iio: trigger: stm32-timer: fix get/set down count direction iio: trigger: stm32-timer: fix write_raw return value iio: trigger: stm32-timer: fix quadrature mode get routine iio: bmp280: properly initialize device for humidity reading
Diffstat (limited to 'drivers/iio/trigger/stm32-timer-trigger.c')
-rw-r--r--drivers/iio/trigger/stm32-timer-trigger.c82
1 files changed, 59 insertions, 23 deletions
diff --git a/drivers/iio/trigger/stm32-timer-trigger.c b/drivers/iio/trigger/stm32-timer-trigger.c
index d22bc56dd9fc..25ad6abfee22 100644
--- a/drivers/iio/trigger/stm32-timer-trigger.c
+++ b/drivers/iio/trigger/stm32-timer-trigger.c
@@ -366,34 +366,32 @@ static int stm32_counter_read_raw(struct iio_dev *indio_dev,
366 int *val, int *val2, long mask) 366 int *val, int *val2, long mask)
367{ 367{
368 struct stm32_timer_trigger *priv = iio_priv(indio_dev); 368 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
369 u32 dat;
369 370
370 switch (mask) { 371 switch (mask) {
371 case IIO_CHAN_INFO_RAW: 372 case IIO_CHAN_INFO_RAW:
372 { 373 regmap_read(priv->regmap, TIM_CNT, &dat);
373 u32 cnt; 374 *val = dat;
374 375 return IIO_VAL_INT;
375 regmap_read(priv->regmap, TIM_CNT, &cnt);
376 *val = cnt;
377 376
377 case IIO_CHAN_INFO_ENABLE:
378 regmap_read(priv->regmap, TIM_CR1, &dat);
379 *val = (dat & TIM_CR1_CEN) ? 1 : 0;
378 return IIO_VAL_INT; 380 return IIO_VAL_INT;
379 }
380 case IIO_CHAN_INFO_SCALE:
381 {
382 u32 smcr;
383 381
384 regmap_read(priv->regmap, TIM_SMCR, &smcr); 382 case IIO_CHAN_INFO_SCALE:
385 smcr &= TIM_SMCR_SMS; 383 regmap_read(priv->regmap, TIM_SMCR, &dat);
384 dat &= TIM_SMCR_SMS;
386 385
387 *val = 1; 386 *val = 1;
388 *val2 = 0; 387 *val2 = 0;
389 388
390 /* in quadrature case scale = 0.25 */ 389 /* in quadrature case scale = 0.25 */
391 if (smcr == 3) 390 if (dat == 3)
392 *val2 = 2; 391 *val2 = 2;
393 392
394 return IIO_VAL_FRACTIONAL_LOG2; 393 return IIO_VAL_FRACTIONAL_LOG2;
395 } 394 }
396 }
397 395
398 return -EINVAL; 396 return -EINVAL;
399} 397}
@@ -403,15 +401,31 @@ static int stm32_counter_write_raw(struct iio_dev *indio_dev,
403 int val, int val2, long mask) 401 int val, int val2, long mask)
404{ 402{
405 struct stm32_timer_trigger *priv = iio_priv(indio_dev); 403 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
404 u32 dat;
406 405
407 switch (mask) { 406 switch (mask) {
408 case IIO_CHAN_INFO_RAW: 407 case IIO_CHAN_INFO_RAW:
409 regmap_write(priv->regmap, TIM_CNT, val); 408 return regmap_write(priv->regmap, TIM_CNT, val);
410 409
411 return IIO_VAL_INT;
412 case IIO_CHAN_INFO_SCALE: 410 case IIO_CHAN_INFO_SCALE:
413 /* fixed scale */ 411 /* fixed scale */
414 return -EINVAL; 412 return -EINVAL;
413
414 case IIO_CHAN_INFO_ENABLE:
415 if (val) {
416 regmap_read(priv->regmap, TIM_CR1, &dat);
417 if (!(dat & TIM_CR1_CEN))
418 clk_enable(priv->clk);
419 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
420 TIM_CR1_CEN);
421 } else {
422 regmap_read(priv->regmap, TIM_CR1, &dat);
423 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN,
424 0);
425 if (dat & TIM_CR1_CEN)
426 clk_disable(priv->clk);
427 }
428 return 0;
415 } 429 }
416 430
417 return -EINVAL; 431 return -EINVAL;
@@ -471,7 +485,7 @@ static int stm32_get_trigger_mode(struct iio_dev *indio_dev,
471 485
472 regmap_read(priv->regmap, TIM_SMCR, &smcr); 486 regmap_read(priv->regmap, TIM_SMCR, &smcr);
473 487
474 return smcr == TIM_SMCR_SMS ? 0 : -EINVAL; 488 return (smcr & TIM_SMCR_SMS) == TIM_SMCR_SMS ? 0 : -EINVAL;
475} 489}
476 490
477static const struct iio_enum stm32_trigger_mode_enum = { 491static const struct iio_enum stm32_trigger_mode_enum = {
@@ -507,9 +521,19 @@ static int stm32_set_enable_mode(struct iio_dev *indio_dev,
507{ 521{
508 struct stm32_timer_trigger *priv = iio_priv(indio_dev); 522 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
509 int sms = stm32_enable_mode2sms(mode); 523 int sms = stm32_enable_mode2sms(mode);
524 u32 val;
510 525
511 if (sms < 0) 526 if (sms < 0)
512 return sms; 527 return sms;
528 /*
529 * Triggered mode sets CEN bit automatically by hardware. So, first
530 * enable counter clock, so it can use it. Keeps it in sync with CEN.
531 */
532 if (sms == 6) {
533 regmap_read(priv->regmap, TIM_CR1, &val);
534 if (!(val & TIM_CR1_CEN))
535 clk_enable(priv->clk);
536 }
513 537
514 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms); 538 regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
515 539
@@ -571,11 +595,14 @@ static int stm32_get_quadrature_mode(struct iio_dev *indio_dev,
571{ 595{
572 struct stm32_timer_trigger *priv = iio_priv(indio_dev); 596 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
573 u32 smcr; 597 u32 smcr;
598 int mode;
574 599
575 regmap_read(priv->regmap, TIM_SMCR, &smcr); 600 regmap_read(priv->regmap, TIM_SMCR, &smcr);
576 smcr &= TIM_SMCR_SMS; 601 mode = (smcr & TIM_SMCR_SMS) - 1;
602 if ((mode < 0) || (mode > ARRAY_SIZE(stm32_quadrature_modes)))
603 return -EINVAL;
577 604
578 return smcr - 1; 605 return mode;
579} 606}
580 607
581static const struct iio_enum stm32_quadrature_mode_enum = { 608static const struct iio_enum stm32_quadrature_mode_enum = {
@@ -592,13 +619,20 @@ static const char *const stm32_count_direction_states[] = {
592 619
593static int stm32_set_count_direction(struct iio_dev *indio_dev, 620static int stm32_set_count_direction(struct iio_dev *indio_dev,
594 const struct iio_chan_spec *chan, 621 const struct iio_chan_spec *chan,
595 unsigned int mode) 622 unsigned int dir)
596{ 623{
597 struct stm32_timer_trigger *priv = iio_priv(indio_dev); 624 struct stm32_timer_trigger *priv = iio_priv(indio_dev);
625 u32 val;
626 int mode;
598 627
599 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_DIR, mode); 628 /* In encoder mode, direction is RO (given by TI1/TI2 signals) */
629 regmap_read(priv->regmap, TIM_SMCR, &val);
630 mode = (val & TIM_SMCR_SMS) - 1;
631 if ((mode >= 0) || (mode < ARRAY_SIZE(stm32_quadrature_modes)))
632 return -EBUSY;
600 633
601 return 0; 634 return regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_DIR,
635 dir ? TIM_CR1_DIR : 0);
602} 636}
603 637
604static int stm32_get_count_direction(struct iio_dev *indio_dev, 638static int stm32_get_count_direction(struct iio_dev *indio_dev,
@@ -609,7 +643,7 @@ static int stm32_get_count_direction(struct iio_dev *indio_dev,
609 643
610 regmap_read(priv->regmap, TIM_CR1, &cr1); 644 regmap_read(priv->regmap, TIM_CR1, &cr1);
611 645
612 return (cr1 & TIM_CR1_DIR); 646 return ((cr1 & TIM_CR1_DIR) ? 1 : 0);
613} 647}
614 648
615static const struct iio_enum stm32_count_direction_enum = { 649static const struct iio_enum stm32_count_direction_enum = {
@@ -672,7 +706,9 @@ static const struct iio_chan_spec_ext_info stm32_trigger_count_info[] = {
672static const struct iio_chan_spec stm32_trigger_channel = { 706static const struct iio_chan_spec stm32_trigger_channel = {
673 .type = IIO_COUNT, 707 .type = IIO_COUNT,
674 .channel = 0, 708 .channel = 0,
675 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 709 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
710 BIT(IIO_CHAN_INFO_ENABLE) |
711 BIT(IIO_CHAN_INFO_SCALE),
676 .ext_info = stm32_trigger_count_info, 712 .ext_info = stm32_trigger_count_info,
677 .indexed = 1 713 .indexed = 1
678}; 714};