Input: adc-joystick: Handle inverted axes
Subsystems (2)
PATCH v2 1/1 Input: adc-joystick: Handle inverted axes
From: Chris Morgan <[email protected]>
When one or more axes are inverted, (where min > max), normalize the data so that min < max and invert the values reported to the input stack.
This ensures we can continue defining the device correctly in the device tree while not breaking downstream assumptions that min is always less than max.
Changes since V1:
- Moved proposed helper for inversion from input stack to adc-joystick driver.
--- drivers/input/joystick/adc-joystick.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/input/joystick/adc-joystick.c +20 -1
@@ -18,6 +18,7 @@ struct adc_joystick_axis {s32 range[2 ];s32 fuzz;s32 flat; +bool inverted; };struct adc_joystick {
@@ -29,6 +30,14 @@ struct adc_joystick {bool polled; }; +static int adc_joystick_invert (struct input_dev *dev,unsigned int axis,int val) +{ +int min = dev->absinfo[axis].minimum; +int max = dev->absinfo[axis].maximum; + +return (max + min) - val; +} +static void adc_joystick_poll (struct input_dev *input) {struct adc_joystick *joy =input_get_drvdata (input);
@@ -38,6 +47,8 @@ static void adc_joystick_poll(struct input_dev *input) ret =iio_read_channel_raw (&joy->chans[i], &val);if (ret <0 )return ; +if (joy->axes[i].inverted) + val =adc_joystick_invert (input, i, val);input_report_abs (input, joy->axes[i].code, val); }input_sync (input);
@@ -86,6 +97,8 @@ static int adc_joystick_handle(const void *data, void *private) val =sign_extend32 (val, msb); else val &= GENMASK(msb, 0); +if (joy->axes[i].inverted) + val =adc_joystick_invert (joy->input, i, val);input_report_abs (joy->input, joy->axes[i].code, val); }
@@ -168,11 +181,17 @@ static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy) goto err_fwnode_put; } +if (axes[i].range[0 ] > axes[i].range[1 ]) { +dev_dbg (dev,"abs-axis %d inverted\n" , i); + axes[i].inverted =1 ; + } +fwnode_property_read_u32 (child,"abs-fuzz" , &axes[i].fuzz);fwnode_property_read_u32 (child,"abs-flat" , &axes[i].flat);input_set_abs_params (joy->input, axes[i].code, - axes[i].range[0 ], axes[i].range[1 ], +min_array (axes[i].range,2 ), +max_array (axes[i].range,2 ), axes[i].fuzz, axes[i].flat);input_set_capability (joy->input,EV_ABS , axes[i].code); }
Signature · 1 line
2.34.1
Hi Chris,
some comments inline.
Quoted 6 lines · Chris Morgan
From: Chris Morgan <[email protected]>
When one or more axes are inverted, (where min > max), normalize the data so that min < max and invert the values reported to the input stack.
Quoted 7 lines
This ensures we can continue defining the device correctly in the device tree while not breaking downstream assumptions that min is always less than max.
Changes since V1:
- Moved proposed helper for inversion from input stack to adc-joystick driver.
The changes summary should go after the "---" separator, otherwise it ends up in the commit description.
Quoted 20 lines
Signed-off-by Chris Morgan <[email protected]>--- drivers/input/joystick/adc-joystick.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)drivers/input/joystick/adc-joystick.c +1
-0@@ -18,6 +18,7 @@ struct adc_joystick_axis {s32 range[2 ];s32 fuzz;s32 flat; +bool inverted; };struct adc_joystick {
drivers/input/joystick/adc-joystick.c struct adc_joystick { · line 34
Quoted 5 lines
drivers/input/joystick/adc-joystick.c +1
-0@@ -29,6 +30,14 @@ struct adc_joystick {bool polled; }; +static int adc_joystick_invert (struct input_dev *dev,unsigned int axis,int val)
As the parameter list exceeds 80 characters, can you move "int val" into a second line?
Quoted 31 lines
drivers/input/joystick/adc-joystick.c +7
-0+{ +int min = dev->absinfo[axis].minimum; +int max = dev->absinfo[axis].maximum; + +return (max + min) - val; +} +static void adc_joystick_poll (struct input_dev *input) {drivers/input/joystick/adc-joystick.c +2
-0struct adc_joystick *joy =input_get_drvdata (input);@@ -38,6 +47,8 @@ static void adc_joystick_poll(struct input_dev *input) ret =iio_read_channel_raw (&joy->chans[i], &val);if (ret <0 )return ; +if (joy->axes[i].inverted) + val =adc_joystick_invert (input, i, val);input_report_abs (input, joy->axes[i].code, val); }drivers/input/joystick/adc-joystick.c +2
-0input_sync (input);@@ -86,6 +97,8 @@ static int adc_joystick_handle(const void *data, void *private) val =sign_extend32 (val, msb); else val &= GENMASK(msb, 0); +if (joy->axes[i].inverted) + val =adc_joystick_invert (joy->input, i, val);input_report_abs (joy->input, joy->axes[i].code, val); }
drivers/input/joystick/adc-joystick.c static int adc_joystick_set_axes(struct device · line 186
Quoted 6 lines
drivers/input/joystick/adc-joystick.c +2
-0@@ -168,11 +181,17 @@ static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy) goto err_fwnode_put; } +if (axes[i].range[0 ] > axes[i].range[1 ]) { +dev_dbg (dev,"abs-axis %d inverted\n" , i);
There is no "abs-axis" property. Make it "Axis %d inverted\n" instead.
drivers/input/joystick/adc-joystick.c static int adc_joystick_set_axes(struct device *dev, struct adc_joystick *joy) · line 186
Quoted 1 line
drivers/input/joystick/adc-joystick.c +1
-0+ axes[i].inverted =1 ;
Turn this into an explicit "true", as its a bool.
With all above nits fixed:
Cheers,
Artur
Quoted 12 lines
drivers/input/joystick/adc-joystick.c +4
-1+ } +fwnode_property_read_u32 (child,"abs-fuzz" , &axes[i].fuzz);fwnode_property_read_u32 (child,"abs-flat" , &axes[i].flat);input_set_abs_params (joy->input, axes[i].code, - axes[i].range[0 ], axes[i].range[1 ], +min_array (axes[i].range,2 ), +max_array (axes[i].range,2 ), axes[i].fuzz, axes[i].flat);input_set_capability (joy->input,EV_ABS , axes[i].code);}