From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 5/7] hwmon: (g762) Make chip configuration devicetree independent
Date: Thu, 4 Jul 2024 14:37:10 -0700 [thread overview]
Message-ID: <20240704213712.2699553-6-linux@roeck-us.net> (raw)
In-Reply-To: <20240704213712.2699553-1-linux@roeck-us.net>
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 <linux@roeck-us.net>
---
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,
--
2.39.2
next prev parent reply other threads:[~2024-07-04 21:37 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-04 21:37 [PATCH 0/7] hwmon: (g762) Convert to with_info API Guenter Roeck
2024-07-04 21:37 ` [PATCH 1/7] hwmon: (g762) Simplify clock initialization Guenter Roeck
2024-07-04 21:37 ` [PATCH 2/7] hwmon: (g762) Drop platform data support Guenter Roeck
2024-07-04 21:37 ` [PATCH 3/7] hwmon: (g762) Reorder include files to be in alphabetic order Guenter Roeck
2024-07-04 21:37 ` [PATCH 4/7] hwmon: (g762) Use bit operations Guenter Roeck
2024-07-04 21:37 ` Guenter Roeck [this message]
2024-07-04 21:37 ` [PATCH 6/7] hwmon: (g762) Convert to use regmap Guenter Roeck
2024-07-04 21:37 ` [PATCH 7/7] hwmon: (g762) Convert to with_info API Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240704213712.2699553-6-linux@roeck-us.net \
--to=linux@roeck-us.net \
--cc=linux-hwmon@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox