All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] input: ad714x - fix divide by 0 error in wheel cal_abs_pos
@ 2012-04-26 17:17 Jean-François Dagenais
  2012-04-26 17:17 ` [PATCH 2/2] input: ad714x - use stage qty to split max_coord for wheel algo Jean-François Dagenais
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-François Dagenais @ 2012-04-26 17:17 UTC (permalink / raw
  To: michael.hennerich
  Cc: dmitry.torokhov, linux-input, device-drivers-devel,
	Jean-François Dagenais, Jean-François Dagenais

V2: added comment, removed "BUG_ON" check and fixed >80 col line

As discussed with Michael Hennerich <michael.hennerich@analog.com>,
another divide by 0 happens when the user is lifting the finger
when all the sensors are back below ambient but we are in case
"ACTIVE".

Signed-off-by: Jean-François Dagenais <dagenaisj@sonatest.com>
Signed-off-by: Jean-François Dagenais <jeff.dagenais@gmail.com>
---
 drivers/input/misc/ad714x.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/input/misc/ad714x.c b/drivers/input/misc/ad714x.c
index ca42c7d..2cf26e5 100644
--- a/drivers/input/misc/ad714x.c
+++ b/drivers/input/misc/ad714x.c
@@ -418,10 +418,15 @@ static void ad714x_wheel_cal_highest_stage(struct ad714x_chip *ad714x, int idx)
 		sw->highest_stage);
 }
 
-static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
+/**
+ * Calculates the sensor value for each stages of the given wheel idx.
+ * Returns true if a sensor detects being touched, false otherwise.
+ */
+static bool ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
 {
 	struct ad714x_wheel_plat *hw = &ad714x->hw->wheel[idx];
 	int i;
+	bool touched = false;
 
 	ad714x->read(ad714x, CDC_RESULT_S0 + hw->start_stage,
 			&ad714x->adc_reg[hw->start_stage],
@@ -430,12 +435,15 @@ static void ad714x_wheel_cal_sensor_val(struct ad714x_chip *ad714x, int idx)
 	for (i = hw->start_stage; i <= hw->end_stage; i++) {
 		ad714x->read(ad714x, STAGE0_AMBIENT + i * PER_STAGE_REG_NUM,
 				&ad714x->amb_reg[i], 1);
-		if (ad714x->adc_reg[i] > ad714x->amb_reg[i])
+		if (ad714x->adc_reg[i] > ad714x->amb_reg[i]) {
 			ad714x->sensor_val[i] =
 				ad714x->adc_reg[i] - ad714x->amb_reg[i];
+			touched = true;
+		}
 		else
 			ad714x->sensor_val[i] = 0;
 	}
+	return touched;
 }
 
 /*
@@ -544,8 +552,8 @@ static void ad714x_wheel_state_machine(struct ad714x_chip *ad714x, int idx)
 
 	case ACTIVE:
 		if (c_state == mask) {
-			if (h_state) {
-				ad714x_wheel_cal_sensor_val(ad714x, idx);
+			if (h_state &&
+			    ad714x_wheel_cal_sensor_val(ad714x, idx)) {
 				ad714x_wheel_cal_highest_stage(ad714x, idx);
 				ad714x_wheel_cal_abs_pos(ad714x, idx);
 				ad714x_wheel_cal_flt_pos(ad714x, idx);
-- 
1.7.9

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] input: ad714x - use stage qty to split max_coord for wheel algo
  2012-04-26 17:17 [PATCH 1/2] input: ad714x - fix divide by 0 error in wheel cal_abs_pos Jean-François Dagenais
@ 2012-04-26 17:17 ` Jean-François Dagenais
  2012-04-27  1:33   ` Jean-François Dagenais
  0 siblings, 1 reply; 3+ messages in thread
From: Jean-François Dagenais @ 2012-04-26 17:17 UTC (permalink / raw
  To: michael.hennerich
  Cc: dmitry.torokhov, linux-input, device-drivers-devel,
	Jean-François Dagenais

Using "end_stage - start_stage" yields "number of used stages -1".
This makes the divisor smaller, hence the dividend greater.

So for example, with 8 (0..7) stages, and max_coord of 1024, the
algo should figure out that 1024 should be split in 8, so 8 slices
of 128. Instead, dividing by 7 will give bigger slices of 146.

While adding these "slices", when stages 7 and 0 are weighted to
determine the abs_pos, a dead-zone appears where we are stuck at
max_coord because of the bigger "slices" and the if that follows:
"if abs_pos > max_coord, then abs_pos = max_coord"

Hence this commit, which re-uses variable "stag_num" initialized with
the number of used stages.

Signed-off-by: Jean-François Dagenais <jeff.dagenais@gmail.com>
---
 drivers/input/misc/ad714x.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/drivers/input/misc/ad714x.c b/drivers/input/misc/ad714x.c
index 2cf26e5..29f5d10 100644
--- a/drivers/input/misc/ad714x.c
+++ b/drivers/input/misc/ad714x.c
@@ -477,8 +477,7 @@ static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
 		ad714x->sensor_val[first_before] +
 		ad714x->sensor_val[first_after];
 
-	sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *
-			a_param) / b_param;
+	sw->abs_pos = ((hw->max_coord / stage_num) * a_param) / b_param;
 
 	if (sw->abs_pos > hw->max_coord)
 		sw->abs_pos = hw->max_coord;
-- 
1.7.9

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] input: ad714x - use stage qty to split max_coord for wheel algo
  2012-04-26 17:17 ` [PATCH 2/2] input: ad714x - use stage qty to split max_coord for wheel algo Jean-François Dagenais
@ 2012-04-27  1:33   ` Jean-François Dagenais
  0 siblings, 0 replies; 3+ messages in thread
From: Jean-François Dagenais @ 2012-04-27  1:33 UTC (permalink / raw
  To: Jean-François Dagenais
  Cc: michael.hennerich, dmitry.torokhov, linux-input,
	device-drivers-devel

Sorry, it's flawed, this causes a skipped range near max_coord.
(i.e. from (max_coord - (max_coord/stage_num)/2) to max_coord)

Will need to investigate more.

On 2012-04-26, at 13:17, Jean-François Dagenais wrote:

> Using "end_stage - start_stage" yields "number of used stages -1".
> This makes the divisor smaller, hence the dividend greater.
> 
> So for example, with 8 (0..7) stages, and max_coord of 1024, the
> algo should figure out that 1024 should be split in 8, so 8 slices
> of 128. Instead, dividing by 7 will give bigger slices of 146.
> 
> While adding these "slices", when stages 7 and 0 are weighted to
> determine the abs_pos, a dead-zone appears where we are stuck at
> max_coord because of the bigger "slices" and the if that follows:
> "if abs_pos > max_coord, then abs_pos = max_coord"
> 
> Hence this commit, which re-uses variable "stag_num" initialized with
> the number of used stages.
> 
> Signed-off-by: Jean-François Dagenais <jeff.dagenais@gmail.com>
> ---
> drivers/input/misc/ad714x.c |    3 +--
> 1 files changed, 1 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/input/misc/ad714x.c b/drivers/input/misc/ad714x.c
> index 2cf26e5..29f5d10 100644
> --- a/drivers/input/misc/ad714x.c
> +++ b/drivers/input/misc/ad714x.c
> @@ -477,8 +477,7 @@ static void ad714x_wheel_cal_abs_pos(struct ad714x_chip *ad714x, int idx)
> 		ad714x->sensor_val[first_before] +
> 		ad714x->sensor_val[first_after];
> 
> -	sw->abs_pos = ((hw->max_coord / (hw->end_stage - hw->start_stage)) *
> -			a_param) / b_param;
> +	sw->abs_pos = ((hw->max_coord / stage_num) * a_param) / b_param;
> 
> 	if (sw->abs_pos > hw->max_coord)
> 		sw->abs_pos = hw->max_coord;
> -- 
> 1.7.9
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-input" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-04-27  1:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-26 17:17 [PATCH 1/2] input: ad714x - fix divide by 0 error in wheel cal_abs_pos Jean-François Dagenais
2012-04-26 17:17 ` [PATCH 2/2] input: ad714x - use stage qty to split max_coord for wheel algo Jean-François Dagenais
2012-04-27  1:33   ` Jean-François Dagenais

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.