From patchwork Thu Sep 19 13:47:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Stefanescu X-Patchwork-Id: 829711 Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11013022.outbound.protection.outlook.com [52.101.67.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B67619CD17; Thu, 19 Sep 2024 13:49:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.67.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726753745; cv=fail; b=eGZl0sCi3eWPUZWjO9JBc3kJBnOAEQnTpDwV7ud2FE/0IeP6bjv5FCuveN0EORDbmyRcUHluX13CttC2LgDAkSjSF25hcz5ijxflB/Dc/nJKN98eUsNe+9wa3gQpeis9Ya0XxIlO0MYoHNa5MzPBoFLwDRT+p9sxk1eVUZg7H9w= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726753745; c=relaxed/simple; bh=WK4Owj4Y4Cm9P4MpXmjHW4vYZ5KrqTdjByDfuUp5bwk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=d+qDmyGPqgjLmA4T0L0lP2fyvjMZNsBSb4LiK7ih5eOId6twS+tjV3yx7BmnaJsSAeLheA6FGl1MC5s3cMMSh33o3++I9lyj2LGQ05DIVWwR/7Hxhd8OrMaVMmdZOkR3yCWrUWWBqz0Cxb396H0u8yQPFjYk1rgcz/bcq+LDFG8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com; spf=pass smtp.mailfrom=oss.nxp.com; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b=LAulT4G9; arc=fail smtp.client-ip=52.101.67.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="LAulT4G9" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=h3RTtupFJhDBM5slP9UfkwO8XkY2GekZeQ171kDvc/BX44yVys1CMYyD0Bp7L0FXt/hKeclPO0p4URT8YnZqCuXjdVMRjqMC7nA2SCDbJUwLjthdX3tNQmelzRAW0SEm4b+jmVEtpwE+PwjBT9goe62+lOyjP7V9sZO0DxzQ7UaFlg1uc1eQSyTAVj6OijuBrXVg5Az7SyQdhF9guZ8OJPeSgRZHccMOZ8f72wwiQzZMYjommFOlbRwZDULdfnYC70uSsvaua+cppl5ya7U96WbQp2BEmUhwBiJfyVlp7M3TUiRxfjarDhCwp7N0LEZttzu6BV7qXnBeNcTaVKL6BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HEHYVemPzQDfpMdhfbuzyQ4zV5E9zOILEQA8q0JwEiU=; b=GBl22elodIIPnPrOGf7yhuAzioodqVBfhuXV/1C2ZvuOmgJJF12qBwV9qq8GuxV6wCII5oTtepbYTTP/qTHQbRz6J6J5TCu97tQpiIYhsxG7W25VF9sWZed8wr4Er4gNWQ/gd+nUJR5ovQFbkTBm/rAqKJPUZKoaOiMkA2OXxz2Wq11DYp8T+WqHFVAnCnNKucGE6LgKvmBvhmpYIckihvhShicxYH3Ya5Kjg86m0frgaBISsamiMFplPusSpmsgCWRbsAvvHUM+qCMYemu3SyYNdX/AQ0rTGque71NrTXiFR7b5VhsKjc+TDjhwjxZwpJ6TuebygOtpTetl1z0OYw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector1-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HEHYVemPzQDfpMdhfbuzyQ4zV5E9zOILEQA8q0JwEiU=; b=LAulT4G9v1KEzUWcvfStcaEZjMsl6ugV8dSWuyn8uHknzWq5mBcFy3wQI3ukLxyYn+AzZBYTyp6Gb7gy7IMoA59es77Spzufd6QWikX9BuVMEylJSbYZS3jV6yN1lj1uaRUFVhmEKI0bPr/zZxvI548JKOn0b4fCyE0tfQpEUgTlBGOAEyA5KE8P/vd1zWsJVCcYF03h1rV0BlkspMiL+m7THS2HO8jYwlOseL7uLEPQJZg+B2h+eecpyeQRoNIs15COzLNF/pziBup42Rm5Z/wNgnuu2+5+ksNp9Jx2w4XpqYoy6a4FmNtt4G6fuuugR3z6tBotv4ervrXIH9HlQg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from AM9PR04MB8487.eurprd04.prod.outlook.com (2603:10a6:20b:41a::6) by VI2PR04MB10267.eurprd04.prod.outlook.com (2603:10a6:800:225::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.24; Thu, 19 Sep 2024 13:48:51 +0000 Received: from AM9PR04MB8487.eurprd04.prod.outlook.com ([fe80::6d7a:8d2:f020:455]) by AM9PR04MB8487.eurprd04.prod.outlook.com ([fe80::6d7a:8d2:f020:455%5]) with mapi id 15.20.7962.022; Thu, 19 Sep 2024 13:48:50 +0000 From: Andrei Stefanescu To: Linus Walleij , Bartosz Golaszewski , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chester Lin , Matthias Brugger , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, NXP S32 Linux Team , Andrei Stefanescu , Krzysztof Kozlowski Subject: [PATCH v3 1/4] drivers: provide devm_platform_get_and_ioremap_resource_byname() Date: Thu, 19 Sep 2024 16:47:21 +0300 Message-ID: <20240919134732.2626144-2-andrei.stefanescu@oss.nxp.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240919134732.2626144-1-andrei.stefanescu@oss.nxp.com> References: <20240919134732.2626144-1-andrei.stefanescu@oss.nxp.com> X-ClientProxiedBy: AM0PR10CA0109.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:208:e6::26) To AM9PR04MB8487.eurprd04.prod.outlook.com (2603:10a6:20b:41a::6) Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM9PR04MB8487:EE_|VI2PR04MB10267:EE_ X-MS-Office365-Filtering-Correlation-Id: 43152e2d-79fa-4372-2929-08dcd8b1cb04 X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|376014|7416014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?yEg/CjfDZ6i6d0DUME00GYcrt9Def4l?= =?utf-8?q?EyycLChiPPNOHMPEb0y7UDwskCUbpkSo3/VkYYs6imYbPgypy6efKJIa8/+anmEtE?= =?utf-8?q?bxXrqR7CQlm3KOsGRwxHQl6Bu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uoPNDqT1KmhhDZ4URSlYrNsH?= =?utf-8?q?soDFrDeeMDB4+2jlQgtUboDPUFPErwMhMy0F0JMoaugd5m2MnsXUYysczMAAL8LUY?= =?utf-8?q?oGAf0ndeoHeaU5WH4S9WisJapUUQE0P5ZPnXPPIjWEcg+EplYrG2tsVM6etU3MSa9?= =?utf-8?q?qsd/vF0rf5ONs2oj2gCtp+XY7WA9kIMcvCjC4lanG+u5ZXlmqxGOlTbShSmHk736V?= =?utf-8?q?RlhCboazN6y1FsViDtDvHtSJ9brQG5I1M2xMteX/iz439nyFSS1UwRyuG9Gc7IbsG?= =?utf-8?q?OJ+2sUhYoExcwKoQUbnTjeDYqq2gmM8nFKGGyaJcipV18aRrWeS1s54nCfd0p/Cjj?= =?utf-8?q?ld8UXIdYP9DpSctjUjQ+XRWoaxlxNVEqiA=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM9PR04MB8487.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(376014)(7416014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?gCLlUU+k76f+yBs7UM73Bm9hc7f0?= =?utf-8?q?Ore/jEJqJ7lhMWXivRqmmTy+pb42RndHCsgEKEdDATz6cG78Bv8+BeEOcV42G8yBX?= =?utf-8?q?DmOmYehpp0M9BaTY1C8LYfT/v8JWWs1QF42hSy5NXBsliN56BNJXVaufpQNkApQcW?= =?utf-8?q?1akKt4t0DKJzRFHgnwhby5varuz7iv3FOeLbqQ7wwignhTkf7nf33ODZ2WqfAUsXO?= =?utf-8?q?5eqZfAmO+YkFSt2AqgztSVC/TAJXnQDu2UgEXstryyNOi5vbsW00gi5h2hB5f/cyX?= =?utf-8?q?iV3YImf4CI8TuB4ARCLSDwDQQjorUf4RV0IF28J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dD3rZ2StA1fcE7iq?= =?utf-8?q?iQ/QmDqQa9Wkj5U675OrD0cu3VCaEz/hdXITLVolgc0r7Ac8g5nILIKTFlOUg0P5M?= =?utf-8?q?S8EZ4lm9u6iJkfm9iC021SdddSwRcRL717pxr/X6b7Kk3YbtWepNLW3anfa/N7CJu?= =?utf-8?q?IFwgE8z8hgdysTgOzsi6FU616S6SRyPJrdUropWbtW2UuIT2RzRQe6AloGW/wSfb3?= =?utf-8?q?pCLxFd3++BCo7Wz2RgM8/Rsb7V4sMjUy+TFy0dYOFjV5AV3xGMaPi7GN9KYOuTTOA?= =?utf-8?q?xg+njsUVlGzj/yR/CRKyOPuV3ostJtOnmAvGxjD0PaXeYZME5PkEyaGoik4FzKx28?= =?utf-8?q?dvR3KaUs0omCHYE3p4T49dm3YwXVoQNi8w=3D=3D?= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 43152e2d-79fa-4372-2929-08dcd8b1cb04 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8487.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2024 13:48:50.8422 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Md9XLRUwzCxo7ePUyx4nV86hJZb+0NzUWMcNYQINp2hG1LTn/eFp+VuEp0GI+qyfTjmxegFfecnFd7xWu3qQ5yUO9OLzU93LIUFrADcvLwE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI2PR04MB10267 Similar to commit 890cc39a879906b63912482dfc41944579df2dc6 ("drivers: provide devm_platform_get_and_ioremap_resource()") add a wrapper for "platform_get_resource_byname" and "devm_ioremap_resource". This new wrapper also returns the resource, if any, via a pointer. Suggested-by: Krzysztof Kozlowski Reviewed-by: Matthias Brugger Signed-off-by: Andrei Stefanescu --- drivers/base/platform.c | 27 +++++++++++++++++++++++++++ include/linux/platform_device.h | 13 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/drivers/base/platform.c b/drivers/base/platform.c index 4c3ee6521ba5..da6827f9462a 100644 --- a/drivers/base/platform.c +++ b/drivers/base/platform.c @@ -108,6 +108,33 @@ devm_platform_get_and_ioremap_resource(struct platform_device *pdev, } EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource); +/** + * devm_platform_get_and_ioremap_resource_byname - call devm_ioremap_resource() + * for a platform device and get + * a resource by its name + * + * @pdev: platform device to use both for memory resource lookup as well as + * resource management + * @name: resource name + * @res: optional output parameter to store a pointer to the obtained resource. + * + * Return: a pointer to the remapped memory or an ERR_PTR() encoded error code + * on failure. + */ +void __iomem * +devm_platform_get_and_ioremap_resource_byname(struct platform_device *pdev, + const char *name, + struct resource **res) +{ + struct resource *r; + + r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); + if (res) + *res = r; + return devm_ioremap_resource(&pdev->dev, r); +} +EXPORT_SYMBOL_GPL(devm_platform_get_and_ioremap_resource_byname); + /** * devm_platform_ioremap_resource - call devm_ioremap_resource() for a platform * device diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h index d422db6eec63..ab7f33f3c426 100644 --- a/include/linux/platform_device.h +++ b/include/linux/platform_device.h @@ -68,6 +68,12 @@ platform_find_device_by_driver(struct device *start, extern void __iomem * devm_platform_get_and_ioremap_resource(struct platform_device *pdev, unsigned int index, struct resource **res); + +extern void __iomem * +devm_platform_get_and_ioremap_resource_byname(struct platform_device *pdev, + const char *name, + struct resource **res); + extern void __iomem * devm_platform_ioremap_resource(struct platform_device *pdev, unsigned int index); @@ -83,6 +89,13 @@ devm_platform_get_and_ioremap_resource(struct platform_device *pdev, return ERR_PTR(-EINVAL); } +static inline void __iomem * +devm_platform_get_and_ioremap_resource_byname(struct platform_device *pdev, + const char *name, + struct resource **res) +{ + return ERR_PTR(-EINVAL); +} static inline void __iomem * devm_platform_ioremap_resource(struct platform_device *pdev, From patchwork Thu Sep 19 13:47:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrei Stefanescu X-Patchwork-Id: 829710 Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazon11013022.outbound.protection.outlook.com [52.101.67.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A8FF19F49C; Thu, 19 Sep 2024 13:49:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.67.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726753750; cv=fail; b=cccZ7p89luLkLc3Bp6WJ9EMmdXnJePQRXaB/noXKs7BlQjD+5lllLg8pzsDjF28GfVyDlJHQkL/6DC/UyVjT74wUXu3Tsm9AKWEcQapRXAvP5hTFstu1zBV42Hr/dfTj5W32KHK1wVtijPHwzrXhPMHZN5iyoWyC45iZCleyvdI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726753750; c=relaxed/simple; bh=hQAolRTSKZ+/lytUBtWWg07LCKf0VBU5y75qgQzBsJQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DnAZXgRPq2+lCakw0j9rq3F9xtiVqvBGdj8Mx7aRUjiDywASR9XUWWmZLZbsqldGky5uRwgNLbqjWV9X/nJel51xXkZfm1bpFcJyyIE8cvysJTCZhmkfin1ijRjEigIH63wlRbAoua1yXaekj1XIpVJNhdmlWlsjkxTGPXxkTao= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com; spf=pass smtp.mailfrom=oss.nxp.com; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b=rXUvkjc2; arc=fail smtp.client-ip=52.101.67.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="rXUvkjc2" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pwm/jql5QADeHVEBipTQzh1cVg1waQS9ZnvlczvAgJhP8XV05zl3/EX7JMz4fwmitfPMiYtW/aLbJWVTbAYfmuxq8FE5JdZ7bDGBfOE177uMkM35L4zGdZ4mVasDAC0Ollawn3iOtWegnrdelFIFEVF1DB5LaGue5boFqWcGVXxce3vSSoBe4a4hZ7eB37Q89E4/qaKpG50E5ewgyYUaevGPa2sRtuinR1LInSdG9qd8aj3QkPWugzaoWpfn2jwvCmbJuh8Bhddvm3gAQl0SUz0oMIyBMj3rcYScukvE8FPC/6PgMyE9Q5yz2p7Tr/Eo/vCMJq+5svh4bDDfR9FOtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MQi7i+4aEouje7mWUyll8Elatm3DJMHU6rtOOmUPpnU=; b=u4zT7az/mJk/Axp8/+NQrlCsDq4Zaoh/WMl8feS0uuJ5kudSch56LbV56jZyNaxhZE+CvyAt7NeqMNxsCz59dJDMhc/lREDRG6lMbkdsbMztGXWvstxW/YV4XgdEfKBWLmgir4MnstSw4mgx926PORBGknaYBhg3qpBJf6IiY0id7ziN/taQQyfzmgqqpHTchaA0J/wQgJOP7aLhptE7YRbgTn3VF51qQ2ElMoq2x1N8tG8kHU/rko9aAyCmsUBrusnwThA/oMemVeiYizlnJE9Z/MdFyrUikQfx0LeESXQlssa0vf9Z24O39LSbUy6vQBX5VeI63+jjGPIaLVhknw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector1-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MQi7i+4aEouje7mWUyll8Elatm3DJMHU6rtOOmUPpnU=; b=rXUvkjc2aL/jvx8Mlk769IjpwW0KGd+ay65JMpBkJG5HFrUF5/llMPt5eYxusivzpIq1XiNKdL4zhDnzQCE/C8HwiZ4YbQS/s1fb5bo64UJH4JaqKOLKdjDwgPC1gmy9o6Ra4Dx2p+/6Zoit7besN7RUeccUAaN1IEQ39EFXelGG0GuIY/nvgsu3HQkTH9TEoWjuN9enDNbN7X+KXzYmhnOT8erAU709/YhsZrncKrs4Daah7XHqCZPVtiuPsNNaS+YqKSGjB6L+iMTRiiHTMQgIcEmpmUccT1YwaGF63ko5hoqF3ETzNB/XCMAATvXUjP65ofaRALFwWGeQhSQO6Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from AM9PR04MB8487.eurprd04.prod.outlook.com (2603:10a6:20b:41a::6) by VI2PR04MB10267.eurprd04.prod.outlook.com (2603:10a6:800:225::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7962.24; Thu, 19 Sep 2024 13:48:54 +0000 Received: from AM9PR04MB8487.eurprd04.prod.outlook.com ([fe80::6d7a:8d2:f020:455]) by AM9PR04MB8487.eurprd04.prod.outlook.com ([fe80::6d7a:8d2:f020:455%5]) with mapi id 15.20.7962.022; Thu, 19 Sep 2024 13:48:54 +0000 From: Andrei Stefanescu To: Linus Walleij , Bartosz Golaszewski , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chester Lin , Matthias Brugger , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-gpio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, NXP S32 Linux Team , Andrei Stefanescu Subject: [PATCH v3 3/4] gpio: siul2-s32g2: add NXP S32G2/S32G3 SoCs support Date: Thu, 19 Sep 2024 16:47:23 +0300 Message-ID: <20240919134732.2626144-4-andrei.stefanescu@oss.nxp.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240919134732.2626144-1-andrei.stefanescu@oss.nxp.com> References: <20240919134732.2626144-1-andrei.stefanescu@oss.nxp.com> X-ClientProxiedBy: AM0PR10CA0109.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:208:e6::26) To AM9PR04MB8487.eurprd04.prod.outlook.com (2603:10a6:20b:41a::6) Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM9PR04MB8487:EE_|VI2PR04MB10267:EE_ X-MS-Office365-Filtering-Correlation-Id: d49c1b87-c674-419b-183e-08dcd8b1cd21 X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|366016|376014|7416014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?kCWe0kPPX/jnJifMF6HenMw3Af6Tusd?= =?utf-8?q?0Hoxip6lAjIg6kUZVtvSIpdLmH5rj29ayC1hU6G1ssgIu/Zs7XFDpNeG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ZxYBi?= =?utf-8?q?qvJ9eFIjQ7oLS+g6g8p2nO8M52Hc1dHPcW0nzjygWGjC1pjjCXd+FpNPOud9UiAzg?= =?utf-8?q?Ovz+eurwlwygZV6bv5oBNAt1rkvhdorLUS5qviH65qOrjex+p3SUL9N0qQHi4Q2bS?= =?utf-8?q?zw1PYxhIrrbDW18xYhPV9D/0PbophUU8E9HX3vOhN8EhS1Q8sMH15J9ztkERx/d3h?= =?utf-8?q?3tdhXHTYSS1rQPv+MLrhJG7VVU36hXd/MXTJl3mq97Z6RB0S7X2SiAGA/ZsiI7tLN?= =?utf-8?q?cxIaDJBv0fl7wugfoG6eBuBB4iuNzTVqsDMRvDIBqmGEsj74VBXuAGX7pTKsK8k4C?= =?utf-8?q?52fQ+lTBklHxjyrH0m/Iu2Lg3h1ZRfJusA7f+cS9KA22tayQ24LNUpmb0ke4TEscp?= =?utf-8?q?XTgKdtWC8K5NcOfPfQqo2KCb4mnL+UQ4DQ=3D=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM9PR04MB8487.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(1800799024)(366016)(376014)(7416014)(52116014)(38350700014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?q8n24dS8lLuVIKKy46DxZuFB9Ie/?= =?utf-8?q?UJTLD6MYsKEK5Wj4IhM4zivSNtVlaoK2AEadXEhLjr4OQAHC2MIh7PQsFh3KFqzXg?= =?utf-8?q?dRvu2LgFIKHjHPDBWr6MZIhKKXD678+st3FhqLABNevqJZIE3yMLSB0jnV9jn5wq7?= =?utf-8?q?mP4h2rPQxEFY3H/Larx+xyhJ7O3zfd6E1prLuIu0iopxmmE2GoqkYt/bMxdVRhVc/?= =?utf-8?q?FCJM79/rK10nIlhoRXdEb5Qrdw+xZ3DVrNs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DW6dzBMxwT3CC7uW18nF+NLEV3NRaEhNs9azUuh?= =?utf-8?q?Mi2zkBR1XjfjvPIimAfMRTBsv2ZqKGYv4IRBbuc4CHj/sG0+vdroWH4AvBvHq5CEm?= =?utf-8?q?ft33WITSjo1pRVdfCXMf+N5KvPWihPw9OdYtZv6erb6Qotsw0S0YU43BKn615svlE?= =?utf-8?q?a1FRvvEZMZD8dKMRDKpBfkWlOt0kT24JcgqdaLq3fo93ca49+iuCyM/lTLrjH2cRU?= =?utf-8?q?rICtdyIKH8xX7kDePNwgMro/uPA7tqdG8Cm0BUHPSLE1uINHzOsfJ3hOZQ29BrJ58?= =?utf-8?q?KtC88+Y8sZRAZecDgQQp5U07hZ5LlNBUBxISkCr/KxChcfc+0ehq/P3aCqKq9pOhV?= =?utf-8?q?N1IrnY9bewnlvp3LHDKi1QRqQR+X0M3pgQiL8j4gM42Cea2pgULk1CruNHMUCwu5Q?= =?utf-8?q?dL+r61p/F1IoHIFIsaxa4Ppe0g5HDle44w=3D=3D?= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: d49c1b87-c674-419b-183e-08dcd8b1cd21 X-MS-Exchange-CrossTenant-AuthSource: AM9PR04MB8487.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2024 13:48:54.3706 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bxIzTghbWeyFM9cQPchyU66HWdhqCOuUOlBIYwXP1WoLs9MV9t5laZ9cp+DK+NtZvs469alT8pEy2RmB3j7Hgbzio/jcdnEsQ8ruIZiTPs8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI2PR04MB10267 Add the GPIO driver for S32G2/S32G3 SoCs. This driver uses the SIUL2 (System Integration Unit Lite2) hardware module. There are two SIUL2 hardware modules present, SIUL2_0(controlling GPIOs 0-101) and SIUL2_1 for the rest. The GPIOs are not fully contiguous, there are some gaps: - GPIO102 up to GPIO111(inclusive) are invalid - GPIO123 up to GPIO143(inclusive) are invalid Some GPIOs are input only(i.e. GPI182) though this restriction is not yet enforced in code. This patch adds basic GPIO functionality(no interrupts, no suspend/resume functions). Signed-off-by: Ghennadi Procopciuc Signed-off-by: Larisa Grigore Signed-off-by: Phu Luu An Signed-off-by: Andrei Stefanescu --- drivers/gpio/Kconfig | 10 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-siul2-s32g2.c | 576 ++++++++++++++++++++++++++++++++ 3 files changed, 587 insertions(+) create mode 100644 drivers/gpio/gpio-siul2-s32g2.c diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 58f43bcced7c..75a6ca60ebc7 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -643,6 +643,16 @@ config GPIO_SIOX Say yes here to support SIOX I/O devices. These are units connected via a SIOX bus and have a number of fixed-direction I/O lines. +config GPIO_SIUL2_S32G2 + tristate "GPIO driver for S32G2/S32G3" + depends on ARCH_S32 || COMPILE_TEST + depends on OF_GPIO + select REGMAP_MMIO + help + This enables support for the SIUL2 GPIOs found on the S32G2/S32G3 + chips. Say yes here to enable the SIUL2 to be used as an GPIO + controller for S32G2/S32G3 platforms. + config GPIO_SNPS_CREG bool "Synopsys GPIO via CREG (Control REGisters) driver" depends on ARC || COMPILE_TEST diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index 64dd6d9d730d..fb6e770a64b9 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -149,6 +149,7 @@ obj-$(CONFIG_GPIO_SCH) += gpio-sch.o obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o obj-$(CONFIG_GPIO_SIM) += gpio-sim.o obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o +obj-$(CONFIG_GPIO_SIUL2_S32G2) += gpio-siul2-s32g2.o obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o obj-$(CONFIG_GPIO_SLOPPY_LOGIC_ANALYZER) += gpio-sloppy-logic-analyzer.o obj-$(CONFIG_GPIO_SODAVILLE) += gpio-sodaville.o diff --git a/drivers/gpio/gpio-siul2-s32g2.c b/drivers/gpio/gpio-siul2-s32g2.c new file mode 100644 index 000000000000..a69cbb3bcfaf --- /dev/null +++ b/drivers/gpio/gpio-siul2-s32g2.c @@ -0,0 +1,576 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * SIUL2 GPIO support. + * + * Copyright (c) 2016 Freescale Semiconductor, Inc. + * Copyright 2018-2024 NXP + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* PGPDOs are 16bit registers that come in big endian + * order if they are grouped in pairs of two. + * + * For example, the order is PGPDO1, PGPDO0, PGPDO3, PGPDO2... + */ +#define SIUL2_PGPDO(N) (((N) ^ 1) * 2) +#define S32G2_SIUL2_NUM 2 +#define S32G2_PADS_DTS_TAG_LEN (7) + +#define SIUL2_GPIO_16_PAD_SIZE 16 + +/** + * struct siul2_device_data - platform data attached to the compatible. + * @pad_access: access table for I/O pads, consists of S32G2_SIUL2_NUM tables. + * @reset_cnt: reset the pin name counter to zero when switching to SIUL2_1. + */ +struct siul2_device_data { + const struct regmap_access_table **pad_access; + const bool reset_cnt; +}; + +/** + * struct siul2_desc - describes a SIUL2 hw module. + * @pad_access: array of valid I/O pads. + * @opadmap: the regmap of the Parallel GPIO Pad Data Out Register. + * @ipadmap: the regmap of the Parallel GPIO Pad Data In Register. + * @gpio_base: the first GPIO pin. + * @gpio_num: the number of GPIO pins. + */ +struct siul2_desc { + const struct regmap_access_table *pad_access; + struct regmap *opadmap; + struct regmap *ipadmap; + u32 gpio_base; + u32 gpio_num; +}; + +/** + * struct siul2_gpio_dev - describes a group of GPIO pins. + * @platdata: the platform data. + * @siul2: SIUL2_0 and SIUL2_1 modules information. + * @pin_dir_bitmap: the bitmap with pin directions. + * @gc: the GPIO chip. + * @lock: mutual access to bitmaps. + */ +struct siul2_gpio_dev { + const struct siul2_device_data *platdata; + struct siul2_desc siul2[S32G2_SIUL2_NUM]; + unsigned long *pin_dir_bitmap; + struct gpio_chip gc; + raw_spinlock_t lock; +}; + +static const struct regmap_range s32g2_siul20_pad_yes_ranges[] = { + regmap_reg_range(SIUL2_PGPDO(0), SIUL2_PGPDO(0)), + regmap_reg_range(SIUL2_PGPDO(1), SIUL2_PGPDO(1)), + regmap_reg_range(SIUL2_PGPDO(2), SIUL2_PGPDO(2)), + regmap_reg_range(SIUL2_PGPDO(3), SIUL2_PGPDO(3)), + regmap_reg_range(SIUL2_PGPDO(4), SIUL2_PGPDO(4)), + regmap_reg_range(SIUL2_PGPDO(5), SIUL2_PGPDO(5)), + regmap_reg_range(SIUL2_PGPDO(6), SIUL2_PGPDO(6)), +}; + +static const struct regmap_access_table s32g2_siul20_pad_access_table = { + .yes_ranges = s32g2_siul20_pad_yes_ranges, + .n_yes_ranges = ARRAY_SIZE(s32g2_siul20_pad_yes_ranges), +}; + +static const struct regmap_range s32g2_siul21_pad_yes_ranges[] = { + regmap_reg_range(SIUL2_PGPDO(7), SIUL2_PGPDO(7)), + regmap_reg_range(SIUL2_PGPDO(9), SIUL2_PGPDO(9)), + regmap_reg_range(SIUL2_PGPDO(10), SIUL2_PGPDO(10)), + regmap_reg_range(SIUL2_PGPDO(11), SIUL2_PGPDO(11)), +}; + +static const struct regmap_access_table s32g2_siul21_pad_access_table = { + .yes_ranges = s32g2_siul21_pad_yes_ranges, + .n_yes_ranges = ARRAY_SIZE(s32g2_siul21_pad_yes_ranges), +}; + +static const struct regmap_access_table *s32g2_pad_access_table[] = { + &s32g2_siul20_pad_access_table, + &s32g2_siul21_pad_access_table +}; + +static_assert(ARRAY_SIZE(s32g2_pad_access_table) == S32G2_SIUL2_NUM); + +static const struct siul2_device_data s32g2_device_data = { + .pad_access = s32g2_pad_access_table, + .reset_cnt = true, +}; + +static int siul2_get_gpio_pinspec(struct platform_device *pdev, + struct of_phandle_args *pinspec, + unsigned int range_index) +{ + struct device_node *np = pdev->dev.of_node; + + return of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, + range_index, pinspec); +} + +static struct regmap *siul2_offset_to_regmap(struct siul2_gpio_dev *dev, + unsigned int offset, + bool input) +{ + struct siul2_desc *siul2; + size_t i; + + for (i = 0; i < ARRAY_SIZE(dev->siul2); i++) { + siul2 = &dev->siul2[i]; + if (offset >= siul2->gpio_base && + offset - siul2->gpio_base < siul2->gpio_num) + return input ? siul2->ipadmap : siul2->opadmap; + } + + return NULL; +} + +static void siul2_gpio_set_direction(struct siul2_gpio_dev *dev, + unsigned int gpio, int dir) +{ + guard(raw_spinlock_irqsave)(&dev->lock); + + if (dir == GPIO_LINE_DIRECTION_IN) + __clear_bit(gpio, dev->pin_dir_bitmap); + else + __set_bit(gpio, dev->pin_dir_bitmap); +} + +static int siul2_get_direction(struct siul2_gpio_dev *dev, + unsigned int gpio) +{ + return test_bit(gpio, dev->pin_dir_bitmap) ? GPIO_LINE_DIRECTION_OUT : + GPIO_LINE_DIRECTION_IN; +} + +static struct siul2_gpio_dev *to_siul2_gpio_dev(struct gpio_chip *chip) +{ + return container_of(chip, struct siul2_gpio_dev, gc); +} + +static int siul2_gpio_dir_in(struct gpio_chip *chip, unsigned int gpio) +{ + struct siul2_gpio_dev *gpio_dev; + int ret = 0; + + ret = pinctrl_gpio_direction_input(chip, gpio); + if (ret) + return ret; + + gpio_dev = to_siul2_gpio_dev(chip); + siul2_gpio_set_direction(gpio_dev, gpio, GPIO_LINE_DIRECTION_IN); + + return 0; +} + +static int siul2_gpio_get_dir(struct gpio_chip *chip, unsigned int gpio) +{ + return siul2_get_direction(to_siul2_gpio_dev(chip), gpio); +} + +static unsigned int siul2_pin2pad(unsigned int pin) +{ + return pin / SIUL2_GPIO_16_PAD_SIZE; +} + +static u16 siul2_pin2mask(unsigned int pin) +{ + /** + * From Reference manual : + * PGPDOx[PPDOy] = GPDO(x × 16) + (15 - y)[PDO_(x × 16) + (15 - y)] + */ + return BIT(SIUL2_GPIO_16_PAD_SIZE - 1 - pin % SIUL2_GPIO_16_PAD_SIZE); +} + +static void siul2_gpio_set_val(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct siul2_gpio_dev *gpio_dev = to_siul2_gpio_dev(chip); + unsigned int pad, reg_offset; + struct regmap *regmap; + u16 mask; + + mask = siul2_pin2mask(offset); + pad = siul2_pin2pad(offset); + + reg_offset = SIUL2_PGPDO(pad); + regmap = siul2_offset_to_regmap(gpio_dev, offset, false); + if (!regmap) + return; + + value = value ? mask : 0; + + regmap_update_bits(regmap, reg_offset, mask, value); +} + +static int siul2_gpio_dir_out(struct gpio_chip *chip, unsigned int gpio, + int val) +{ + struct siul2_gpio_dev *gpio_dev; + int ret = 0; + + gpio_dev = to_siul2_gpio_dev(chip); + siul2_gpio_set_val(chip, gpio, val); + + ret = pinctrl_gpio_direction_output(chip, gpio); + if (ret) + return ret; + + siul2_gpio_set_direction(gpio_dev, gpio, GPIO_LINE_DIRECTION_OUT); + + return 0; +} + +static void siul2_gpio_set(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct siul2_gpio_dev *gpio_dev = to_siul2_gpio_dev(chip); + + if (!gpio_dev) + return; + + if (siul2_get_direction(gpio_dev, offset) == GPIO_LINE_DIRECTION_IN) + return; + + siul2_gpio_set_val(chip, offset, value); +} + +static int siul2_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct siul2_gpio_dev *gpio_dev = to_siul2_gpio_dev(chip); + unsigned int mask, pad, reg_offset, data = 0; + struct regmap *regmap; + + mask = siul2_pin2mask(offset); + pad = siul2_pin2pad(offset); + + reg_offset = SIUL2_PGPDO(pad); + regmap = siul2_offset_to_regmap(gpio_dev, offset, true); + if (!regmap) + return -EINVAL; + + regmap_read(regmap, reg_offset, &data); + + return !!(data & mask); +} + +static const struct regmap_config siul2_regmap_conf = { + .val_bits = 32, + .reg_bits = 32, + .reg_stride = 4, + .cache_type = REGCACHE_FLAT, +}; + +static struct regmap *common_regmap_init(struct platform_device *pdev, + struct regmap_config *conf, + const char *name) +{ + struct device *dev = &pdev->dev; + struct resource *res; + resource_size_t size; + void __iomem *base; + + base = devm_platform_get_and_ioremap_resource_byname(pdev, name, &res); + if (IS_ERR(base)) { + dev_err(&pdev->dev, "Failed to get MEM resource: %s\n", name); + return ERR_PTR(-EINVAL); + } + + size = resource_size(res); + conf->val_bits = conf->reg_stride * 8; + conf->max_register = size - conf->reg_stride; + conf->name = name; + conf->use_raw_spinlock = true; + + if (conf->cache_type != REGCACHE_NONE) + conf->num_reg_defaults_raw = size / conf->reg_stride; + + return devm_regmap_init_mmio(dev, base, conf); +} + +static bool not_writable(__always_unused struct device *dev, + __always_unused unsigned int reg) +{ + return false; +} + +static struct regmap *init_padregmap(struct platform_device *pdev, + struct siul2_gpio_dev *gpio_dev, + int selector, bool input) +{ + const struct siul2_device_data *platdata = gpio_dev->platdata; + struct regmap_config regmap_conf = siul2_regmap_conf; + char dts_tag[S32G2_PADS_DTS_TAG_LEN]; + int err; + + regmap_conf.reg_stride = 2; + + if (selector != 0 && selector != 1) + return ERR_PTR(-EINVAL); + + regmap_conf.rd_table = platdata->pad_access[selector]; + + err = snprintf(dts_tag, ARRAY_SIZE(dts_tag), "%cpads%d", + input ? 'i' : 'o', selector); + if (err < 0) + return ERR_PTR(-EINVAL); + + if (input) { + regmap_conf.writeable_reg = not_writable; + regmap_conf.cache_type = REGCACHE_NONE; + } else { + regmap_conf.wr_table = platdata->pad_access[selector]; + } + + return common_regmap_init(pdev, ®map_conf, dts_tag); +} + +static int siul2_gpio_pads_init(struct platform_device *pdev, + struct siul2_gpio_dev *gpio_dev) +{ + struct device *dev = &pdev->dev; + size_t i; + + for (i = 0; i < ARRAY_SIZE(gpio_dev->siul2); i++) { + gpio_dev->siul2[i].opadmap = init_padregmap(pdev, gpio_dev, i, + false); + if (IS_ERR(gpio_dev->siul2[i].opadmap)) { + dev_err(dev, + "Failed to initialize opad2%zu regmap config\n", + i); + return PTR_ERR(gpio_dev->siul2[i].opadmap); + } + + gpio_dev->siul2[i].ipadmap = init_padregmap(pdev, gpio_dev, i, + true); + if (IS_ERR(gpio_dev->siul2[i].ipadmap)) { + dev_err(dev, + "Failed to initialize ipad2%zu regmap config\n", + i); + return PTR_ERR(gpio_dev->siul2[i].ipadmap); + } + } + + return 0; +} + +static int siul2_gen_names(struct device *dev, unsigned int cnt, char **names, + char *ch_index, unsigned int *num_index) +{ + unsigned int i; + + for (i = 0; i < cnt; i++) { + if (i != 0 && !(*num_index % 16)) + (*ch_index)++; + + names[i] = devm_kasprintf(dev, GFP_KERNEL, "P%c_%02d", + *ch_index, 0xFU & (*num_index)++); + if (!names[i]) + return -ENOMEM; + } + + return 0; +} + +static int siul2_gpio_remove_reserved_names(struct device *dev, + struct siul2_gpio_dev *gpio_dev, + char **names) +{ + struct device_node *np = dev->of_node; + int num_ranges, i, j, ret; + u32 base_gpio, num_gpio; + + /* Parse the gpio-reserved-ranges to know which GPIOs to exclude. */ + + num_ranges = of_property_count_u32_elems(dev->of_node, + "gpio-reserved-ranges"); + + /* The "gpio-reserved-ranges" is optional. */ + if (num_ranges < 0) + return 0; + num_ranges /= 2; + + for (i = 0; i < num_ranges; i++) { + ret = of_property_read_u32_index(np, "gpio-reserved-ranges", + i * 2, &base_gpio); + if (ret) { + dev_err(dev, "Could not parse the start GPIO: %d\n", + ret); + return ret; + } + + ret = of_property_read_u32_index(np, "gpio-reserved-ranges", + i * 2 + 1, &num_gpio); + if (ret) { + dev_err(dev, "Could not parse num. GPIOs: %d\n", ret); + return ret; + } + + if (base_gpio + num_gpio > gpio_dev->gc.ngpio) { + dev_err(dev, "Reserved GPIOs outside of GPIO range\n"); + return -EINVAL; + } + + /* Remove names set for reserved GPIOs. */ + for (j = base_gpio; j < base_gpio + num_gpio; j++) { + devm_kfree(dev, names[j]); + names[j] = NULL; + } + } + + return 0; +} + +static int siul2_gpio_populate_names(struct device *dev, + struct siul2_gpio_dev *gpio_dev) +{ + unsigned int num_index = 0; + char ch_index = 'A'; + char **names; + int i, ret; + + names = devm_kcalloc(dev, gpio_dev->gc.ngpio, sizeof(*names), + GFP_KERNEL); + if (!names) + return -ENOMEM; + + for (i = 0; i < S32G2_SIUL2_NUM; i++) { + ret = siul2_gen_names(dev, gpio_dev->siul2[i].gpio_num, + names + gpio_dev->siul2[i].gpio_base, + &ch_index, &num_index); + if (ret) { + dev_err(dev, "Could not set names for SIUL2_%d GPIOs\n", + i); + return ret; + } + + if (gpio_dev->platdata->reset_cnt) + num_index = 0; + + ch_index++; + } + + ret = siul2_gpio_remove_reserved_names(dev, gpio_dev, names); + if (ret) + return ret; + + gpio_dev->gc.names = (const char *const *)names; + + return 0; +} + +static int siul2_gpio_probe(struct platform_device *pdev) +{ + struct siul2_gpio_dev *gpio_dev; + struct device *dev = &pdev->dev; + struct of_phandle_args pinspec; + size_t i, bitmap_size; + struct gpio_chip *gc; + int ret = 0; + + gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL); + if (!gpio_dev) + return -ENOMEM; + + gpio_dev->platdata = &s32g2_device_data; + + for (i = 0; i < S32G2_SIUL2_NUM; i++) + gpio_dev->siul2[i].pad_access = + gpio_dev->platdata->pad_access[i]; + + ret = siul2_gpio_pads_init(pdev, gpio_dev); + if (ret) + return ret; + + gc = &gpio_dev->gc; + + platform_set_drvdata(pdev, gpio_dev); + + raw_spin_lock_init(&gpio_dev->lock); + + for (i = 0; i < ARRAY_SIZE(gpio_dev->siul2); i++) { + ret = siul2_get_gpio_pinspec(pdev, &pinspec, i); + if (ret) { + dev_err(dev, + "unable to get pinspec %zu from device tree\n", + i); + return -EINVAL; + } + + of_node_put(pinspec.np); + + if (pinspec.args_count != 3) { + dev_err(dev, "Invalid pinspec count: %d\n", + pinspec.args_count); + return -EINVAL; + } + + gpio_dev->siul2[i].gpio_base = pinspec.args[1]; + gpio_dev->siul2[i].gpio_num = pinspec.args[2]; + } + + gc->base = -1; + + /* In some cases, there is a gap between the SIUL GPIOs. */ + gc->ngpio = gpio_dev->siul2[S32G2_SIUL2_NUM - 1].gpio_base + + gpio_dev->siul2[S32G2_SIUL2_NUM - 1].gpio_num; + + ret = siul2_gpio_populate_names(&pdev->dev, gpio_dev); + if (ret) + return ret; + + bitmap_size = BITS_TO_LONGS(gc->ngpio) * + sizeof(*gpio_dev->pin_dir_bitmap); + gpio_dev->pin_dir_bitmap = devm_kzalloc(dev, bitmap_size, GFP_KERNEL); + if (!gpio_dev->pin_dir_bitmap) + return -ENOMEM; + + gc->parent = dev; + gc->label = dev_name(dev); + + gc->set = siul2_gpio_set; + gc->get = siul2_gpio_get; + gc->set_config = gpiochip_generic_config; + gc->request = gpiochip_generic_request; + gc->free = gpiochip_generic_free; + gc->direction_output = siul2_gpio_dir_out; + gc->direction_input = siul2_gpio_dir_in; + gc->get_direction = siul2_gpio_get_dir; + gc->owner = THIS_MODULE; + + ret = devm_gpiochip_add_data(dev, gc, gpio_dev); + if (ret) + return dev_err_probe(dev, ret, "unable to add gpiochip\n"); + + return 0; +} + +static const struct of_device_id siul2_gpio_dt_ids[] = { + { .compatible = "nxp,s32g2-siul2-gpio" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, siul2_gpio_dt_ids); + +static struct platform_driver siul2_gpio_driver = { + .driver = { + .name = "s32g2-siul2-gpio", + .of_match_table = siul2_gpio_dt_ids, + }, + .probe = siul2_gpio_probe, +}; + +module_platform_driver(siul2_gpio_driver); + +MODULE_AUTHOR("NXP"); +MODULE_DESCRIPTION("NXP SIUL2 GPIO"); +MODULE_LICENSE("GPL");