From patchwork Sun Dec 31 20:56:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Morgan X-Patchwork-Id: 13507506 Received: from mail-oa1-f47.google.com (mail-oa1-f47.google.com [209.85.160.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 012FDBA2E for ; Sun, 31 Dec 2023 20:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z4jPum1f" Received: by mail-oa1-f47.google.com with SMTP id 586e51a60fabf-2053f5e97b2so484081fac.3 for ; Sun, 31 Dec 2023 12:56:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704056210; x=1704661010; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IOZ8rZbenKYNbmmaBMIB+79ZT/meJMpi+b7m7niorzQ=; b=Z4jPum1fCmyRSi6EL454ehejZigQYe2HWIj7OvAdWn0p68dtXLfn6Unb/eY6C1m6lC fE3RwsLsToNZA22/7vNStVQ+FB4PbKZJX3E8B7jL0TxbY+W0QMg9AWMs24Wex7cyj7dN ZZJ/HD0IbgUezI/B+lFtvpDo3O/Z4sYE8qwtlC/Be0T3E8Ria0zcGCl5dS/xBMK8+nG3 0YZeVCdcQiMGCgXvAT1AX8AobnytL6Qt7lPvDvD3BiFXupE+HoY8Abt4DwBZJI5wJpPf kMnZ+wrwNQyuqqUDnt6rIe0qmXQOnkmn1A52nABrbDS4aEFvOfTLi7ojik0fr0Czz4wC p01Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704056210; x=1704661010; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IOZ8rZbenKYNbmmaBMIB+79ZT/meJMpi+b7m7niorzQ=; b=pACSafoMaFEG2emuoaaDxq7behWKhwmMpV1kqzNIVarLUYNMiEx7DxLMfX6Oc6I4Wp bI7bvl/4kAxtqAqd1QOneYsWY6UKeuPEFnJx0uZ0E7JLADbYXPUHaAJ1WKst+zEeli3g MubTFkSnm+FA4+KFCfddCMs5cSVLIoTq498uTU+nvbDWz6LRaVzIiQJzm0SnxtwMGRtd lFVa/fQ16+CKq+MlFsM26/0RTitDGcQ7X63PERA0aMarh+d1WWVlVhKRJXJSV68b6KcT 14koVkSGCle+r+z5mipGEGT89wXOQbSwdJKdXZdCWwRU/ekM7sJL65gi7I0Sxr4DOhX1 vjTg== X-Gm-Message-State: AOJu0YxyEZ/bErQiEKhQiMaN67BtU4yHTzqXxu+v8/R2LKEcBCidSTkn G0qkIjZO+ltemL8ORundsf1vz2XAY4Nc7A== X-Google-Smtp-Source: AGHT+IGHvYe70NxEjhFN5mEhshjW6Y13FXnI/bBF3aK51wBx5gMGpXgg5hYZ1a4WRVX3FgXHNFVG/w== X-Received: by 2002:a05:6870:224d:b0:1fb:75a:de75 with SMTP id j13-20020a056870224d00b001fb075ade75mr17551631oaf.99.1704056209689; Sun, 31 Dec 2023 12:56:49 -0800 (PST) Received: from localhost.localdomain ([75.28.21.198]) by smtp.gmail.com with ESMTPSA id v1-20020a056870954100b00203d8ce415bsm4843404oal.50.2023.12.31.12.56.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Dec 2023 12:56:49 -0800 (PST) From: Chris Morgan To: linux-input@vger.kernel.org Cc: dmitry.torokhov@gmail.com, hdegoede@redhat.com, paul@crapouillou.net, peter.hutterer@who-t.net, svv@google.com, biswarupp@google.com, contact@artur-rojek.eu, Chris Morgan Subject: [PATCH 1/2] Input: add input_invert_abs() Date: Sun, 31 Dec 2023 14:56:42 -0600 Message-Id: <20231231205643.129435-2-macroalpha82@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231231205643.129435-1-macroalpha82@gmail.com> References: <20231231205643.129435-1-macroalpha82@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chris Morgan Add a helper function to make it easier for a driver to invert abs values when needed. It is up to the driver itself to track axes that need to be inverted and normalize the data before it is passed on. This function assumes that drivers will set the min and max values so that min < max and then will simply call this function each time the values need to be inverted. Signed-off-by: Chris Morgan --- drivers/input/input.c | 19 +++++++++++++++++++ include/linux/input.h | 1 + 2 files changed, 20 insertions(+) diff --git a/drivers/input/input.c b/drivers/input/input.c index 8c5fdb0f858a..f135aed165a1 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -552,6 +552,25 @@ void input_copy_abs(struct input_dev *dst, unsigned int dst_axis, } EXPORT_SYMBOL(input_copy_abs); +/** + * input_invert_abs - Invert the abs value for an inverted axis. + * @dev: Input device with absolute events + * @axis: ABS_* value selecting the destination axis for the event to + * invert. + * @val: Value to be inverted based on min and max values of the axis. + * + * Return an inverted value for a given ABS axis based on its min and + * max values. + */ +int input_invert_abs(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; +} +EXPORT_SYMBOL(input_invert_abs); + /** * input_grab_device - grabs device for exclusive use * @handle: input handle that wants to own the device diff --git a/include/linux/input.h b/include/linux/input.h index de6503c0edb8..deb5f8bb0ec7 100644 --- a/include/linux/input.h +++ b/include/linux/input.h @@ -477,6 +477,7 @@ void input_set_abs_params(struct input_dev *dev, unsigned int axis, int min, int max, int fuzz, int flat); void input_copy_abs(struct input_dev *dst, unsigned int dst_axis, const struct input_dev *src, unsigned int src_axis); +int input_invert_abs(struct input_dev *dev, unsigned int axis, int val); #define INPUT_GENERATE_ABS_ACCESSORS(_suffix, _item) \ static inline int input_abs_get_##_suffix(struct input_dev *dev, \ From patchwork Sun Dec 31 20:56:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Morgan X-Patchwork-Id: 13507507 Received: from mail-oo1-f49.google.com (mail-oo1-f49.google.com [209.85.161.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC6ABBA3F for ; Sun, 31 Dec 2023 20:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ANRMX9nM" Received: by mail-oo1-f49.google.com with SMTP id 006d021491bc7-59426ca689cso4252727eaf.0 for ; Sun, 31 Dec 2023 12:56:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704056210; x=1704661010; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YFjD0OofEpNNXwvQ9D6zzIIWI1ti5uwannNYZ+Y+q54=; b=ANRMX9nM99kKUYgNg6Ks2y+G9CLinJb/BfORNC6+0dWbWe4yf9VK1pgK0cMqoEN4d6 eSNKAz+VKlc50ZSTbgVmvhp5nm0Z+s67Ql9nBjPnGgYq1K+nMPyLqsBRdGaThjXDQddy AgJK17suvuLaX7rLxSPNP0RTlvCoMli1ezxX0iecXaEzGEONjgelxt7VywO2BWj8kO6F C+QEZV8vi95DZhD7mAnk9Z8vHOdSP1z1bv/wWP0eSvqPrqq13q+eIlLQDnm+k3kG29N+ e45CMug1LOLXIZdd+Bfqcf18nC/pJljuCM9A6wbkXezT3wBtjubb6kDNRFqNr/PFziPu HR3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704056210; x=1704661010; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YFjD0OofEpNNXwvQ9D6zzIIWI1ti5uwannNYZ+Y+q54=; b=Q2kdJsrYfYkTcFcFbnKd3c3OXcQuDYtEAQYa9sPf4DVyMSFtJ6KNOVlM/zmEjaz05X Em22PFpMjLxbxpcx9XNryXlTiZ7xHKniJGYgfDOKzlxFgAjx8KnOhaMkeMQClM3Gf1EX rUw4ROu/Lu3ITXVcK22ZhfQ8bpJzf3V0tvn+NVCabJxfPNKL3OZRnwQFV08f8JbSUn/W a31ScHrZ842cQth9JIYEvw4kuK9MSxc7yYZ8ak9NplGFkUd0Xn257HE87mEEgorc3OIa qOgvcEncbBLgxCvhejtNAjurKManGdK88XQzxFnvM4do7HyqoKQsO6EXzo6Nwbaw+h1p qY2A== X-Gm-Message-State: AOJu0Yxp5/ee2YRubKeMUR8KfETFIOXhEWRn/Hz9JAe/avo+tUXM7sGg 5GzS+bEF70d28mAJaEhllEqHD5PyVyhiMg== X-Google-Smtp-Source: AGHT+IGcXC0aRB3Smo/fkIKGE13NrCAAJBBbFmJjT/zmDT+gC8y8kKSWXUJPKOhjbyAXmrnS4B3/Sw== X-Received: by 2002:a05:6870:d148:b0:204:369e:74b9 with SMTP id f8-20020a056870d14800b00204369e74b9mr11205235oac.7.1704056210444; Sun, 31 Dec 2023 12:56:50 -0800 (PST) Received: from localhost.localdomain ([75.28.21.198]) by smtp.gmail.com with ESMTPSA id v1-20020a056870954100b00203d8ce415bsm4843404oal.50.2023.12.31.12.56.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Dec 2023 12:56:49 -0800 (PST) From: Chris Morgan To: linux-input@vger.kernel.org Cc: dmitry.torokhov@gmail.com, hdegoede@redhat.com, paul@crapouillou.net, peter.hutterer@who-t.net, svv@google.com, biswarupp@google.com, contact@artur-rojek.eu, Chris Morgan Subject: [PATCH 2/2] Input: adc-joystick: Handle inverted axes Date: Sun, 31 Dec 2023 14:56:43 -0600 Message-Id: <20231231205643.129435-3-macroalpha82@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231231205643.129435-1-macroalpha82@gmail.com> References: <20231231205643.129435-1-macroalpha82@gmail.com> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Chris Morgan When one or more axes are inverted, (where min > max), normalize the data so that min < max and call a helper function to 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. Signed-off-by: Chris Morgan --- drivers/input/joystick/adc-joystick.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c index c0deff5d4282..4e8d446987b6 100644 --- a/drivers/input/joystick/adc-joystick.c +++ b/drivers/input/joystick/adc-joystick.c @@ -18,6 +18,7 @@ struct adc_joystick_axis { s32 range[2]; s32 fuzz; s32 flat; + bool inverted; }; struct adc_joystick { @@ -38,6 +39,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 = input_invert_abs(input, i, val); input_report_abs(input, joy->axes[i].code, val); } input_sync(input); @@ -86,6 +89,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 = input_invert_abs(joy->input, i, val); input_report_abs(joy->input, joy->axes[i].code, val); } @@ -168,11 +173,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); }