From patchwork Thu Jul 4 21:37:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724289 Received: from mail-il1-f173.google.com (mail-il1-f173.google.com [209.85.166.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3C3813A260 for ; Thu, 4 Jul 2024 21:37:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129043; cv=none; b=h1LnKVL6JyClBIVpKXH8NUszSwHLobVKFybFcRbopl+91nQ+zE6c9Xlho1qWYyrxMGXf024KTXY+Q7tf4NxAE4DmqwiqkJl3IRgoOs7+Dmv1MdkjolslmHv2J3GQ3wL0BUPNhtvIunNLeIyBJbEevnh7+6wEEyuvwGCaFypnT/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129043; c=relaxed/simple; bh=mIIsDFzMw6/AWQVQ/iQUOQ1uB4z3DigcyVbPvPZFgfs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YMggIF6CBrwRp2gX7VSbBi1qFEdxjcwWA4KUEkxB3lhWtUQWWO31WMMI3URk3HrZVV8LBkaPYGb41s0jdVm6J51ibmpQQxjKgp0LUDbxQL+eSNBVyDzxnLfp4HLeJqHDXnrm6is+zQglGYGQUVsAz9ELCNLZ6hPMWPKGBzm6gtE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=T9X/osc/; arc=none smtp.client-ip=209.85.166.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T9X/osc/" Received: by mail-il1-f173.google.com with SMTP id e9e14a558f8ab-3737dc4a669so3596835ab.0 for ; Thu, 04 Jul 2024 14:37:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129040; x=1720733840; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=5dG6iRobVZTYBkdbLDz9IGF27zkktf4osxkRKl6w5hc=; b=T9X/osc/aX6Dma8orlkJjbMgDFUqMrZKh44lD15xSahD41b5A2C2Gkmr/QC2yM8INX 8WT0wKvgIqu9Wy1bU/Ybb6uRSl6wZQbPdcn4WhoFknzfFnO653YcQErmiI5Znmce34UX AxsMQHXgZkT/MNIiDsfqNKlXnD5ofNZ7MnjrLI8EStS+Kx+X6h5TC5fsMbnrnJhN1v6F gyqEymHfRnYzMUJa73Kyyngpk6MwY6WWJ77GVlanxMF8UDA1w5SF3PhjMIYR5tgEdcJJ lSagVXkLOn0AgeMdR8GsawkNEf/iYBmxW8fXkpQB502KaxMgHZ9ceOXMLUHbNqOWKBjS JEFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129040; x=1720733840; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5dG6iRobVZTYBkdbLDz9IGF27zkktf4osxkRKl6w5hc=; b=bn7uwHb2QyysvQU3YxAq3T0qS9AFP5W5aqd8dZXBVcv3PzfQjBLHLeI7sE7cDG41Tc a+y72wFpKaGSVFXhq22XUqikholoQ5k99YjNBZt7Q05mI1yJuixY9QfcXHyajj2ZaTHq FL3re6pthXAKFoBa6kJoj15MoWoETmEziHwPEKSVwI0N5lfDLZR7WmS8zG4264+jQNqC zOPonVjnslpon5aD9ND+IQVZfVhaCzwN1j1QkIfm+Yi+OGQt+EwMfTXT/QlSB0r+oLo5 LtTGDlTYezFUpMel0/VvHzMyZGzpp+nym8f0IYel334chFaGKzL4C22ksC5vrs7pCMv/ rKZg== X-Gm-Message-State: AOJu0YwGb/m65SKau0XwGk3bp5EjzNTryoWvhsW4SICAJqceF1IX84YT dezsdNurvi7IIELWpqHEngxv4jnUzn4TkYVapaF0zZkFA1mDGsownohBuQ== X-Google-Smtp-Source: AGHT+IEK+ssAk4HUs/ckLB6evmHUEZZny+PqJO79Dtl52z1hNG8cDplUuyVUNr/n5/t295OhMU91Fg== X-Received: by 2002:a05:6e02:18c5:b0:376:148f:d6c6 with SMTP id e9e14a558f8ab-3839a6f85cbmr34645675ab.24.1720129040211; Thu, 04 Jul 2024 14:37:20 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7623cd854b2sm968059a12.38.2024.07.04.14.37.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:19 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 1/7] hwmon: (g762) Simplify clock initialization Date: Thu, 4 Jul 2024 14:37:06 -0700 Message-Id: <20240704213712.2699553-2-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use a device managed function to prepare and enable the clock. To match the current code, only let it fails if a device node is present (i.e., when it is mandatory). Otherwise set the default clock speed. No functional change intended, even though the code now does set the default frequency if there is neither a devicetree node nor platform data. Signed-off-by: Guenter Roeck --- drivers/hwmon/g762.c | 66 +++++++++----------------------------------- 1 file changed, 13 insertions(+), 53 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index af1228708e25..da43a26f558d 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -115,7 +115,6 @@ enum g762_regs { struct g762_data { struct i2c_client *client; - struct clk *clk; /* update mutex */ struct mutex update_lock; @@ -574,66 +573,27 @@ MODULE_DEVICE_TABLE(of, g762_dt_match); /* * Grab clock (a required property), enable it, get (fixed) clock frequency - * and store it. Note: upon success, clock has been prepared and enabled; it - * must later be unprepared and disabled (e.g. during module unloading) by a - * call to g762_of_clock_disable(). Note that a reference to clock is kept - * in our private data structure to be used in this function. + * and store it. */ -static void g762_of_clock_disable(void *data) +static int g762_of_clock_enable(struct device *dev) { - struct g762_data *g762 = data; - - clk_disable_unprepare(g762->clk); - clk_put(g762->clk); -} - -static int g762_of_clock_enable(struct i2c_client *client) -{ - struct g762_data *data; - unsigned long clk_freq; + unsigned long clk_freq = 0; struct clk *clk; int ret; - if (!client->dev.of_node) - return 0; - - clk = of_clk_get(client->dev.of_node, 0); + clk = devm_clk_get_enabled(dev, NULL); if (IS_ERR(clk)) { - dev_err(&client->dev, "failed to get clock\n"); - return PTR_ERR(clk); + if (dev->of_node) + return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n"); + } else { + clk_freq = clk_get_rate(clk); } - ret = clk_prepare_enable(clk); - if (ret) { - dev_err(&client->dev, "failed to enable clock\n"); - goto clk_put; - } - - clk_freq = clk_get_rate(clk); - ret = do_set_clk_freq(&client->dev, clk_freq); - if (ret) { - dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq); - goto clk_unprep; - } - - data = i2c_get_clientdata(client); - data->clk = clk; - - ret = devm_add_action(&client->dev, g762_of_clock_disable, data); - if (ret) { - dev_err(&client->dev, "failed to add disable clock action\n"); - goto clk_unprep; - } + ret = do_set_clk_freq(dev, clk_freq); + if (ret) + return dev_err_probe(dev, ret, "invalid clock freq %lu\n", clk_freq); return 0; - - clk_unprep: - clk_disable_unprepare(clk); - - clk_put: - clk_put(clk); - - return ret; } static int g762_of_prop_import_one(struct i2c_client *client, @@ -682,7 +642,7 @@ static int g762_of_prop_import(struct i2c_client *client) return 0; } -static int g762_of_clock_enable(struct i2c_client *client) +static int g762_of_clock_enable(struct device *dev) { return 0; } @@ -1062,7 +1022,7 @@ static int g762_probe(struct i2c_client *client) return ret; /* Get configuration via DT ... */ - ret = g762_of_clock_enable(client); + ret = g762_of_clock_enable(dev); if (ret) return ret; ret = g762_of_prop_import(client); From patchwork Thu Jul 4 21:37:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724290 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ED31143888 for ; Thu, 4 Jul 2024 21:37:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129045; cv=none; b=LLM3O6l6plMB/n23lTAJapZMiDdUAQC2npAWgsYlHh5tLdbil7gPUGtOokNR+KvIY86kMKOCZkuHlQisiQjL40jHdRb3m4QviuRSWDQH31vurWIOD8zBDZmJGmkEx3/8TTwZA375oubp1I3wJqm2G4y0gkvjI51p5+Mnc2ON+d8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129045; c=relaxed/simple; bh=RPHBgDZxfcAB6HPPQzxHs4E1QEJkQXRPY/aZ5bC3glg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SpfVzQofVQ5EGV4NFPJJrJAIMCUlYbbxKXcomdXstm2Uwad72tVS0V272iAgVeZd2Idn/LMbtkSdAJ1mxR4UZrXQCo81S63PfjKPLSx/8B/KOrMPdnkcsKOdPlRgFM7feAikzj62Ctq9AELAfrLaiScst2lzcjp764zvpzdpxts= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Dpr7Aj23; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Dpr7Aj23" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fb1c918860so13580185ad.1 for ; Thu, 04 Jul 2024 14:37:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129042; x=1720733842; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Cfcj/kKnCMp1UHLZbqSv0iOiyGovljmsk2n2/55swXc=; b=Dpr7Aj2355YgcDdX5akDS07aldeC8o7rTITZvBlF9jKdbcLNK5hpQTFkpQyH1Wr+kA i6J0qkNM/w4S/fakQkkYHwDuqGV3wJ5J3Z0NS/gLoMzDm4rZhR+51gZBQ6B0/3C96KQu e8ygiRQLmNh9PHgve5LoxIYbIqgHv1ufs1U+0gE2F9oUIEWVau11yY+E69rytIkngar6 XRtrOUzcoI+o1CDky8Hgfx9Di8Vp4E1oHYb2re6CeNGASECxspzPdMvvqbLogBeFHbPW gBf2Q8UL+DaWMKb6sFxM0eG45FQLqy6PFx2JfBsOFycbHHElU1hfZpUKkEjpiU1DKswR gpaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129042; x=1720733842; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Cfcj/kKnCMp1UHLZbqSv0iOiyGovljmsk2n2/55swXc=; b=KhkEtkG1Vue/hcZub6QvjnudT3DPTcmNEE38lroochUtT7MI7lk+Ay9UrfMjPbeA6i 9SE6Y72PS8J7s0riE5ImeVMt+QvLd71jLoebWhDvQ86oDLKzj/x0Ymou1Cwcf1bX6661 ogkJkwZ3yXj32nIAIJZb5irwLqlcZ0qd7CO/HNsow9Kw3EpJPyKuKVuTgzEZq1u3cx/9 OBAbasXDQ/s0S5R7a/YMGjYMrYgT1XnruTRZq94fnnsXZisPKhIkQzA/RJMZg96SobBW 456Xu26ISnRcm1sgz2GlW0e7U5aNWxIU2OcyaA070C9JgizqbhjsLTGdoBE5gZVc2EFe 4tiA== X-Gm-Message-State: AOJu0YwLRT+7dSWztkb2EW+MzgovQ6HvOXLpQe8HQ+mXuWt3LNif34or EIiyVQuMwLHMHUNZiAJJuzV+u2pUP+waXISvBy2Y+uB94qB9/dyXtHERWA== X-Google-Smtp-Source: AGHT+IHZd9z5rqWgJmibnPwNP1D5Zu34eCsLESbHkt2d3em4ng/PsWme4biMMMNvrD0KYygv2GhZsQ== X-Received: by 2002:a17:902:e84a:b0:1f6:6a94:76c5 with SMTP id d9443c01a7336-1fb3705f98emr36455795ad.20.1720129041931; Thu, 04 Jul 2024 14:37:21 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10d1353sm127704715ad.11.2024.07.04.14.37.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:21 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 2/7] hwmon: (g762) Drop platform data support Date: Thu, 4 Jul 2024 14:37:07 -0700 Message-Id: <20240704213712.2699553-3-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 There is no in-tree user (or at least not anymore), so drop platform data to simplify the code. Signed-off-by: Guenter Roeck --- Documentation/hwmon/g762.rst | 4 +--- drivers/hwmon/g762.c | 33 ------------------------------ include/linux/platform_data/g762.h | 24 ---------------------- 3 files changed, 1 insertion(+), 60 deletions(-) delete mode 100644 include/linux/platform_data/g762.h diff --git a/Documentation/hwmon/g762.rst b/Documentation/hwmon/g762.rst index 0371b3365c48..3dc5294b2181 100644 --- a/Documentation/hwmon/g762.rst +++ b/Documentation/hwmon/g762.rst @@ -17,9 +17,7 @@ done via a userland daemon like fancontrol. Note that those entries do not provide ways to setup the specific hardware characteristics of the system (reference clock, pulses per fan revolution, ...); Those can be modified via devicetree bindings -documented in Documentation/devicetree/bindings/hwmon/g762.txt or -using a specific platform_data structure in board initialization -file (see include/linux/platform_data/g762.h). +documented in Documentation/devicetree/bindings/hwmon/g762.txt. fan1_target: set desired fan speed. This only makes sense in closed-loop diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index da43a26f558d..8573865a8989 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -39,7 +39,6 @@ #include #include #include -#include #define DRVNAME "g762" @@ -648,34 +647,6 @@ static int g762_of_clock_enable(struct device *dev) } #endif -/* - * Helper to import hardware characteristics from .dts file and push - * those to the chip. - */ - -static int g762_pdata_prop_import(struct i2c_client *client) -{ - struct g762_platform_data *pdata = dev_get_platdata(&client->dev); - int ret; - - if (!pdata) - return 0; - - ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode); - if (ret) - return ret; - - ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity); - if (ret) - return ret; - - ret = do_set_fan_startv(&client->dev, pdata->fan_startv); - if (ret) - return ret; - - return do_set_clk_freq(&client->dev, pdata->clk_freq); -} - /* * sysfs attributes */ @@ -1026,10 +997,6 @@ static int g762_probe(struct i2c_client *client) if (ret) return ret; ret = g762_of_prop_import(client); - if (ret) - return ret; - /* ... or platform_data */ - ret = g762_pdata_prop_import(client); if (ret) return ret; diff --git a/include/linux/platform_data/g762.h b/include/linux/platform_data/g762.h deleted file mode 100644 index 249257ee2132..000000000000 --- a/include/linux/platform_data/g762.h +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Platform data structure for g762 fan controller driver - * - * Copyright (C) 2013, Arnaud EBALARD - */ -#ifndef __LINUX_PLATFORM_DATA_G762_H__ -#define __LINUX_PLATFORM_DATA_G762_H__ - -/* - * Following structure can be used to set g762 driver platform specific data - * during board init. Note that passing a sparse structure is possible but - * will result in non-specified attributes to be set to default value, hence - * overloading those installed during boot (e.g. by u-boot). - */ - -struct g762_platform_data { - u32 fan_startv; - u32 fan_gear_mode; - u32 pwm_polarity; - u32 clk_freq; -}; - -#endif /* __LINUX_PLATFORM_DATA_G762_H__ */ From patchwork Thu Jul 4 21:37:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724291 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E7A913A260 for ; Thu, 4 Jul 2024 21:37:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129047; cv=none; b=JEKWcnRH8Xa5gFoFNZXZFJQhwckb7KL9mlVZLl7za1Tslns/JFWHni3wiEGCLRQBWA8LyfGdJeGLURTnux58oD2nOPgXqt0Tq4Dutk1Zbj6KcnAgbOAbKlUeAbmj6DcpHio37luH1tkvD/ZBhcFpBO491MVHQLTRBqnWRW6Nm1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129047; c=relaxed/simple; bh=H1S3doi9gWB27jtoAzqBjVERy8ev1ffSYGerIRU0hpU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nJaBnpz4MI7GQRl1BGRIIBPif1ZHF4x78tSDKkrKRJJrIPkzg9PQVcmTdhK3R92WdhrvJBY8lHJBGVtlb/EGoTJNYeNremjwBc6bNuwiiby7TFnd+eGjUisf2DKUZgLQmnamQbK6K2pve0lexN4bFso7OwaJHXGDKFMbOzMwciE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FGM2cxoN; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FGM2cxoN" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5c46c8b0defso526213eaf.1 for ; Thu, 04 Jul 2024 14:37:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129044; x=1720733844; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=XrRefz4Ffas4iljHEqtXLusZyTaOKGs5s4BFiqr3zuM=; b=FGM2cxoNSSFgBtGrueO/pNdMi9hm1weP1nRfupB0oq3BSjiA9IehfybicqTRYQS10s RHPn/D+20hPt7oczFoUv7vPwil4C43EU3wwWEL7CBWr8UGsexi71d6ahZfMu6HtCcw8m Yy9uXmFt0CV188T2oS5yZruaJRRg3KTf2z+ab1PIu904OMGb6hMRWFz2/yn1ah41Kbik oGo+TDlbql/6H8it+jqZriJqCJXV8F7KD7MCF5z+NpjgifvlC7RmRVVsiNHwdcxgxDkh i7QgjBZM0SBnF8EYdLFRIoDUZ2nG/K1jI6exOZ00Vi6rDYuhxt9Ir71RMP3XrUmMR7Vl JGgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129044; x=1720733844; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XrRefz4Ffas4iljHEqtXLusZyTaOKGs5s4BFiqr3zuM=; b=baZLkVF+TfoLwBJzmi6fCRUKZVWk0Z65O6b5czc7+QsIWJfPkJtgY6O8BBBSWp1qH1 cGIL7hcZsQXbCDNkXjNUEPSzIfs3njAXKdr11HH140MZY0DyJ3gfK2M6Sdi2ranbP9JV +r6CXQg2CYK+2f/mqdGzMyGGHu8FSHtzLTGLwDB6d/VDLn4CH8phikddsgDNBJ2FY6yA 7GR6xIgR9BkCn9dA3FOfEVW6IumVJ3eNBE8VCnQ9nc/QfrixvA2WGjAw/l7xB1+WwrsF VFKJ78XleKGUm9c3CMFlmwhz2YJ7Kq+jd1fv3qKB0QWoXR/y44q4vnCKiA4wry5K8aNb /6Ew== X-Gm-Message-State: AOJu0YznUxzf/j4ql0mBvVdf1HHyeCxyMFi3YHIOLKhG5SzZlkTdQsg8 dzWxVHi/WPTqXOtCPo1CE1MotTb+AXVXP4p1UluIXXpRW+gyLBX4hFPH7g== X-Google-Smtp-Source: AGHT+IE5mcrX/bwRJuXBGdPKyt9EF7pvBtgoQ6md6ZJqOf701HqnR5pqzSci38LpEze7QIitVsGymQ== X-Received: by 2002:a05:6218:2601:b0:1a5:28a5:f2b9 with SMTP id e5c5f4694b2df-1aa98b1c61dmr257889555d.2.1720129043695; Thu, 04 Jul 2024 14:37:23 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c99a9cd133sm2004799a91.38.2024.07.04.14.37.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:23 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 3/7] hwmon: (g762) Reorder include files to be in alphabetic order Date: Thu, 4 Jul 2024 14:37:08 -0700 Message-Id: <20240704213712.2699553-4-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Alphabetic order for include files help deciding where to add new include files when needed. No functional change. Signed-off-by: Guenter Roeck --- drivers/hwmon/g762.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index 8573865a8989..37d8a45610a3 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -27,17 +27,17 @@ * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf */ +#include #include -#include -#include -#include +#include #include +#include #include #include -#include -#include +#include #include -#include +#include +#include #include #define DRVNAME "g762" From patchwork Thu Jul 4 21:37:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724292 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F14A13A260 for ; Thu, 4 Jul 2024 21:37:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129049; cv=none; b=m4zR9FqWjSNod+u9wg7TEsuon9WIkf8RFBecxvYrBDMBOnhRk0vCs8CgkhQMsF2/wSHK4P9IlYtCKrdlNVZak4XvGSLCCTx5samc0WpEoDDSlPancOzwMoWUXPn/gMscBpvoKwCblNpkFF0bRJIbenf8vt7Y9yTzIt35XuQ13SM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129049; c=relaxed/simple; bh=nSYTLfykq6Q7koEBiKI6IES9qjeWkhUyTRkStFXF/3Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=quyv8iElwBKKMM3LRwBKWj1yJ8lDrxfS8Jg8R/P/A3SJA5xl7FcspvriZP04tqC9GycD2HW8LoKwtSFd9MrNIukxxke5RiIQtrP6FDmeDwVbIOaZ/gT0y7dGvnEwVd67CIGioHe9KWarLCnnXedFNQFCE8WIsFn44aqiucL1gxA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U4ybxYzA; arc=none smtp.client-ip=209.85.167.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U4ybxYzA" Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3d55f198f1eso511341b6e.0 for ; Thu, 04 Jul 2024 14:37:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129046; x=1720733846; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=tRzuR0zVRtJexew852wMyGOvZW75YybIt7u3laQ/oyY=; b=U4ybxYzAE+Hn8TxesBE+mWLuMIdJgVmcGa2C6UdCL+R0toXNXstX5JmYYhYkpihc6k iQqD7t/95udPjCYknPHEvX6Fq+RGPDgC1/DLAuyawQtpHxiSlKGgmIHq4SdibYVRVdkC lNUrfKRCYjGFf+cH2zYD5awx3qgik9ZJgVUhpx/FyHZLMv14RsLPUj3Oov2yU03XEiP+ +h6LcQGzNGVAu5i+pZ9LODarRDtB3685lC1gIEIvYCqHtDTQvLxtI7YogyH25mKkpM2T WsYv+kWpork5DVPHvzHXKr4GihwUlR2vip9AoOk+/12mCxhtiA2+JQoanenjs7Qcdnzs Ndyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129046; x=1720733846; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tRzuR0zVRtJexew852wMyGOvZW75YybIt7u3laQ/oyY=; b=QltdzMESUkbl6pjuY9HkQ4cf35D5zMW3LJd+p7z/o6M3NVhYLSW6xmOjDPLU00Tl94 B5rcc2W2yNluG/bgjEG7HBmXHWi8TVZz4eLbbOY0XozFxFNFPVE+D1dLtp1N4oDxFLkR qYnUURvnleaQaEcsHuU0YlfXGOQMStnnDO//MO4NPtHJawQRNTdaq8Y1KZtbIvtRKmwt 1LLph1VZdjRi4/WJ8EBuYcXeqULsCz3h2d9uNxaHjFSPfiw7c6jzsITgD84ZA+MblZO7 11nxlIpjQ4d3zUQYh4DLWv8nNiwl+MVKd1/FZ+4HPnK/CAAKT/7W3wRy/9gnkO1A7KzC vIZg== X-Gm-Message-State: AOJu0YzOK+Ew6ijRdDOZ6VNC3N4pDlTlIiFLfin7hAmfvgxbHWSSVLym 2kJCyFc1eJaNU0fa1KsZm5iyGTM9zvIgd0eGPgGg5IFFVbwOBU4Z21MVew== X-Google-Smtp-Source: AGHT+IGLPmxmiXKpO5Lqv/tEXrBJFNWmKGOWGAlL6mxH6S5vBO0oDcbGZPkDXJs6QG5EKOi6f3Gj0A== X-Received: by 2002:a05:6808:3090:b0:3d5:6306:97af with SMTP id 5614622812f47-3d914eae92dmr3111866b6e.46.1720129045605; Thu, 04 Jul 2024 14:37:25 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70803ed31ebsm12724240b3a.110.2024.07.04.14.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:24 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 4/7] hwmon: (g762) Use bit operations Date: Thu, 4 Jul 2024 14:37:09 -0700 Message-Id: <20240704213712.2699553-5-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use bit operations to make the code easier to read and avoid duplication. While at it, improve column alignment in defines. No functional change. Signed-off-by: Guenter Roeck --- drivers/hwmon/g762.c | 130 +++++++++++++------------------------------ 1 file changed, 40 insertions(+), 90 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index 37d8a45610a3..59077e54d47e 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -27,6 +27,8 @@ * http://www.gmt.com.tw/product/datasheet/EDS-762_3.pdf */ +#include +#include #include #include #include @@ -59,35 +61,32 @@ enum g762_regs { }; /* Config register bits */ -#define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80 /* enable fan_fail signal */ -#define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40 /* enable fan_out_of_control */ -#define G762_REG_FAN_CMD1_OUT_MODE 0x20 /* out mode: PWM or DC */ -#define G762_REG_FAN_CMD1_FAN_MODE 0x10 /* fan mode: closed/open-loop */ -#define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08 /* clock divisor value */ -#define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04 -#define G762_REG_FAN_CMD1_PWM_POLARITY 0x02 /* PWM polarity */ -#define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01 /* pulse per fan revolution */ +#define G762_REG_FAN_CMD1_DET_FAN_FAIL BIT(7) /* enable fan_fail signal */ +#define G762_REG_FAN_CMD1_DET_FAN_OOC BIT(6) /* enable fan_out_of_control */ +#define G762_REG_FAN_CMD1_OUT_MODE BIT(5) /* out mode: PWM or DC */ +#define G762_REG_FAN_CMD1_FAN_MODE BIT(4) /* fan mode: closed/open-loop */ +#define G762_REG_FAN_CMD1_CLK_DIV_MASK GENMASK(3, 2) +#define G762_REG_FAN_CMD1_PWM_POLARITY BIT(1) /* PWM polarity */ +#define G762_REG_FAN_CMD1_PULSE_PER_REV BIT(0) /* pulse per fan revolution */ -#define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08 /* fan gear mode */ -#define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04 -#define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02 /* fan startup voltage */ -#define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01 +#define G762_REG_FAN_CMD2_GEAR_MASK GENMASK(3, 2) +#define G762_REG_FAN_CMD2_FAN_STARTV_MASK GENMASK(1, 0) /* fan startup voltage */ -#define G762_REG_FAN_STA_FAIL 0x02 /* fan fail */ -#define G762_REG_FAN_STA_OOC 0x01 /* fan out of control */ +#define G762_REG_FAN_STA_FAIL BIT(1) /* fan fail */ +#define G762_REG_FAN_STA_OOC BIT(0) /* fan out of control */ /* Config register values */ -#define G762_OUT_MODE_PWM 1 -#define G762_OUT_MODE_DC 0 +#define G762_OUT_MODE_PWM 1 +#define G762_OUT_MODE_DC 0 -#define G762_FAN_MODE_CLOSED_LOOP 2 -#define G762_FAN_MODE_OPEN_LOOP 1 +#define G762_FAN_MODE_CLOSED_LOOP 2 +#define G762_FAN_MODE_OPEN_LOOP 1 -#define G762_PWM_POLARITY_NEGATIVE 1 -#define G762_PWM_POLARITY_POSITIVE 0 +#define G762_PWM_POLARITY_NEGATIVE 1 +#define G762_PWM_POLARITY_POSITIVE 0 /* Register data is read (and cached) at most once per second. */ -#define G762_UPDATE_INTERVAL HZ +#define G762_UPDATE_INTERVAL HZ /* * Extract pulse count per fan revolution value (2 or 4) from given @@ -101,16 +100,14 @@ enum g762_regs { * register value. */ #define G762_CLKDIV_FROM_REG(reg) \ - (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \ - G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2)) + BIT(FIELD_GET(G762_REG_FAN_CMD1_CLK_DIV_MASK, reg)) /* - * Extract fan gear mode multiplier value (0, 2 or 4) from given + * Extract fan gear mode multiplier value (1, 2 or 4) from given * FAN_CMD2 register value. */ #define G762_GEARMULT_FROM_REG(reg) \ - (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \ - G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2)) + BIT(FIELD_GET(G762_REG_FAN_CMD2_GEAR_MASK, reg)) struct g762_data { struct i2c_client *client; @@ -303,32 +300,15 @@ static int do_set_fan_div(struct device *dev, unsigned long val) if (IS_ERR(data)) return PTR_ERR(data); + if (hweight_long(val) != 1 || val > 8) + return -EINVAL; + mutex_lock(&data->update_lock); - switch (val) { - case 1: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0; - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1; - break; - case 2: - data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1; - break; - case 4: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0; - data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; - break; - case 8: - data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0; - data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1; - break; - default: - ret = -EINVAL; - goto out; - } + data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_MASK; + data->fan_cmd1 |= FIELD_PREP(G762_REG_FAN_CMD1_CLK_DIV_MASK, __ffs(val)); ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, data->fan_cmd1); data->valid = false; - out: mutex_unlock(&data->update_lock); return ret; @@ -343,28 +323,15 @@ static int do_set_fan_gear_mode(struct device *dev, unsigned long val) if (IS_ERR(data)) return PTR_ERR(data); + if (val > 2) + return -EINVAL; + mutex_lock(&data->update_lock); - switch (val) { - case 0: - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1; - break; - case 1: - data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0; - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1; - break; - case 2: - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0; - data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1; - break; - default: - ret = -EINVAL; - goto out; - } + data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MASK; + data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_GEAR_MASK, val); ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, data->fan_cmd2); data->valid = false; - out: mutex_unlock(&data->update_lock); return ret; @@ -526,32 +493,15 @@ static int do_set_fan_startv(struct device *dev, unsigned long val) if (IS_ERR(data)) return PTR_ERR(data); + if (val > 3) + return -EINVAL; + mutex_lock(&data->update_lock); - switch (val) { - case 0: - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0; - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1; - break; - case 1: - data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1; - break; - case 2: - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0; - data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; - break; - case 3: - data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0; - data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1; - break; - default: - ret = -EINVAL; - goto out; - } + data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_MASK; + data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_FAN_STARTV_MASK, val); ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, data->fan_cmd2); data->valid = false; - out: mutex_unlock(&data->update_lock); return ret; @@ -722,7 +672,7 @@ static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da, if (IS_ERR(data)) return PTR_ERR(data); - return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1)); + return sprintf(buf, "%ld\n", G762_CLKDIV_FROM_REG(data->fan_cmd1)); } static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, @@ -753,7 +703,7 @@ static ssize_t fan1_pulses_show(struct device *dev, if (IS_ERR(data)) return PTR_ERR(data); - return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1)); + return sprintf(buf, "%ld\n", G762_PULSE_FROM_REG(data->fan_cmd1)); } static ssize_t fan1_pulses_store(struct device *dev, From patchwork Thu Jul 4 21:37:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724293 Received: from mail-oa1-f54.google.com (mail-oa1-f54.google.com [209.85.160.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19756143892 for ; Thu, 4 Jul 2024 21:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129050; cv=none; b=a+E44mHDFj1byq+XPLo5y/UUjeM64zO5KWlaintqNbySqYK7OEGZ3kAN+R06y0gqCbsoLr5nh2n1BIPADvmUFwH2NP/uD+BRQfgvt7szDBcKOJrl4vP1szA7MQqWJJgRA8YiJQpEhRyU1DpBGjSniV5mJ3C5b+NJrxaZsCWCysE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129050; c=relaxed/simple; bh=ri2VHkpN01jzXgoDotq/TFm7g2Ji5nHuOShxvHRpqMQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E1LTFbYMXPM5jhvPe47A2m5dL2NU+YNeNtUYMQ0h8OB+xApLVJvpLs7VGPRIhrNYw40EaMtEAAsVYH6tbD/eNEHR0ZJsHciTPfYa8AE6N/MIPHQae73SWE+wUpT3foxM2QFLtSHcsywnDIzRnjJEUgpg2GaowFazmXHM/k9GpbM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KeLoyzlM; arc=none smtp.client-ip=209.85.160.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KeLoyzlM" Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-25e3d8db819so150061fac.0 for ; Thu, 04 Jul 2024 14:37:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129047; x=1720733847; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=buU1sa3D8kYKB4RAOEfOuNvdlYsBO0jca9eI/hF/X6Y=; b=KeLoyzlMU7Eu96jiKkkbkLLyHYSKjppJHm0cvKIkxTp0HNn6GDRXqysV63NJwATEq0 KJ3GGnc8l6PYcpHa03lcUtapw3oRASeqZMKm6Wc190BmQVMlGz/sxTE2qi0ehtSjXv5c AXwsCXimVZTQZqCuYaqq+jJCBYgTdnvduA2QImTwWZyvkyHVtxcqlPF1y8xtuXN1qCZc hr3Hcq7NN5+Pe6iCCv2R2O99K6E0GUuT9A8bocWAq+dDsUkiTRarloVtz3LyyR7HSfCT 1me1DAnVMdw5AYWuz1dPkAaQlCsAdh1QaT6laIIVt0WtKAZvAqW2NzddWQtZirhmhSV6 xP5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129047; x=1720733847; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=buU1sa3D8kYKB4RAOEfOuNvdlYsBO0jca9eI/hF/X6Y=; b=iy2f4itgwn93U3Ia37oQhCKkJXpsMmmi9yGz4C38pl38Q6qJPVYKfqvH8t9mavBFLe FLx5Jer44Fr31Eicd7JXtb451j/+RqHRuvvOU5Qhv//w1wDBQr69Gvj4o0EH/YHcWWkn UOZ4lfZ39SxOzXQvSLXWPoo1GmPsKM1MV4noMTqIfvMLCGW862m90jvNTwG3+wqbxgsl Q3DtSBcz4861Q51gLHnnWsl4F5IFzCvNUCU4hzLLj1IYt8e7nnrSibD8Hx2+8FPHgja5 8xgf03Ipua9W9lSFkgLzUEtk/G/aaAgEqMd7WhXOrozYKS3P9v9Ctg2SuKycvdwZU30L pMtA== X-Gm-Message-State: AOJu0YyjENgR50j+1Cb7NWHcRYoe8/yQnVWg+14N/79g11UbETTgJpa4 rVK21GM2tnCT3zPnls90PKtKtcPROd8KXljiylyKv7oTgUZKwu6TtPFS5w== X-Google-Smtp-Source: AGHT+IE+ipZHt9ZDnEveZ5qB56P0vzA5faVN070cH3wOgcCDQmAP0zPa+WluSTbEjBDR/8B/0OCGog== X-Received: by 2002:a05:6871:24cd:b0:24f:cddc:ccfe with SMTP id 586e51a60fabf-25e2b59025fmr2437619fac.0.1720129047250; Thu, 04 Jul 2024 14:37:27 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-74a3594c5ffsm5188505a12.35.2024.07.04.14.37.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:26 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 5/7] hwmon: (g762) Make chip configuration devicetree independent Date: Thu, 4 Jul 2024 14:37:10 -0700 Message-Id: <20240704213712.2699553-6-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move chip configuration code to one place, and make it devicetree independent by using device property instead of devicetree functions to read configuration data. Signed-off-by: Guenter Roeck --- drivers/hwmon/g762.c | 187 ++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 110 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index 59077e54d47e..0ddaa0bd1075 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -44,13 +44,6 @@ #define DRVNAME "g762" -static const struct i2c_device_id g762_id[] = { - { "g762" }, - { "g763" }, - { } -}; -MODULE_DEVICE_TABLE(i2c, g762_id); - enum g762_regs { G762_REG_SET_CNT = 0x00, G762_REG_ACT_CNT = 0x01, @@ -315,7 +308,7 @@ static int do_set_fan_div(struct device *dev, unsigned long val) } /* Set fan gear mode. Accepts either 0, 1 or 2. */ -static int do_set_fan_gear_mode(struct device *dev, unsigned long val) +static int do_set_fan_gear_mode(struct device *dev, u32 val) { struct g762_data *data = g762_update_client(dev); int ret; @@ -507,96 +500,6 @@ static int do_set_fan_startv(struct device *dev, unsigned long val) return ret; } -/* - * Helper to import hardware characteristics from .dts file and push - * those to the chip. - */ - -#ifdef CONFIG_OF -static const struct of_device_id g762_dt_match[] = { - { .compatible = "gmt,g762" }, - { .compatible = "gmt,g763" }, - { }, -}; -MODULE_DEVICE_TABLE(of, g762_dt_match); - -/* - * Grab clock (a required property), enable it, get (fixed) clock frequency - * and store it. - */ -static int g762_of_clock_enable(struct device *dev) -{ - unsigned long clk_freq = 0; - struct clk *clk; - int ret; - - clk = devm_clk_get_enabled(dev, NULL); - if (IS_ERR(clk)) { - if (dev->of_node) - return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n"); - } else { - clk_freq = clk_get_rate(clk); - } - - ret = do_set_clk_freq(dev, clk_freq); - if (ret) - return dev_err_probe(dev, ret, "invalid clock freq %lu\n", clk_freq); - - return 0; -} - -static int g762_of_prop_import_one(struct i2c_client *client, - const char *pname, - int (*psetter)(struct device *dev, - unsigned long val)) -{ - int ret; - u32 pval; - - if (of_property_read_u32(client->dev.of_node, pname, &pval)) - return 0; - - dev_dbg(&client->dev, "found %s (%d)\n", pname, pval); - ret = (*psetter)(&client->dev, pval); - if (ret) - dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval); - - return ret; -} - -static int g762_of_prop_import(struct i2c_client *client) -{ - int ret; - - if (!client->dev.of_node) - return 0; - - ret = g762_of_prop_import_one(client, "fan_gear_mode", - do_set_fan_gear_mode); - if (ret) - return ret; - - ret = g762_of_prop_import_one(client, "pwm_polarity", - do_set_pwm_polarity); - if (ret) - return ret; - - return g762_of_prop_import_one(client, "fan_startv", - do_set_fan_startv); -} - -#else -static int g762_of_prop_import(struct i2c_client *client) -{ - return 0; -} - -static int g762_of_clock_enable(struct device *dev) -{ - return 0; -} -#endif - /* * sysfs attributes */ @@ -918,6 +821,65 @@ static inline int g762_fan_init(struct device *dev) data->fan_cmd1); } +/* + * Grab clock (a required property), enable it, get (fixed) clock frequency + * and store it. + */ +static int g762_clock_enable(struct device *dev) +{ + unsigned long clk_freq = 0; + struct clk *clk; + int ret; + + clk = devm_clk_get_enabled(dev, NULL); + if (IS_ERR(clk)) { + if (dev->of_node) + return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n"); + } else { + clk_freq = clk_get_rate(clk); + } + + ret = do_set_clk_freq(dev, clk_freq); + if (ret) + return dev_err_probe(dev, ret, "invalid clock freq %lu\n", clk_freq); + + return 0; +} + +static int g762_configure(struct device *dev) +{ + u32 property; + int ret; + + /* Enable fan failure detection and fan out of control protection */ + ret = g762_fan_init(dev); + if (ret) + return ret; + + ret = g762_clock_enable(dev); + if (ret) + return ret; + + if (!device_property_read_u32(dev, "fan_gear_mode", &property)) { + ret = do_set_fan_gear_mode(dev, property); + if (ret) + return ret; + } + + if (!device_property_read_u32(dev, "pwm_polarity", &property)) { + ret = do_set_pwm_polarity(dev, property); + if (ret) + return ret; + } + + if (!device_property_read_u32(dev, "fan_startv", &property)) { + ret = do_set_fan_startv(dev, property); + if (ret) + return ret; + } + return 0; +} + static int g762_probe(struct i2c_client *client) { struct device *dev = &client->dev; @@ -933,20 +895,11 @@ static int g762_probe(struct i2c_client *client) if (!data) return -ENOMEM; - i2c_set_clientdata(client, data); + dev_set_drvdata(dev, data); data->client = client; mutex_init(&data->update_lock); - /* Enable fan failure detection and fan out of control protection */ - ret = g762_fan_init(dev); - if (ret) - return ret; - - /* Get configuration via DT ... */ - ret = g762_of_clock_enable(dev); - if (ret) - return ret; - ret = g762_of_prop_import(client); + ret = g762_configure(dev); if (ret) return ret; @@ -955,10 +908,24 @@ static int g762_probe(struct i2c_client *client) return PTR_ERR_OR_ZERO(hwmon_dev); } +static const struct of_device_id g762_dt_match[] = { + { .compatible = "gmt,g762" }, + { .compatible = "gmt,g763" }, + { }, +}; +MODULE_DEVICE_TABLE(of, g762_dt_match); + +static const struct i2c_device_id g762_id[] = { + { "g762" }, + { "g763" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, g762_id); + static struct i2c_driver g762_driver = { .driver = { .name = DRVNAME, - .of_match_table = of_match_ptr(g762_dt_match), + .of_match_table = g762_dt_match, }, .probe = g762_probe, .id_table = g762_id, From patchwork Thu Jul 4 21:37:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724294 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3DEB143892 for ; Thu, 4 Jul 2024 21:37:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129053; cv=none; b=HszFG9aUrBaF/h2vJqxQ+kMj94x2kn7YYZykL28YAvjywyc6dTe6hww0ul1fiPD81O+FsS4Xc93YCLaOgl1k8odafwgVMIecEbPddjVHQSjOhnEj61NIgdfHkxxwULDZdsOuYWPi+pySJ7gqTSMVniRbo8YUtZHjyCNLZJlX7LU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129053; c=relaxed/simple; bh=bL6En/O/0JQVpkxdQxIohzKYBdIwJTRNIUeG7yFtKRg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hQ+U7Apjd8ACQyvdfDulsSgcURNtiQDSQYPUAnAoJQR/AHAg2JyRh7UE6bip0+ArSfT7E8C/2j8ZK5sfS01rPR2pER+6dDf54st3KahVTgz75BbwIQf066mwmVxhQiHT9mD0CYTogi+tpmwt8AkHibVc3EmU5oRu+/sqj75owIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CQ6C3nYY; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CQ6C3nYY" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-70b07bdbfbcso341866b3a.0 for ; Thu, 04 Jul 2024 14:37:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129049; x=1720733849; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=S7DuJTXSlON7TohY9mwLqeoreQHAYCXkdnK3Jl9kcBM=; b=CQ6C3nYYbrEESmJxajwLynOY5zFcpNlKELlBSUQzBp8PtKwrP3N3WRVD4UurpoNMdR tB4VZaOFoIBzm4gHpKzjOfHvAG62iAxdqAd3lcR8P4FUEYI6BbUFJki0ms5LASoF7nsw Br6ByNtWVz28zZKs8FoXhbIQV9CtndHPoyNZW3UzdfU8oXPyXjd00Z4Jpv9W1gQzr6mp rUaj/cOwjfg5V16w4AfREDWi1Q6yWVfWNaF7XJoukRTw9XU60wREI6huJeQBcVx3Pqp5 Pd1Z9kHwqdE1a+ASDBjALPrSUMkkHoNZcopnWFuwmiJYbi65k1dDEJR/X0zBvys5JkSP /VAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129049; x=1720733849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=S7DuJTXSlON7TohY9mwLqeoreQHAYCXkdnK3Jl9kcBM=; b=qDeaRZKp9iaD1HFOU2BdG7yOukSDrnK6AptY8T9I3wdwVskkIWHrtxp1IHB5mjU+l3 NIFpXqGXEFEXWmEZZf4FtyZmVNHfqK+1aB9UVSh6bnDFpmYliOuLQiMo1W1IS3wGFirv 0YswkQAExXGc99F6fv7cMhocLtyDlHGa0KddWUnhtEZDVVnWKbwVeQx7fy500/xQPssy 4EA58HO3pAxDaNPyvkAoInL1BJlVZK5luj/CYiLPCV0g4Y2NFma4evZdrT1qaJwaLWXu DWe+dliqnbsVZANtpzvGMNQkl3Jrq+VJWJg3l5YTgDSBPXZDz0taiwUAjxb/nJAe8KZE lOcg== X-Gm-Message-State: AOJu0Yyv1ndpCW9OgdAyIW6x75fLOivfWwZQHH/PUzXogsmF0JVm4v/G WjpnZVCwKfvxTq7SMD4r9JJEr/gBWyhLRmFfZBkOhy96N+/5/kniMk2OSg== X-Google-Smtp-Source: AGHT+IHDfSOwsXepk/+u5w9dGLhstEP76VZii2mbXBhNPc0XTGv12MeiDZMYPnd/mM7KTBhglkD1eg== X-Received: by 2002:a05:6a21:995:b0:1b5:cc1f:38d4 with SMTP id adf61e73a8af0-1c0cd1a5e58mr2715740637.17.1720129049082; Thu, 04 Jul 2024 14:37:29 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7080256afaasm12715970b3a.81.2024.07.04.14.37.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:28 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 6/7] hwmon: (g762) Convert to use regmap Date: Thu, 4 Jul 2024 14:37:11 -0700 Message-Id: <20240704213712.2699553-7-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use regmap for caching and regmap operations for register bit manipulations to simplify the code. No functional change intended. Signed-off-by: Guenter Roeck --- Documentation/hwmon/g762.rst | 2 - drivers/hwmon/g762.c | 523 +++++++++++++---------------------- 2 files changed, 195 insertions(+), 330 deletions(-) diff --git a/Documentation/hwmon/g762.rst b/Documentation/hwmon/g762.rst index 3dc5294b2181..0c170596a1ac 100644 --- a/Documentation/hwmon/g762.rst +++ b/Documentation/hwmon/g762.rst @@ -68,5 +68,3 @@ is performed to match that target value. The fan speed value is computed based on the parameters associated with the physical characteristics of the system: a reference clock source frequency, a number of pulses per fan revolution, etc. - -Note that the driver will update its values at most once per second. diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index 0ddaa0bd1075..5f8fd723a694 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -36,11 +36,11 @@ #include #include #include -#include #include #include #include #include +#include #define DRVNAME "g762" @@ -68,19 +68,6 @@ enum g762_regs { #define G762_REG_FAN_STA_FAIL BIT(1) /* fan fail */ #define G762_REG_FAN_STA_OOC BIT(0) /* fan out of control */ -/* Config register values */ -#define G762_OUT_MODE_PWM 1 -#define G762_OUT_MODE_DC 0 - -#define G762_FAN_MODE_CLOSED_LOOP 2 -#define G762_FAN_MODE_OPEN_LOOP 1 - -#define G762_PWM_POLARITY_NEGATIVE 1 -#define G762_PWM_POLARITY_POSITIVE 0 - -/* Register data is read (and cached) at most once per second. */ -#define G762_UPDATE_INTERVAL HZ - /* * Extract pulse count per fan revolution value (2 or 4) from given * FAN_CMD1 register value. @@ -103,48 +90,13 @@ enum g762_regs { BIT(FIELD_GET(G762_REG_FAN_CMD2_GEAR_MASK, reg)) struct g762_data { - struct i2c_client *client; + struct regmap *regmap; /* update mutex */ struct mutex update_lock; /* board specific parameters. */ u32 clk_freq; - - /* g762 register cache */ - bool valid; - unsigned long last_updated; /* in jiffies */ - - u8 set_cnt; /* controls fan rotation speed in closed-loop mode */ - u8 act_cnt; /* provides access to current fan RPM value */ - u8 fan_sta; /* bit 0: set when actual fan speed is more than - * 25% outside requested fan speed - * bit 1: set when no transition occurs on fan - * pin for 0.7s - */ - u8 set_out; /* controls fan rotation speed in open-loop mode */ - u8 fan_cmd1; /* 0: FG_PLS_ID0 FG pulses count per revolution - * 0: 2 counts per revolution - * 1: 4 counts per revolution - * 1: PWM_POLARITY 1: negative_duty - * 0: positive_duty - * 2,3: [FG_CLOCK_ID0, FG_CLK_ID1] - * 00: Divide fan clock by 1 - * 01: Divide fan clock by 2 - * 10: Divide fan clock by 4 - * 11: Divide fan clock by 8 - * 4: FAN_MODE 1:closed-loop, 0:open-loop - * 5: OUT_MODE 1:PWM, 0:DC - * 6: DET_FAN_OOC enable "fan ooc" status - * 7: DET_FAN_FAIL enable "fan fail" status - */ - u8 fan_cmd2; /* 0,1: FAN_STARTV 0,1,2,3 -> 0,32,64,96 dac_code - * 2,3: FG_GEAR_MODE - * 00: multiplier = 1 - * 01: multiplier = 2 - * 10: multiplier = 4 - * 4: Mask ALERT# (g763 only) - */ }; /* @@ -179,59 +131,6 @@ static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p, return DIV_ROUND_CLOSEST(f1, rpm * f2); } -/* helper to grab and cache data, at most one time per second */ -static struct g762_data *g762_update_client(struct device *dev) -{ - struct g762_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; - int ret = 0; - - mutex_lock(&data->update_lock); - if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) && - likely(data->valid)) - goto out; - - ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT); - if (ret < 0) - goto out; - data->set_cnt = ret; - - ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT); - if (ret < 0) - goto out; - data->act_cnt = ret; - - ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA); - if (ret < 0) - goto out; - data->fan_sta = ret; - - ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT); - if (ret < 0) - goto out; - data->set_out = ret; - - ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1); - if (ret < 0) - goto out; - data->fan_cmd1 = ret; - - ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2); - if (ret < 0) - goto out; - data->fan_cmd2 = ret; - - data->last_updated = jiffies; - data->valid = true; - out: - mutex_unlock(&data->update_lock); - - if (ret < 0) /* upon error, encode it in return value */ - data = ERR_PTR(ret); - - return data; -} - /* helpers for writing hardware parameters */ /* @@ -257,125 +156,72 @@ static int do_set_clk_freq(struct device *dev, unsigned long val) /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ static int do_set_pwm_mode(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); - int ret; + struct g762_data *data = dev_get_drvdata(dev); - if (IS_ERR(data)) - return PTR_ERR(data); + if (val && val != 1) + return -EINVAL; - mutex_lock(&data->update_lock); - switch (val) { - case G762_OUT_MODE_PWM: - data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE; - break; - case G762_OUT_MODE_DC: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE; - break; - default: - ret = -EINVAL; - goto out; - } - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); - data->valid = false; - out: - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, + G762_REG_FAN_CMD1_OUT_MODE, + val ? G762_REG_FAN_CMD1_OUT_MODE : 0); } /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ static int do_set_fan_div(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); - int ret; - - if (IS_ERR(data)) - return PTR_ERR(data); + struct g762_data *data = dev_get_drvdata(dev); if (hweight_long(val) != 1 || val > 8) return -EINVAL; - mutex_lock(&data->update_lock); - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_MASK; - data->fan_cmd1 |= FIELD_PREP(G762_REG_FAN_CMD1_CLK_DIV_MASK, __ffs(val)); - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); - data->valid = false; - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, + G762_REG_FAN_CMD1_CLK_DIV_MASK, + FIELD_PREP(G762_REG_FAN_CMD1_CLK_DIV_MASK, __ffs(val))); } /* Set fan gear mode. Accepts either 0, 1 or 2. */ static int do_set_fan_gear_mode(struct device *dev, u32 val) { - struct g762_data *data = g762_update_client(dev); - int ret; - - if (IS_ERR(data)) - return PTR_ERR(data); + struct g762_data *data = dev_get_drvdata(dev); if (val > 2) return -EINVAL; - mutex_lock(&data->update_lock); - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MASK; - data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_GEAR_MASK, val); - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, - data->fan_cmd2); - data->valid = false; - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD2, + G762_REG_FAN_CMD2_GEAR_MASK, + FIELD_PREP(G762_REG_FAN_CMD2_GEAR_MASK, val)); } /* Set number of fan pulses per revolution. Accepts either 2 or 4. */ static int do_set_fan_pulses(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); - int ret; + struct g762_data *data = dev_get_drvdata(dev); - if (IS_ERR(data)) - return PTR_ERR(data); + if (val != 2 && val != 4) + return -EINVAL; - mutex_lock(&data->update_lock); - switch (val) { - case 2: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV; - break; - case 4: - data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV; - break; - default: - ret = -EINVAL; - goto out; - } - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); - data->valid = false; - out: - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, + G762_REG_FAN_CMD1_PULSE_PER_REV, + val == 4 ? G762_REG_FAN_CMD1_PULSE_PER_REV : 0); } /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ static int do_set_pwm_enable(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + if (val != 1 && val != 2) + return -EINVAL; mutex_lock(&data->update_lock); - switch (val) { - case G762_FAN_MODE_CLOSED_LOOP: - data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE; - break; - case G762_FAN_MODE_OPEN_LOOP: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE; + if (val == 1) { + u32 regval; + + ret = regmap_read(regmap, G762_REG_SET_CNT, ®val); + if (ret) + goto unlock; /* * BUG FIX: if SET_CNT register value is 255 then, for some * unknown reason, fan will not rotate as expected, no matter @@ -383,52 +229,26 @@ static int do_set_pwm_enable(struct device *dev, unsigned long val) * only in PWM mode). To workaround this bug, we give SET_CNT * value of 254 if it is 255 when switching to open-loop. */ - if (data->set_cnt == 0xff) - i2c_smbus_write_byte_data(data->client, - G762_REG_SET_CNT, 254); - break; - default: - ret = -EINVAL; - goto out; + if (regval == 0xff) + regmap_write(regmap, G762_REG_SET_CNT, 254); } - - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); - data->valid = false; - out: + ret = regmap_update_bits(regmap, G762_REG_FAN_CMD1, G762_REG_FAN_CMD1_FAN_MODE, + val == 2 ? G762_REG_FAN_CMD1_FAN_MODE : 0); +unlock: mutex_unlock(&data->update_lock); - return ret; } /* Set PWM polarity. Accepts either 0 (positive duty) or 1 (negative duty) */ static int do_set_pwm_polarity(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); - int ret; + struct g762_data *data = dev_get_drvdata(dev); - if (IS_ERR(data)) - return PTR_ERR(data); + if (val && val != 1) + return -EINVAL; - mutex_lock(&data->update_lock); - switch (val) { - case G762_PWM_POLARITY_POSITIVE: - data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY; - break; - case G762_PWM_POLARITY_NEGATIVE: - data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY; - break; - default: - ret = -EINVAL; - goto out; - } - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); - data->valid = false; - out: - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, G762_REG_FAN_CMD1_PWM_POLARITY, + val ? G762_REG_FAN_CMD1_PWM_POLARITY : 0); } /* @@ -438,18 +258,11 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val) static int do_set_pwm(struct device *dev, unsigned long val) { struct g762_data *data = dev_get_drvdata(dev); - struct i2c_client *client = data->client; - int ret; if (val > 255) return -EINVAL; - mutex_lock(&data->update_lock); - ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val); - data->valid = false; - mutex_unlock(&data->update_lock); - - return ret; + return regmap_write(data->regmap, G762_REG_SET_OUT, val); } /* @@ -458,52 +271,81 @@ static int do_set_pwm(struct device *dev, unsigned long val) */ static int do_set_fan_target(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; + u32 cmd1, cmd2; + u8 set_cnt; int ret; - if (IS_ERR(data)) - return PTR_ERR(data); - mutex_lock(&data->update_lock); - data->set_cnt = cnt_from_rpm(val, data->clk_freq, - G762_PULSE_FROM_REG(data->fan_cmd1), - G762_CLKDIV_FROM_REG(data->fan_cmd1), - G762_GEARMULT_FROM_REG(data->fan_cmd2)); - ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT, - data->set_cnt); - data->valid = false; - mutex_unlock(&data->update_lock); + ret = regmap_read(regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret) + goto unlock; + ret = regmap_read(regmap, G762_REG_FAN_CMD2, &cmd2); + if (ret) + goto unlock; + + set_cnt = cnt_from_rpm(val, data->clk_freq, + G762_PULSE_FROM_REG(cmd1), + G762_CLKDIV_FROM_REG(cmd1), + G762_GEARMULT_FROM_REG(cmd2)); + ret = regmap_write(regmap, G762_REG_SET_CNT, set_cnt); +unlock: + mutex_unlock(&data->update_lock); return ret; } /* Set fan startup voltage. Accepted values are either 0, 1, 2 or 3. */ static int do_set_fan_startv(struct device *dev, unsigned long val) { - struct g762_data *data = g762_update_client(dev); - int ret; - - if (IS_ERR(data)) - return PTR_ERR(data); + struct g762_data *data = dev_get_drvdata(dev); if (val > 3) return -EINVAL; - mutex_lock(&data->update_lock); - data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_MASK; - data->fan_cmd2 |= FIELD_PREP(G762_REG_FAN_CMD2_FAN_STARTV_MASK, val); - ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2, - data->fan_cmd2); - data->valid = false; - mutex_unlock(&data->update_lock); - - return ret; + return regmap_update_bits(data->regmap, G762_REG_FAN_CMD2, + G762_REG_FAN_CMD2_FAN_STARTV_MASK, + FIELD_PREP(G762_REG_FAN_CMD2_FAN_STARTV_MASK, val)); } /* * sysfs attributes */ +static int get_fan_rpm_locked(struct g762_data *data, int reg) +{ + struct regmap *regmap = data->regmap; + u32 cmd1, cmd2, count; + int ret; + + ret = regmap_read(regmap, reg, &count); + if (ret) + return ret; + ret = regmap_read(regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret) + return ret; + ret = regmap_read(regmap, G762_REG_FAN_CMD2, &cmd2); + if (ret) + return ret; + + return rpm_from_cnt(count, data->clk_freq, + G762_PULSE_FROM_REG(cmd1), + G762_CLKDIV_FROM_REG(cmd1), + G762_GEARMULT_FROM_REG(cmd2)); +} + +static int get_fan_rpm(struct g762_data *data, int reg) +{ + int ret; + + mutex_lock(&data->update_lock); + ret = get_fan_rpm_locked(data, reg); + mutex_unlock(&data->update_lock); + + return ret; +} + /* * Read function for fan1_input sysfs file. Return current fan RPM value, or * 0 if fan is out of control. @@ -511,23 +353,24 @@ static int do_set_fan_startv(struct device *dev, unsigned long val) static ssize_t fan1_input_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); - unsigned int rpm = 0; - - if (IS_ERR(data)) - return PTR_ERR(data); + struct g762_data *data = dev_get_drvdata(dev); + u32 status; + int ret; mutex_lock(&data->update_lock); + ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); /* reverse logic: fan out of control reporting is enabled low */ - if (data->fan_sta & G762_REG_FAN_STA_OOC) { - rpm = rpm_from_cnt(data->act_cnt, data->clk_freq, - G762_PULSE_FROM_REG(data->fan_cmd1), - G762_CLKDIV_FROM_REG(data->fan_cmd1), - G762_GEARMULT_FROM_REG(data->fan_cmd2)); - } - mutex_unlock(&data->update_lock); + if (ret || !(status & G762_REG_FAN_STA_OOC)) + goto unlock; - return sprintf(buf, "%u\n", rpm); + ret = get_fan_rpm_locked(data, G762_REG_ACT_CNT); + if (ret < 0) + goto unlock; + + ret = sprintf(buf, "%u\n", ret); +unlock: + mutex_unlock(&data->update_lock); + return ret; } /* @@ -537,13 +380,15 @@ static ssize_t fan1_input_show(struct device *dev, static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 cmd1; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret < 0) + return ret; - return sprintf(buf, "%d\n", - !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE)); + return sprintf(buf, "%d\n", !!(cmd1 & G762_REG_FAN_CMD1_OUT_MODE)); } static ssize_t pwm1_mode_store(struct device *dev, @@ -570,12 +415,15 @@ static ssize_t pwm1_mode_store(struct device *dev, static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 cmd1; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret < 0) + return ret; - return sprintf(buf, "%ld\n", G762_CLKDIV_FROM_REG(data->fan_cmd1)); + return sprintf(buf, "%ld\n", G762_CLKDIV_FROM_REG(cmd1)); } static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, @@ -601,12 +449,15 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, static ssize_t fan1_pulses_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 cmd1; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret < 0) + return ret; - return sprintf(buf, "%ld\n", G762_PULSE_FROM_REG(data->fan_cmd1)); + return sprintf(buf, "%ld\n", G762_PULSE_FROM_REG(cmd1)); } static ssize_t fan1_pulses_store(struct device *dev, @@ -643,13 +494,16 @@ static ssize_t fan1_pulses_store(struct device *dev, static ssize_t pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 cmd1; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); + if (ret < 0) + return ret; return sprintf(buf, "%d\n", - (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1); + !!(cmd1 & G762_REG_FAN_CMD1_FAN_MODE) + 1); } static ssize_t pwm1_enable_store(struct device *dev, @@ -677,12 +531,15 @@ static ssize_t pwm1_enable_store(struct device *dev, static ssize_t pwm1_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + int ret; + u32 pwm; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_SET_OUT, &pwm); + if (ret < 0) + return ret; - return sprintf(buf, "%d\n", data->set_out); + return sprintf(buf, "%u\n", pwm); } static ssize_t pwm1_store(struct device *dev, struct device_attribute *da, @@ -715,20 +572,14 @@ static ssize_t pwm1_store(struct device *dev, struct device_attribute *da, static ssize_t fan1_target_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); - unsigned int rpm; + struct g762_data *data = dev_get_drvdata(dev); + int rpm; - if (IS_ERR(data)) - return PTR_ERR(data); + rpm = get_fan_rpm(data, G762_REG_SET_CNT); + if (rpm < 0) + return rpm; - mutex_lock(&data->update_lock); - rpm = rpm_from_cnt(data->set_cnt, data->clk_freq, - G762_PULSE_FROM_REG(data->fan_cmd1), - G762_CLKDIV_FROM_REG(data->fan_cmd1), - G762_GEARMULT_FROM_REG(data->fan_cmd2)); - mutex_unlock(&data->update_lock); - - return sprintf(buf, "%u\n", rpm); + return sprintf(buf, "%d\n", rpm); } static ssize_t fan1_target_store(struct device *dev, @@ -752,12 +603,15 @@ static ssize_t fan1_target_store(struct device *dev, static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 status; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); + if (ret < 0) + return ret; - return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL)); + return sprintf(buf, "%u\n", !!(status & G762_REG_FAN_STA_FAIL)); } /* @@ -767,12 +621,15 @@ static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da, static ssize_t fan1_alarm_show(struct device *dev, struct device_attribute *da, char *buf) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); + u32 status; + int ret; - if (IS_ERR(data)) - return PTR_ERR(data); + ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); + if (ret < 0) + return ret; - return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC)); + return sprintf(buf, "%u\n", !(status & G762_REG_FAN_STA_OOC)); } static DEVICE_ATTR_RW(pwm1); @@ -802,23 +659,14 @@ static struct attribute *g762_attrs[] = { ATTRIBUTE_GROUPS(g762); /* - * Enable both fan failure detection and fan out of control protection. The - * function does not protect change/access to data structure; it must thus - * only be called during initialization. + * Enable both fan failure detection and fan out of control protection. */ static inline int g762_fan_init(struct device *dev) { - struct g762_data *data = g762_update_client(dev); + struct g762_data *data = dev_get_drvdata(dev); - if (IS_ERR(data)) - return PTR_ERR(data); - - data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL; - data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC; - data->valid = false; - - return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1, - data->fan_cmd1); + return regmap_set_bits(data->regmap, G762_REG_FAN_CMD1, + G762_REG_FAN_CMD1_DET_FAN_FAIL | G762_REG_FAN_CMD1_DET_FAN_OOC); } /* @@ -880,6 +728,26 @@ static int g762_configure(struct device *dev) return 0; } +static bool g762_writeable_reg(struct device *dev, unsigned int reg) +{ + return reg == G762_REG_SET_CNT || reg == G762_REG_SET_OUT || + reg == G762_REG_FAN_CMD1 || reg == G762_REG_FAN_CMD2; +} + +static bool g762_volatile_reg(struct device *dev, unsigned int reg) +{ + return reg == G762_REG_ACT_CNT || reg == G762_REG_FAN_STA; +} + +static const struct regmap_config g762_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = G762_REG_FAN_CMD2, + .writeable_reg = g762_writeable_reg, + .volatile_reg = g762_volatile_reg, + .cache_type = REGCACHE_MAPLE, +}; + static int g762_probe(struct i2c_client *client) { struct device *dev = &client->dev; @@ -887,16 +755,15 @@ static int g762_probe(struct i2c_client *client) struct g762_data *data; int ret; - if (!i2c_check_functionality(client->adapter, - I2C_FUNC_SMBUS_BYTE_DATA)) - return -ENODEV; - data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL); if (!data) return -ENOMEM; + data->regmap = devm_regmap_init_i2c(client, &g762_regmap_config); + if (IS_ERR(data->regmap)) + return PTR_ERR(data->regmap); + dev_set_drvdata(dev, data); - data->client = client; mutex_init(&data->update_lock); ret = g762_configure(dev); From patchwork Thu Jul 4 21:37:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 13724295 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA8F914389F for ; Thu, 4 Jul 2024 21:37:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129054; cv=none; b=NsBFjTAP8ZmcaXnp8OmgclThySA0GVdzWqZzM6z2Y6tzzsdPeP3l+iKjkyApJQy9/a36rzhFKCXw9ccti9ASPMG6DctMiCiiFUPKCZVUsywo2vUkh+9/90ZaeSVZEcLex54LMEXj8nx+xO9RBUFyCSgQHdzJdvPbUuDqyrElAH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720129054; c=relaxed/simple; bh=SE8x7ElzEj02p6L5ldM/1HhpMRKeg4zA2WFskxz/GJU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GZNCaBp38cJsicfEzwINdknaRphtPx7dae7k/aJ/5EawV2iuctSpWzkpv+1tCDHEk37QMxZeGFxfuCT/QFBxAN0GxuQqFYPAm2QxuwW8CjzY4PuJB6RD/VzXL4DyejzgKSJGolMMemSOvLK91BlhvcFBAe9neaiInWOEaVk5vFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MtLVi3di; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=roeck-us.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MtLVi3di" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7067108f2cdso733164b3a.1 for ; Thu, 04 Jul 2024 14:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720129051; x=1720733851; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=UUjCqcLJAoHtIkTsAxEvDiD5CBWAL7JIlIPUnZDZHJE=; b=MtLVi3diATrvdeOtgQuIo5KLOxC1exzUR1DsLJ7cgCqrRXcsiicrkWK5KT4yy5kCwk QJgWlShRdVcktiPgUIitTNo3kM+LdLl/x7nhSrzcBgYBzWcSmv83DSAAJ/fz4/0MSCVb z/IcaMlZMrPP9ur0UgHRjjNB/jWQIlC7a2DU/awIL5pgKa+7uA2ntPmJoJR45G8ilBPR nFZKIV6VX8yeoUggW3NNziivq6W7O08SESL0IDmZ2zeTq8cn4YhGuc3KxOZFJwkXY6ZI OjyoujgW8wj/+GO5t+3wIauAZSokJ/YgV+7BzAwJa0NsQ+lnk9ZFaKeGe25Y5rIvbDXu qHdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720129051; x=1720733851; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=UUjCqcLJAoHtIkTsAxEvDiD5CBWAL7JIlIPUnZDZHJE=; b=VHwBVZG4m4DSMwd0j5K8N9gnxkwvLrYjSF6sHjAf0upvsXGTRJ7yXss/bSxR7qfpQs AMzUtoTq5UtgOZrLGXrOsd6QD6EQTo3TXlyTJgqw7Lm4TWLV47JjbvW9nhIl50G3S21D fivJcvxqBineeTIOdodzNZNM2Wv5RSKGLQ/V/uLspWZZQwim7Y7H3jUILFTXpLfNhQmH o3uPkvG4Af0uHGyZ1wSEMfgaRW+ijbpc8vtgsV6qquBjSdTwQ+wOHJEbyUiEAi1HvC8V VjQPPWQQbxXroif6kAP46MOOXt1eqfwFfUxULPBQwI5eoJ16ruB+ikYiSkd3tr2it01A AkHQ== X-Gm-Message-State: AOJu0Yxr/VQk++BOJsyLIxBGSOLgfKIx9c/H5yc2eWHw/82wVEgFmLJk 87aXTu3G6MGPOdLckNrM5ekJCPsTYIld+nKMw07AJBJ9nZM3eUQHRwwG8A== X-Google-Smtp-Source: AGHT+IFtIAbg3DRUNbuzAMypARxHOyNwUFi5VL6v4KjrLV/mkxVFQXOJzLUblbrmyByWWPOPlrEKcg== X-Received: by 2002:a05:6a00:c8c:b0:705:c0a1:61d8 with SMTP id d2e1a72fcca58-70b00915013mr2800303b3a.4.1720129050979; Thu, 04 Jul 2024 14:37:30 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70801007c62sm12738188b3a.0.2024.07.04.14.37.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jul 2024 14:37:30 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: Hardware Monitoring Cc: Guenter Roeck Subject: [PATCH 7/7] hwmon: (g762) Convert to with_info API Date: Thu, 4 Jul 2024 14:37:12 -0700 Message-Id: <20240704213712.2699553-8-linux@roeck-us.net> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net> References: <20240704213712.2699553-1-linux@roeck-us.net> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Convert to with_info API to simplify the code and make it easier to maintain. The conversion reduces code size by about 25%. No functional change intended. Signed-off-by: Guenter Roeck --- drivers/hwmon/g762.c | 473 ++++++++++++++++--------------------------- 1 file changed, 180 insertions(+), 293 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index 5f8fd723a694..e513cd47fade 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -154,11 +153,11 @@ static int do_set_clk_freq(struct device *dev, unsigned long val) } /* Set pwm mode. Accepts either 0 (PWM mode) or 1 (DC mode) */ -static int do_set_pwm_mode(struct device *dev, unsigned long val) +static int do_set_pwm_mode(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); - if (val && val != 1) + if (val < 0 || val > 1) return -EINVAL; return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, @@ -167,11 +166,11 @@ static int do_set_pwm_mode(struct device *dev, unsigned long val) } /* Set fan clock divisor. Accepts either 1, 2, 4 or 8. */ -static int do_set_fan_div(struct device *dev, unsigned long val) +static int do_set_fan_div(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); - if (hweight_long(val) != 1 || val > 8) + if (val < 0 || hweight_long(val) != 1 || val > 8) return -EINVAL; return regmap_update_bits(data->regmap, G762_REG_FAN_CMD1, @@ -193,7 +192,7 @@ static int do_set_fan_gear_mode(struct device *dev, u32 val) } /* Set number of fan pulses per revolution. Accepts either 2 or 4. */ -static int do_set_fan_pulses(struct device *dev, unsigned long val) +static int do_set_fan_pulses(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); @@ -206,7 +205,7 @@ static int do_set_fan_pulses(struct device *dev, unsigned long val) } /* Set fan mode. Accepts either 1 (open-loop) or 2 (closed-loop). */ -static int do_set_pwm_enable(struct device *dev, unsigned long val) +static int do_set_pwm_enable(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); struct regmap *regmap = data->regmap; @@ -255,11 +254,11 @@ static int do_set_pwm_polarity(struct device *dev, unsigned long val) * Set pwm value. Accepts values between 0 (stops the fan) and * 255 (full speed). This only makes sense in open-loop mode. */ -static int do_set_pwm(struct device *dev, unsigned long val) +static int do_set_pwm(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); - if (val > 255) + if (val < 0 || val > 255) return -EINVAL; return regmap_write(data->regmap, G762_REG_SET_OUT, val); @@ -269,7 +268,7 @@ static int do_set_pwm(struct device *dev, unsigned long val) * Set fan RPM value. Can be called both in closed and open-loop mode * but effect will only be seen after closed-loop mode is configured. */ -static int do_set_fan_target(struct device *dev, unsigned long val) +static int do_set_fan_target(struct device *dev, long val) { struct g762_data *data = dev_get_drvdata(dev); struct regmap *regmap = data->regmap; @@ -277,6 +276,9 @@ static int do_set_fan_target(struct device *dev, unsigned long val) u8 set_cnt; int ret; + if (val < 0) + return -EINVAL; + mutex_lock(&data->update_lock); ret = regmap_read(regmap, G762_REG_FAN_CMD1, &cmd1); @@ -346,317 +348,201 @@ static int get_fan_rpm(struct g762_data *data, int reg) return ret; } -/* - * Read function for fan1_input sysfs file. Return current fan RPM value, or - * 0 if fan is out of control. - */ -static ssize_t fan1_input_show(struct device *dev, - struct device_attribute *da, char *buf) +static int g762_fan_read(struct device *dev, u32 attr, long *val) { struct g762_data *data = dev_get_drvdata(dev); - u32 status; + u32 regval; int ret; - mutex_lock(&data->update_lock); - ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); - /* reverse logic: fan out of control reporting is enabled low */ - if (ret || !(status & G762_REG_FAN_STA_OOC)) - goto unlock; - - ret = get_fan_rpm_locked(data, G762_REG_ACT_CNT); - if (ret < 0) - goto unlock; - - ret = sprintf(buf, "%u\n", ret); -unlock: - mutex_unlock(&data->update_lock); - return ret; + switch (attr) { + case hwmon_fan_target: + ret = get_fan_rpm(data, G762_REG_SET_CNT); + if (ret < 0) + return ret; + *val = ret; + break; + case hwmon_fan_div: + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, ®val); + if (ret < 0) + return ret; + *val = G762_CLKDIV_FROM_REG(regval); + break; + case hwmon_fan_pulses: + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, ®val); + if (ret < 0) + return ret; + *val = G762_PULSE_FROM_REG(regval); + break; + case hwmon_fan_input: + mutex_lock(&data->update_lock); + ret = regmap_read(data->regmap, G762_REG_FAN_STA, ®val); + if (ret || !(regval & G762_REG_FAN_STA_OOC)) { + mutex_unlock(&data->update_lock); + return ret ? : -ENODATA; + } + ret = get_fan_rpm_locked(data, G762_REG_ACT_CNT); + mutex_unlock(&data->update_lock); + if (ret < 0) + return ret; + *val = ret; + break; + case hwmon_fan_alarm: + ret = regmap_read(data->regmap, G762_REG_FAN_STA, ®val); + if (ret < 0) + return ret; + /* G762_REG_FAN_STA_OOC is active low */ + *val = !(regval & G762_REG_FAN_STA_OOC); + break; + case hwmon_fan_fault: + ret = regmap_read(data->regmap, G762_REG_FAN_STA, ®val); + if (ret < 0) + return ret; + *val = !!(regval & G762_REG_FAN_STA_FAIL); + break; + default: + return -EOPNOTSUPP; + } + return 0; } -/* - * Read and write functions for pwm1_mode sysfs file. Get and set fan speed - * control mode i.e. PWM (1) or DC (0). - */ -static ssize_t pwm1_mode_show(struct device *dev, struct device_attribute *da, - char *buf) +static int g762_pwm_read(struct device *dev, u32 attr, long *val) { struct g762_data *data = dev_get_drvdata(dev); - u32 cmd1; + u32 regval; int ret; - ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); - if (ret < 0) - return ret; - - return sprintf(buf, "%d\n", !!(cmd1 & G762_REG_FAN_CMD1_OUT_MODE)); + switch (attr) { + case hwmon_pwm_input: + ret = regmap_read(data->regmap, G762_REG_SET_OUT, ®val); + if (ret < 0) + return ret; + *val = regval; + break; + case hwmon_pwm_mode: + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, ®val); + if (ret < 0) + return ret; + *val = !!(regval & G762_REG_FAN_CMD1_OUT_MODE); + break; + case hwmon_pwm_enable: + ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, ®val); + if (ret < 0) + return ret; + *val = !!(regval & G762_REG_FAN_CMD1_FAN_MODE) + 1; + break; + default: + return -EOPNOTSUPP; + } + return 0; } -static ssize_t pwm1_mode_store(struct device *dev, - struct device_attribute *da, const char *buf, - size_t count) +static int g762_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *val) { - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_pwm_mode(dev, val); - if (ret < 0) - return ret; - - return count; + switch (type) { + case hwmon_fan: + return g762_fan_read(dev, attr, val); + case hwmon_pwm: + return g762_pwm_read(dev, attr, val); + default: + return -EOPNOTSUPP; + } } -/* - * Read and write functions for fan1_div sysfs file. Get and set fan - * controller prescaler value - */ -static ssize_t fan1_div_show(struct device *dev, struct device_attribute *da, - char *buf) +static int g762_fan_write(struct device *dev, u32 attr, long val) { - struct g762_data *data = dev_get_drvdata(dev); - u32 cmd1; - int ret; - - ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); - if (ret < 0) - return ret; - - return sprintf(buf, "%ld\n", G762_CLKDIV_FROM_REG(cmd1)); + switch (attr) { + case hwmon_fan_target: + return do_set_fan_target(dev, val); + case hwmon_fan_div: + return do_set_fan_div(dev, val); + case hwmon_fan_pulses: + return do_set_fan_pulses(dev, val); + default: + return -EOPNOTSUPP; + } } -static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, - const char *buf, size_t count) +static int g762_pwm_write(struct device *dev, u32 attr, long val) { - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_fan_div(dev, val); - if (ret < 0) - return ret; - - return count; + switch (attr) { + case hwmon_pwm_input: + return do_set_pwm(dev, val); + case hwmon_pwm_mode: + return do_set_pwm_mode(dev, val); + case hwmon_pwm_enable: + return do_set_pwm_enable(dev, val); + default: + return -EOPNOTSUPP; + } } -/* - * Read and write functions for fan1_pulses sysfs file. Get and set number - * of tachometer pulses per fan revolution. - */ -static ssize_t fan1_pulses_show(struct device *dev, - struct device_attribute *da, char *buf) +static int g762_write(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long val) { - struct g762_data *data = dev_get_drvdata(dev); - u32 cmd1; - int ret; - - ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); - if (ret < 0) - return ret; - - return sprintf(buf, "%ld\n", G762_PULSE_FROM_REG(cmd1)); + switch (type) { + case hwmon_fan: + return g762_fan_write(dev, attr, val); + case hwmon_pwm: + return g762_pwm_write(dev, attr, val); + default: + return -EOPNOTSUPP; + } } -static ssize_t fan1_pulses_store(struct device *dev, - struct device_attribute *da, const char *buf, - size_t count) +static umode_t g762_is_visible(const void *_data, enum hwmon_sensor_types type, + u32 attr, int channel) { - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_fan_pulses(dev, val); - if (ret < 0) - return ret; - - return count; + switch (type) { + case hwmon_fan: + switch (attr) { + case hwmon_fan_input: + case hwmon_fan_alarm: + case hwmon_fan_fault: + return 0444; + case hwmon_fan_target: + case hwmon_fan_div: + case hwmon_fan_pulses: + return 0644; + default: + break; + } + break; + case hwmon_pwm: + switch (attr) { + case hwmon_pwm_input: + case hwmon_pwm_mode: + case hwmon_pwm_enable: + return 0644; + default: + break; + } + break; + default: + break; + } + return 0; } -/* - * Read and write functions for pwm1_enable. Get and set fan speed control mode - * (i.e. closed or open-loop). - * - * Following documentation about hwmon's sysfs interface, a pwm1_enable node - * should accept the following: - * - * 0 : no fan speed control (i.e. fan at full speed) - * 1 : manual fan speed control enabled (use pwm[1-*]) (open-loop) - * 2+: automatic fan speed control enabled (use fan[1-*]_target) (closed-loop) - * - * but we do not accept 0 as this mode is not natively supported by the chip - * and it is not emulated by g762 driver. -EINVAL is returned in this case. - */ -static ssize_t pwm1_enable_show(struct device *dev, - struct device_attribute *da, char *buf) -{ - struct g762_data *data = dev_get_drvdata(dev); - u32 cmd1; - int ret; - - ret = regmap_read(data->regmap, G762_REG_FAN_CMD1, &cmd1); - if (ret < 0) - return ret; - - return sprintf(buf, "%d\n", - !!(cmd1 & G762_REG_FAN_CMD1_FAN_MODE) + 1); -} - -static ssize_t pwm1_enable_store(struct device *dev, - struct device_attribute *da, const char *buf, - size_t count) -{ - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_pwm_enable(dev, val); - if (ret < 0) - return ret; - - return count; -} - -/* - * Read and write functions for pwm1 sysfs file. Get and set pwm value - * (which affects fan speed) in open-loop mode. 0 stops the fan and 255 - * makes it run at full speed. - */ -static ssize_t pwm1_show(struct device *dev, struct device_attribute *da, - char *buf) -{ - struct g762_data *data = dev_get_drvdata(dev); - int ret; - u32 pwm; - - ret = regmap_read(data->regmap, G762_REG_SET_OUT, &pwm); - if (ret < 0) - return ret; - - return sprintf(buf, "%u\n", pwm); -} - -static ssize_t pwm1_store(struct device *dev, struct device_attribute *da, - const char *buf, size_t count) -{ - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_pwm(dev, val); - if (ret < 0) - return ret; - - return count; -} - -/* - * Read and write function for fan1_target sysfs file. Get/set the fan speed in - * closed-loop mode. Speed is given as a RPM value; then the chip will regulate - * the fan speed using pulses from fan tachometer. - * - * Refer to rpm_from_cnt() implementation above to get info about count number - * calculation. - * - * Also note that due to rounding errors it is possible that you don't read - * back exactly the value you have set. - */ -static ssize_t fan1_target_show(struct device *dev, - struct device_attribute *da, char *buf) -{ - struct g762_data *data = dev_get_drvdata(dev); - int rpm; - - rpm = get_fan_rpm(data, G762_REG_SET_CNT); - if (rpm < 0) - return rpm; - - return sprintf(buf, "%d\n", rpm); -} - -static ssize_t fan1_target_store(struct device *dev, - struct device_attribute *da, const char *buf, - size_t count) -{ - unsigned long val; - int ret; - - if (kstrtoul(buf, 10, &val)) - return -EINVAL; - - ret = do_set_fan_target(dev, val); - if (ret < 0) - return ret; - - return count; -} - -/* read function for fan1_fault sysfs file. */ -static ssize_t fan1_fault_show(struct device *dev, struct device_attribute *da, - char *buf) -{ - struct g762_data *data = dev_get_drvdata(dev); - u32 status; - int ret; - - ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); - if (ret < 0) - return ret; - - return sprintf(buf, "%u\n", !!(status & G762_REG_FAN_STA_FAIL)); -} - -/* - * read function for fan1_alarm sysfs file. Note that OOC condition is - * enabled low - */ -static ssize_t fan1_alarm_show(struct device *dev, - struct device_attribute *da, char *buf) -{ - struct g762_data *data = dev_get_drvdata(dev); - u32 status; - int ret; - - ret = regmap_read(data->regmap, G762_REG_FAN_STA, &status); - if (ret < 0) - return ret; - - return sprintf(buf, "%u\n", !(status & G762_REG_FAN_STA_OOC)); -} - -static DEVICE_ATTR_RW(pwm1); -static DEVICE_ATTR_RW(pwm1_mode); -static DEVICE_ATTR_RW(pwm1_enable); -static DEVICE_ATTR_RO(fan1_input); -static DEVICE_ATTR_RO(fan1_alarm); -static DEVICE_ATTR_RO(fan1_fault); -static DEVICE_ATTR_RW(fan1_target); -static DEVICE_ATTR_RW(fan1_div); -static DEVICE_ATTR_RW(fan1_pulses); - -/* Driver data */ -static struct attribute *g762_attrs[] = { - &dev_attr_fan1_input.attr, - &dev_attr_fan1_alarm.attr, - &dev_attr_fan1_fault.attr, - &dev_attr_fan1_target.attr, - &dev_attr_fan1_div.attr, - &dev_attr_fan1_pulses.attr, - &dev_attr_pwm1.attr, - &dev_attr_pwm1_mode.attr, - &dev_attr_pwm1_enable.attr, +static const struct hwmon_channel_info * const g762_info[] = { + HWMON_CHANNEL_INFO(fan, + HWMON_F_INPUT | HWMON_F_ALARM | HWMON_F_FAULT | + HWMON_F_TARGET | HWMON_F_DIV | HWMON_F_PULSES), + HWMON_CHANNEL_INFO(pwm, + HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE), NULL }; -ATTRIBUTE_GROUPS(g762); +static const struct hwmon_ops g762_hwmon_ops = { + .is_visible = g762_is_visible, + .read = g762_read, + .write = g762_write, +}; + +static const struct hwmon_chip_info g762_chip_info = { + .ops = &g762_hwmon_ops, + .info = g762_info, +}; /* * Enable both fan failure detection and fan out of control protection. @@ -770,8 +656,9 @@ static int g762_probe(struct i2c_client *client) if (ret) return ret; - hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - data, g762_groups); + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, + data, &g762_chip_info, + NULL); return PTR_ERR_OR_ZERO(hwmon_dev); }