linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
To: Jiri Kosina <jikos@kernel.org>
Cc: "Emmanuel Gil Peyrot" <linkmauve@linkmauve.fr>,
	linux-input@vger.kernel.org, "Ash Logan" <ash@heyquark.com>,
	"Jonathan Neuschäfer" <j.ne@posteo.net>,
	"Barnabás Pőcze" <pobrn@protonmail.com>,
	"Benjamin Tissoires" <benjamin.tissoires@redhat.com>,
	linux-kernel@vger.kernel.org,
	"Daniel J . Ogorchock" <djogorchock@gmail.com>
Subject: [PATCH v5 5/5] HID: nintendo: drc: add battery reporting
Date: Wed, 27 Oct 2021 12:10:43 +0200	[thread overview]
Message-ID: <20211027101043.31609-6-linkmauve@linkmauve.fr> (raw)
In-Reply-To: <20211027101043.31609-1-linkmauve@linkmauve.fr>

On my DRC the values only go between 142 (battery LED blinking red
before shutdown) and 178 (charge LED stopping), it seems to be the same
on other units according to other testers.  This might be the raw
voltage value as reported by an ADC, so adding a linear interpolation
between two common battery voltage values.

A spinlock is used to avoid the battery level and status from being
reported unsynchronised.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
---
 drivers/hid/hid-nintendo-wiiu.c | 136 ++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

diff --git a/drivers/hid/hid-nintendo-wiiu.c b/drivers/hid/hid-nintendo-wiiu.c
index 813abb104275..b18fa403eb42 100644
--- a/drivers/hid/hid-nintendo-wiiu.c
+++ b/drivers/hid/hid-nintendo-wiiu.c
@@ -17,6 +17,11 @@
 #include <linux/input.h>
 #include <linux/minmax.h>
 #include <linux/module.h>
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+#include <linux/fixp-arith.h>
+#include <linux/power_supply.h>
+#include <linux/spinlock.h>
+#endif
 #include "hid-ids.h"
 #include "hid-nintendo.h"
 
@@ -72,6 +77,13 @@
 #define MAGNET_MIN	-(1 << 15)
 #define MAGNET_MAX	((1 << 15) - 1)
 
+/* ADC constants for the battery */
+#define BATTERY_CHARGING_BIT	BIT(6)
+#define BATTERY_MIN	142
+#define BATTERY_MAX	178
+#define VOLTAGE_MIN	3270000
+#define VOLTAGE_MAX	4100000
+
 /*
  * The device is setup with multiple input devices:
  * - A joypad with the buttons and sticks.
@@ -85,6 +97,14 @@ struct drc {
 	struct input_dev *joy_input_dev;
 	struct input_dev *touch_input_dev;
 	struct input_dev *accel_input_dev;
+
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+	struct power_supply *battery;
+	struct power_supply_desc battery_desc;
+	spinlock_t battery_lock;
+	u8 battery_energy;
+	int battery_status;
+#endif
 };
 
 /*
@@ -101,6 +121,9 @@ int wiiu_hid_event(struct hid_device *hdev, struct hid_report *report,
 	struct drc *drc = hid_get_drvdata(hdev);
 	int i, x, y, z, pressure, base;
 	u32 buttons;
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+	unsigned long flags;
+#endif
 
 	if (len != 128)
 		return -EINVAL;
@@ -219,6 +242,19 @@ int wiiu_hid_event(struct hid_device *hdev, struct hid_report *report,
 	input_report_abs(drc->accel_input_dev, ABS_WHEEL, (int16_t)z);
 	input_sync(drc->accel_input_dev);
 
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+	/* battery */
+	spin_lock_irqsave(&drc->battery_lock, flags);
+	drc->battery_energy = data[5];
+	if (drc->battery_energy == BATTERY_MAX)
+		drc->battery_status = POWER_SUPPLY_STATUS_FULL;
+	else if (data[4] & BATTERY_CHARGING_BIT)
+		drc->battery_status = POWER_SUPPLY_STATUS_CHARGING;
+	else
+		drc->battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
+	spin_unlock_irqrestore(&drc->battery_lock, flags);
+#endif
+
 	/* let hidraw and hiddev handle the report */
 	return 0;
 }
@@ -368,6 +404,98 @@ static bool drc_setup_accel(struct drc *drc,
 	return true;
 }
 
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+static enum power_supply_property drc_battery_props[] = {
+	POWER_SUPPLY_PROP_STATUS,
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_VOLTAGE_MAX,
+	POWER_SUPPLY_PROP_VOLTAGE_MIN,
+	POWER_SUPPLY_PROP_VOLTAGE_NOW,
+	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_SCOPE,
+};
+
+static int drc_battery_get_property(struct power_supply *psy,
+				    enum power_supply_property psp,
+				    union power_supply_propval *val)
+{
+	struct drc *drc = power_supply_get_drvdata(psy);
+	unsigned long flags;
+	int ret = 0;
+	u8 battery_energy;
+	int battery_status;
+
+	spin_lock_irqsave(&drc->battery_lock, flags);
+	battery_energy = drc->battery_energy;
+	battery_status = drc->battery_status;
+	spin_unlock_irqrestore(&drc->battery_lock, flags);
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_STATUS:
+		val->intval = battery_status;
+		break;
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = 1;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
+		val->intval = VOLTAGE_MAX;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
+		val->intval = VOLTAGE_MIN;
+		break;
+	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+		val->intval = fixp_linear_interpolate(BATTERY_MIN, VOLTAGE_MIN,
+						      BATTERY_MAX, VOLTAGE_MAX,
+						      battery_energy);
+		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		val->intval = fixp_linear_interpolate(BATTERY_MIN, 0,
+						      BATTERY_MAX, 100,
+						      battery_energy);
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+static int drc_setup_battery(struct drc *drc,
+			     struct hid_device *hdev)
+{
+	struct power_supply_config psy_cfg = { .drv_data = drc, };
+	static atomic_t drc_num = ATOMIC_INIT(0);
+	int ret;
+
+	spin_lock_init(&drc->battery_lock);
+
+	drc->battery_desc.properties = drc_battery_props;
+	drc->battery_desc.num_properties = ARRAY_SIZE(drc_battery_props);
+	drc->battery_desc.get_property = drc_battery_get_property;
+	drc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
+	drc->battery_desc.use_for_apm = 0;
+
+	drc->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
+						"wiiu-drc-%i-battery", atomic_fetch_inc(&drc_num));
+	if (!drc->battery_desc.name)
+		return -ENOMEM;
+
+	drc->battery = devm_power_supply_register(&hdev->dev, &drc->battery_desc, &psy_cfg);
+	if (IS_ERR(drc->battery)) {
+		ret = PTR_ERR(drc->battery);
+		hid_err(hdev, "Unable to register battery device\n");
+		return ret;
+	}
+
+	power_supply_powers(drc->battery, &hdev->dev);
+
+	return 0;
+}
+#endif
+
 int wiiu_hid_probe(struct hid_device *hdev,
 		   const struct hid_device_id *id)
 {
@@ -396,6 +524,14 @@ int wiiu_hid_probe(struct hid_device *hdev,
 		return -ENOMEM;
 	}
 
+#ifdef CONFIG_HID_BATTERY_STRENGTH
+	ret = drc_setup_battery(drc, hdev);
+	if (ret) {
+		hid_err(hdev, "could not allocate battery interface\n");
+		return ret;
+	}
+#endif
+
 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW | HID_CONNECT_DRIVER);
 	if (ret) {
 		hid_err(hdev, "hw start failed\n");
-- 
2.33.1


      parent reply	other threads:[~2021-10-27 10:11 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-02 23:28 [PATCH 0/4] RFC: HID: wiiu-drc: Add a driver for the Wii U gamepad Emmanuel Gil Peyrot
2021-05-02 23:28 ` [PATCH 1/4] HID: wiiu-drc: Add a driver for this gamepad Emmanuel Gil Peyrot
2021-05-05 22:33   ` Jonathan Neuschäfer
2021-05-06 10:07     ` Emmanuel Gil Peyrot
2021-05-06 10:29       ` Jonathan Neuschäfer
2021-05-06 11:53   ` Barnabás Pőcze
2021-05-02 23:28 ` [PATCH 2/4] HID: wiiu-drc: Implement touch reports Emmanuel Gil Peyrot
2021-05-05 22:43   ` Jonathan Neuschäfer
2021-05-06 10:20     ` Emmanuel Gil Peyrot
2021-05-02 23:28 ` [PATCH 3/4] HID: wiiu-drc: Add accelerometer, gyroscope and magnetometer readings Emmanuel Gil Peyrot
2021-05-02 23:28 ` [PATCH 4/4] HID: wiiu-drc: Add battery reporting Emmanuel Gil Peyrot
2021-05-06 11:45   ` Barnabás Pőcze
2021-05-19  8:59 ` [PATCH v3 0/4] HID: wiiu-drc: Add a driver for the Wii U gamepad Emmanuel Gil Peyrot
2021-05-19  8:59   ` [PATCH v3 1/4] HID: wiiu-drc: Add a driver for this gamepad Emmanuel Gil Peyrot
2021-05-19  8:59   ` [PATCH v3 2/4] HID: wiiu-drc: Implement touch reports Emmanuel Gil Peyrot
2021-05-19  8:59   ` [PATCH v3 3/4] HID: wiiu-drc: Add accelerometer, gyroscope and magnetometer readings Emmanuel Gil Peyrot
2021-05-19  8:59   ` [PATCH v3 4/4] HID: wiiu-drc: Add battery reporting Emmanuel Gil Peyrot
2021-09-21 15:08   ` [PATCH v3 0/4] HID: wiiu-drc: Add a driver for the Wii U gamepad Emmanuel Gil Peyrot
2021-10-19  9:14     ` Jiri Kosina
2021-10-19  9:27       ` Emmanuel Gil Peyrot
2021-10-19  9:30         ` Jiri Kosina
2021-10-19  9:36           ` Emmanuel Gil Peyrot
2021-11-04 11:21           ` Emmanuel Gil Peyrot
2021-11-05 17:27             ` François-Xavier Carton
2021-10-19 23:59         ` François-Xavier Carton
2021-10-20  6:24           ` Emmanuel Gil Peyrot
2021-10-19 11:04   ` [PATCH v4 0/5] HID: nintendo: Add support " Emmanuel Gil Peyrot
2021-10-19 11:04     ` [PATCH v4 1/5] HID: nintendo: split switch support into its own file Emmanuel Gil Peyrot
2021-10-22  8:32       ` kernel test robot
2021-10-22 10:25       ` kernel test robot
2021-10-19 11:04     ` [PATCH v4 2/5] HID: nintendo: drc: add support for the Wii U gamepad Emmanuel Gil Peyrot
2021-11-05 20:55       ` kernel test robot
2021-10-19 11:04     ` [PATCH v4 3/5] HID: nintendo: drc: implement touch reports Emmanuel Gil Peyrot
2021-10-19 11:04     ` [PATCH v4 4/5] HID: nintendo: drc: add accelerometer, gyroscope and magnetometer readings Emmanuel Gil Peyrot
2021-10-19 11:04     ` [PATCH v4 5/5] HID: nintendo: drc: add battery reporting Emmanuel Gil Peyrot
2021-10-27  8:10     ` [PATCH v4 0/5] HID: nintendo: Add support for the Wii U gamepad Jiri Kosina
2021-10-27 10:10     ` [PATCH v5 " Emmanuel Gil Peyrot
2021-10-27 10:10       ` [PATCH v5 1/5] HID: nintendo: split switch support into its own file Emmanuel Gil Peyrot
2021-10-27 10:10       ` [PATCH v5 2/5] HID: nintendo: drc: add support for the Wii U gamepad Emmanuel Gil Peyrot
2021-10-27 10:10       ` [PATCH v5 3/5] HID: nintendo: drc: implement touch reports Emmanuel Gil Peyrot
2021-10-27 10:10       ` [PATCH v5 4/5] HID: nintendo: drc: add accelerometer, gyroscope and magnetometer readings Emmanuel Gil Peyrot
2021-10-27 10:10       ` Emmanuel Gil Peyrot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211027101043.31609-6-linkmauve@linkmauve.fr \
    --to=linkmauve@linkmauve.fr \
    --cc=ash@heyquark.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=djogorchock@gmail.com \
    --cc=j.ne@posteo.net \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pobrn@protonmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).