From patchwork Mon Jan 28 04:04:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10783215 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 72E891390 for ; Mon, 28 Jan 2019 05:05:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C67328D82 for ; Mon, 28 Jan 2019 05:05:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4CF392A343; Mon, 28 Jan 2019 05:05:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 85D3B2A33C for ; Mon, 28 Jan 2019 05:05:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726137AbfA1FF4 (ORCPT ); Mon, 28 Jan 2019 00:05:56 -0500 Received: from mail-yb1-f195.google.com ([209.85.219.195]:37941 "EHLO mail-yb1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725782AbfA1FFz (ORCPT ); Mon, 28 Jan 2019 00:05:55 -0500 Received: by mail-yb1-f195.google.com with SMTP id x9so6204106ybj.5 for ; Sun, 27 Jan 2019 21:05:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CNYCjCshwPonB2FImSccnNJ5GHzNsQHGwSB7zHHQAMw=; b=UsqQcWUWB6joqEg7vewUam1OV0PqD0e1aSXTaFYZtWP7cqWEEFGmBPMpBDUmH5mf16 s1aTGEc4+nh1usDIJvd8nUIyk+0W1aqRD5ljLGxMaK8zIw6LjX2UoQYiTVhi3CYezziR hrSQHkABcESKz2dXa775EOJwZbmpGfGlcDolZc3Dk7dTD0+2/q5NKm4xPYHLGEpRUaE5 b0z3HiUYpOx3y/seywR4ZA8d33IyNM9zVpkVU5rmqEjkDjDU+GmUwhNz8F1BcsvdfUr3 SJ9pwPbxzbdqU2kYtYS2k8P3A4bMEzJDelnTd85NkNw/Tr8q0XNJ8X9h5Jk/iq8TDiO1 MMzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CNYCjCshwPonB2FImSccnNJ5GHzNsQHGwSB7zHHQAMw=; b=Sadb93ncbS1gDAsYT6goEjKLc3swGAcFbvHm5+Vr1mvPzOXM0q7Y2EeRV4nXTLkmFy +shRexzUNeeqoBjwOYUcsnjjbv9JhVDFL9vP26N8if0nNoWAPKvSA01ANr/yAlCk9KGl 12vdSSjS6wEPm10KBQrlvZHpBiY6nlYoegxc0fNJDR2yRZD9UBuWHmWCzh9rRkmVCyMR VExJtBho8yyXaoOBTZ1oQ69OYOJKbUDtAoHlWlThAkaSxnBnubeGOAPKU8k9u6JfP0b3 6FbJj0Ipz5kMTH9vz8A3RU6mTmGPfI0SB43ObyYlYPS6vBeGp3etd6i6LnkDJQFlY5rk EYOA== X-Gm-Message-State: AJcUukc6iaGkyLW/YcSkQWxye6k1UJH8T5T6K0Tt25ACZiJ4/Nzmk9lD mnVCFzZzMwyNcyUbhEoDAN00RPHGE4s= X-Google-Smtp-Source: ALg8bN5FsPlPSuGTvXskW9olx4A/poIdGuLxpPPzLTSajwqceo3R2/axvDmG3lpKgblVKgQildHLAw== X-Received: by 2002:a25:b806:: with SMTP id v6mr18755754ybj.278.1548648310323; Sun, 27 Jan 2019 20:05:10 -0800 (PST) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id x9sm12405569ywc.8.2019.01.27.20.05.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 Jan 2019 20:05:09 -0800 (PST) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, "Daniel J. Ogorchock" Subject: [PATCH v3 3/3] HID: switchcon: add power supply support Date: Sun, 27 Jan 2019 22:04:21 -0600 Message-Id: <20190128040421.31878-4-djogorchock@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128040421.31878-1-djogorchock@gmail.com> References: <20190128040421.31878-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds power_supply functionality to the switch controller driver for its battery. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/hid-switchcon.c | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/drivers/hid/hid-switchcon.c b/drivers/hid/hid-switchcon.c index 7dee7b6c0dc2..8b2217b9f1aa 100644 --- a/drivers/hid/hid-switchcon.c +++ b/drivers/hid/hid-switchcon.c @@ -11,6 +11,7 @@ * https://github.com/MTCKC/ProconXInput * hid-wiimote kernel hid driver * hid-logitech-hidpp driver + * hid-sony driver * * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The * Pro Controllers can either be used over USB or Bluetooth. @@ -27,6 +28,7 @@ #include #include #include +#include #include /* @@ -208,6 +210,7 @@ struct switchcon_ctlr { struct led_classdev leds[SC_NUM_LEDS]; enum switchcon_ctlr_type type; enum switchcon_ctlr_state ctlr_state; + spinlock_t lock; /* The following members are used for synchronous sends/receives */ enum switchcon_msg_type msg_type; @@ -225,6 +228,12 @@ struct switchcon_ctlr { struct switchcon_stick_cal right_stick_cal_x; struct switchcon_stick_cal right_stick_cal_y; + /* power supply data */ + struct power_supply *battery; + struct power_supply_desc battery_desc; + u8 battery_capacity; + bool battery_charging; + bool host_powered; }; static int __switchcon_hid_send(struct hid_device *hdev, u8 *data, size_t len) @@ -366,8 +375,40 @@ static void switchcon_parse_report(struct switchcon_ctlr *ctlr, u8 *data) { struct input_dev *dev = ctlr->input; enum switchcon_ctlr_type type = ctlr->type; + unsigned long flags; + u8 tmp; u32 btns; + /* Parse the battery status */ + tmp = data[2]; + spin_lock_irqsave(&ctlr->lock, flags); + ctlr->host_powered = tmp & BIT(0); + ctlr->battery_charging = tmp & BIT(4); + tmp = tmp >> 5; + switch (tmp) { + case 0: /* empty */ + ctlr->battery_capacity = 0; + break; + case 1: /* critical */ + ctlr->battery_capacity = 1; + break; + case 2: /* low */ + ctlr->battery_capacity = 30; + break; + case 3: /* medium */ + ctlr->battery_capacity = 60; + break; + case 4: /* full */ + ctlr->battery_capacity = 100; + break; + default: + ctlr->battery_capacity = 0; + hid_warn(ctlr->hdev, "Invalid battery status\n"); + break; + } + spin_unlock_irqrestore(&ctlr->lock, flags); + + /* Parse the buttons and sticks */ btns = hid_field_extract(ctlr->hdev, data + 3, 0, 24); if (type == SWITCHCON_CTLR_TYPE_PROCON || @@ -601,6 +642,89 @@ static int switchcon_player_leds_create(struct switchcon_ctlr *ctlr) return ret; } +static int switchcon_battery_get_property(struct power_supply *supply, + enum power_supply_property prop, + union power_supply_propval *val) +{ + struct switchcon_ctlr *ctlr = power_supply_get_drvdata(supply); + unsigned long flags; + int ret = 0; + u8 capacity; + bool charging; + bool powered; + + spin_lock_irqsave(&ctlr->lock, flags); + capacity = ctlr->battery_capacity; + charging = ctlr->battery_charging; + powered = ctlr->host_powered; + spin_unlock_irqrestore(&ctlr->lock, flags); + + switch(prop) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + case POWER_SUPPLY_PROP_SCOPE: + val->intval = POWER_SUPPLY_SCOPE_DEVICE; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = capacity; + break; + case POWER_SUPPLY_PROP_STATUS: + if (charging) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else if (capacity == 100 && powered) + val->intval = POWER_SUPPLY_STATUS_FULL; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + break; + default: + ret = -EINVAL; + break; + } + return ret; +} + +static enum power_supply_property switchcon_battery_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_SCOPE, + POWER_SUPPLY_PROP_STATUS, +}; + +static int switchcon_power_supply_create(struct switchcon_ctlr *ctlr) +{ + struct hid_device *hdev = ctlr->hdev; + struct power_supply_config supply_config = { .drv_data = ctlr, }; + const char * const name_fmt = "nintendo_switch_controller_battery_%s"; + int ret = 0; + + /* Set initially to 100 before receiving first input report */ + ctlr->battery_capacity = 100; + + /* Configure the battery's description */ + ctlr->battery_desc.properties = switchcon_battery_props; + ctlr->battery_desc.num_properties = + ARRAY_SIZE(switchcon_battery_props); + ctlr->battery_desc.get_property = switchcon_battery_get_property; + ctlr->battery_desc.use_for_apm = 0; + ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, + name_fmt, + dev_name(&hdev->dev)); + if (!ctlr->battery_desc.name) + return -ENOMEM; + + ctlr->battery = devm_power_supply_register(&hdev->dev, + &ctlr->battery_desc, + &supply_config); + if (IS_ERR(ctlr->battery)) { + ret = PTR_ERR(ctlr->battery); + hid_err(hdev, "Failed to register battery; ret=%d\n", ret); + return ret; + } + power_supply_powers(ctlr->battery, &hdev->dev); + return 0; +} + /* data input must have at least 9 bytes */ static void switchcon_parse_lstick_calibration(u8 *data, struct switchcon_ctlr *ctlr) @@ -755,6 +879,7 @@ static struct switchcon_ctlr *switchcon_ctlr_create(struct hid_device *hdev) hid_set_drvdata(hdev, ctlr); mutex_init(&ctlr->output_mutex); init_waitqueue_head(&ctlr->wait); + spin_lock_init(&ctlr->lock); return ctlr; } @@ -870,6 +995,13 @@ static int switchcon_hid_probe(struct hid_device *hdev, goto err_close; } + /* Initialize the battery power supply */ + ret = switchcon_power_supply_create(ctlr); + if (ret) { + hid_err(hdev, "Failed to create power_supply; ret=%d\n", ret); + goto err_close; + } + ctlr->ctlr_state = SWITCHCON_CTLR_STATE_READ; hid_dbg(hdev, "probe - success\n");