Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753639AbcKYKAz (ORCPT ); Fri, 25 Nov 2016 05:00:55 -0500 Received: from mail-he1eur01on0110.outbound.protection.outlook.com ([104.47.0.110]:33023 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750850AbcKYKA2 (ORCPT ); Fri, 25 Nov 2016 05:00:28 -0500 X-Greylist: delayed 47622 seconds by postgrey-1.27 at vger.kernel.org; Fri, 25 Nov 2016 04:59:52 EST Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=peda@axentia.se; Subject: Re: Getting at gpio- and pinctrl-devices as a consumer To: Linus Walleij References: <1479908035-18284-1-git-send-email-peda@axentia.se> <20161124151420.GF4271@katana> <46be889f-a558-0174-f638-c685b23c8ed9@axentia.se> <20161124195230.GA1666@katana> CC: Alexandre Courbot , Wolfram Sang , "linux-kernel@vger.kernel.org" , Peter Korsgaard , Wolfram Sang , "linux-i2c@vger.kernel.org" , "linux-gpio@vger.kernel.org" From: Peter Rosin Organization: Axentia Technologies AB Message-ID: <45787444-a237-7363-5f03-0cd207cb82c2@axentia.se> Date: Fri, 25 Nov 2016 10:24:33 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [217.210.101.82] X-ClientProxiedBy: DB5PR08CA0019.eurprd08.prod.outlook.com (10.163.102.157) To DB6PR0201MB2312.eurprd02.prod.outlook.com (10.169.222.151) X-MS-Office365-Filtering-Correlation-Id: 662a61db-dd96-429e-83b9-08d41514e151 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001);SRVR:DB6PR0201MB2312; X-Microsoft-Exchange-Diagnostics: 1;DB6PR0201MB2312;3:Ia5boqjTiV6V7f3pArRYbHEEX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: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6060326)(6040361)(6045199)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6041248)(6061324)(20161123560025)(20161123555025)(20161123564025)(20161123562025)(2016111802025)(6043046);SRVR:DB6PR0201MB2312;BCL:0;PCL:0;RULEID:;SRVR:DB6PR0201MB2312; X-Microsoft-Exchange-Diagnostics: 1;DB6PR0201MB2312;4:LNUxeeInauiPWC2DzWXgLh9wX5cn6J2Wg1sfAU7787xmwFib9YIyZrHmTrgRZK7hHKkv3LirXdYWppTjla/UgyeV2EH3XgZut6WL3Z/YMpthV6h0ain55ngIZEa1T0AehcHLU933TxJ5SBmwA6SeD6qCZLOyDQbSyHEk0UWXsmhFi3sti9r5vr2u8bcHVb+Q0dUCFGxo9rmHMr4s1Lx1tyX9tfSe9iGVvyJu5JiHdP3MdgxOX21z3mv34xWrtnqrlL5EtwXsW2/RtfpWRTQQUDTKzzmXzGgWeVJRS/9lHbUAGp0ZmVfh/yYWNHoBzHfcwqSrPx34DIiX3hLUSsucShekhB30Q0mMqO3xVFU+ZQNsxRSQm5dKn5m9EidJk7EN9tW41RXMDC+qO8AF60nYOtACGjMCiZnm1GFZz1F8UMumIztB/iChudafDbEbA3wcOb8m07L6HuMK0pgHBoBH463WatdlaIJaHGs+CjVBcTNRAruGB3343KWKxD1ROlBle2GlykyAh6ZDsuql4ELNKHMN1FZ3DrGFVZ75p9PbkVHmMaX4rTXXKb8gCfPUBjnpE1/HMnRnUei8+KtmC+Mn65o0KBXT60QN+Y3bRz40VbzkwatsiM3dIHEbScpfkDWkWioTBieHcdwmrIZRfvwu/mvOOEvw7mMwXXUOem4M29A= X-Forefront-PRVS: 01371B902F X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(6049001)(7916002)(189002)(24454002)(377454003)(377424004)(199003)(31696002)(65956001)(305945005)(117156001)(81166006)(65806001)(23676002)(6666003)(7736002)(101416001)(65826007)(38730400001)(68736007)(5660300001)(97736004)(4001350100001)(4001150100001)(39400400001)(39380400001)(39410400001)(110136003)(81156014)(8676002)(39060400001)(86362001)(6916009)(2950100002)(83506001)(36756003)(7846002)(229853002)(47776003)(189998001)(64126003)(76176999)(77096005)(54356999)(33646002)(50466002)(50986999)(2906002)(42186005)(4326007)(106356001)(92566002)(6116002)(74482002)(66066001)(105586002)(230700001)(3846002)(31686004)(39450400002)(93886004)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:DB6PR0201MB2312;H:[192.168.0.125];FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtEQjZQUjAyMDFNQjIzMTI7MjM6L3JCVlZWYys2c2lQN2R2bGhQaktheS9k?= =?utf-8?B?SDFJa3gyeDNrb1o2aWQzQUtPMUlManVRTURIODJwdmFFTU9QbkhVTTZsRTVw?= =?utf-8?B?UURmcGNVRUYwcDJDdE5OWHpHdytORUFrWDV6VWI1T0hjVzlkQThMRW5QTEs1?= =?utf-8?B?TlhwL1pBcm9CbjRCQmVwdmxoOTdBSFpxNEF5VGo1dEVQTTFLTG5BSXNmVXd2?= =?utf-8?B?YTB3NjMxdWlPUmpLS2xweGFwMG53dmVrNVcydG9OZWpWeThqV3lpZWRoNWJW?= =?utf-8?B?bXFoMHVBdHdjWG82Tk03ZGJCaTZUcGFYTnpHV2NCd0dlOFRzZ0hJNVR0S1hS?= =?utf-8?B?Tml3Uy82SmgvUGg4Z0tsZDh4YzA5WFU2NUV5NUxSRmc1cFgzWlQvZ09kRnNt?= =?utf-8?B?ZGhUdlpqY2dCQ1VnTytteTlDRHpLamk3L00ra0F4ZGtycjVqREVLQjdxa1Yw?= =?utf-8?B?V291N3BBTUovMGdjdXBsSmxHZmkxQjFWTlVuTFkxL0MxcVVEL21vZkt5Z1gv?= =?utf-8?B?dVloMnhlRkJqVlRZUTk5MHJFNWNsYktLSkNqN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m9AhBT2FoYjLE4jfGLoTpUMIo7ShhDlVnrlljvzUW25iPBJifSvc7z04QWUyzOrUnMnH+sT+Dq81O2c/9Aonq4rqDlpT1ZPwJnwNPAp9BxKdIi0l+2jeEAqclh6fXgKo+JM6Ar6Dyyhx9Z/SYGUzNBMLwGDn9QcR7nLtXsv6PKTFK6q7llfDFCEwuP/lrRDmgY8RYxgM8qCBRSxOwPfSYPesNi/U3FDwIyiojCRQ=;5:jcEtPDbTIwW/g3+DSMbV8uIi7lThtB54XKvAGsAKUxEZovjb3ua+q8XtB+71sT2lVFrJiALlcS9hGTbASnNR2nx7FXL9/2VWsO128pn/eBaNNX6e2vLUL8iEesWCBJnwSDAvB+CNUaQvFE82uo044w==;24:Ks7H56RaGZses1VGKm1g97pnIhkHvuyq+aoZkJWkzWk7dzFrQqBqgTrhrqUdBKKwrBUSIKO+AMDpEbz/V36zIBeCq4Z15H+8AqWVMewURpw= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DB6PR0201MB2312;7:qKyNk7vSX+C4a7ZNArt9YT+eK5ckX7HbVOfpHglHBomRc1jN/SkiuYrDw5CPZmi0+l0flai2vIhkpUl4qPlmJANzDCTcOTvijPIPJpclA/0Wngg1ANdLCXpUn0846pEAsv+l4pp645UeoxwTY6uhz7yXIh7oA1picAtn13OZV6kJOCFJDYKIQiPbS71b7H2TNj59XeFEyyMBUcJAJorOjxPaPd7h0M1eV9w/853CKR9MKEyd+bIaC7P+Y7TUeHIF6NZFI5CmlNntKOL7DP5EH/yhkKm2wZp04gpjfe4eBp/OW3pyB/2P84WxTiMEd5wBB/57AI72qqn0R5UDGL+slfsY4+NpU4mozWLcukYPIaPJdtEbsG0+sXQ3B3dxEvlKljlUzNHcv6eanYXTp4YOvtepKi3nT86mi1W6HNg2YBeK/F5/pxE/BHJ/dcoHrfX+JE8Ta/USzaayo+b0E9m7vw== X-OriginatorOrg: axentia.se X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Nov 2016 09:24:36.9870 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0201MB2312 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4513 Lines: 117 On 2016-11-25 00:29, Linus Walleij wrote: > On Thu, Nov 24, 2016 at 10:35 PM, Peter Rosin wrote: > >> The background is that the gpio- and pinctrl-based i2c-mux drivers >> need to know if the device that is used to control the mux of the >> i2c-bus is also sitting on that very same i2c-bus. If it is, the >> locking has to be different and a bit more relaxed. This relaxed >> mode cannot be used always, as that would change the mux behavior >> in an unacceptable way for stuff expecting the (traditional) >> stricter locking. See Documentation/i2c/i2c-topology for more >> details if you need it. >> >> To check this, the i2c mux drivers dig out the device connected to >> each gpio-pin (or pinctrl-state) and walks up the device tree to see >> if the root i2c adapter that is muxed is in the loop. >> >> When I wrote this code, I could not find a clean way to go from a >> struct gpio_desc * to the relevant device, short of doing >> >> #include "../../gpio/gpiolib.h" >> >> gpio_dev = &gpio_desc->gdev->dev; >> >> And similarly for pinctrl: >> >> #include "../../pinctrl/core.h" >> >> struct pinctrl_setting *setting; >> pinctrl_dev = setting->pctldev->dev; >> >> I'm not very proud of that, and wonder if there is a better way >> to get at the needed struct device? If not, then perhaps there >> should be? > > Surely if I can be convinced that we need helpers for this > in GPIO and/or pin control we can add them. > > They just need to be named something reasonable and > be generally useful for other situations of similar nature. > > struct device *gpiod_get_backing_device(struct gpio_desc *d); > > Is simple but is it really what you want? Well, my first attempt was to simply have a property in the devicetree stating that the mux was controlled from the same i2c bus it was muxing, but that was shot down because it should be possible to deduce this from the implementation (or something of that meaning, it was a while ago), which to me meant examining the "struct device"-tree. For the gpio_desc it is easy. However, it is worse for the pinctrl case. The pinctrl consumer currently deals in states, but each state has a number of pinctrl_settings and it is these settings that are backed by a device implementing that state. It is in other words possibly several devices involved with one pinctrl_state. This can be handled in in three ways that spring to mind. 1. Return a single device connected to a pinctrl state, if it is the same device backing all settings, and error out if more than one device is involved. I mean, how silly would it me to control a mux from pins not controlled by the same pinctrl? That just seems extremely odd, like one pin each on two different i2c-controlled io-expanders sitting on the same i2c-bus that is also muxed using these two pins. The risk for regression because of this should be ... low. 2. Return an array of devices, one for each pinctrl setting connected to the state. This also needs to expose the number of settings for a state. 3. Introduce some kind of list_for_each_entry wrapper (or something) so that the consumer can iterate over the settings connected to a state, coupled with a function to get the struct device from a setting. #1 is not as generally useful as #2 or #3, as it assumes that the corner case is not interesting. But it might very well be the case that the next user is looking for exactly this corner case. But at the same time, the next user isn't here yet, at least not that I know of... #2 and #3 exposes more of the internals to the consumer, since the consumer currently does not need to worry about neither the number of settings connected to a state nor that fact that there are settings at all. #3 would be closest to the current implementation in i2c-mux-pinctrl (included below). So, for the case in question I deem #1 good enough, and it exposes less of the internals. But #2 or #3 might be better for the next case, and also makes the current case not risk the unlikely regression. Any preference? Cheers, Peter static struct i2c_adapter *i2c_mux_pinctrl_root_adapter( struct pinctrl_state *state) { struct i2c_adapter *root = NULL; struct pinctrl_setting *setting; struct i2c_adapter *pin_root; list_for_each_entry(setting, &state->settings, node) { pin_root = i2c_root_adapter(setting->pctldev->dev); if (!pin_root) return NULL; if (!root) root = pin_root; else if (root != pin_root) return NULL; } return root; }