From patchwork Tue Dec 13 23:56:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Rosin X-Patchwork-Id: 9473547 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DD2F760762 for ; Wed, 14 Dec 2016 00:31:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CD2C8285F8 for ; Wed, 14 Dec 2016 00:31:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BEEA4286D7; Wed, 14 Dec 2016 00:31:18 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable 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 33149285F8 for ; Wed, 14 Dec 2016 00:31:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751550AbcLNAbR (ORCPT ); Tue, 13 Dec 2016 19:31:17 -0500 Received: from mail-db5eur01on0090.outbound.protection.outlook.com ([104.47.2.90]:3379 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750899AbcLNAbQ (ORCPT ); Tue, 13 Dec 2016 19:31:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axentiatech.onmicrosoft.com; s=selector1-axentia-se; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=5RAaUyLSYxMBnNuMRMBFSYIX/VDrcGyoeI3efLwgy+k=; b=pesyKnoWqTlLdsDGqki9KTIFIQkC1omeZUwX6Vkj8Dt6wAqOpHsVub8lfnw1A0cwrmCqs4SdKQtZ2j04AcP4LpaFJM6TqjMFw1vR7dgRz6ASMXxQZEUhAXwbsrI31PexSgrBzppEXbWt+DaDhRNeKcBMOAdww1vqZWQD/mXR0sc= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=peda@axentia.se; Received: from localhost.localdomain (217.210.101.82) by AM5PR0201MB2305.eurprd02.prod.outlook.com (10.169.242.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.771.8; Tue, 13 Dec 2016 23:57:13 +0000 From: Peter Rosin To: linux-kernel@vger.kernel.org Cc: Peter Rosin , Sebastian Reichel , Rob Herring , Mark Rutland , linux-pm@vger.kernel.org, devicetree@vger.kernel.org Subject: [PATCH v2 3/3] power: supply: bq24735-charger: allow chargers to share the ac-detect gpio Date: Wed, 14 Dec 2016 00:56:45 +0100 Message-Id: <1481673405-4547-4-git-send-email-peda@axentia.se> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1481673405-4547-1-git-send-email-peda@axentia.se> References: <1481673405-4547-1-git-send-email-peda@axentia.se> MIME-Version: 1.0 X-Originating-IP: [217.210.101.82] X-ClientProxiedBy: DB6P195CA0006.EURP195.PROD.OUTLOOK.COM (10.171.120.144) To AM5PR0201MB2305.eurprd02.prod.outlook.com (10.169.242.149) X-MS-Office365-Filtering-Correlation-Id: 89007c6e-586d-4754-8689-08d423b3c2d5 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:AM5PR0201MB2305; X-Microsoft-Exchange-Diagnostics: 1; AM5PR0201MB2305; 3:lfDFNg+kyTO8cfdbogk/ctIe2GlHL8pgyun2FnJguqBqAfwbufVSOEFQo4emWIAZnnCGS4GVuXG9L62xdASk6ezWPxtqP1UUTOs03lhLwVo7KNJlRG5Rsx/OvSsmOdVqoxu6NE2iDRVE4ngzhGYkxyI0WZHgpNUYb1LbRDrFe2BdIHKX+Jryu7RAv1p6i590TpH78+pdXEzEP5hUVSd5DuYM+e3zjGehz7f0w+6qC5h0Lk2bdlUCJjejCfEW++MsGy1tVYsrFfd3jFf3eDK5Bw==; 25:fClZAh28BFZO+qmksUVxLKigYz4jdb/tjATV9rqH3ZFdjEFIH9E8eYN0ztvk4WqiAjHfxnshCOJmv5MgrHiBaTNnymc5qFu4MV2mAZV/vKuD/PyrAItalBv7whuMmcbuzKah5r+0f2Tw7AKtpHiIU596PIeiVyd5aeaZrpo4y5sMeZ8A7AJT0ix2L1fmGvUNzjrBaCIhXK2jjVTES2axixaL+JA0kZWvEsQmkSjex/aGESTWPXLEZ0Jmrnm3jaCOnxqO88YEUjZ1ubP8RGE8pbWJRvwRMIfUZLjy5eJ/M0Fi40jqvh8jOlmvZim1lvhbL1/L9gXx4pIPBuBFyZVIrPznxN9PFxa6B57yYI4LMJjS/yDs0dOq3zx/P4WM++UirMo0FjfIzL3ILaOYuRLm1VlcbiMLu4IUHURL7juY1g9h27NBzsNVwdZ+2wk7NmKAELOg9NCKGTh8hgZf3QYYMw== X-Microsoft-Exchange-Diagnostics: 1; AM5PR0201MB2305; 31:MN7O58vaeY1TX7CPX5uVnbeYKp9o71Y57lsjTlSAhbYoo4hbYyPA2Ahc8LV8nM17xyiMRmtcXTf8XnqD0pC3YCbZ6KykbZRPXlgEEVDdoOisDX04WsUIZvhVran0vxw52r8CkpTY0eUFCyYKb5xTqALtwNCW/aJdnFuQ8bUuQjvPSpe1KQ9Gi1gewkBDfpREfO/RPSXnpiKw2Q++Wy9lKrwolShkfpBQGjCu0vIpw98zfCfgFs3YartBEq18IKNO+Do22Lxr7ET+G78TbwtN8w== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6041248)(20161123562025)(20161123564025)(20161123560025)(20161123555025)(20161123558021)(2016111802025)(6072148)(6043046); SRVR:AM5PR0201MB2305; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0201MB2305; X-Microsoft-Exchange-Diagnostics: 1; AM5PR0201MB2305; 4:Rbd/GDJqpSOLMw9P6Hn3tjSSgAzhnvWYB7xaONmPveTxRmipEx4x4bdJyxXvuC6KMnlt/uNFHe1BidfYQK0WH3/QizfCpv42sZ+jpWPE/BLdBAGhwqLMUXUdCQJuaNiV38JRw15ep+/HJyJo1AnrQbZGPYt2Hia/VxrkPK2OkwAPpzmr9RmL+ua3BPTwEP1Q9K+X9Dw7cwO5XmoEJc9P6BZQpoccO7lh3hAobfHGIysb2XPuh3oMkDaAn+oQgt3RO5h9ZGzhSPsiG4xa5WRlm45GIn1mqlLqH9FljNPQdNW9Kb84vOCld+9T3UPrjzGglL91TPPnSm6DMYSnm7/PPOH/QQo0coLuN421H3epkL+Zg5+//v8nVodMJX7e1XoeqqzJAgPaR4+IMd1gbWz+Ogk8vjlBY7TzLD2ND24OmycWSy5ChrCPpgyvBbVfwfoDj2unSR069H4q8n5vfZDSuAg8SZzeLt6ZtXCCndHWUnRUoDQiv97JK1O30PoObjXk4EU16UeGHBzw7doUdrRJOBJZ8SeISpug3N3G2mnetMSstraugj84uhAStKnHAm/wiqWBQ0FlizVucC60PMOHoDZ/mc9gP2uJxYcg5pXYTErJBgxWWtFxBOlnvGvK0OwZCqFWKX4ZbmVXV6KmjPtYPneLLV/bVAT1ufHO/a3cLgc= X-Forefront-PRVS: 01559F388D X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(6069001)(7916002)(39450400003)(39830400002)(39410400002)(199003)(189002)(92566002)(86362001)(6666003)(47776003)(2950100002)(38730400001)(6916009)(305945005)(7736002)(36756003)(66066001)(50986999)(33646002)(2906002)(42186005)(48376002)(50466002)(105586002)(106356001)(2351001)(68736007)(74482002)(6506006)(97736004)(101416001)(3846002)(76176999)(2361001)(6116002)(189998001)(6486002)(81156014)(5003940100001)(4326007)(8676002)(50226002)(110136003)(5660300001)(81166006)(6512006)(42262002); DIR:OUT; SFP:1102; SCL:1; SRVR:AM5PR0201MB2305; H:localhost.localdomain; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: axentia.se does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; AM5PR0201MB2305; 23:q8jnIscQipKYeUWl46xX+KW42JBDTs55aH8ISrv?= =?us-ascii?Q?8fLl8KrL9qNJwb2UjEKbcyUfpZ8CbjcBzWvnmXx1b9+llvvDUEPwXqN6PaNX?= =?us-ascii?Q?ziOMKTnvSrZWfW6goO1VMITZeDOue3E4gjCkG76gYxfZvR3sVxOYkRTMPOoz?= =?us-ascii?Q?dHbfTONGi2cK1Vlfde9m/saMR1sM3A0fmPWWCn+5llVglOBcwv+uBmhLo2YN?= =?us-ascii?Q?SXwAlZE/VXEBRD4U/Mb1qOHLzJydeYDmoYxd0rgXHecrBwYfTiNOUmueMmXQ?= =?us-ascii?Q?VfvBFdW3PhUM0WSpPsaof/m+bnQBwXbs/7KkQ68jQ5Fo8rVk06u5mCrIt0a3?= =?us-ascii?Q?eGecTKW9UkO1axdougLPlzv6Fx0Sf/XT0xz+Jk7mQRYR61AHaBeCNK/ZRMwB?= =?us-ascii?Q?5pmiuwnX1+vHHAF6Njqud1KyIEEGgzc2P79lps7vOgMsiSBaFSKuP77AV1gP?= =?us-ascii?Q?T0AZOPmwk7CZ80VZzwv8PeQ8sxOTPouuORSq9efZ96LGGlS2CbKM6TurB1+W?= =?us-ascii?Q?kAn0kNKB3x7DfaesF9kENVvDs9OW7Mxf5pNrYM7RgQglJBpMo/AJzuKshFps?= =?us-ascii?Q?fxSjHzCNjEs+EDe4tws42YRPIR5/FN4yJfdtNgQmyQCZdruOzWbMO0Z8HqOz?= =?us-ascii?Q?J/BI8xatjXudXRJas49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SCCrsqFGJT5ig9fQrZZRbtmHnvU13/Zr4h5oHueGxNR7VHzTA/qDfk/pKciQBzql0tDnttz4eT2iaPYXmsN31MPXE4tNl0gn; 5:TEbkChHl51NhpFA4rn8qcxlINduQ8NnquRp5UIWbXu2mJrR7hgvzdlO0gExgNtzfbqX1UbcIGn0fm/S3r/E7XAhNdRorlWjiVViQaXEo2NvZsMWj8DnDkimq5chiXbt11qYWPgMYUqDcsLAWGqno39485hYL/OZtanpJ+gtCcrM=; 24:sHEs4Q5DqIG3Z7kzcJmrRGSvg2qDWAMhMJUMzX5kPPRhmEEzTadDsKH3H6RkFDuoyPWHcgR/ZPJgAwnHKSWve4eFYkW4EH/u7zWPlXfBFPE= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; AM5PR0201MB2305; 7:vow2BFlgK208gM8ly6XHhMhcH4xsG9z4qFhrMF7PpkfTd0Mi1oGygJvpqvc+X1ccXb8yMwLsT5BEFgpv6cE/YRxU9ARgoipsfwUqqBhTL+Eu58c6/2mHU9CplBJWN85qNeQtRzCK8rWiB8UvqzJHCjYYsgiHMxNuddYmvILR+VChf90h5OWGV3o2pZYKJAEQJRWQ0j48QC+1aN7mhumQMBd1eFge54C8QOzBZn40egt1RUdagje/qiVu0ZscPaj2vZYSf47atjPsKSVt12+eKm476BM40Phdl2ZalPPp6UL8uRwdOU8cTYbg7ilf9FaYCElrfMzMoxZcowJ70D2kzI+cbKP+U5SDPFDECdhqzTu5JzdLrH8/xpUuBrG6xrXzowqoopgP49PNyLgXJmCAHKWw6bTJvThQtLLNN1XaZNJRqO0TMrn7wXyg7tpLvshGUbzRdSMHu8dOGySh439rVg== X-OriginatorOrg: axentia.se X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Dec 2016 23:57:13.0525 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0201MB2305 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If several parallel bq24735 chargers have their ac-detect gpios wired together (or if only one of the parallel bq24735 chargers have its ac-detect pin wired to a gpio, and the others are assumed to react the same), then all driver instances need to check the same gpio. But the gpio subsystem does not allow sharing gpios, so handle that locally. However, only do this for the polling case, sharing is not supported if the ac detection is handled with interrupts. Signed-off-by: Peter Rosin --- drivers/power/supply/bq24735-charger.c | 111 +++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 11 deletions(-) diff --git a/drivers/power/supply/bq24735-charger.c b/drivers/power/supply/bq24735-charger.c index f45876927676..c2403c4d5ece 100644 --- a/drivers/power/supply/bq24735-charger.c +++ b/drivers/power/supply/bq24735-charger.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -43,12 +44,24 @@ #define BQ24735_MANUFACTURER_ID 0xfe #define BQ24735_DEVICE_ID 0xff +struct bq24735; + +struct bq24735_shared { + struct list_head list; + struct bq24735 *owner; + struct gpio_desc *status_gpio; +}; + +static DEFINE_MUTEX(shared_lock); +static LIST_HEAD(shared_list); + struct bq24735 { struct power_supply *charger; struct power_supply_desc charger_desc; struct i2c_client *client; struct bq24735_platform *pdata; struct mutex lock; + struct bq24735_shared *shared; struct gpio_desc *status_gpio; struct delayed_work poll; u32 poll_interval; @@ -346,6 +359,75 @@ static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client) return pdata; } +static struct gpio_desc *bq24735_get_status_gpio(struct bq24735 *charger) +{ + struct device *dev = &charger->client->dev; + struct bq24735_shared *shared; + int gpio; + enum of_gpio_flags flags; + int active_low; + struct list_head *pos; + + if (of_property_read_bool(dev->of_node, "ti,ac-detect-gpios")) + gpio = of_get_named_gpio_flags(dev->of_node, + "ti,ac-detect-gpios", 0, &flags); + else if (of_property_read_bool(dev->of_node, "ti,ac-detect-gpio")) + gpio = of_get_named_gpio_flags(dev->of_node, + "ti,ac-detect-gpio", 0, &flags); + else + return NULL; + + if (!gpio_is_valid(gpio)) + return ERR_PTR(gpio); + active_low = flags & OF_GPIO_ACTIVE_LOW; + + mutex_lock(&shared_lock); + list_for_each(pos, &shared_list) { + shared = list_entry(pos, struct bq24735_shared, list); + if (gpio != desc_to_gpio(shared->status_gpio)) + continue; + if (!active_low ^ !gpiod_is_active_low(shared->status_gpio)) + continue; + + get_device(&shared->owner->client->dev); + dev_dbg(dev, "sharing gpio with %s\n", + shared->owner->pdata->name); + charger->shared = shared; + mutex_unlock(&shared_lock); + return shared->status_gpio; + } + + shared = devm_kzalloc(dev, sizeof(*shared), GFP_KERNEL); + if (!shared) { + mutex_unlock(&shared_lock); + return ERR_PTR(-ENOMEM); + } + shared->owner = charger; + shared->status_gpio = gpiod_get(dev, "ti,ac-detect", GPIOD_IN); + if (!IS_ERR(shared->status_gpio)) { + charger->shared = shared; + list_add(&shared->list, &shared_list); + } + mutex_unlock(&shared_lock); + + return shared->status_gpio; +} + +static void bq24735_put_status_gpio(struct bq24735 *charger) +{ + if (!charger->shared) + return; + + mutex_lock(&shared_lock); + if (charger->shared->owner != charger) { + put_device(&charger->shared->owner->client->dev); + } else { + list_del(&charger->shared->list); + gpiod_put(charger->shared->status_gpio); + } + mutex_unlock(&shared_lock); +} + static int bq24735_charger_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -402,9 +484,7 @@ static int bq24735_charger_probe(struct i2c_client *client, i2c_set_clientdata(client, charger); - charger->status_gpio = devm_gpiod_get_optional(&client->dev, - "ti,ac-detect", - GPIOD_IN); + charger->status_gpio = bq24735_get_status_gpio(charger); if (IS_ERR(charger->status_gpio)) { ret = PTR_ERR(charger->status_gpio); dev_err(&client->dev, "Getting gpio failed: %d\n", ret); @@ -416,28 +496,30 @@ static int bq24735_charger_probe(struct i2c_client *client, if (ret < 0) { dev_err(&client->dev, "Failed to read manufacturer id : %d\n", ret); - return ret; + goto out; } else if (ret != 0x0040) { dev_err(&client->dev, "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret); - return -ENODEV; + ret = -ENODEV; + goto out; } ret = bq24735_read_word(client, BQ24735_DEVICE_ID); if (ret < 0) { dev_err(&client->dev, "Failed to read device id : %d\n", ret); - return ret; + goto out; } else if (ret != 0x000B) { dev_err(&client->dev, "device id mismatch. 0x000b != 0x%04x\n", ret); - return -ENODEV; + ret = -ENODEV; + goto out; } } ret = bq24735_config_charger(charger); if (ret < 0) { dev_err(&client->dev, "failed in configuring charger"); - return ret; + goto out; } /* check for AC adapter presence */ @@ -445,7 +527,7 @@ static int bq24735_charger_probe(struct i2c_client *client, ret = bq24735_enable_charging(charger); if (ret < 0) { dev_err(&client->dev, "Failed to enable charging\n"); - return ret; + goto out; } } @@ -455,7 +537,7 @@ static int bq24735_charger_probe(struct i2c_client *client, ret = PTR_ERR(charger->charger); dev_err(&client->dev, "Failed to register power supply: %d\n", ret); - return ret; + goto out; } if (client->irq) { @@ -470,7 +552,7 @@ static int bq24735_charger_probe(struct i2c_client *client, dev_err(&client->dev, "Unable to register IRQ %d err %d\n", client->irq, ret); - return ret; + goto out; } } else if (charger->status_gpio) { ret = of_property_read_u32(client->dev.of_node, @@ -487,6 +569,11 @@ static int bq24735_charger_probe(struct i2c_client *client, } return 0; + +out: + bq24735_put_status_gpio(charger); + + return ret; } static int bq24735_charger_remove(struct i2c_client *client) @@ -496,6 +583,8 @@ static int bq24735_charger_remove(struct i2c_client *client) if (charger->poll_interval) cancel_delayed_work_sync(&charger->poll); + bq24735_put_status_gpio(charger); + return 0; }