From patchwork Tue Dec 10 21:27:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13902086 Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03on2085.outbound.protection.outlook.com [40.107.103.85]) (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 DF17323239E for ; Tue, 10 Dec 2024 21:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.103.85 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866056; cv=fail; b=sL6jNUxfdvp1w+7c0YV99JfbUwyUXbjhWIJeuKb9oOm4tMcZHpy5Xr6TlNJsoitfXUEYbMukEP5Fiz5zeKPlAEQhXM/pvWZ+hvXkwVAatv9rp2bTyLPotBwGhIlQ/apza+S7I0Pt/GzejWEpO2GgLSZUfsewMKQbBUCHRS9K+Vg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733866056; c=relaxed/simple; bh=UBRH/DZ+712KxK9ZDlNSlCh6M3g3qSAyI/ZL35VH1OU=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=LxmugOeDntdnaNySm0qNukAz2i1CBeNh+2bfMOmva8BAryHTCd2/ZAEXqqUeA/nNYG3ild15Hzib95Z50pb9B8F0fsCpfC6m/nJlh8YpwIV9hixEDoTsvf4s77iCODZ4UwRudi/fhy/HDr9e/519YnsY8fjErihX/5XLNQ3RdRU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b=DYSJN+kj; arc=fail smtp.client-ip=40.107.103.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b="DYSJN+kj" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ueofw3H2iiPyQ2ZFg/wBYbWb04MhvV1t43TWYB7cahEL28X77W9eTftniWlnxlfDXwVig75NCqpiYqswxJL72F9yiPwgU8qJdwagAF3GKe/YUVk13oezjc0f2NIuWnOXSTdcYlrKmtM0/UBm5XyBsAOSkVniksqRAjEb56LnzK/YctzmPC+iCoodd2bGNv9KE3XIWu+UoxKdrKzTev6g3a+ZknkGfM9VByNwaIEcVUB+kd4Pc1URxxV7roT6uW0+ng4NF3HMOKvosoF78ZhsQ1CpIiZkuhiRysRjBcWw0xqDf1UF6GlpsJ4SoFmVeMmreSSTiJqKVL8e3bg7b8/IxQ== 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=eKuh2jrYk7ZziHGoSy1nV2WC7g4UIPaOeOprVp6g0hw=; b=X+f1VImxa+vYQJafinOis0L55qSTJGNhwmL4qWwsUpbl0CiP1mtzGxu6/J2f2PThOrYbqSYQsyYWjmVYuV9Yo8pDbmaTs4blmpBDF6sjxSxp49QyEPi0DzRdIpoxIbOw2ht7oQe4wruvfjkZtbm2IfDyeWRhY7sqNnhJ0yM2m2cuji0lIuzCcL4nUrTsv/qt3uiVNGtGxpVHYoIxpEvI+FcJ0I0AdPB7g0RjujzEKgFLka02kAX6rn9kzsiKzx8jIdPgmsEv3tFwetXPJoJ95XZIZgUS7+zZguUhiGn95yK2aFHP5I7gVE38etRc4fnMdDFQk2zNJWFeDTG8ZjzkJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=eKuh2jrYk7ZziHGoSy1nV2WC7g4UIPaOeOprVp6g0hw=; b=DYSJN+kje0BnGFNq3sOnGRelXxmXwnKlisY5ckEvsK/0MpRGICt4YpBeYseQLOYxa3ptf6755m/TDpwuQekzAgKxM2WkZa+NXDyDW8IsZU3i6804so5jWJAz6VeygFYGNbJn0h1wBwcGDrRprCES/IC7zSces0mWWTKwc7iciTFu7nPJIxs4r7jHGgTx/+342uRzqavcA08fxNUDDcQA2IhZTsZ5NRzrzOpMfPDd6AM4/3cQKnRvPIF523yAob7iavAWfBQ+izV08oUFr2j1/vYy5joNG9tDjRTlx7JLp5WJElov2On20dOTXv06j8Lm0yZPa+43T8p/U3xdtlAWhA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by PA4PR04MB7550.eurprd04.prod.outlook.com (2603:10a6:102:f0::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Tue, 10 Dec 2024 21:27:30 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%5]) with mapi id 15.20.8230.010; Tue, 10 Dec 2024 21:27:30 +0000 From: Frank Li Date: Tue, 10 Dec 2024 16:27:03 -0500 Subject: [PATCH v2 2/2] thermal: imx91: Add support for i.MX91 thermal monitoring unit Message-Id: <20241210-imx91tmu-v2-2-5032aad4d88e@nxp.com> References: <20241210-imx91tmu-v2-0-5032aad4d88e@nxp.com> In-Reply-To: <20241210-imx91tmu-v2-0-5032aad4d88e@nxp.com> To: "Rafael J. Wysocki" , Daniel Lezcano , Zhang Rui , Lukasz Luba , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Pengfei Li , Marco Felsch Cc: linux-pm@vger.kernel.org, devicetree@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Frank Li , Peng Fan X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1733866037; l=9362; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=GaTapRVMfFiP49k8WRVidYkY+FWOXePac372dgGJZ+Q=; b=C/pECoT9LFUgiheY2l7kSsB4nDk9ANUu0WALyWaP+LGXAP7eHfODra31o+yMdlK46RO8Y3wne JscyjrDtXUzB1mqdKe9jgn0XE/E46mGhtUrbvUYoFqZLJ5xIbjyNnKJ X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: SJ0P220CA0018.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::28) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: imx@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|PA4PR04MB7550:EE_ X-MS-Office365-Filtering-Correlation-Id: f5e46868-6744-4e22-4a0f-08dd1961741e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014|52116014|921020|38350700014; X-Microsoft-Antispam-Message-Info: =?utf-8?q?b6c6lMLT39zkis3M40x4TtLtc3/swxb?= =?utf-8?q?RoQ9CoFBmPgzCg4nJbDk9LFFZT6C5l6qh8yWdy5gnCy+9QT8Rsi3OXbfXSWv434zA?= =?utf-8?q?/RtSxB8v8EQm0216s4t918/5WKx8pRBg4yNtiFQJY7hDyJh2usRcHV/9NKvgS2xIL?= =?utf-8?q?RbkzPMZloq/XE64JC15wwqitllNDVH+ljbYBMqqnesPa0YV5CSTgd6AicknV8JROg?= =?utf-8?q?6Bj7aAml2nGOy/d+MuFe1duSNMkvYAXD+Wmz7tlWTll0VoYdOGyA/V2gcbnZYz1ov?= =?utf-8?q?YV4UXMaMC2O2xjI7a9QBPhDo65N+Yboyqy/W7FltQ9RLV87occctyT5SkVph+FsdL?= =?utf-8?q?tmgUiuAzVmK0JjiGDwDAiTKgn30K+WWgr6g3kHB+i0yAFLcR8kXXYZJsXdjTAw8CS?= =?utf-8?q?6Hvj/i+ijHcDO7xvTCTVTpTkzYB+2SoTGsou5szARnPGiC3S0Z6Zwe2M29KCawz3T?= =?utf-8?q?hMD+ZjT6EkRzSKbWkgzT8ypRYDLqlJzYHIMkypsRWPAwE3d4UlyZm/KaRov3bj+eM?= =?utf-8?q?kWk7SQDADEpzK3JJpCrT5qngx6e9J02ORUqcn2MJmbu3nn8hyQVt0oirmpFmpb5DF?= =?utf-8?q?8rpoyq4rDHVznnZ9myZavWdOTPhcYcrV8TCza20vIY66TvsgG0d21GJ6dC5w5qJn9?= =?utf-8?q?esIT0wFe9/tElHMHiKldklmoWuuCiW+HEj9ZT7WJFCkBvSGAdFAdfDE29Pa3cpV45?= =?utf-8?q?+nn6giepk1wiBnzqCzpnfH2qvw+pFEgakSRZU6LF/+eDJ+Z56IoIFuDNs5V/HnKXw?= =?utf-8?q?cIxywItlmNnLbgpmu1zZQpg/OoobKXeQIdbwzBOTKwLWfsZsDooMCh8FqleTNKkWX?= =?utf-8?q?O/uKaNtl2d6EG4GAn7KkqIN+Gx4EfdlMjy8Dgnl0MdW5U4HIPLI6Fkl3LqVAFTm3V?= =?utf-8?q?lqcuM1JIZTFRrAeYOVObWe9k5ZPQxj5UQpf4Cz436lik0b4uzNmaFG8i0+6WneOsb?= =?utf-8?q?vLco63wAygeHBcCZMKhRIOTTBAnxUPPAL/2pPF8+C9diuHKeHPiXR9g9GstIcXgrs?= =?utf-8?q?+o2sysMbalg+qaoszL/rVcn0glDI8ZipnfWx2g+tQDSktjQxTazgVbAH20SxzaThR?= =?utf-8?q?B2hUy3yo6AobS3qsWhWoavvcVkZIDQsuNvKKURP13ep8Soe/verRH8oqF2lSN+jgd?= =?utf-8?q?pRsojjWSuDbGmnWeQLKDoZuGxU5sv5MOSlltTCTx3WsfzwVVH/Usyw/M5+djbJ6e/?= =?utf-8?q?9bUXFZCcTG7m+VQJO3by7hfgNzcLunL7IglRY9386+98McW3mMlIi0cNrsfjnz7UT?= =?utf-8?q?ghgGtwXSbr7Vc9JUo10MGOzoFdtOLgZefohe5ab4KHTHbxVcLxZm7B9XF7j/FA1HO?= =?utf-8?q?dmmk0nScnH7vXVQh7l/q7SedsZeoMX7aADEmR31Szd/kqYGZmC8CTO0=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014)(52116014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?IkUb7RNzPIWQuzmLNT75x+sUXOK2?= =?utf-8?q?2ncs+yQWTxByQr59ndVXCMY/Ox4XDvcR9e5U6qNoztN/sHH7XYfBgLSidRo2wokXh?= =?utf-8?q?F4WSucLP35tm+nlLW3SvS59VXWQ8AbSBUIwKwi9Lx18VmFhuEcBhmpVIJt723A30p?= =?utf-8?q?ZsBJG6s78Rjq9gxr0euLdi4wTlmk2Tz61X/iL0CBf3l6616jybZAuJvp1l5TKH5bc?= =?utf-8?q?7ynGW5eJNMiM+Io8wQ1GaCASFXKJb1Fs8x//GVIgypBEsOpgbAluI0GUQ/m8Fo5F/?= =?utf-8?q?Bvi2eDDD7Jy7pdBDRyjKNjcUPV64d5UEMfe4phq2SyREfX1eMy+nEqKORdrrrQr8h?= =?utf-8?q?pR02KTWyygygjRQgpHoJTbqP4/PU9Bsx6JKXDpRMj3Gpj9DRFcwhqvkQzFgR+yS8V?= =?utf-8?q?VOn8+/Xns+CX8lE+2VOaaLuD4JhzqW+54bLJM77Bt+40/PSO0I7Z2pDD82FF24uyM?= =?utf-8?q?zPqT3PTWtbp21HGa44NOxAPGqtAi8WtewEX9ExQqohgrlIRI1CCoCZVnfqUEh8bcr?= =?utf-8?q?1nO8gJjP5d8OFmCVzZHvY8+KrlLlkiw9MzfSYwH6Fnxl3XJMJgXzeV0uIBr5BVrGt?= =?utf-8?q?mhy+kytmRjFs1f2w6w5/vsE4ybaCJSbbE8wg4rVLQOCtSD7WCml3BJ1zujcJeEv/0?= =?utf-8?q?BDa6DGEnlTijZkbmgASBAROqrGHUT1PId8uXTRw6+Tc0PTc+07HegUnjRRyblCTTE?= =?utf-8?q?YJhMe9hWWfLl8t1z3oVmnQDoRFqNmE2Jv/kitgvAvbpJf3vw64bGXhJzCLLReC4z7?= =?utf-8?q?TxXpyW6tAzKUv4PEbbi/4tgyka1EOogU+j8VB7YCkpzX8CtMERSOdZXlIl/NG4YoI?= =?utf-8?q?KVqqh53d5zkHan5jkJeg1DgkX9RWm86ONo7/ZlxTlTooSPuvlHZZW0x0a8UNN6aCG?= =?utf-8?q?56ClqyFyHZHecDLPimkT+zHORRoqodtjR6GBcncNckYL143Md9e27Y8Q2VPSK4lWH?= =?utf-8?q?CT0MjrSCqs71Y1PQwF7BDqAfVayRXIfOqkp5SMFkqLNgdWgZb2TFbUXOOZRYy0cKu?= =?utf-8?q?thHWxltdyVP/1v9iYLTRaYZgkQisPSnPfJoa5SK5hJSng5lES29FBL4gPIwLbQLqp?= =?utf-8?q?xk/NdQl8fCEsSo/dXO9LVld70Gl74HGtK3Ker9QZ6lYVS79IJj/iZKeE8aTZ7mbd7?= =?utf-8?q?4KEbRZr+e7v2y7XPf/Ic3yZ3sSw5xsuXTd6muYl7g+ncDUGUkoHauD9KR4aiP238+?= =?utf-8?q?vqYbCfDdcivs9pDt5dTNGy8MghfwF+a7kGsV4Ul0UktzL2SuIIuRJgkHQ9JdDCR9i?= =?utf-8?q?j67AItYATozWwQAzkVQtTKvjvy6DMfYoofUJ5UL6hyT0Yhr4i2n2SF0RL/NA9sekC?= =?utf-8?q?JFO4eqjy8gR4OYemUJeRYW/KaLRS07Wv5IS37ZZb6MsQT/KUgRuG0K9Vpg6J/y5re?= =?utf-8?q?i/63M3RSy5oSriSn/WDx3u6pvxFzyJIUi44bw/JjudWyRt5uu8cn1/0Zwldaxlt0M?= =?utf-8?q?VwtDoQsOCxWLSR0zpj2odIuL45UQsZ2iSPcds5IHFHQEGbd7n2v3IIsGLkOMXEIXJ?= =?utf-8?q?5D9gPapq1G5k?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: f5e46868-6744-4e22-4a0f-08dd1961741e X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2024 21:27:30.8167 (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: FL245M2iodEP6gHvbLFSXtz2sUd4XYPzKmUviD9X5hmZdhE4dtS1kc/Sswyat6i3y0Q4A0axq8kEl+2cw+Tovw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR04MB7550 From: Pengfei Li Introduce support for the i.MX91 thermal monitoring unit, which features a single sensor for the CPU. The register layout differs from other chips, necessitating the creation of a dedicated file for this. Signed-off-by: Pengfei Li Signed-off-by: Peng Fan Signed-off-by: Frank Li --- change from v1 to v2 - use low case for hexvalue - combine struct imx91_tmu and tmu_sensor - simplify imx91_tmu_start() and imx91_tmu_enable() - use s16 for imx91_tmu_get_temp(), which may negative value - use reverse christmas tree style - use run time pm - use oneshot to sample temp - register thermal zone after hardware init --- drivers/thermal/Kconfig | 10 ++ drivers/thermal/Makefile | 1 + drivers/thermal/imx91_thermal.c | 265 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 276 insertions(+) diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index d3f9686e26e71..da403ed86aeb1 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -296,6 +296,16 @@ config IMX8MM_THERMAL cpufreq is used as the cooling device to throttle CPUs when the passive trip is crossed. +config IMX91_THERMAL + tristate "Temperature sensor driver for NXP i.MX91 SoC" + depends on ARCH_MXC || COMPILE_TEST + depends on OF + help + Support for Temperature sensor found on NXP i.MX91 SoC. + It supports one critical trip point and one passive trip point. The + cpufreq is used as the cooling device to throttle CPUs when the passive + trip is crossed. + config K3_THERMAL tristate "Texas Instruments K3 thermal support" depends on ARCH_K3 || COMPILE_TEST diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile index 9abf43a74f2bb..08da241e6a598 100644 --- a/drivers/thermal/Makefile +++ b/drivers/thermal/Makefile @@ -50,6 +50,7 @@ obj-$(CONFIG_ARMADA_THERMAL) += armada_thermal.o obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o obj-$(CONFIG_IMX_SC_THERMAL) += imx_sc_thermal.o obj-$(CONFIG_IMX8MM_THERMAL) += imx8mm_thermal.o +obj-$(CONFIG_IMX91_THERMAL) += imx91_thermal.o obj-$(CONFIG_MAX77620_THERMAL) += max77620_thermal.o obj-$(CONFIG_QORIQ_THERMAL) += qoriq_thermal.o obj-$(CONFIG_DA9062_THERMAL) += da9062-thermal.o diff --git a/drivers/thermal/imx91_thermal.c b/drivers/thermal/imx91_thermal.c new file mode 100644 index 0000000000000..ebb59eda92951 --- /dev/null +++ b/drivers/thermal/imx91_thermal.c @@ -0,0 +1,265 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2024 NXP. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CTRL0 0x0 + +#define STAT0 0x10 +#define STAT0_DRDY0_IF_MASK BIT(16) + +#define DATA0 0x20 + +#define THR_CTRL01 0x30 +#define THR_CTRL23 0x40 + +#define CTRL1 0x200 +#define CTRL1_SET 0x204 +#define CTRL1_CLR 0x208 +#define CTRL1_EN BIT(31) +#define CTRL1_START BIT(30) +#define CTRL1_STOP BIT(29) +#define CTRL1_RES_MASK GENMASK(19, 18) +#define CTRL1_MEAS_MODE_MASK GENMASK(25, 24) +#define CTRL1_MEAS_MODE_SINGLE 0 +#define CTRL1_MEAS_MODE_CONTINUES 1 +#define CTRL1_MEAS_MODE_PERIODIC 2 + +#define REF_DIV 0x280 +#define DIV_EN BIT(31) +#define DIV_MASK GENMASK(23, 16) + +#define PUD_ST_CTRL 0x2B0 +#define PUDL_MASK GENMASK(23, 16) + +#define TRIM1 0x2E0 +#define TRIM2 0x2F0 + +#define TMU_TEMP_LOW_LIMIT -40000 +#define TMU_TEMP_HIGH_LIMIT 125000 + +#define DEFAULT_TRIM1_CONFIG 0xb561bc2d +#define DEFAULT_TRIM2_CONFIG 0x65d4 + +struct imx91_tmu { + void __iomem *base; + struct clk *clk; + struct device *dev; + struct thermal_zone_device *tzd; +}; + +static void imx91_tmu_start(struct imx91_tmu *tmu, bool start) +{ + u32 val = start ? CTRL1_START : CTRL1_STOP; + + writel_relaxed(val, tmu->base + CTRL1_SET); +} + +static void imx91_tmu_enable(struct imx91_tmu *tmu, bool enable) +{ + u32 reg = enable ? CTRL1_SET : CTRL1_CLR; + + writel_relaxed(CTRL1_EN, tmu->base + reg); +} + +static int imx91_tmu_get_temp(struct thermal_zone_device *tz, int *temp) +{ + struct imx91_tmu *tmu = thermal_zone_device_priv(tz); + s16 data; + int ret; + u32 val; + + ret = pm_runtime_resume_and_get(tmu->dev); + if (ret < 0) + return ret; + + ret = readl_relaxed_poll_timeout(tmu->base + STAT0, val, + val & STAT0_DRDY0_IF_MASK, 1000, + 40000); + if (ret) + return -EAGAIN; + + /* DATA0 is 16bit signed number */ + data = readw_relaxed(tmu->base + DATA0); + *temp = data * 1000 / 64; + if (*temp < TMU_TEMP_LOW_LIMIT || *temp > TMU_TEMP_HIGH_LIMIT) + return -EAGAIN; + + pm_runtime_put(tmu->dev); + + return 0; +} + +static struct thermal_zone_device_ops tmu_tz_ops = { + .get_temp = imx91_tmu_get_temp, +}; + +static int imx91_init_from_nvmem_cells(struct imx91_tmu *tmu) +{ + struct device *dev = tmu->dev; + u32 trim1, trim2; + int ret; + + ret = nvmem_cell_read_u32(dev, "trim1", &trim1); + if (ret) + return ret; + + ret = nvmem_cell_read_u32(dev, "trim2", &trim2); + if (ret) + return ret; + + if (trim1 == 0 || trim2 == 0) + return -EINVAL; + + writel_relaxed(trim1, tmu->base + TRIM1); + writel_relaxed(trim2, tmu->base + TRIM2); + + return 0; +} + +static int imx91_tmu_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct imx91_tmu *tmu; + unsigned long rate; + u32 div; + int ret; + + tmu = devm_kzalloc(&pdev->dev, sizeof(struct imx91_tmu), GFP_KERNEL); + if (!tmu) + return -ENOMEM; + + tmu->dev = &pdev->dev; + + tmu->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(tmu->base)) + return PTR_ERR(tmu->base); + + tmu->clk = devm_clk_get_enabled(dev, NULL); + if (IS_ERR(tmu->clk)) + return dev_err_probe(dev, PTR_ERR(tmu->clk), "failed to get tmu clock\n"); + + platform_set_drvdata(pdev, tmu); + + /* disable the monitor during initialization */ + imx91_tmu_enable(tmu, false); + imx91_tmu_start(tmu, false); + + ret = imx91_init_from_nvmem_cells(tmu); + if (ret) { + writel_relaxed(DEFAULT_TRIM1_CONFIG, tmu->base + TRIM1); + writel_relaxed(DEFAULT_TRIM2_CONFIG, tmu->base + TRIM2); + } + + /* The typical conv clk is 4MHz, the output freq is 'rate / (div + 1)' */ + rate = clk_get_rate(tmu->clk); + div = (rate / 4000000) - 1; + if (div > FIELD_GET(DIV_MASK, DIV_MASK)) + return -EINVAL; + + /* Set divider value and enable divider */ + writel_relaxed(DIV_EN | FIELD_PREP(DIV_MASK, div), tmu->base + REF_DIV); + + /* Set max power up delay: 'Tpud(ms) = 0xFF * 1000 / 4000000' */ + writel_relaxed(FIELD_PREP(PUDL_MASK, 100U), tmu->base + PUD_ST_CTRL); + + /* + * Set resolution mode + * 00b - Conversion time = 0.59325 ms + * 01b - Conversion time = 1.10525 ms + * 10b - Conversion time = 2.12925 ms + * 11b - Conversion time = 4.17725 ms + */ + writel_relaxed(FIELD_PREP(CTRL1_RES_MASK, 0x3), tmu->base + CTRL1_CLR); + writel_relaxed(FIELD_PREP(CTRL1_RES_MASK, 0x1), tmu->base + CTRL1_SET); + + writel_relaxed(CTRL1_MEAS_MODE_MASK, tmu->base + CTRL1_CLR); + writel_relaxed(FIELD_PREP(CTRL1_MEAS_MODE_MASK, CTRL1_MEAS_MODE_SINGLE), + tmu->base + CTRL1_SET); + + clk_disable_unprepare(tmu->clk); + pm_runtime_set_suspended(dev); + pm_runtime_enable(dev); + + tmu->tzd = devm_thermal_of_zone_register(dev, 0, tmu, &tmu_tz_ops); + if (IS_ERR(tmu->tzd)) + return dev_err_probe(dev, PTR_ERR(tmu->tzd), + "failed to register thermal zone sensor\n"); + + return 0; +} + +static void imx91_tmu_remove(struct platform_device *pdev) +{ + struct imx91_tmu *tmu = platform_get_drvdata(pdev); + + /* disable tmu */ + imx91_tmu_start(tmu, false); + imx91_tmu_enable(tmu, false); +} + +static int imx91_tmu_runtime_suspend(struct device *dev) +{ + struct imx91_tmu *tmu = dev_get_drvdata(dev); + + /* disable tmu */ + imx91_tmu_start(tmu, false); + imx91_tmu_enable(tmu, false); + + clk_disable_unprepare(tmu->clk); + + return 0; +} + +static int imx91_tmu_runtime_resume(struct device *dev) +{ + struct imx91_tmu *tmu = dev_get_drvdata(dev); + int ret; + + ret = clk_prepare_enable(tmu->clk); + if (ret) + return ret; + + imx91_tmu_enable(tmu, true); + imx91_tmu_start(tmu, true); + + return 0; +} + +static const struct dev_pm_ops imx91_tmu_pm_ops = { + SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) + RUNTIME_PM_OPS(imx91_tmu_runtime_suspend, imx91_tmu_runtime_resume, NULL) +}; + +static const struct of_device_id imx91_tmu_table[] = { + { .compatible = "fsl,imx91-tmu", }, + { }, +}; +MODULE_DEVICE_TABLE(of, imx91_tmu_table); + +static struct platform_driver imx91_tmu = { + .driver = { + .name = "i.MX91_thermal", + .pm = pm_ptr(&imx91_tmu_pm_ops), + .of_match_table = imx91_tmu_table, + }, + .probe = imx91_tmu_probe, + .remove = imx91_tmu_remove, +}; +module_platform_driver(imx91_tmu); + +MODULE_AUTHOR("Peng Fan "); +MODULE_DESCRIPTION("i.MX91 Thermal Monitor Unit driver"); +MODULE_LICENSE("GPL");